pacto 0.3.1 → 0.4.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (204) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.rubocop.yml +29 -7
  4. data/.travis.yml +8 -1
  5. data/CONTRIBUTING.md +3 -6
  6. data/Gemfile +13 -2
  7. data/Guardfile +4 -4
  8. data/Procfile +1 -0
  9. data/README.md +47 -13
  10. data/Rakefile +66 -19
  11. data/TODO.md +33 -10
  12. data/bin/pacto +4 -0
  13. data/changelog.md +30 -0
  14. data/docs/configuration.md +69 -0
  15. data/docs/consumer.md +18 -0
  16. data/docs/cops.md +39 -0
  17. data/docs/forensics.md +66 -0
  18. data/docs/generation.md +65 -0
  19. data/docs/rake_tasks.md +10 -0
  20. data/docs/rspec.md +0 -0
  21. data/docs/samples.md +133 -0
  22. data/docs/server.md +34 -0
  23. data/docs/server_cli.md +18 -0
  24. data/docs/stenographer.md +20 -0
  25. data/features/configuration/strict_matchers.feature +10 -10
  26. data/features/evolve/existing_services.feature +12 -10
  27. data/features/generate/generation.feature +11 -11
  28. data/features/steps/pacto_steps.rb +17 -12
  29. data/features/stub/templates.feature +4 -4
  30. data/features/support/env.rb +21 -9
  31. data/features/validate/meta_validation.feature +9 -17
  32. data/features/validate/validation.feature +5 -6
  33. data/lib/pacto.rb +41 -33
  34. data/lib/pacto/actor.rb +5 -0
  35. data/lib/pacto/actors/from_examples.rb +67 -0
  36. data/lib/pacto/actors/json_generator.rb +20 -0
  37. data/lib/pacto/cli.rb +75 -0
  38. data/lib/pacto/cli/helpers.rb +20 -0
  39. data/lib/pacto/consumer.rb +80 -0
  40. data/lib/pacto/consumer/faraday_driver.rb +34 -0
  41. data/lib/pacto/contract.rb +48 -20
  42. data/lib/pacto/contract_builder.rb +125 -0
  43. data/lib/pacto/contract_factory.rb +31 -12
  44. data/lib/pacto/contract_files.rb +1 -0
  45. data/lib/pacto/contract_set.rb +12 -0
  46. data/lib/pacto/cops.rb +46 -0
  47. data/lib/pacto/cops/body_cop.rb +23 -0
  48. data/lib/pacto/cops/request_body_cop.rb +10 -0
  49. data/lib/pacto/cops/response_body_cop.rb +10 -0
  50. data/lib/pacto/{validators/response_header_validator.rb → cops/response_header_cop.rb} +9 -15
  51. data/lib/pacto/cops/response_status_cop.rb +18 -0
  52. data/lib/pacto/core/configuration.rb +16 -5
  53. data/lib/pacto/core/contract_registry.rb +13 -32
  54. data/lib/pacto/core/hook.rb +1 -0
  55. data/lib/pacto/core/http_middleware.rb +23 -0
  56. data/lib/pacto/core/investigation_registry.rb +60 -0
  57. data/lib/pacto/core/modes.rb +1 -0
  58. data/lib/pacto/core/pacto_request.rb +59 -0
  59. data/lib/pacto/core/pacto_response.rb +41 -0
  60. data/lib/pacto/dash.rb +9 -0
  61. data/lib/pacto/erb_processor.rb +1 -0
  62. data/lib/pacto/exceptions/invalid_contract.rb +1 -0
  63. data/lib/pacto/extensions.rb +3 -16
  64. data/lib/pacto/forensics/investigation_filter.rb +90 -0
  65. data/lib/pacto/forensics/investigation_matcher.rb +80 -0
  66. data/lib/pacto/generator.rb +31 -53
  67. data/lib/pacto/generator/filters.rb +8 -7
  68. data/lib/pacto/generator/hint.rb +26 -0
  69. data/lib/pacto/generator/native_contract_generator.rb +74 -0
  70. data/lib/pacto/hooks/erb_hook.rb +2 -1
  71. data/lib/pacto/investigation.rb +49 -0
  72. data/lib/pacto/logger.rb +1 -0
  73. data/lib/pacto/meta_schema.rb +12 -6
  74. data/lib/pacto/native_contract_factory.rb +60 -0
  75. data/lib/pacto/observers/stenographer.rb +42 -0
  76. data/lib/pacto/provider.rb +27 -0
  77. data/lib/pacto/rake_task.rb +25 -70
  78. data/lib/pacto/request_clause.rb +31 -29
  79. data/lib/pacto/request_pattern.rb +20 -3
  80. data/lib/pacto/resettable.rb +22 -0
  81. data/lib/pacto/response_clause.rb +5 -12
  82. data/lib/pacto/rspec.rb +38 -31
  83. data/lib/pacto/server.rb +4 -0
  84. data/lib/pacto/stubs/uri_pattern.rb +21 -11
  85. data/lib/pacto/stubs/webmock_adapter.rb +69 -34
  86. data/lib/pacto/swagger_contract_factory.rb +90 -0
  87. data/lib/pacto/test_helper.rb +37 -0
  88. data/lib/pacto/ui.rb +32 -2
  89. data/lib/pacto/uri.rb +2 -1
  90. data/lib/pacto/version.rb +2 -1
  91. data/pacto-server.gemspec +24 -0
  92. data/pacto.gemspec +13 -9
  93. data/resources/contract_schema.json +46 -18
  94. data/resources/draft-04.json +150 -0
  95. data/sample_apis/album/cover_api.rb +12 -0
  96. data/sample_apis/config.ru +25 -0
  97. data/sample_apis/echo_api.rb +26 -0
  98. data/sample_apis/files_api.rb +50 -0
  99. data/sample_apis/hello_api.rb +14 -0
  100. data/sample_apis/ping_api.rb +11 -0
  101. data/sample_apis/reverse_api.rb +20 -0
  102. data/samples/README.md +11 -0
  103. data/samples/Rakefile +2 -0
  104. data/samples/configuration.rb +33 -0
  105. data/samples/consumer.rb +15 -0
  106. data/samples/contracts/README.md +1 -0
  107. data/samples/contracts/contract.js +93 -0
  108. data/samples/contracts/get_album_cover.json +48 -0
  109. data/samples/contracts/localhost/api/echo.json +37 -0
  110. data/samples/contracts/localhost/api/ping.json +38 -0
  111. data/samples/cops.rb +30 -0
  112. data/samples/forensics.rb +54 -0
  113. data/samples/generation.rb +48 -0
  114. data/samples/rake_tasks.sh +7 -0
  115. data/samples/rspec.rb +1 -0
  116. data/samples/samples.rb +92 -0
  117. data/samples/scripts/bootstrap +2 -0
  118. data/samples/scripts/wrapper +11 -0
  119. data/samples/server.rb +24 -0
  120. data/samples/server_cli.sh +12 -0
  121. data/samples/stenographer.rb +17 -0
  122. data/spec/coveralls_helper.rb +1 -0
  123. data/spec/fabricators/contract_fabricator.rb +94 -0
  124. data/spec/fabricators/http_fabricator.rb +48 -0
  125. data/spec/fabricators/webmock_fabricator.rb +24 -0
  126. data/spec/{unit/data → fixtures/contracts}/contract.json +2 -2
  127. data/spec/fixtures/contracts/contract_with_examples.json +58 -0
  128. data/spec/{unit/data → fixtures/contracts}/simple_contract.json +5 -3
  129. data/spec/{integration/data → fixtures/contracts}/strict_contract.json +5 -3
  130. data/spec/{integration/data → fixtures/contracts}/templating_contract.json +3 -2
  131. data/spec/{integration/data/simple_contract.json → fixtures/deprecated_contracts/deprecated_contract.json} +2 -1
  132. data/spec/fixtures/swagger/petstore.yaml +101 -0
  133. data/spec/integration/e2e_spec.rb +19 -20
  134. data/spec/integration/forensics/integration_matcher_spec.rb +90 -0
  135. data/spec/integration/rspec_spec.rb +22 -25
  136. data/spec/integration/templating_spec.rb +7 -6
  137. data/spec/pacto/dummy_server.rb +4 -0
  138. data/spec/pacto/{server → dummy_server}/dummy.rb +7 -6
  139. data/spec/pacto/dummy_server/jruby_workaround_helper.rb +23 -0
  140. data/spec/pacto/{server → dummy_server}/playback_servlet.rb +3 -2
  141. data/spec/spec_helper.rb +16 -7
  142. data/spec/unit/actors/from_examples_spec.rb +70 -0
  143. data/spec/unit/actors/json_generator_spec.rb +105 -0
  144. data/spec/unit/pacto/actor_spec.rb +23 -0
  145. data/spec/unit/pacto/configuration_spec.rb +7 -6
  146. data/spec/unit/pacto/consumer/faraday_driver_spec.rb +40 -0
  147. data/spec/unit/pacto/contract_builder_spec.rb +89 -0
  148. data/spec/unit/pacto/contract_factory_spec.rb +62 -11
  149. data/spec/unit/pacto/contract_files_spec.rb +1 -0
  150. data/spec/unit/pacto/contract_set_spec.rb +36 -0
  151. data/spec/unit/pacto/contract_spec.rb +51 -39
  152. data/spec/unit/pacto/cops/body_cop_spec.rb +107 -0
  153. data/spec/unit/pacto/{validators/response_header_validator_spec.rb → cops/response_header_cop_spec.rb} +30 -19
  154. data/spec/unit/pacto/cops/response_status_cop_spec.rb +26 -0
  155. data/spec/unit/pacto/cops_spec.rb +75 -0
  156. data/spec/unit/pacto/core/configuration_spec.rb +6 -5
  157. data/spec/unit/pacto/core/contract_registry_spec.rb +16 -83
  158. data/spec/unit/pacto/core/http_middleware_spec.rb +36 -0
  159. data/spec/unit/pacto/core/investigation_spec.rb +62 -0
  160. data/spec/unit/pacto/core/modes_spec.rb +5 -4
  161. data/spec/unit/pacto/erb_processor_spec.rb +3 -2
  162. data/spec/unit/pacto/extensions_spec.rb +10 -20
  163. data/spec/unit/pacto/generator/filters_spec.rb +11 -10
  164. data/spec/unit/pacto/generator/native_contract_generator_spec.rb +171 -0
  165. data/spec/unit/{hooks → pacto/hooks}/erb_hook_spec.rb +18 -11
  166. data/spec/unit/pacto/investigation_registry_spec.rb +77 -0
  167. data/spec/unit/pacto/logger_spec.rb +6 -5
  168. data/spec/unit/pacto/meta_schema_spec.rb +5 -4
  169. data/spec/unit/pacto/native_contract_factory_spec.rb +26 -0
  170. data/spec/unit/pacto/pacto_spec.rb +13 -28
  171. data/spec/unit/pacto/request_clause_spec.rb +16 -51
  172. data/spec/unit/pacto/request_pattern_spec.rb +6 -5
  173. data/spec/unit/pacto/response_clause_spec.rb +6 -19
  174. data/spec/unit/pacto/server/playback_servlet_spec.rb +21 -18
  175. data/spec/unit/pacto/stubs/observers/stenographer_spec.rb +33 -0
  176. data/spec/unit/pacto/stubs/uri_pattern_spec.rb +39 -11
  177. data/spec/unit/pacto/stubs/webmock_adapter_spec.rb +67 -117
  178. data/spec/unit/pacto/swagger_contract_factory_spec.rb +56 -0
  179. data/spec/unit/pacto/uri_spec.rb +1 -0
  180. data/tasks/release.rake +57 -0
  181. metadata +247 -76
  182. data/.rubocop-todo.yml +0 -24
  183. data/.ruby-gemset +0 -1
  184. data/.ruby-version +0 -1
  185. data/CHANGELOG +0 -12
  186. data/features/validate/body_only.feature +0 -85
  187. data/lib/pacto/contract_list.rb +0 -17
  188. data/lib/pacto/contract_validator.rb +0 -29
  189. data/lib/pacto/core/validation_registry.rb +0 -40
  190. data/lib/pacto/stubs/webmock_helper.rb +0 -69
  191. data/lib/pacto/validation.rb +0 -54
  192. data/lib/pacto/validators/body_validator.rb +0 -49
  193. data/lib/pacto/validators/request_body_validator.rb +0 -26
  194. data/lib/pacto/validators/response_body_validator.rb +0 -26
  195. data/lib/pacto/validators/response_status_validator.rb +0 -24
  196. data/spec/pacto/server.rb +0 -2
  197. data/spec/unit/pacto/contract_list_spec.rb +0 -35
  198. data/spec/unit/pacto/contract_validator_spec.rb +0 -85
  199. data/spec/unit/pacto/core/validation_registry_spec.rb +0 -76
  200. data/spec/unit/pacto/core/validation_spec.rb +0 -60
  201. data/spec/unit/pacto/generator_spec.rb +0 -132
  202. data/spec/unit/pacto/stubs/webmock_helper_spec.rb +0 -20
  203. data/spec/unit/pacto/validators/body_validator_spec.rb +0 -118
  204. data/spec/unit/pacto/validators/response_status_validator_spec.rb +0 -20
