Skip to content

Commit cc15a72

Browse files
committed
fix: correctly parse matching rules for request paths
fix: pact-foundation/pact-ruby-standalone#44
1 parent bbccb62 commit cc15a72

File tree

4 files changed

+66
-6
lines changed

4 files changed

+66
-6
lines changed

lib/pact/consumer_contract/interaction_v3_parser.rb

+15-5
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ def self.call hash, options
2020
Pact.configuration.error_stream.puts("WARN: Currently only 1 provider state is supported. Ignoring ")
2121
end
2222
metadata = parse_metadata(hash['metadata'])
23-
Interaction.new(symbolize_keys(hash).merge(request: request,
24-
response: response,
25-
provider_states: provider_states,
23+
Interaction.new(symbolize_keys(hash).merge(request: request,
24+
response: response,
25+
provider_states: provider_states,
2626
provider_state: provider_state,
2727
metadata: metadata))
2828
end
@@ -47,14 +47,14 @@ def self.parse_response response_hash, options
4747

4848
def self.parse_request_with_non_string_body request_hash, request_matching_rules, options
4949
request_hash = request_hash.keys.each_with_object({}) do | key, new_hash |
50-
new_hash[key] = Pact::MatchingRules.merge(request_hash[key], request_matching_rules[key], options)
50+
new_hash[key] = Pact::MatchingRules.merge(request_hash[key], look_up_matching_rules(key, request_matching_rules), options)
5151
end
5252
Pact::Request::Expected.from_hash(request_hash)
5353
end
5454

5555
def self.parse_response_with_non_string_body response_hash, response_matching_rules, options
5656
response_hash = response_hash.keys.each_with_object({}) do | key, new_hash |
57-
new_hash[key] = Pact::MatchingRules.merge(response_hash[key], response_matching_rules[key], options)
57+
new_hash[key] = Pact::MatchingRules.merge(response_hash[key], look_up_matching_rules(key, response_matching_rules), options)
5858
end
5959
Pact::Response.from_hash(response_hash)
6060
end
@@ -78,5 +78,15 @@ def self.parse_provider_states provider_states
7878
def self.parse_metadata metadata_hash
7979
symbolize_keys(metadata_hash)
8080
end
81+
82+
def self.look_up_matching_rules(key, matching_rules)
83+
# The matching rules for the path operate on the object itself and don't have sub paths
84+
# Convert it into the format that Merge expects.
85+
if key == 'path'
86+
matching_rules[key] ? { '$.' => matching_rules[key] } : nil
87+
else
88+
matching_rules[key]
89+
end
90+
end
8191
end
8292
end

spec/lib/pact/consumer_contract/consumer_contract_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ module Pact
145145
let(:interaction2) { double('Pact::Interaction') }
146146
let(:interaction3) { double('Pact::Interaction') }
147147
let(:interaction4) { double('Pact::Interaction') }
148-
148+
149149
before do
150150
allow(interaction1).to receive(:metadata).and_return(write_to_pact: false)
151151
allow(interaction2).to receive(:metadata).and_return(write_to_pact: true)

spec/lib/pact/consumer_contract/interaction_v3_parser_spec.rb

+27
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,33 @@ module Pact
4343
expect(subject.provider_state).to eq "foo"
4444
end
4545
end
46+
47+
describe "parsing an interaction with matching rules for the path" do
48+
let(:interaction_hash) do
49+
JSON.parse('{
50+
"request": {
51+
"method": "GET",
52+
"path": "/path",
53+
"matchingRules": {
54+
"path": {
55+
"matchers": [
56+
{
57+
"match": "type"
58+
}
59+
]
60+
}
61+
}
62+
},
63+
"response": {
64+
"status": 200
65+
}
66+
}')
67+
end
68+
69+
it "correctly merges the rules and the path" do
70+
expect(subject.request.path).to eq Pact::SomethingLike.new("/path")
71+
end
72+
end
4673
end
4774
end
4875
end

spec/lib/pact/matching_rules/v3/merge_spec.rb

+23
Original file line numberDiff line numberDiff line change
@@ -490,5 +490,28 @@
490490
.with("WARN: Ignoring unsupported combine AND for path $['foo']")
491491
end
492492
end
493+
494+
context "when the top level object is a string" do
495+
before do
496+
allow(Pact.configuration.error_stream).to receive(:puts)
497+
end
498+
499+
let(:expected) do
500+
"/some/path"
501+
end
502+
503+
let(:matching_rules) do
504+
{
505+
"$." => {
506+
"matchers" => [{ "match" => "type" }],
507+
"combine" => "AND"
508+
}
509+
}
510+
end
511+
512+
it "returns a SomethingLike" do
513+
expect(subject).to eq Pact::SomethingLike.new("/some/path")
514+
end
515+
end
493516
end
494517
end

0 commit comments

Comments
 (0)