Skip to content
Browse files

Added support for Addressable >= 2.3.0. Addressable 2.3.0 removed sup…

…port for multiple query value notations and broke backwards compatibility.


sporkmonger/addressable@f51e290
sporkmonger/addressable#77
  • Loading branch information...
1 parent f964815 commit 64eefebdbfe7390857e2225e622e1502bd4c36f5 @bblimke committed Aug 15, 2012
View
1 lib/webmock.rb
@@ -8,6 +8,7 @@
require 'webmock/errors'
+require 'webmock/util/query_mapper'
require 'webmock/util/uri'
require 'webmock/util/headers'
require 'webmock/util/hash_counter'
View
2 lib/webmock/http_lib_adapters/excon_adapter.rb
@@ -42,7 +42,7 @@ def self.build_request(params)
params = params.dup
method = (params.delete(:method) || :get).to_s.downcase.to_sym
params[:query] = to_query(params[:query]) if params[:query].is_a?(Hash)
- uri = Addressable::URI.new(params).to_s
+ uri = Addressable::URI.new(params).to_s
WebMock::RequestSignature.new method, uri, :body => params[:body], :headers => params[:headers]
end
View
2 lib/webmock/http_lib_adapters/httpclient_adapter.rb
@@ -132,7 +132,7 @@ def build_webmock_response(httpclient_response)
def build_request_signature(req, reuse_existing = false)
uri = WebMock::Util::URI.heuristic_parse(req.header.request_uri.to_s)
- uri.query_values = req.header.request_query if req.header.request_query
+ uri.query = WebMock::Util::QueryMapper.values_to_query(req.header.request_query) if req.header.request_query
uri.port = req.header.request_uri.port
uri = uri.omit(:userinfo)
View
12 lib/webmock/request_pattern.rb
@@ -95,7 +95,7 @@ def add_query_params(query_params)
elsif rSpecHashIncludingMatcher?(query_params)
WebMock::Matchers::HashIncludingMatcher.from_rspec_matcher(query_params)
else
- Addressable::URI.parse('?' + query_params).query_values
+ WebMock::Util::QueryMapper.query_to_values(query_params)
end
end
@@ -109,7 +109,7 @@ def to_s
class URIRegexpPattern < URIPattern
def matches?(uri)
WebMock::Util::URI.variations_of_uri_as_strings(uri).any? { |u| u.match(@pattern) } &&
- (@query_params.nil? || @query_params == uri.query_values)
+ (@query_params.nil? || @query_params == WebMock::Util::QueryMapper.query_to_values(uri.query))
end
def to_s
@@ -123,7 +123,8 @@ class URIStringPattern < URIPattern
def matches?(uri)
if @pattern.is_a?(Addressable::URI)
if @query_params
- uri.omit(:query) === @pattern && (@query_params.nil? || @query_params == uri.query_values)
+ uri.omit(:query) === @pattern &&
+ (@query_params.nil? || @query_params == WebMock::Util::QueryMapper.query_to_values(uri.query))
else
uri === @pattern
end
@@ -135,7 +136,8 @@ def matches?(uri)
def add_query_params(query_params)
super
if @query_params.is_a?(Hash) || @query_params.is_a?(String)
- @pattern.query_values = (@pattern.query_values || {}).merge(@query_params)
+ query_hash = (WebMock::Util::QueryMapper.query_to_values(@pattern.query) || {}).merge(@query_params)
+ @pattern.query = WebMock::Util::QueryMapper.values_to_query(query_hash)
@query_params = nil
end
end
@@ -199,7 +201,7 @@ def body_as_hash(body, content_type)
when :xml then
Crack::XML.parse(body)
else
- Addressable::URI.parse('?' + body).query_values
+ WebMock::Util::QueryMapper.query_to_values(body)
end
end
View
2 lib/webmock/request_stub.rb
@@ -81,7 +81,7 @@ def self.from_request_signature(signature)
if signature.body.to_s != ''
body = if signature.url_encoded?
- Addressable::URI.parse('?' + signature.body).query_values
+ WebMock::Util::QueryMapper.query_to_values(signature.body)
else
signature.body
end
View
188 lib/webmock/util/query_mapper.rb
@@ -0,0 +1,188 @@
+module WebMock::Util
+ class QueryMapper
+ #This class is based on Addressable::URI pre 2.3.0
+
+ ##
+ # Converts the query component to a Hash value.
+ #
+ # @option [Symbol] notation
+ # May be one of <code>:flat</code>, <code>:dot</code>, or
+ # <code>:subscript</code>. The <code>:dot</code> notation is not
+ # supported for assignment. Default value is <code>:subscript</code>.
+ #
+ # @return [Hash, Array] The query string parsed as a Hash or Array object.
+ #
+ # @example
+ # WebMock::Util::QueryMapper.query_to_values("?one=1&two=2&three=3")
+ # #=> {"one" => "1", "two" => "2", "three" => "3"}
+ # WebMock::Util::QueryMapper("?one[two][three]=four").query_values
+ # #=> {"one" => {"two" => {"three" => "four"}}}
+ # WebMock::Util::QueryMapper.query_to_values("?one.two.three=four",
+ # :notation => :dot
+ # )
+ # #=> {"one" => {"two" => {"three" => "four"}}}
+ # WebMock::Util::QueryMapper.query_to_values("?one[two][three]=four",
+ # :notation => :flat
+ # )
+ # #=> {"one[two][three]" => "four"}
+ # WebMock::Util::QueryMapper.query_to_values("?one.two.three=four",
+ # :notation => :flat
+ # )
+ # #=> {"one.two.three" => "four"}
+ # WebMock::Util::QueryMapper(
+ # "?one[two][three][]=four&one[two][three][]=five"
+ # )
+ # #=> {"one" => {"two" => {"three" => ["four", "five"]}}}
+ # WebMock::Util::QueryMapper.query_to_values(
+ # "?one=two&one=three").query_values(:notation => :flat_array)
+ # #=> [['one', 'two'], ['one', 'three']]
+ def self.query_to_values(query, options={})
+ defaults = {:notation => :subscript}
+ options = defaults.merge(options)
+ if ![:flat, :dot, :subscript, :flat_array].include?(options[:notation])
+ raise ArgumentError,
+ "Invalid notation. Must be one of: " +
+ "[:flat, :dot, :subscript, :flat_array]."
+ end
+ dehash = lambda do |hash|
+ hash.each do |(key, value)|
+ if value.kind_of?(Hash)
+ hash[key] = dehash.call(value)
+ end
+ end
+ if hash != {} && hash.keys.all? { |key| key =~ /^\d+$/ }
+ hash.sort.inject([]) do |accu, (_, value)|
+ accu << value; accu
+ end
+ else
+ hash
+ end
+ end
+ return nil if query == nil
+ empty_accumulator = :flat_array == options[:notation] ? [] : {}
+ return ((query.split("&").map do |pair|
+ pair.split("=", 2) if pair && !pair.empty?
+ end).compact.inject(empty_accumulator.dup) do |accumulator, (key, value)|
+ value = true if value.nil?
+ key = Addressable::URI.unencode_component(key)
+ if value != true
+ value = Addressable::URI.unencode_component(value.gsub(/\+/, " "))
+ end
+ if options[:notation] == :flat
+ if accumulator[key]
+ raise ArgumentError, "Key was repeated: #{key.inspect}"
+ end
+ accumulator[key] = value
+ elsif options[:notation] == :flat_array
+ accumulator << [key, value]
+ else
+ if options[:notation] == :dot
+ array_value = false
+ subkeys = key.split(".")
+ elsif options[:notation] == :subscript
+ array_value = !!(key =~ /\[\]$/)
+ subkeys = key.split(/[\[\]]+/)
+ end
+ current_hash = accumulator
+ for i in 0...(subkeys.size - 1)
+ subkey = subkeys[i]
+ current_hash[subkey] = {} unless current_hash[subkey]
+ current_hash = current_hash[subkey]
+ end
+ if array_value
+ current_hash[subkeys.last] = [] unless current_hash[subkeys.last]
+ current_hash[subkeys.last] << value
+ else
+ current_hash[subkeys.last] = value
+ end
+ end
+ accumulator
+ end).inject(empty_accumulator.dup) do |accumulator, (key, value)|
+ if options[:notation] == :flat_array
+ accumulator << [key, value]
+ else
+ accumulator[key] = value.kind_of?(Hash) ? dehash.call(value) : value
+ end
+ accumulator
+ end
+ end
+
+ ##
+ # Sets the query component for this URI from a Hash object.
+ # This method produces a query string using the :subscript notation.
+ # An empty Hash will result in a nil query.
+ #
+ # @param [Hash, #to_hash, Array] new_query_values The new query values.
+ def self.values_to_query(new_query_values)
+ if new_query_values == nil
+ return nil
+ end
+
+ if !new_query_values.is_a?(Array)
+ if !new_query_values.respond_to?(:to_hash)
+ raise TypeError,
+ "Can't convert #{new_query_values.class} into Hash."
+ end
+ new_query_values = new_query_values.to_hash
+ new_query_values = new_query_values.map do |key, value|
+ key = key.to_s if key.kind_of?(Symbol)
+ [key, value]
+ end
+ # Useful default for OAuth and caching.
+ # Only to be used for non-Array inputs. Arrays should preserve order.
+ new_query_values.sort!
+ end
+
+ ##
+ # Joins and converts parent and value into a properly encoded and
+ # ordered URL query.
+ #
+ # @private
+ # @param [String] parent an URI encoded component.
+ # @param [Array, Hash, Symbol, #to_str] value
+ #
+ # @return [String] a properly escaped and ordered URL query.
+ to_query = lambda do |parent, value|
+ if value.is_a?(Hash)
+ value = value.map do |key, val|
+ [
+ Addressable::URI.encode_component(key, Addressable::URI::CharacterClasses::UNRESERVED),
+ val
+ ]
+ end
+ value.sort!
+ buffer = ""
+ value.each do |key, val|
+ new_parent = "#{parent}[#{key}]"
+ buffer << "#{to_query.call(new_parent, val)}&"
+ end
+ return buffer.chop
+ elsif value.is_a?(Array)
+ buffer = ""
+ value.each_with_index do |val, i|
+ new_parent = "#{parent}[#{i}]"
+ buffer << "#{to_query.call(new_parent, val)}&"
+ end
+ return buffer.chop
+ elsif value == true
+ return parent
+ else
+ encoded_value = Addressable::URI.encode_component(
+ value, Addressable::URI::CharacterClasses::UNRESERVED
+ )
+ return "#{parent}=#{encoded_value}"
+ end
+ end
+
+ # new_query_values have form [['key1', 'value1'], ['key2', 'value2']]
+ buffer = ""
+ new_query_values.each do |parent, value|
+ encoded_parent = Addressable::URI.encode_component(
+ parent, Addressable::URI::CharacterClasses::UNRESERVED
+ )
+ buffer << "#{to_query.call(encoded_parent, value)}&"
+ end
+ return buffer.chop
+ end
+ end
+end
View
19 lib/webmock/util/uri.rb
@@ -1,23 +1,22 @@
-module Addressable
- class URI
- module CharacterClasses
- USERINFO = UNRESERVED + SUB_DELIMS + "\\:"
- end
- end
-end
-
module WebMock
module Util
class URI
+ module CharacterClasses
+ USERINFO = Addressable::URI::CharacterClasses::UNRESERVED + Addressable::URI::CharacterClasses::SUB_DELIMS + "\\:"
+ end
+
ADDRESSABLE_URIS = Hash.new do |hash, key|
hash[key] = Addressable::URI.heuristic_parse(key)
end
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
+ if normalized_uri.query_values
+ sorted_query_values = sort_query_values(WebMock::Util::QueryMapper.query_to_values(normalized_uri.query) || {})
+ normalized_uri.query = WebMock::Util::QueryMapper.values_to_query(sorted_query_values)
+ end
normalized_uri = normalized_uri.normalize #normalize! is slower
normalized_uri.port = normalized_uri.inferred_port unless normalized_uri.port
hash[uri] = normalized_uri
@@ -63,7 +62,7 @@ def self.strip_default_port_from_uri_string(uri_string)
end
def self.encode_unsafe_chars_in_userinfo(userinfo)
- Addressable::URI.encode_component(userinfo, Addressable::URI::CharacterClasses::USERINFO)
+ Addressable::URI.encode_component(userinfo, WebMock::Util::URI::CharacterClasses::USERINFO)
end
def self.is_uri_localhost?(uri)
View
2 spec/acceptance/httpclient/httpclient_spec_helper.rb
@@ -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] || {}]
+ WebMock::Util::QueryMapper.query_to_values(uri.query), options[:body], options[:headers] || {}]
if HTTPClientSpecHelper.async_mode
connection = c.request_async(*params)
connection.join
View
6 spec/unit/util/uri_spec.rb
@@ -170,19 +170,19 @@
it "should successfully handle array parameters" do
uri_string = 'http://www.example.com:80/path?a[]=b&a[]=c'
uri = WebMock::Util::URI.normalize_uri(uri_string)
- uri.query_values.should == {"a"=>["b", "c"]}
+ WebMock::Util::QueryMapper.query_to_values(uri.query).should == {"a"=>["b", "c"]}
end
it "should successfully handle hash parameters" do
uri_string = 'http://www.example.com:80/path?a[d]=b&a[e]=c&a[b][c]=1'
uri = WebMock::Util::URI.normalize_uri(uri_string)
- uri.query_values.should == {"a"=>{"d"=>"b", "e"=>"c", "b"=>{"c"=>"1"}}}
+ WebMock::Util::QueryMapper.query_to_values(uri.query).should == {"a"=>{"d"=>"b", "e"=>"c", "b"=>{"c"=>"1"}}}
end
it "should successfully handle nested hash parameters" do
uri_string = 'http://www.example.com:80/path?one[two][three][]=four&one[two][three][]=five'
uri = WebMock::Util::URI.normalize_uri(uri_string)
- uri.query_values.should == {"one"=>{"two"=>{"three" => ["four", "five"]}}}
+ WebMock::Util::QueryMapper.query_to_values(uri.query).should == {"one"=>{"two"=>{"three" => ["four", "five"]}}}
end
end
View
2 webmock.gemspec
@@ -14,7 +14,7 @@ Gem::Specification.new do |s|
s.rubyforge_project = 'webmock'
- s.add_dependency 'addressable', '~> 2.2.8'
+ s.add_dependency 'addressable', '>= 2.2.7'
s.add_dependency 'crack', '>=0.1.7'
s.add_development_dependency 'rspec', '~> 2.10'

0 comments on commit 64eefeb

Please sign in to comment.
Something went wrong with that request. Please try again.