Skip to content

Commit

Permalink
fix: correctly parse matching rules for request paths
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Feb 27, 2020
1 parent bbccb62 commit cc15a72
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 6 deletions.
20 changes: 15 additions & 5 deletions lib/pact/consumer_contract/interaction_v3_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ def self.call hash, options
Pact.configuration.error_stream.puts("WARN: Currently only 1 provider state is supported. Ignoring ")
end
metadata = parse_metadata(hash['metadata'])
Interaction.new(symbolize_keys(hash).merge(request: request,
response: response,
provider_states: provider_states,
Interaction.new(symbolize_keys(hash).merge(request: request,
response: response,
provider_states: provider_states,
provider_state: provider_state,
metadata: metadata))
end
Expand All @@ -47,14 +47,14 @@ def self.parse_response response_hash, options

def self.parse_request_with_non_string_body request_hash, request_matching_rules, options
request_hash = request_hash.keys.each_with_object({}) do | key, new_hash |
new_hash[key] = Pact::MatchingRules.merge(request_hash[key], request_matching_rules[key], options)
new_hash[key] = Pact::MatchingRules.merge(request_hash[key], look_up_matching_rules(key, request_matching_rules), options)
end
Pact::Request::Expected.from_hash(request_hash)
end

def self.parse_response_with_non_string_body response_hash, response_matching_rules, options
response_hash = response_hash.keys.each_with_object({}) do | key, new_hash |
new_hash[key] = Pact::MatchingRules.merge(response_hash[key], response_matching_rules[key], options)
new_hash[key] = Pact::MatchingRules.merge(response_hash[key], look_up_matching_rules(key, response_matching_rules), options)
end
Pact::Response.from_hash(response_hash)
end
Expand All @@ -78,5 +78,15 @@ def self.parse_provider_states provider_states
def self.parse_metadata metadata_hash
symbolize_keys(metadata_hash)
end

def self.look_up_matching_rules(key, matching_rules)
# The matching rules for the path operate on the object itself and don't have sub paths
# Convert it into the format that Merge expects.
if key == 'path'
matching_rules[key] ? { '$.' => matching_rules[key] } : nil
else
matching_rules[key]
end
end
end
end
2 changes: 1 addition & 1 deletion spec/lib/pact/consumer_contract/consumer_contract_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ module Pact
let(:interaction2) { double('Pact::Interaction') }
let(:interaction3) { double('Pact::Interaction') }
let(:interaction4) { double('Pact::Interaction') }

before do
allow(interaction1).to receive(:metadata).and_return(write_to_pact: false)
allow(interaction2).to receive(:metadata).and_return(write_to_pact: true)
Expand Down
27 changes: 27 additions & 0 deletions spec/lib/pact/consumer_contract/interaction_v3_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,33 @@ module Pact
expect(subject.provider_state).to eq "foo"
end
end

describe "parsing an interaction with matching rules for the path" do
let(:interaction_hash) do
JSON.parse('{
"request": {
"method": "GET",
"path": "/path",
"matchingRules": {
"path": {
"matchers": [
{
"match": "type"
}
]
}
}
},
"response": {
"status": 200
}
}')
end

it "correctly merges the rules and the path" do
expect(subject.request.path).to eq Pact::SomethingLike.new("/path")
end
end
end
end
end
23 changes: 23 additions & 0 deletions spec/lib/pact/matching_rules/v3/merge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -490,5 +490,28 @@
.with("WARN: Ignoring unsupported combine AND for path $['foo']")
end
end

context "when the top level object is a string" do
before do
allow(Pact.configuration.error_stream).to receive(:puts)
end

let(:expected) do
"/some/path"
end

let(:matching_rules) do
{
"$." => {
"matchers" => [{ "match" => "type" }],
"combine" => "AND"
}
}
end

it "returns a SomethingLike" do
expect(subject).to eq Pact::SomethingLike.new("/some/path")
end
end
end
end

0 comments on commit cc15a72

Please sign in to comment.