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

前端 未结 2 404
爱一瞬间的悲伤
爱一瞬间的悲伤 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条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-08 12:01

    CGI::parse(querystring) will parse a querystring into a hash. Then, CGI::unescape(string) will undo any URL-encoding in the value.

    Alternatively, you can use Rack::Utils.parse_query and Rack::Utils.unescape if you're on a recent Rack-based version of Rails, and want to be super-modern.

    I'm not aware of any Rails-specific helper methods that wrap these utility functions, but they're pretty simple to use, and CGI or Rack is already loaded in the Rails environment anyway.

提交回复
热议问题