From 4e9ca9c1f9025574dd2ca40185ec8174e4925a34 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Fri, 13 Nov 2020 10:12:52 +1100 Subject: [PATCH] fix: ensure expected and actual query strings are parsed consistently --- lib/pact/consumer_contract/query.rb | 2 +- lib/pact/consumer_contract/query_hash.rb | 2 +- lib/pact/shared/request.rb | 5 +--- .../pact/consumer_contract/query_hash_spec.rb | 27 ++++++++++++++++++- 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/lib/pact/consumer_contract/query.rb b/lib/pact/consumer_contract/query.rb index af91613..bd670ec 100644 --- a/lib/pact/consumer_contract/query.rb +++ b/lib/pact/consumer_contract/query.rb @@ -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 | diff --git a/lib/pact/consumer_contract/query_hash.rb b/lib/pact/consumer_contract/query_hash.rb index 2e7d9c6..e447d3c 100644 --- a/lib/pact/consumer_contract/query_hash.rb +++ b/lib/pact/consumer_contract/query_hash.rb @@ -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 diff --git a/lib/pact/shared/request.rb b/lib/pact/shared/request.rb index 888a0a0..ff74e0b 100644 --- a/lib/pact/shared/request.rb +++ b/lib/pact/shared/request.rb @@ -3,9 +3,7 @@ require 'pact/consumer_contract/query' module Pact - module Request - class Base include Pact::SymbolizeKeys @@ -91,7 +89,6 @@ def display_path def display_query (query.nil? || query.empty?) ? '' : "?#{Pact::Reification.from_term(query)}" end - end end -end \ No newline at end of file +end diff --git a/spec/lib/pact/consumer_contract/query_hash_spec.rb b/spec/lib/pact/consumer_contract/query_hash_spec.rb index 439c06f..129bf3c 100644 --- a/spec/lib/pact/consumer_contract/query_hash_spec.rb +++ b/spec/lib/pact/consumer_contract/query_hash_spec.rb @@ -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¶m=2') } @@ -149,6 +175,5 @@ module Pact expect(subject.to_json).to eq query_with_array.to_json end end - end end