Skip to content

Commit 4e9ca9c

Browse files
committed
fix: ensure expected and actual query strings are parsed consistently
1 parent 19671a3 commit 4e9ca9c

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

lib/pact/consumer_contract/query.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def self.parse_string query_string
1818
parsed_query = parse_query(query_string)
1919

2020
# If Rails nested params...
21-
if parsed_query.keys.any?{ | key| key.include?("[") }
21+
if parsed_query.keys.any?{ | key| key =~ /\[.*\]/ }
2222
parse_nested_query(query_string)
2323
else
2424
parsed_query.each_with_object({}) do | (key, value), new_hash |

lib/pact/consumer_contract/query_hash.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def ==(other)
3232
# from the actual query string.
3333
def difference(other)
3434
require 'pact/matchers' # avoid recursive loop between this file, pact/reification and pact/matchers
35-
Pact::Matchers.diff(query, symbolize_keys(CGI::parse(other.query)), allow_unexpected_keys: false)
35+
Pact::Matchers.diff(query, symbolize_keys(convert_to_hash_of_arrays(Query.parse_string(other.query))), allow_unexpected_keys: false)
3636
end
3737

3838
def query

lib/pact/shared/request.rb

+1-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
require 'pact/consumer_contract/query'
44

55
module Pact
6-
76
module Request
8-
97
class Base
108
include Pact::SymbolizeKeys
119

@@ -91,7 +89,6 @@ def display_path
9189
def display_query
9290
(query.nil? || query.empty?) ? '' : "?#{Pact::Reification.from_term(query)}"
9391
end
94-
9592
end
9693
end
97-
end
94+
end

spec/lib/pact/consumer_contract/query_hash_spec.rb

+26-1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,32 @@ module Pact
7373
end
7474
end
7575

76+
context "with a real example" do
77+
let(:other) { QueryString.new('q%5B%5D%5Bpacticipant%5D=Foo&q%5B%5D%5Bversion%5D=1.2.3&q%5B%5D%5Bpacticipant%5D=Bar&q%5B%5D%5Bversion%5D=4.5.6&latestby=cvpv') }
78+
79+
let(:query) do
80+
{
81+
"q" => [
82+
{
83+
"pacticipant" => "Foo",
84+
"version" => "1.2.3"
85+
},
86+
{
87+
"pacticipant" => "Bar",
88+
"version" => "4.5.6"
89+
}
90+
],
91+
"latestby" => [
92+
"cvpv"
93+
]
94+
}
95+
end
96+
97+
it "matches" do
98+
expect(subject.difference(other)).to be_empty
99+
end
100+
end
101+
76102
context "when there is an ArrayLike" do
77103
let(:query) { { param: Pact.each_like("1") } }
78104
let(:other) { QueryString.new('param=1&param=2') }
@@ -149,6 +175,5 @@ module Pact
149175
expect(subject.to_json).to eq query_with_array.to_json
150176
end
151177
end
152-
153178
end
154179
end

0 commit comments

Comments
 (0)