19

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
  expected_return_to = 'http://test.com/foo'
  @request.env['HTTP_REFERER'] = expected_return_to
  get :fazbot
  # @response.redirected_to looks like http://service.com?...&return_to=[URI-encoded version of URL above]&...
  encoded_return_to = (something_here)[:return_to]
  assert_equal expected_return_to, URI.unencode(encoded_return_to)
end

It's a Rails ActionController::TestCase, so I have access to all sorts of helper methods; I just can't find the right one.

Of course I could use URI.parse to get the params part of the URL, then split it on /&|?/ and then split again on '=', but I'm hoping this is already done for me. Plus, what if I miss some obscure rule in URL escaping or parameter parsing? There has to be something in ActionPack or ActiveSupport to do this, but I can't find it.

Thanks :)

James A. Rosen
  • 64,193
  • 61
  • 179
  • 261

2 Answers2

43

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.

chrismear
  • 836
  • 8
  • 12
  • 8
    CGI::parse wraps all values in an array. +1 for Rack::Utils.parse_query – zaius Jul 24 '10 at 21:50
  • CGI::parse handles bad characters you sometimes see in URLs, rack parse_query will fail on {,},',", ... – Dark Castle Mar 24 '12 at 06:18
  • 3
    If you have nested query, give it a try to `Rack::Utils.parse_nested_query(q)`: `Rack::Utils.parse_nested_query 'foo=1&bar%5Bbaz%5D=baz'` => `{"foo"=>"1", "bar"=>{"baz"=>"baz"}}` – sequielo Oct 30 '13 at 19:08
  • 1
    on ruby 2.2 I see `CGI::parse` doing automatic URL decoding so the extra `unescape` can do double the work and in edge cases produce wrong results – akostadinov Dec 28 '15 at 19:28
29

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.

uday
  • 8,544
  • 4
  • 30
  • 54
Bob Aman
  • 32,839
  • 9
  • 71
  • 95
  • 2
    You are biased as the maintainer, I am not and would rather use this then anything else. Wonderful gem, thanks for making it happen. – Kjellski Jan 30 '13 at 11:25
  • 1
    Thanks for the kind words. It's been quite a few years of hard work building and improving it. – Bob Aman Feb 04 '13 at 09:00
  • Addressable is a replacement for the URI implementation that is part of Ruby's standard library. Addressable works in Ruby 1.8.x, 1.9.x, and JRuby. – Misha Mar 25 '15 at 21:03