Skip to content

Commit 2e48892

Browse files
committed
feat: update message classes to support pact-message
1 parent 93839cf commit 2e48892

File tree

4 files changed

+120
-88
lines changed

4 files changed

+120
-88
lines changed

lib/pact/consumer_contract/interaction.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
require 'pact/errors'
77

88
module Pact
9-
class Interaction
9+
class Interaction
1010
include ActiveSupportSupport
1111
include SymbolizeKeys
1212

lib/pact/consumer_contract/message.rb

+98-80
Original file line numberDiff line numberDiff line change
@@ -5,100 +5,118 @@
55
require 'pact/errors'
66
require 'pact/consumer/request'
77
require 'pact/consumer_contract/response'
8+
require 'pact/consumer_contract/message/content'
89

910
module Pact
10-
class Message
11-
include ActiveSupportSupport
12-
include SymbolizeKeys
13-
14-
attr_accessor :description, :content, :provider_state
15-
16-
def initialize attributes = {}
17-
@description = attributes[:description]
18-
@provider_state = attributes[:provider_state] || attributes[:providerState]
19-
@content = attributes[:content]
20-
end
21-
22-
def self.from_hash hash
23-
content_hash = Pact::MatchingRules.merge(hash['content'], hash['content']['matchingRules'])
24-
content = Pact::Message::Content.new(content_hash)
25-
new(symbolize_keys(hash).merge(content: content))
26-
end
27-
28-
def to_hash
29-
{
30-
description: description,
31-
provider_state: provider_state,
32-
content: content.to_hash,
33-
}
34-
end
35-
36-
37-
def request
38-
@request ||= Pact::Consumer::Request::Actual.from_hash(
39-
path: '/',
40-
method: 'POST',
41-
query: nil,
42-
headers: {'Content-Type' => 'application/json'},
43-
body: {
11+
class ConsumerContract
12+
class Message
13+
include Pact::ActiveSupportSupport
14+
include Pact::SymbolizeKeys
15+
16+
attr_accessor :description, :content, :provider_state
17+
18+
def initialize attributes = {}
19+
@description = attributes[:description]
20+
@provider_state = attributes[:provider_state] || attributes[:providerState]
21+
@content = attributes[:content]
22+
end
23+
24+
def self.from_hash hash
25+
content_hash = Pact::MatchingRules.merge(hash['content'], hash['content']['matchingRules'])
26+
content = Pact::ConsumerContract::Message::Content.new(content_hash)
27+
new(symbolize_keys(hash).merge(content: content))
28+
end
29+
30+
def to_hash
31+
{
4432
description: description,
45-
providerStates: [{
46-
name: provider_state
47-
}]
33+
provider_state: provider_state,
34+
content: content.to_hash,
4835
}
49-
)
50-
end
51-
52-
# custom media type?
53-
def response
54-
@response ||= Pact::Response.new(
55-
status: 200,
56-
headers: {'Content-Type' => 'application/json'},
57-
body: {
58-
content: content
36+
end
37+
38+
# todo move this proper decorator
39+
def as_json
40+
{
41+
description: description,
42+
providerState: provider_state,
43+
content: content.as_json
5944
}
60-
)
61-
end
45+
end
46+
47+
48+
def request
49+
@request ||= Pact::Consumer::Request::Actual.from_hash(
50+
path: '/',
51+
method: 'POST',
52+
query: nil,
53+
headers: {'Content-Type' => 'application/json'},
54+
body: {
55+
description: description,
56+
providerStates: [{
57+
name: provider_state
58+
}]
59+
}
60+
)
61+
end
6262

63-
def http?
64-
false
65-
end
63+
# custom media type?
64+
def response
65+
@response ||= Pact::Response.new(
66+
status: 200,
67+
headers: {'Content-Type' => 'application/json'},
68+
body: {
69+
content: content
70+
}
71+
)
72+
end
6673

67-
def message?
68-
true
69-
end
74+
def http?
75+
false
76+
end
7077

71-
def validate!
72-
raise Pact::InvalidMessageError.new(self) unless description && content
73-
end
78+
def message?
79+
true
80+
end
7481

75-
def == other
76-
other.is_a?(Message) && to_hash == other.to_hash
77-
end
82+
def validate!
83+
raise Pact::InvalidMessageError.new(self) unless description && content
84+
end
85+
86+
def == other
87+
other.is_a?(Message) && to_hash == other.to_hash
88+
end
7889

79-
def matches_criteria? criteria
80-
criteria.each do | key, value |
81-
unless match_criterion self.send(key.to_s), value
82-
return false
90+
def matches_criteria? criteria
91+
criteria.each do | key, value |
92+
unless match_criterion self.send(key.to_s), value
93+
return false
94+
end
8395
end
96+
true
8497
end
85-
true
86-
end
8798

88-
def match_criterion target, criterion
89-
target == criterion || (criterion.is_a?(Regexp) && criterion.match(target))
90-
end
99+
def match_criterion target, criterion
100+
target == criterion || (criterion.is_a?(Regexp) && criterion.match(target))
101+
end
102+
103+
def eq? other
104+
self == other
105+
end
91106

92-
def eq? other
93-
self == other
94-
end
107+
def description_with_provider_state_quoted
108+
provider_state ? "\"#{description}\" given \"#{provider_state}\"" : "\"#{description}\""
109+
end
95110

96-
def description_with_provider_state_quoted
97-
provider_state ? "\"#{description}\" given \"#{provider_state}\"" : "\"#{description}\""
98-
end
111+
def to_s
112+
to_hash.to_s
113+
end
114+
end
115+
end
116+
end
99117

100-
def to_s
101-
to_hash.to_s
102-
end
103-
end
118+
module Pact::Message
119+
def self.new *args
120+
Pact::ConsumerContract::Message.new(*args)
121+
end
104122
end

lib/pact/consumer_contract/message/content.rb

+20-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
module Pact
2-
class Message
3-
class Content < Hash
4-
include ActiveSupportSupport
5-
include SymbolizeKeys
2+
class ConsumerContract
3+
class Message
4+
class Content
5+
include ActiveSupportSupport
6+
include SymbolizeKeys
67

7-
def initialize hash
8-
merge!(hash)
8+
def initialize content
9+
@content = content
10+
end
11+
12+
def to_s
13+
if @content.is_a?(Hash) || @content.is_a?(Array)
14+
@content.to_json
15+
else
16+
@content.to_s
17+
end
18+
end
19+
20+
def as_json
21+
@content
22+
end
923
end
1024
end
1125
end

spec/lib/pact/consumer_contract/consumer_contract_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ module Pact
6262

6363
it "should have messages" do
6464
expect(loaded_pact.interactions).to be_instance_of Array
65-
expect(loaded_pact.interactions.first).to be_instance_of Pact::Message
65+
expect(loaded_pact.interactions.first).to be_instance_of Pact::ConsumerContract::Message
6666
end
6767

6868
it "should have a consumer" do

0 commit comments

Comments
 (0)