Skip to content

Commit

Permalink
fix: ensure expected and actual query strings are parsed consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Nov 12, 2020
1 parent 19671a3 commit 4e9ca9c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/pact/consumer_contract/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def self.parse_string query_string
parsed_query = parse_query(query_string)

# If Rails nested params...
if parsed_query.keys.any?{ | key| key.include?("[") }
if parsed_query.keys.any?{ | key| key =~ /\[.*\]/ }
parse_nested_query(query_string)
else
parsed_query.each_with_object({}) do | (key, value), new_hash |
Expand Down
2 changes: 1 addition & 1 deletion lib/pact/consumer_contract/query_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def ==(other)
# from the actual query string.
def difference(other)
require 'pact/matchers' # avoid recursive loop between this file, pact/reification and pact/matchers
Pact::Matchers.diff(query, symbolize_keys(CGI::parse(other.query)), allow_unexpected_keys: false)
Pact::Matchers.diff(query, symbolize_keys(convert_to_hash_of_arrays(Query.parse_string(other.query))), allow_unexpected_keys: false)
end

def query
Expand Down
5 changes: 1 addition & 4 deletions lib/pact/shared/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
require 'pact/consumer_contract/query'

module Pact

module Request

class Base
include Pact::SymbolizeKeys

Expand Down Expand Up @@ -91,7 +89,6 @@ def display_path
def display_query
(query.nil? || query.empty?) ? '' : "?#{Pact::Reification.from_term(query)}"
end

end
end
end
end
27 changes: 26 additions & 1 deletion spec/lib/pact/consumer_contract/query_hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,32 @@ module Pact
end
end

context "with a real example" do
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') }

let(:query) do
{
"q" => [
{
"pacticipant" => "Foo",
"version" => "1.2.3"
},
{
"pacticipant" => "Bar",
"version" => "4.5.6"
}
],
"latestby" => [
"cvpv"
]
}
end

it "matches" do
expect(subject.difference(other)).to be_empty
end
end

context "when there is an ArrayLike" do
let(:query) { { param: Pact.each_like("1") } }
let(:other) { QueryString.new('param=1&param=2') }
Expand Down Expand Up @@ -149,6 +175,5 @@ module Pact
expect(subject.to_json).to eq query_with_array.to_json
end
end

end
end

0 comments on commit 4e9ca9c

Please sign in to comment.