@@ -0,0 +1,105 @@
1
+ # -*- encoding : utf-8 -*-
2
+ RSpec.shared_examples 'uses defaults' do
3
+ it 'uses the default values for the request' do
4
+ expect(request.body['foo']).to eq 'custom default value'
5
+ end
6
+
7
+ it 'uses the default values for the response' do
8
+ response = generator.build_response contract # , request
9
+ expect(response.body['foo']).to eq 'custom default value'
10
+ end
11
+ end
12
+
13
+ RSpec.shared_examples 'uses dumb values' do
14
+ it 'uses dumb values (request)' do
15
+ expect(request.body['foo']).to eq 'bar'
16
+ end
17
+
18
+ it 'uses dumb values (response)' do
19
+ response = generator.build_response contract # , request
20
+ expect(response.body['foo']).to eq 'bar'
21
+ end
22
+ end
23
+
24
+ module Pacto
25
+ module Actors
26
+ describe JSONGenerator do
27
+ subject(:generator) { described_class.new }
28
+ let(:request_clause) { Fabricate(:request_clause, schema: schema) }
29
+ let(:response_clause) { Fabricate(:response_clause, schema: schema) }
30
+ let(:contract) { Fabricate(:contract, request: request_clause, response: response_clause) }
31
+ let(:request) { generator.build_request contract }
32
+
33
+ context 'using default values from schema' do
34
+ context 'draft3' do
35
+ let(:schema) do
36
+ {
37
+ '$schema' => 'http://json-schema.org/draft-03/schema#',
38
+ 'type' => 'object',
39
+ 'required' => true,
40
+ 'properties' => {
41
+ 'foo' => {
42
+ 'type' => 'string',
43
+ 'required' => true,
44
+ 'default' => 'custom default value'
45
+ }
46
+ }
47
+ }
48
+ end
49
+ include_examples 'uses defaults'
50
+ end
51
+ context 'draft4' do
52
+ let(:schema) do
53
+ {
54
+ '$schema' => 'http://json-schema.org/draft-04/schema#',
55
+ 'type' => 'object',
56
+ 'required' => ['foo'],
57
+ 'properties' => {
58
+ 'foo' => {
59
+ 'type' => 'string',
60
+ 'default' => 'custom default value'
61
+ }
62
+ }
63
+ }
64
+ end
65
+ skip 'draft4 is not supported by JSONGenerator'
66
+ # include_examples 'uses defaults'
67
+ end
68
+ end
69
+ context 'using dumb values (no defaults)' do
70
+ context 'draft3' do
71
+ let(:schema) do
72
+ {
73
+ '$schema' => 'http://json-schema.org/draft-03/schema#',
74
+ 'type' => 'object',
75
+ 'required' => true,
76
+ 'properties' => {
77
+ 'foo' => {
78
+ 'type' => 'string',
79
+ 'required' => true
80
+ }
81
+ }
82
+ }
83
+ end
84
+ include_examples 'uses dumb values'
85
+ end
86
+ context 'draft4' do
87
+ let(:schema) do
88
+ {
89
+ '$schema' => 'http://json-schema.org/draft-04/schema#',
90
+ 'type' => 'object',
91
+ 'required' => ['foo'],
92
+ 'properties' => {
93
+ 'foo' => {
94
+ 'type' => 'string'
95
+ }
96
+ }
97
+ }
98
+ end
99
+ skip 'draft4 is not supported by JSONGenerator'
100
+ # include_examples 'uses dumb values'
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding : utf-8 -*-
2
+ RSpec.shared_examples 'an actor' do
3
+ # let(:contract) { Fabricate(:contract) }
4
+ let(:data) do
5
+ {}
6
+ end
7
+
8
+ describe '#build_request' do
9
+ let(:request) { subject.build_request contract, data }
10
+ it 'creates a PactoRequest' do
11
+ expect(request).to be_an_instance_of Pacto::PactoRequest
12
+ end
13
+ end
14
+
15
+ describe '#build_response' do
16
+ # Shouldn't build response be building a response for a request?
17
+ # let(:request) { Fabricate :pacto_request }
18
+ let(:response) { subject.build_response contract, data }
19
+ it 'creates a PactoResponse' do
20
+ expect(response).to be_an_instance_of Pacto::PactoResponse
21
+ end
22
+ end
23
+ end
@@ -1,18 +1,19 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  module Pacto
2
3
  describe Configuration do
