Skip to content

Commit

Permalink
fix: add back support for using providerState instead of providerStat…
Browse files Browse the repository at this point in the history
…es when updating a pact
  • Loading branch information
bethesque committed Jan 16, 2020
1 parent 2b56b68 commit b494a76
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
23 changes: 17 additions & 6 deletions lib/pact/consumer_contract/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ def self.from_hash hash, options = {}
contents_hash = Pact::MatchingRules.merge(hash['contents'], contents_matching_rules, opts)
contents = Pact::ConsumerContract::Message::Contents.from_hash(contents_hash)
metadata = hash['metaData'] || hash['metadata']
provider_state = hash['providerStates'] && hash['providerStates'].first && hash['providerStates'].first['name']
provider_states = parse_provider_states(hash['providerStates'])

provider_state_name = parse_provider_state_name(hash['providerState'], hash['providerStates'])
provider_states = parse_provider_states(provider_state_name, hash['providerStates'])
new(symbolize_keys(hash).merge(
contents: contents,
provider_state: provider_state,
provider_state: provider_state_name,
provider_states: provider_states,
metadata: metadata))
end
Expand Down Expand Up @@ -120,9 +121,19 @@ def to_s

private

def self.parse_provider_states provider_states
(provider_states || []).collect do | provider_state_hash |
Pact::ProviderState.new(provider_state_hash['name'], provider_state_hash['params'])
def self.parse_provider_state_name provider_state, provider_states
(provider_states && provider_states.first && provider_states.first['name']) || provider_state
end

def self.parse_provider_states provider_state_name, provider_states
if provider_states && provider_states.any?
provider_states.collect do | provider_state_hash |
Pact::ProviderState.new(provider_state_hash['name'], provider_state_hash['params'])
end
elsif provider_state_name
[Pact::ProviderState.new(provider_state_name, {})]
else
[]
end
end
end
Expand Down
16 changes: 16 additions & 0 deletions spec/lib/pact/consumer_contract/message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module Pact
before do
message_hash['providerStates'] = []
end

it "sets the provider state to nil" do
expect(subject.provider_state).to be nil
end
Expand All @@ -61,6 +62,21 @@ module Pact
expect(subject.provider_states.last.params).to eq 'foo' => 'bar'
end
end

context "when there is a providerState instead of providerStates" do
before do
message_hash['providerState'] = message_hash['providerStates'].first['name']
message_hash.delete('providerStates')
end

it "sets the provider_state string" do
expect(subject.provider_state).to eq "an alligator named Mary exists"
end

it "sets the provider_states array to a single item" do
expect(subject.provider_states.first.name).to eq "an alligator named Mary exists"
end
end
end
end
end
Expand Down

0 comments on commit b494a76

Please sign in to comment.