Permalink
Browse files

URI params w/ duplicate keys were being lost on normalization. Change…

…d to use Addressable::URI's :flat_array notation, which preserves data in the case of duplicate keys (e.g. "?a=1&a=2" as opposed to "?a[]=1&a[]=2").
  • Loading branch information...
1 parent 5df99d1 commit 1fc074b349a8d1f784db0cbd2bdb5c977a745189 Jen-Mei Wu & Kurt Ruppel committed Jan 31, 2012
Showing with 25 additions and 6 deletions.
  1. +22 −3 lib/webmock/request_pattern.rb
  2. +2 −2 lib/webmock/util/uri.rb
  3. +1 −1 spec/acceptance/httpclient/httpclient_spec_helper.rb
@@ -106,11 +106,30 @@ def matches?(uri)
end
end
+ # Someone might want to look at URIRegexpPattern and see if this should
+ # be used there, too.
+ def convert_hash_to_array(query_params)
+ query_params.inject([]) do |memo, pair|
+ key, value = *pair
+ if value.kind_of?(Array)
+ value.each do |val|
+ memo << [key + '[]', val]
+ end
+ else
+ memo << [key + '[]', value]
+ end
+ memo
+ end
+ end
+
def add_query_params(query_params)
- if !query_params.is_a?(Hash)
- query_params = Addressable::URI.parse('?' + query_params).query_values
+ query_params = convert_hash_to_array(query_params) if query_params.is_a?(Hash)
+
+ if !query_params.is_a?(Array)
+ query_params = Addressable::URI.parse('?' + query_params).query_values(:notation => :flat_array)
end
- @pattern.query_values = (@pattern.query_values || {}).merge(query_params)
+ qv = (@pattern.query_values(:notation => :flat_array) || []) + query_params #(@pattern.query_values || {}).merge(query_params)
+ @pattern.query_values = qv.sort
end
def to_s
View
@@ -17,7 +17,7 @@ class URI
NORMALIZED_URIS = Hash.new do |hash, uri|
normalized_uri = WebMock::Util::URI.heuristic_parse(uri)
- normalized_uri.query_values = sort_query_values(normalized_uri.query_values) if normalized_uri.query_values
+ normalized_uri.query_values = sort_query_values(normalized_uri.query_values(:notation => :flat_array)) if normalized_uri.query_values
normalized_uri = normalized_uri.normalize #normalize! is slower
normalized_uri.port = normalized_uri.inferred_port unless normalized_uri.port
hash[uri] = normalized_uri
@@ -74,7 +74,7 @@ def self.is_uri_localhost?(uri)
private
def self.sort_query_values(query_values)
- Hash[*query_values.sort.inject([]) { |values, pair| values + pair}]
+ query_values.sort
end
def self.uris_with_inferred_port_and_without(uris)
@@ -9,7 +9,7 @@ def http_request(method, uri, options = {}, &block)
c.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
c.set_basic_auth(nil, uri.user, uri.password) if uri.user
params = [method, "#{uri.omit(:userinfo, :query).normalize.to_s}",
- uri.query_values, options[:body], options[:headers] || {}]
+ uri.query_values(:notation => :flat_array), options[:body], options[:headers] || {}]
if HTTPClientSpecHelper.async_mode
connection = c.request_async(*params)
connection.join

0 comments on commit 1fc074b

Please sign in to comment.