3
- subject(:configuration) { Configuration.new }
4
+ subject(:configuration) { described_class.new }
4
5
  let(:contracts_path) { 'path_to_contracts' }
5
6
 
6
- it 'sets the stub provider by default to WebMockAdapter' do
7
- expect(configuration.provider).to be_kind_of Stubs::WebMockAdapter
7
+ it 'sets the http adapter by default to WebMockAdapter' do
8
+ expect(configuration.adapter).to be_kind_of Stubs::WebMockAdapter
8
9
  end
9
10
 
10
11
  it 'sets strict matchers by default to true' do
11
- expect(configuration.strict_matchers).to be_true
12
+ expect(configuration.strict_matchers).to be true
12
13
  end
13
14
 
14
- it 'sets contracts path by default to nil' do
15
- expect(configuration.contracts_path).to be_nil
15
+ it 'sets contracts path by default to .' do
16
+ expect(configuration.contracts_path).to eq('.')
16
17
  end
17
18
 
18
19
  it 'sets logger by default to Logger' do
@@ -0,0 +1,40 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Pacto
3
+ class Consumer
4
+ describe FaradayDriver do
5
+ subject(:strategy) { described_class.new }
6
+ let(:get_request) { Fabricate(:pacto_request, method: :get, host: 'http://localhost/', path: 'hello_world', params: { 'foo' => 'bar' }) }
7
+ let(:post_request) { Fabricate(:pacto_request, method: :post, host: 'http://localhost/', path: 'hello_world', params: { 'foo' => 'bar' }) }
8
+
9
+ describe '#execute' do
10
+
11
+ before do
12
+ WebMock.stub_request(:get, 'http://localhost/hello_world?foo=bar').
13
+ to_return(status: 200, body: '', headers: {})
14
+ WebMock.stub_request(:post, 'http://localhost/hello_world?foo=bar').
15
+ to_return(status: 200, body: '', headers: {})
16
+ end
17
+
18
+ context 'for any request' do
19
+ it 'returns the a Pacto::PactoResponse' do
20
+ expect(strategy.execute get_request).to be_a Pacto::PactoResponse
21
+ end
22
+ end
23
+
24
+ context 'for a GET request' do
25
+ it 'makes the request thru the http client' do
26
+ strategy.execute get_request
27
+ expect(WebMock).to have_requested(:get, 'http://localhost/hello_world?foo=bar')
28
+ end
29
+ end
30
+
31
+ context 'for a POST request' do
32
+ it 'makes the request thru the http client' do
33
+ strategy.execute post_request
34
+ expect(WebMock).to have_requested(:post, 'http://localhost/hello_world?foo=bar')
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,89 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Pacto
3
+ describe ContractBuilder do
4
+ let(:data) { subject.build_hash }
5
+ describe '#name' do
6
+ it 'sets the contract name' do
7
+ subject.name = 'foo'
8
+ expect(data).to include(name: 'foo')
9
+ end
10
+ end
11
+
12
+ describe '#add_example' do
13
+ let(:examples) { subject.build_hash[:examples] }
14
+ it 'adds named examples to the contract' do
15
+ subject.add_example 'foo', Fabricate(:pacto_request), Fabricate(:pacto_response)
16
+ subject.add_example 'bar', Fabricate(:pacto_request), Fabricate(:pacto_response)
17
+ expect(examples).to be_a(Hash)
18
+ expect(examples.keys).to include('foo', 'bar')
19
+ expect(examples['foo'][:response]).to include(status: 200)
20
+ expect(data)
21
+ end
22
+ end
23
+
24
+ context 'without examples' do
25
+ describe '#infer_schemas' do
26
+ it 'does not add schemas' do
27
+ subject.name = 'test'
28
+ subject.infer_schemas
29
+ expect(data[:request][:schema]).to be_nil
30
+ expect(data[:response][:schema]).to be_nil
31
+ end
32
+ end
33
+ end
34
+
35
+ context 'with examples' do
36
+ before(:each) do
37
+ subject.add_example 'success', Fabricate(:pacto_request), Fabricate(:pacto_response)
38
+ subject.add_example 'not found', Fabricate(:pacto_request), Fabricate(:pacto_response)
39
+ end
40
+
41
+ describe '#without_examples' do
42
+ it 'stops the builder from including examples in the final data' do
43
+ expect(subject.build_hash.keys).to include(:examples)
44
+ expect(subject.without_examples.build_hash.keys).to_not include(:examples)
45
+ end
46
+ end
47
+
48
+ describe '#infer_schemas' do
49
+ it 'adds schemas' do
50
+ subject.name = 'test'
51
+ subject.infer_schemas
52
+ contract = subject.build
53
+ expect(contract.request.schema).to_not be_nil
54
+ expect(contract.request.schema).to_not be_nil
55
+ end
56
+ end
57
+ end
58
+
59
+ context 'generating from interactions' do
60
+ let(:request) { Fabricate(:pacto_request) }
61
+ let(:response) { Fabricate(:pacto_response) }
62
+ let(:data) { subject.generate_response(request, response).build_hash }
63
+ let(:contract) { subject.generate_contract(request, response).build }
64
+
65
+ describe '#generate_response' do
66
+ it 'sets the response status' do
67
+ expect(data[:response]).to include(
68
+ status: 200
69
+ )
70
+ end
71
+
72
+ it 'sets response headers' do
73
+ expect(data[:response][:headers]).to be_a(Hash)
74
+ end
75
+ end
76
+
77
+ describe '#infer_schemas' do
78
+ it 'sets the schemas based on the examples' do
79
+ expect(contract.request.schema).to_not be_nil
80
+ expect(contract.request.schema).to_not be_nil
81
+ end
82
+ end
83
+ end
84
+
85
+ skip '#add_request_header'
86
+ skip '#add_response_header'
87
+ skip '#filter'
88
+ end
89
+ end
@@ -1,23 +1,74 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  require 'spec_helper'
2
3
 
3
4
  module Pacto
4
5
  describe ContractFactory do
5
6
  let(:host) { 'http://localhost' }
6
7
  let(:contract_name) { 'contract' }
7
- let(:contracts_path) { %w(spec unit data) }
8
+ let(:contracts_path) { %w(spec fixtures contracts) }
8
9
  let(:contract_path) { File.join(contracts_path, "#{contract_name}.json") }
9
- subject(:contract_factory) { ContractFactory.new }
10
+ let(:contract_files) { [contract_path, contract_path] }
11
+ subject(:contract_factory) { described_class }
10
12
 
11
- it 'builds a contract given a JSON file path and a host' do
12
- contract = contract_factory.build_from_file(contract_path, host)
13
- expect(contract).to be_a(Contract)
14
- end
13
+ describe '#build' do
14
+ context 'default contract format' do
15
+ it 'builds contracts from a list of file paths and a host' do
16
+ contracts = contract_factory.build(contract_files, host)
17
+ contracts.each do |contract|
18
+ expect(contract).to be_a(Contract)
19
+ end
20
+ end
21
+ end
22
+
23
+ context 'custom format' do
24
+ let(:dummy_contract) { double }
25
+
26
+ class CustomContractFactory
27
+ def initialize(dummy_contract)
28
+ @dummy_contract = dummy_contract # rubocop:disable RSpec/InstanceVariable
29
+ end
30
+
31
+ def build_from_file(_contract_path, _host)
32
+ @dummy_contract # rubocop:disable RSpec/InstanceVariable
33
+ end
34
+ end
35
+
36
+ before do
37
+ subject.add_factory :custom, CustomContractFactory.new(dummy_contract)
38
+ end
39
+
40
+ it 'delegates to the registered factory' do
41
+ expect(contract_factory.build(contract_files, host, :custom)).to eq([dummy_contract, dummy_contract])
42
+ end
43
+ end
44
+
45
+ context 'flattening' do
46
+ let(:contracts_per_file) { 4 }
47
+
48
+ class MultiContractFactory
49
+ def initialize(contracts)
50
+ @contracts = contracts # rubocop:disable RSpec/InstanceVariable
51
+ end
52
+
53
+ def build_from_file(_contract_path, _host)
54
+ @contracts # rubocop:disable RSpec/InstanceVariable
55
+ end
56
+ end
57
+
58
+ before do
59
+ contracts = contracts_per_file.times.map do
60
+ double
61
+ end
62
+ subject.add_factory :multi, MultiContractFactory.new(contracts)
63
+ end
15
64
 
