How do I easily parse a URL with parameters in a Rails test?

前端 未结 2 408
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-08 11:24

I have a some code that embeds a return_to URL into a redirect (like OpenID) that I want to test:

def test_uses_referrer_for_return_to
  expecte         


        
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-08 11:41

    You want Addressable for this.

    uri = Addressable::URI.parse("http://example.com/?var=value")
    uri.query_values # => {"var"=>"value"}
    uri.query_values = {"one" => "1", "two" => "2"}
    uri.to_s # => "http://example.com/?two=2&one=1"
    

    It'll automatically handle all the escaping rules for you, and it has some other useful features, like not throwing exceptions for perfectly valid but obscure URIs like the built-in URI parser.

提交回复
热议问题