Skip to content

Commit

Permalink
convert query values to strings before comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbegin committed Sep 8, 2015
1 parent 640925c commit b1cb946
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/webmock/util/query_mapper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "webmock/util/query_value_stringifier"

module WebMock::Util
class QueryMapper
class << self
Expand Down Expand Up @@ -184,6 +186,9 @@ def values_to_query(new_query_values, options = {})
end
new_query_values = new_query_values.to_hash
new_query_values = new_query_values.inject([]) do |object, (key, value)|

value = QueryValueStringifier.stringify(value)

key = key.to_s if key.is_a?(::Symbol) || key.nil?
if value.is_a?(Array)
value.each { |v| object << [key.to_s + '[]', v] }
Expand Down
17 changes: 17 additions & 0 deletions lib/webmock/util/query_value_stringifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module WebMock::Util
class QueryValueStringifier
class << self
def stringify(value)
return value if value.is_a?(String)
return value.to_s unless value.respond_to? :map

case value
when Array
value.map { |v| stringify(v) }
when Hash
Hash[value.map { |k, v| [k, stringify(v)] }]
end
end
end
end
end
11 changes: 11 additions & 0 deletions spec/acceptance/shared/stubbing_requests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@
end

describe "based on query params" do
it "should turn query values into strings before compairing" do
stub_request(:get, "www.example.com").with(:query => {"a" => [5, "c d"]}).to_return(:body => "abc")
expect(http_request(:get, "http://www.example.com/?a[]=5&a[]=c%20d").body).to eq("abc")

stub_request(:get, "www.example.com").with(:query => {"a" => [:j, "c d"]}).to_return(:body => "abc")
expect(http_request(:get, "http://www.example.com/?a[]=j&a[]=c%20d").body).to eq("abc")

stub_request(:get, "www.example.com").with(:query => {"a" => [true, "c d"]}).to_return(:body => "abc")
expect(http_request(:get, "http://www.example.com/?a[]=true&a[]=c%20d").body).to eq("abc")
end

it "should return stubbed response when stub declares query params as a hash" do
stub_request(:get, "www.example.com").with(:query => {"a" => ["b x", "c d"]}).to_return(:body => "abc")
expect(http_request(:get, "http://www.example.com/?a[]=b+x&a[]=c%20d").body).to eq("abc")
Expand Down
31 changes: 31 additions & 0 deletions spec/unit/util/query_value_stringifier_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require "spec_helper"

RSpec.describe WebMock::Util::QueryValueStringifier do
describe ".stringify" do
it "handles integers" do
expect(described_class.stringify(1)).to eq("1")
end

it "handles booleans" do
expect(described_class.stringify(true)).to eq("true")
end

it "handles symbols" do
expect(described_class.stringify(:hello)).to eq("hello")
end

it "handles hashes" do
input = { :a => :a, :b => :b, :c => 1, :d => [true, false] }
expect(described_class.stringify(input)).to eq(
{ :a => "a", :b => "b", :c => "1", :d => ["true", "false"] }
)
end

it "handles arrays" do
input = [ :a, 1, [true, false], {"a" => :b} ]
expect(described_class.stringify(input)).to eq(
[ "a", "1", ["true", "false"], {"a" => "b"} ]
)
end
end
end

0 comments on commit b1cb946

Please sign in to comment.