Skip to content

Commit

Permalink
fix: correctly serialize query params that use a Pact.each_like in pa…
Browse files Browse the repository at this point in the history
…ct file

Fixes: #58
  • Loading branch information
bethesque committed Jul 13, 2018
1 parent 31237ce commit b3414dd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
6 changes: 3 additions & 3 deletions lib/pact/reification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions spec/lib/pact/consumer_contract/query_hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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&param=2') }

it 'returns an empty diff' do
expect(subject.difference(other)).to be_empty
end
end
end
end

Expand Down
17 changes: 11 additions & 6 deletions spec/lib/pact/reification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,38 +122,43 @@ 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")
end
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/)})}

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'})}

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&param=1")
end
end
end
end

0 comments on commit b3414dd

Please sign in to comment.