Skip to content

Instantly share code, notes, and snippets.

@dsingleton
Created August 17, 2010 21:50
Show Gist options
  • Save dsingleton/532242 to your computer and use it in GitHub Desktop.
Save dsingleton/532242 to your computer and use it in GitHub Desktop.
# Using standard lib methods
assert redirect? "not redirected"
query_hash = CGI.parse(URI.parse(redirected_to).query)
assert query_hash.fetch(query_param, false), "'#{query_param}' param is not set, or has an empty value"
url = URI.parse(url)
params = CGI.parse(url.query || '')
params[query_param] = query_hash[query_param]
url.query = params.map{|k,v| "#{CGI::escape(k)}=#{CGI::escape(v.first)}"}.join('&')
assert url.to_s == redirected_to, "redirection URL is incorrect"
# Or, a slightly shitty regex
assert redirect?, "not redirected"
assert_false "No token in query" unless redirected_to.gsub!(/(?:&|\?)token=.*/, '')
assert_equal redirected_to, url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment