pact-support 1.2.99.alpha.1 → 1.3.0.alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bb6aa8891e3b73c96d764ca9dd4c43849dc2972b
4
- data.tar.gz: 79d896556e2d8eb9679aa41ae84200fe26fec695
3
+ metadata.gz: e10753ec318df69161ec6f2a534ea7fb131c3d51
4
+ data.tar.gz: 23b534ba416c93e6288f90e3da177fbc8dad0c66
5
5
  SHA512:
6
- metadata.gz: 9c3ca1df6aef0c0c5fa2944f9a75980dd5c2970d99c23c3940d8a1812fce409f9ac2f527733a3c9d06505453ae09b7dbddc6383170b62b1191c4036c0ffb6dac
7
- data.tar.gz: b16006ef3b27268ecfa08793847709f9aee236c4b7ebf11a8a539bf41ffc4f38e7a1e76d63149e04bf9fc7bc916b05660e799e3bf974f487c35ab52a9e951104
6
+ metadata.gz: 183ac6a64f3fe3fe5f2e698e33a18ca53f48f73b93fd27a2763555d83aff31004ab2c514178d4ea2c3e5090a7b7eb26dee4ce7d2423ec72d50ee7cfdaafd73b5
7
+ data.tar.gz: c40e1da71b32c101961908a4caa4bccf11a5918ae5084853588fcb019411d0b62896f673865558c50615cc56dc4a9b32d7f7555b2716300cccef012006dc07ba
@@ -9,10 +9,11 @@ require 'open-uri'
9
9
  require 'pact/consumer_contract/service_consumer'
10
10
  require 'pact/consumer_contract/service_provider'
11
11
  require 'pact/consumer_contract/interaction'
12
+ require 'pact/consumer_contract/message'
12
13
  require 'pact/consumer_contract/pact_file'
13
- require 'pact/consumer_contract/http_consumer_contract_parser'
14
14
 
15
15
  module Pact
16
+
16
17
  class ConsumerContract
17
18
 
18
19
  include SymbolizeKeys
@@ -29,19 +30,21 @@ module Pact
29
30
  @provider = attributes[:provider]
30
31
  end
31
32
 
32
- def self.add_parser consumer_contract_parser
33
- parsers << consumer_contract_parser
34
- end
35
-
36
- def self.parsers
37
- @parsers ||= [Pact::HttpConsumerContractParser.new]
38
- end
39
-
40
33
  def self.from_hash(hash)
41
- parsers.each do | parser |
42
- return parser.call(hash) if parser.can_parse?(hash)
34
+ hash = symbolize_keys(hash)
35
+ interactions = if hash[:interactions]
36
+ hash[:interactions].collect { |hash| Interaction.from_hash(hash)}
37
+ elsif hash[:messages]
38
+ hash[:messages].collect { |hash| Message.from_hash(hash)}
39
+ else
40
+ [] # or raise an error?
43
41
  end
44
- raise Pact::Error.new("No consumer contract parser found for hash: #{hash}")
42
+
43
+ new(
44
+ :consumer => ServiceConsumer.from_hash(hash[:consumer]),
45
+ :provider => ServiceProvider.from_hash(hash[:provider]),
46
+ :interactions => interactions
47
+ )
45
48
  end
46
49
 
47
50
  def self.from_json string
@@ -40,6 +40,10 @@ module Pact
40
40
  true
41
41
  end
42
42
 
43
+ def message?
44
+ false
45
+ end
46
+
43
47
  def validate!
44
48
  raise Pact::InvalidInteractionError.new(self) unless description && request && response
45
49
  end
@@ -0,0 +1,146 @@
1
+ require 'pact/consumer_contract/message/content'
2
+ require 'pact/symbolize_keys'
3
+ require 'pact/shared/active_support_support'
4
+ require 'pact/matching_rules'
5
+ require 'pact/errors'
6
+ require 'pact/consumer/request'
7
+ require 'pact/consumer_contract/response'
8
+ require 'pact/consumer_contract/message/content'
9
+
10
+ module Pact
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
+ {
32
+ description: description,
33
+ provider_state: provider_state,
34
+ content: content.to_hash,
35
+ }
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
44
+ }
45
+ end
46
+
47
+ def request
48
+ @request ||= Pact::Consumer::Request::Actual.from_hash(
49
+ path: '/',
50
+ method: 'POST',
51
+ query: nil,
52
+ headers: {'Content-Type' => 'application/json'},
53
+ body: {
54
+ description: description,
55
+ providerStates: [{
56
+ name: provider_state
57
+ }]
58
+ }
59
+ )
60
+ end
61
+
62
+ # custom media type?
63
+ def response
64
+ @response ||= Pact::Response.new(
65
+ status: 200,
66
+ headers: {'Content-Type' => 'application/json'},
67
+ body: {
68
+ content: content
69
+ }
70
+ )
71
+ end
72
+
73
+ def http?
74
+ false
75
+ end
76
+
77
+ def message?
78
+ true
79
+ end
80
+
81
+ def validate!
82
+ raise Pact::InvalidMessageError.new(self) unless description && content
83
+ end
84
+
85
+ def == other
86
+ other.is_a?(Message) && to_hash == other.to_hash
87
+ end
88
+
89
+ def matches_criteria? criteria
90
+ criteria.each do | key, value |
91
+ unless match_criterion self.send(key.to_s), value
92
+ return false
93
+ end
94
+ end
95
+ true
96
+ end
97
+
98
+ def match_criterion target, criterion
99
+ target == criterion || (criterion.is_a?(Regexp) && criterion.match(target))
100
+ end
101
+
102
+ def eq? other
103
+ self == other
104
+ end
105
+
106
+ def description_with_provider_state_quoted
107
+ provider_state ? "\"#{description}\" given \"#{provider_state}\"" : "\"#{description}\""
108
+ end
109
+
110
+ def to_s
111
+ to_hash.to_s
112
+ end
113
+ end
114
+ end
115
+ end
116
+
117
+ # I'm not sure whether to make Pact::Message a module or a class at this stage, so making
118
+ # the "public interface" to the pact-support library support Pact::Message.new either way
119
+
120
+ if Pact.const_defined?('Message') && Pact::Message.class == Module
121
+ module Pact
122
+ module Message
123
+ def self.new *args
124
+ Pact::ConsumerContract::Message.new(*args)
125
+ end
126
+
127
+ def self.from_hash *args
128
+ Pact::ConsumerContract::Message.from_hash(*args)
129
+ end
130
+ end
131
+ end
132
+ end
133
+
134
+ if Pact.const_defined?('Message') && Pact::Message.class == Class
135
+ module Pact
136
+ class Message
137
+ def self.new *args
138
+ Pact::ConsumerContract::Message.new(*args)
139
+ end
140
+
141
+ def self.from_hash *args
142
+ Pact::ConsumerContract::Message.from_hash(*args)
143
+ end
144
+ end
145
+ end
146
+ end
@@ -0,0 +1,61 @@
1
+ module Pact
2
+ class ConsumerContract
3
+ class Message
4
+ class Content
5
+ include ActiveSupportSupport
6
+ include SymbolizeKeys
7
+
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
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ # I'm not sure whether to make Pact::Message a module or a class at this stage, so making
29
+ # the "public interface" to the pact-support library support Pact::Message.new either way
30
+
31
+ if Pact.const_defined?('Message') && Pact::Message.class == Module
32
+ module Pact
33
+ module Message
34
+ class Content
35
+ def self.new *args
36
+ Pact::ConsumerContract::Message::Content.new(*args)
37
+ end
38
+
39
+ def self.from_hash *args
40
+ Pact::ConsumerContract::Message::Content.from_hash(*args)
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ if Pact.const_defined?('Message') && Pact::Message.class == Class
48
+ module Pact
49
+ class Message
50
+ class Content
51
+ def self.new *args
52
+ Pact::ConsumerContract::Message::Content.new(*args)
53
+ end
54
+ end
55
+
56
+ def self.from_hash *args
57
+ Pact::ConsumerContract::Message::Content.from_hash(*args)
58
+ end
59
+ end
60
+ end
61
+ end
@@ -2,6 +2,9 @@ module Pact
2
2
  class Error < ::StandardError
