|
5 | 5 | require 'pact/errors'
|
6 | 6 | require 'pact/consumer/request'
|
7 | 7 | require 'pact/consumer_contract/response'
|
| 8 | +require 'pact/consumer_contract/message/content' |
8 | 9 |
|
9 | 10 | 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 | + { |
44 | 32 | description: description,
|
45 |
| - providerStates: [{ |
46 |
| - name: provider_state |
47 |
| - }] |
| 33 | + provider_state: provider_state, |
| 34 | + content: content.to_hash, |
48 | 35 | }
|
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 |
59 | 44 | }
|
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 |
62 | 62 |
|
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 |
66 | 73 |
|
67 |
| - def message? |
68 |
| - true |
69 |
| - end |
| 74 | + def http? |
| 75 | + false |
| 76 | + end |
70 | 77 |
|
71 |
| - def validate! |
72 |
| - raise Pact::InvalidMessageError.new(self) unless description && content |
73 |
| - end |
| 78 | + def message? |
| 79 | + true |
| 80 | + end |
74 | 81 |
|
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 |
78 | 89 |
|
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 |
83 | 95 | end
|
| 96 | + true |
84 | 97 | end
|
85 |
| - true |
86 |
| - end |
87 | 98 |
|
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 |
91 | 106 |
|
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 |
95 | 110 |
|
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 |
99 | 117 |
|
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 |
104 | 122 | end
|
0 commit comments