16
- it 'builds contracts from a list of file paths and a host' do
17
- contract_files = [contract_path, contract_path]
18
- contracts = contract_factory.build(contract_files, host)
19
- contracts.each do |contract|
20
- expect(contract).to be_a(Contract)
65
+ it 'delegates to the registered factory' do
66
+ loaded_contracts = contract_factory.build(contract_files, host, :multi)
67
+ expected_size = contracts_per_file * contract_files.size
68
+ # If the ContractFactory doesn't flatten returned contracts the size will be off. It needs
69
+ # to flatten because some factories load a single contract per file, others load multiple.
70
+ expect(loaded_contracts.size).to eq(expected_size)
71
+ end
21
72
  end
22
73
  end
23
74
  end
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  require 'spec_helper'
2
3
  require 'fileutils'
3
4
 
@@ -0,0 +1,36 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ module Pacto
5
+ describe ContractSet do
6
+ let(:contract1) { Fabricate(:contract) }
7
+ let(:contract2) { Fabricate(:contract, request: Fabricate(:request_clause, host: 'www2.example.com')) }
8
+
9
+ it 'holds a list of contracts' do
10
+ list = ContractSet.new([contract1, contract2])
11
+ expect(list).to eq(Set.new([contract1, contract2]))
12
+ end
13
+
14
+ context 'when validating' do
15
+ it 'validates every contract on the list' do
16
+ expect(contract1).to receive(:simulate_request)
17
+ expect(contract2).to receive(:simulate_request)
18
+
19
+ list = ContractSet.new([contract1, contract2])
20
+ list.simulate_consumers
21
+ end
22
+ end
23
+
24
+ context 'when stubbing' do
25
+ let(:values) { Hash.new }
26
+
27
+ it 'stubs every contract on the list' do
28
+ expect(contract1).to receive(:stub_contract!).with(values)
29
+ expect(contract2).to receive(:stub_contract!).with(values)
30
+
31
+ list = ContractSet.new([contract1, contract2])
32
+ list.stub_providers(values)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -1,73 +1,85 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  module Pacto
2
3
  describe Contract do
