Permalink
Browse files

make query param order not matter to url equality

  • Loading branch information...
1 parent 7d30e5f commit 7710376539c6b8bee960f3cea484dc34fa1a36d8 @cap10morgan cap10morgan committed Oct 23, 2010
Showing with 27 additions and 2 deletions.
  1. +20 −2 lib/fake_web_matcher/request_matcher.rb
  2. +7 −0 spec/lib/fake_web_matcher/matchers_spec.rb
@@ -2,6 +2,24 @@ module FakeWebMatcher
# Matcher class, following RSpec's expectations. Used to confirm whether a
# request has been made on a given method and URI.
#
+
+ # Monkey patch to add a normalize! method to the URI::HTTP class.
+ # It doesn't currently sort the query params which means two URLs
+ # that are equivalent except for a different order of query params
+ # will be !=. This isn't how FakeWeb works, so we patch it here.
+ class ::URI::HTTP
+
+ # Normalize an HTTP URI so that query param order differences don't
+ # affect equality.
+ def normalize!
+ if query
+ query_array = self.query.split('&')
+ set_query(query_array.sort.join('&'))
+ end
+ super
+ end
+ end
+
class RequestMatcher
attr_reader :url, :method
@@ -81,10 +99,10 @@ def match_method(method)
# @return [Boolean] true if exprexted URI and called URI match.
#
def match_url(url)
- regex?(@url) ? @url.match(url.to_s) : @url == url
+ return @url.match(url.to_s) if regex?(@url)
+ @url == url
end
-
# Expected method formatted to be an uppercase string. Example: :get becomes
# "GET".
#
@@ -32,4 +32,11 @@ class Matchbox
FakeWeb.should_not have_requested(:get, 'http://example.com')
end
+
+ it "should not care about the order of the query parameters" do
+ FakeWeb.register_uri(:get, URI.parse('http://example.com/page?foo=bar&baz=qux'), :body => 'foo')
+ open('http://example.com/page?baz=qux&foo=bar')
+
+ FakeWeb.should have_requested(:get, 'http://example.com/page?foo=bar&baz=qux')
+ end
end

0 comments on commit 7710376

Please sign in to comment.