From b3414dd25784d73feac155512be8112de7bd2077 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Sat, 14 Jul 2018 09:23:40 +1000 Subject: [PATCH] fix: correctly serialize query params that use a Pact.each_like in pact file Fixes: https://github.com/pact-foundation/pact-support/issues/58 --- lib/pact/reification.rb | 6 +++--- .../pact/consumer_contract/query_hash_spec.rb | 9 +++++++++ spec/lib/pact/reification_spec.rb | 17 +++++++++++------ 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/lib/pact/reification.rb b/lib/pact/reification.rb index d8045e7..dcbe4e2 100644 --- a/lib/pact/reification.rb +++ b/lib/pact/reification.rb @@ -26,13 +26,13 @@ def self.from_term(term) when Pact::QueryString from_term(term.query) when Pact::QueryHash - term.query.map { |k, v| + from_term(term.query).map { |k, v| if v.nil? k elsif v.is_a?(Array) #For cases where there are multiple instance of the same parameter - v.map { |x| "#{k}=#{escape(from_term(x))}"}.join('&') + v.map { |x| "#{k}=#{escape(x)}"}.join('&') else - "#{k}=#{escape(from_term(v))}" + "#{k}=#{escape(v)}" end }.join('&') else diff --git a/spec/lib/pact/consumer_contract/query_hash_spec.rb b/spec/lib/pact/consumer_contract/query_hash_spec.rb index 50dbcd2..439c06f 100644 --- a/spec/lib/pact/consumer_contract/query_hash_spec.rb +++ b/spec/lib/pact/consumer_contract/query_hash_spec.rb @@ -72,6 +72,15 @@ module Pact expect(subject.difference(other).keys).to contain_exactly(:"param[a]", :"param[a][aa]", :"param[a][bb]") end end + + context "when there is an ArrayLike" do + let(:query) { { param: Pact.each_like("1") } } + let(:other) { QueryString.new('param=1¶m=2') } + + it 'returns an empty diff' do + expect(subject.difference(other)).to be_empty + end + end end end diff --git a/spec/lib/pact/reification_spec.rb b/spec/lib/pact/reification_spec.rb index 1c5a4d4..4a83d22 100644 --- a/spec/lib/pact/reification_spec.rb +++ b/spec/lib/pact/reification_spec.rb @@ -122,10 +122,9 @@ module Pact end context "when Hash Query with UTF-8 string" do - subject { Reification.from_term(query)} - let(:query) { QueryHash.new( {param: 'ILove', extra: '寿司'})} + let(:query) { QueryHash.new(param: 'ILove', extra: '寿司') } it "returns the hash with escaping UTF-8 string" do expect(subject).to eq("param=ILove&extra=%E5%AF%BF%E5%8F%B8") @@ -133,7 +132,6 @@ module Pact end context "when Hash Query with embeded terms" do - subject { Reification.from_term(query)} let(:query) { QueryHash.new( {param: 'hello', extra: Pact::Term.new(generate: "wonderworld", matcher: /\w+world/)})} @@ -141,10 +139,9 @@ module Pact it "returns the hash in the natural order, and fills in Terms appropriately" do expect(subject).to eq("param=hello&extra=wonderworld") end - end - context "when Hash Query with Arrays and multiple params with the same name" do + context "when Hash Query with Arrays and multiple params with the same name" do subject { Reification.from_term(query)} let(:query) { QueryHash.new( {param: 'hello', double: [Pact::Term.new(generate: "wonder", matcher: /\w+/), 'world'], simple: 'bye'})} @@ -152,8 +149,16 @@ module Pact it "returns the hash in the natural order, and fills in Terms appropriately" do expect(subject).to eq("param=hello&double=wonder&double=world&simple=bye") end - end + context "when Hash Query with an ArrayLike" do + subject { Reification.from_term(query)} + + let(:query) { QueryHash.new(param: Pact.each_like("1", min: 2)) } + + it "turns the hash into a string with the right number of params" do + expect(subject).to eq("param=1¶m=1") + end + end end end