3
- let(:request) { double 'request' }
4
- let(:response) { double 'response definition' }
5
- let(:provider) { double 'provider' }
6
- let(:file) { 'contranct.json' }
7
- let(:request_pattern_provider) { double(for: nil) }
8
-
9
- subject(:contract) { Contract.new(request, response, file, 'sample', request_pattern_provider) }
4
+ let(:request_clause) do
5
+ Pacto::RequestClause.new(
6
+ http_method: 'GET',
7
+ host: 'http://example.com',
8
+ path: '/',
9
+ schema: {
10
+ type: 'object',
11
+ required: true # , :properties => double('body definition properties')
12
+ }
13
+ )
14
+ end
15
+ let(:response_clause) do
16
+ Pacto::ResponseClause.new(status: 200)
17
+ end
18
+ let(:adapter) { double 'provider' }
19
+ let(:file) { 'contract.json' }
20
+ let(:consumer_driver) { double }
21
+ let(:provider_actor) { double }
10
22
 
11
- before do
12
- Pacto.configuration.provider = provider
23
+ subject(:contract) do
24
+ described_class.new(
25
+ request: request_clause,
26
+ response: response_clause,
27
+ file: file,
28
+ name: 'sample'
29
+ )
13
30
  end
14
31
 
15
- it 'has a request pattern' do
16
- pattern = double
17
- expect(request_pattern_provider).to receive(:for).and_return(pattern)
18
- expect(contract.request_pattern).to eq pattern
32
+ before do
33
+ Pacto.configuration.adapter = adapter
34
+ allow(consumer_driver).to receive(:respond_to?).with(:execute).and_return true
35
+ allow(provider_actor).to receive(:respond_to?).with(:build_response).and_return true
36
+ Pacto.configuration.default_consumer.driver = consumer_driver
37
+ Pacto.configuration.default_provider.actor = provider_actor
19
38
  end
