Skip to content

Commit b1cb946

Browse files
committed
convert query values to strings before comparison
1 parent 640925c commit b1cb946

4 files changed

Lines changed: 64 additions & 0 deletions

File tree

lib/webmock/util/query_mapper.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require "webmock/util/query_value_stringifier"
2+
13
module WebMock::Util
24
class QueryMapper
35
class << self
@@ -184,6 +186,9 @@ def values_to_query(new_query_values, options = {})
184186
end
185187
new_query_values = new_query_values.to_hash
186188
new_query_values = new_query_values.inject([]) do |object, (key, value)|
189+
190+
value = QueryValueStringifier.stringify(value)
191+
187192
key = key.to_s if key.is_a?(::Symbol) || key.nil?
188193
if value.is_a?(Array)
189194
value.each { |v| object << [key.to_s + '[]', v] }
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module WebMock::Util
2+
class QueryValueStringifier
3+
class << self
4+
def stringify(value)
5+
return value if value.is_a?(String)
6+
return value.to_s unless value.respond_to? :map
7+
8+
case value
9+
when Array
10+
value.map { |v| stringify(v) }
11+
when Hash
12+
Hash[value.map { |k, v| [k, stringify(v)] }]
13+
end
14+
end
15+
end
16+
end
17+
end

spec/acceptance/shared/stubbing_requests.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@
4444
end
4545

4646
describe "based on query params" do
47+
it "should turn query values into strings before compairing" do
48+
stub_request(:get, "www.example.com").with(:query => {"a" => [5, "c d"]}).to_return(:body => "abc")
49+
expect(http_request(:get, "http://www.example.com/?a[]=5&a[]=c%20d").body).to eq("abc")
50+
51+
stub_request(:get, "www.example.com").with(:query => {"a" => [:j, "c d"]}).to_return(:body => "abc")
52+
expect(http_request(:get, "http://www.example.com/?a[]=j&a[]=c%20d").body).to eq("abc")
53+
54+
stub_request(:get, "www.example.com").with(:query => {"a" => [true, "c d"]}).to_return(:body => "abc")
55+
expect(http_request(:get, "http://www.example.com/?a[]=true&a[]=c%20d").body).to eq("abc")
56+
end
57+
4758
it "should return stubbed response when stub declares query params as a hash" do
4859
stub_request(:get, "www.example.com").with(:query => {"a" => ["b x", "c d"]}).to_return(:body => "abc")
4960
expect(http_request(:get, "http://www.example.com/?a[]=b+x&a[]=c%20d").body).to eq("abc")
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require "spec_helper"
2+
3+
RSpec.describe WebMock::Util::QueryValueStringifier do
4+
describe ".stringify" do
5+
it "handles integers" do
6+
expect(described_class.stringify(1)).to eq("1")
7+
end
8+
9+
it "handles booleans" do
10+
expect(described_class.stringify(true)).to eq("true")
11+
end
12+
13+
it "handles symbols" do
14+
expect(described_class.stringify(:hello)).to eq("hello")
15+
end
16+
17+
it "handles hashes" do
18+
input = { :a => :a, :b => :b, :c => 1, :d => [true, false] }
19+
expect(described_class.stringify(input)).to eq(
20+
{ :a => "a", :b => "b", :c => "1", :d => ["true", "false"] }
21+
)
22+
end
23+
24+
it "handles arrays" do
25+
input = [ :a, 1, [true, false], {"a" => :b} ]
26+
expect(described_class.stringify(input)).to eq(
27+
[ "a", "1", ["true", "false"], {"a" => "b"} ]
28+
)
29+
end
30+
end
31+
end

0 commit comments

Comments
 (0)