3
3
  end
4
4
 
5
+ class InvalidMessageError < Error
6
+ end
7
+
5
8
  # Raised when the interaction is not defined correctly
6
9
  class InvalidInteractionError < Error
7
10
  def initialize(interaction)
@@ -1,5 +1,5 @@
1
1
  module Pact
2
2
  module Support
3
- VERSION = "1.2.99.alpha.1"
3
+ VERSION = "1.3.0.alpha.1"
4
4
  end
5
5
  end
@@ -52,6 +52,28 @@ module Pact
52
52
  end
53
53
  end
54
54
  end
55
+
56
+ context "with a Message contract" do
57
+ let(:string) { '{"messages":[{"content": {"foo": "bar"}}], "consumer": {"name" : "Bob"} , "provider": {"name" : "Mary"}}' }
58
+
59
+ it "should create a Pact" do
60
+ expect(loaded_pact).to be_instance_of ConsumerContract
61
+ end
62
+
63
+ it "should have messages" do
64
+ expect(loaded_pact.interactions).to be_instance_of Array
65
+ expect(loaded_pact.interactions.first).to be_instance_of Pact::ConsumerContract::Message
66
+ end
67
+
68
+ it "should have a consumer" do
69
+ expect(loaded_pact.consumer).to be_instance_of Pact::ServiceConsumer
70
+ end
71
+
72
+ it "should have a provider" do
73
+ expect(loaded_pact.provider).to be_instance_of Pact::ServiceProvider
74
+ end
75
+
76
+ end
55
77
  end
56
78
 
57
79
  describe "find_interactions" do
@@ -0,0 +1,25 @@
1
+ # I'm not sure whether to make Pact::Message a module or a class at this stage, so making
2
+ # the "public interface" to the pact-support library support Pact::Message.new either way
3
+
4
+ module Pact
5
+ class Message; end
6
+ end
7
+
8
+ load 'pact/consumer_contract/message.rb'
9
+ load 'pact/consumer_contract/message/content.rb'
10
+
11
+ describe Pact::Message::Content do
12
+ describe "new" do
13
+ it "returns an instance of Pact::Message::ConsumerContract::Message::Content" do
14
+ expect(Pact::Message::Content.new('foo')).to be_a(Pact::ConsumerContract::Message::Content)
15
+ end
16
+ end
17
+ end
18
+
19
+ describe Pact::Message do
20
+ describe "new" do
21
+ it "returns an instance of Pact::Message::ConsumerContract::Message" do
22
+ expect(Pact::Message.new).to be_a(Pact::ConsumerContract::Message)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ # I'm not sure whether to make Pact::Message a module or a class at this stage, so making
2
+ # the "public interface" to the pact-support library support Pact::Message.new either way
3
+
4
+ module Pact
5
+ module Message; end
6
+ end
7
+
8
+ load 'pact/consumer_contract/message.rb'
9
+ load 'pact/consumer_contract/message/content.rb'
10
+
11
+ describe Pact::Message::Content do
12
+ describe "new" do
13
+ it "returns an instance of Pact::Message::ConsumerContract::Message::Content" do
14
+ expect(Pact::Message::Content.new('foo')).to be_a(Pact::ConsumerContract::Message::Content)
15
+ end
16
+ end
17
+ end
18
+
19
+ describe Pact::Message do
20
+ describe "new" do
21
+ it "returns an instance of Pact::Message::ConsumerContract::Message" do
22
+ expect(Pact::Message.new).to be_a(Pact::ConsumerContract::Message)
23
+ end
24
+ end
25
+ end
@@ -2,6 +2,14 @@ require 'rspec/core/rake_task'
2
2
 
3
3
  RSpec::Core::RakeTask.new(:spec)
4
4
 