20
39
 
21
40
  describe '#stub_contract!' do
22
41
  it 'register a stub for the contract' do
23
- provider.should_receive(:stub_request!).with(request, response)
42
+ expect(adapter).to receive(:stub_request!).with(contract)
24
43
  contract.stub_contract!
25
44
  end
26
45
  end
27
46
 
28
- context 'validations' do
29
- let(:fake_response) { double('fake response') }
30
- let(:validation_result) { double 'validation result' }
47
+ context 'investigations' do
48
+ let(:request) { Pacto.configuration.default_consumer.build_request contract }
49
+ let(:fake_response) { Fabricate(:pacto_response) } # double('fake response') }
50
+ let(:cop) { double 'cop' }
51
+ let(:investigation_citations) { [double('investigation result')] }
31
52
 
32
53
  before do
33
- allow(Pacto::ContractValidator).to receive(:validate).with(contract, request, fake_response, {}).and_return validation_result
34
- end
35
-
36
- describe '#validate_consumer' do
37
- it 'returns the result of the validation' do
38
- expect(Pacto::ContractValidator).to receive(:validate).with(contract, request, fake_response, {})
39
- expect(contract.validate_consumer request, fake_response).to eq validation_result
40
- end
41
-
42
- it 'does not generate another response' do
43
- request.should_not_receive :execute
44
- contract.validate_consumer request, fake_response
45
- end
54
+ Pacto::Cops.active_cops.clear
55
+ Pacto::Cops.active_cops << cop
56
+ allow(cop).to receive(:investigate).with(an_instance_of(Pacto::PactoRequest), fake_response, contract).and_return investigation_citations
46
57
  end
47
58
 
48
- describe '#validate_provider' do
59
+ describe '#simulate_request' do
49
60
  before do
50
- allow(request).to receive(:execute).and_return fake_response
61
+ allow(consumer_driver).to receive(:execute).with(an_instance_of(Pacto::PactoRequest)).and_return fake_response
51
62
  end
52
63
 
53
64
  it 'generates the response' do
54
- request.should_receive :execute
55
- contract.validate_provider
65
+ expect(consumer_driver).to receive(:execute).with(an_instance_of(Pacto::PactoRequest))
66
+ contract.simulate_request
56
67
  end
57
68
 
58
69
  it 'returns the result of the validating the generated response' do
59
- expect(Pacto::ContractValidator).to receive(:validate).with(contract, request, fake_response, {})
60
- expect(contract.validate_provider).to eq validation_result
70
+ expect(cop).to receive(:investigate).with(an_instance_of(Pacto::PactoRequest), fake_response, contract)
71
+ investigation = contract.simulate_request
72
+ expect(investigation.citations).to eq investigation_citations
61
73
  end
62
74
  end
63
75
  end
64
76
 
65
77
  describe '#matches?' do
66
- let(:request_pattern) { double(matches?: true) }
67
- let(:request_signature) { double }
78
+ let(:request_pattern) { double('request_pattern') }
79
+ let(:request_signature) { double('request_signature') }
68
80
 
69
81
  it 'delegates to the request pattern' do
70
- expect(request_pattern_provider).to receive(:for).and_return(request_pattern)
82
+ expect(Pacto::RequestPattern).to receive(:for).and_return(request_pattern)
71
83
  expect(request_pattern).to receive(:matches?).with(request_signature)
72
84
 
73
85
  contract.matches?(request_signature)