5
+ RSpec::Core::RakeTask.new(:spec_with_message_class) do | task |
6
+ task.pattern = "spec/lib/pact/consumer_contract/message_spec_with_message_class.rb"
7
+ end
8
+
9
+ RSpec::Core::RakeTask.new(:spec_with_message_module) do | task |
10
+ task.pattern = "spec/lib/pact/consumer_contract/message_spec_with_message_module.rb"
11
+ end
12
+
5
13
  task :set_active_support_on do
6
14
  ENV["LOAD_ACTIVE_SUPPORT"] = 'true'
7
15
  end
@@ -11,4 +19,4 @@ task :spec_with_active_support => [:set_active_support_on] do
11
19
  Rake::Task['spec'].execute
12
20
  end
13
21
 
14
- task :default => [:spec, :spec_with_active_support]
22
+ task :default => [:spec, :spec_with_active_support, :spec_with_message_class, :spec_with_message_module]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pact-support
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.99.alpha.1
4
+ version: 1.3.0.alpha.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Fraser
@@ -12,7 +12,7 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2018-03-08 00:00:00.000000000 Z
15
+ date: 2018-03-01 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: randexp
@@ -240,8 +240,9 @@ files:
240
240
  - lib/pact/consumer_contract/consumer_contract.rb
241
241
  - lib/pact/consumer_contract/file_name.rb
242
242
  - lib/pact/consumer_contract/headers.rb
243
- - lib/pact/consumer_contract/http_consumer_contract_parser.rb
244
243
  - lib/pact/consumer_contract/interaction.rb
244
+ - lib/pact/consumer_contract/message.rb
245
+ - lib/pact/consumer_contract/message/content.rb
245
246
  - lib/pact/consumer_contract/pact_file.rb
246
247
  - lib/pact/consumer_contract/query.rb
247
248
  - lib/pact/consumer_contract/query_hash.rb
@@ -304,6 +305,8 @@ files:
304
305
  - spec/lib/pact/consumer_contract/file_name_spec.rb
305
306
  - spec/lib/pact/consumer_contract/headers_spec.rb
306
307
  - spec/lib/pact/consumer_contract/interaction_spec.rb
308
+ - spec/lib/pact/consumer_contract/message_spec_with_message_class.rb
309
+ - spec/lib/pact/consumer_contract/message_spec_with_message_module.rb
307
310
  - spec/lib/pact/consumer_contract/pact_file_spec.rb
308
311
  - spec/lib/pact/consumer_contract/query_hash_spec.rb
309
312
  - spec/lib/pact/consumer_contract/query_string_spec.rb
@@ -399,6 +402,8 @@ test_files:
399
402
  - spec/lib/pact/consumer_contract/file_name_spec.rb
400
403
  - spec/lib/pact/consumer_contract/headers_spec.rb
401
404
  - spec/lib/pact/consumer_contract/interaction_spec.rb
405
+ - spec/lib/pact/consumer_contract/message_spec_with_message_class.rb
406
+ - spec/lib/pact/consumer_contract/message_spec_with_message_module.rb
402
407
  - spec/lib/pact/consumer_contract/pact_file_spec.rb
403
408
  - spec/lib/pact/consumer_contract/query_hash_spec.rb
404
409
  - spec/lib/pact/consumer_contract/query_string_spec.rb
@@ -1,26 +0,0 @@
1
- module Pact
2
- class HttpConsumerContractParser
3
- include SymbolizeKeys
4
-
5
- def call(hash)
6
- hash = symbolize_keys(hash)
7
- interactions = if hash[:interactions]
8
- hash[:interactions].collect { |hash| Interaction.from_hash(hash)}
9
- elsif hash[:messages]
10
- hash[:messages].collect { |hash| Message.from_hash(hash)}
11
- else
12
- [] # or raise an error?
13
- end
14
-
15
- ConsumerContract.new(
16
- :consumer => ServiceConsumer.from_hash(hash[:consumer]),
17
- :provider => ServiceProvider.from_hash(hash[:provider]),
18
- :interactions => interactions
19
- )
20
- end
21
-
22
- def can_parse?(hash)
23
- hash.key?('interactions') || hash.key?(:interactions)
24
- end
25
- end
26
- end