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,77 @@
1
+ # -*- encoding : utf-8 -*-
2
+ describe Pacto::InvestigationRegistry do
3
+ subject(:registry) { described_class.instance }
4
+ let(:request_pattern) { Fabricate(:webmock_request_pattern) }
5
+ let(:request_signature) { Fabricate(:webmock_request_signature) }
6
+ let(:pacto_response) { Fabricate(:pacto_response) }
7
+ let(:different_request_signature) { Fabricate(:webmock_request_signature, uri: 'www.thoughtworks.com') }
8
+ let(:investigation) { Pacto::Investigation.new(request_signature, pacto_response, nil, []) }
9
+ let(:investigation_for_a_similar_request) { Pacto::Investigation.new(request_signature, pacto_response, nil, []) }
10
+ let(:investigation_for_a_different_request) { Pacto::Investigation.new(different_request_signature, pacto_response, nil, []) }
11
+
12
+ before(:each) do
13
+ registry.reset!
14
+ end
15
+
16
+ describe 'reset!' do
17
+ before(:each) do
18
+ registry.register_investigation(investigation)
19
+ end
20
+
21
+ it 'cleans investigations' do
22
+ expect { registry.reset! }.to change { registry.validated? request_pattern }.from([investigation]).to(nil)
23
+ end
24
+ end
25
+
26
+ describe 'registering and reporting registered investigations' do
27
+ it 'returns registered investigation' do
28
+ expect(registry.register_investigation investigation).to eq(investigation)
29
+ end
30
+
31
+ it 'reports if investigation is not registered' do
32
+ expect(registry.validated? request_pattern).to be_falsey
33
+ end
34
+
35
+ it 'registers and returns matching investigations' do
36
+ registry.register_investigation(investigation)
37
+ registry.register_investigation(investigation_for_a_similar_request)
38
+ registry.register_investigation(investigation_for_a_different_request)
39
+ expect(registry.validated? request_pattern).to eq([investigation, investigation_for_a_similar_request])
40
+ end
41
+ end
42
+
43
+ describe '.unmatched_investigations' do
44
+ let(:contract) { Fabricate(:contract) }
45
+
46
+ it 'returns investigations with no contract' do
47
+ investigation_with_citations = Pacto::Investigation.new(different_request_signature, pacto_response, contract, [])
48
+ registry.register_investigation(investigation)
49
+ registry.register_investigation(investigation_for_a_similar_request)
50
+ registry.register_investigation(investigation_for_a_different_request)
51
+ registry.register_investigation(investigation_with_citations)
52
+
53
+ expect(registry.unmatched_investigations).to match_array([investigation, investigation_for_a_similar_request, investigation_for_a_different_request])
54
+ end
55
+ end
56
+
57
+ describe '.failed_investigations' do
58
+ let(:contract) { Fabricate(:contract) }
59
+ let(:citations2) { ['a sample citation'] }
60
+
61
+ it 'returns investigations with unsuccessful citations' do
62
+ allow(contract).to receive(:name).and_return 'test'
63
+ investigation_with_successful_citations = Pacto::Investigation.new(request_signature, pacto_response, nil, ['error'])
64
+ investigation_with_unsuccessful_citations = Pacto::Investigation.new(request_signature, pacto_response, nil, %w(error2 error3))
65
+
66
+ # Twice because of debug statement...
67
+ expect(investigation_with_successful_citations).to receive(:successful?).twice.and_return true
68
+ expect(investigation_with_unsuccessful_citations).to receive(:successful?).twice.and_return false
69
+
70
+ registry.register_investigation(investigation)
71
+ registry.register_investigation(investigation_with_successful_citations)
72
+ registry.register_investigation(investigation_with_unsuccessful_citations)
73
+
74
+ expect(registry.failed_investigations).to match_array([investigation_with_unsuccessful_citations])
75
+ end
76
+ end
77
+ end
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  module Pacto
2
3
  module Logger
3
4
  describe SimpleLogger do
@@ -9,27 +10,27 @@ module Pacto
9
10
  let(:logger_lib) { ::Logger.new(StringIO.new) }
10
11
 
11
12
  it 'delegates debug to the logger lib' do
12
- logger_lib.should_receive(:debug)
13
+ expect(logger_lib).to receive(:debug)
13
14
  logger.debug
14
15
  end
15
16
 
16
17
  it 'delegates info to the logger lib' do
17
- logger_lib.should_receive(:info)
18
+ expect(logger_lib).to receive(:info)
18
19
  logger.info
19
20
  end
20
21
 
21
22
  it 'delegates warn to the logger lib' do
22
- logger_lib.should_receive(:warn)
23
+ expect(logger_lib).to receive(:warn)
23
24
  logger.warn
24
25
  end
25
26
 
26
27
  it 'delegates error to the logger lib' do
27
- logger_lib.should_receive(:error)
28
+ expect(logger_lib).to receive(:error)
28
29
  logger.error
29
30
  end
30
31
 
31
32
  it 'delegates fatal to the logger lib' do
32
- logger_lib.should_receive(:error)
33
+ expect(logger_lib).to receive(:error)
33
34
  logger.error
34
35
  end
35
36
 
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  module Pacto
2
3
  describe MetaSchema do
3
4
  let(:valid_contract) do
@@ -17,7 +18,7 @@ module Pacto
17
18
  "headers": {
18
19
  "Content-Type": "application/json"
19
20
  },
20
- "body": {
21
+ "schema": {
21
22
  "description": "A simple response",
22
23
  "type": "object",
23
24
  "properties": {
@@ -64,7 +65,7 @@ module Pacto
64
65
  "headers": {
65
66
  "Content-Type": "application/json"
66
67
  },
67
- "body": {
68
+ "schema": {
68
69
  "description": "A simple response",
69
70
  "required": {},
70
71
  "type": "object",
@@ -79,14 +80,14 @@ module Pacto
79
80
  EOF
80
81
  end
81
82
 
82
- subject(:schema) { MetaSchema.new }
83
+ subject(:schema) { described_class.new }
83
84
 
84
85
  describe 'when validating a contract against the master schema' do
85
86
  context 'with a valid contract structure' do
86
87
  it 'does not raise any exceptions' do
87
88
  expect do
88
89
  schema.validate(valid_contract)
89
- end.to_not raise_error(Exception)
90
+ end.to_not raise_error
90
91
  end
91
92
  end
92
93
 
@@ -0,0 +1,26 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ module Pacto
5
+ describe NativeContractFactory do
6
+ let(:host) { 'http://localhost' }
7
+ let(:contract_name) { 'contract' }
8
+ let(:contracts_path) { %w(spec fixtures contracts) }
9
+ let(:contract_path) { File.join(contracts_path, "#{contract_name}.json") }
10
+ subject(:contract_factory) { described_class.new }
11
+
12
+ it 'builds a contract given a JSON file path and a host' do
13
+ contract = contract_factory.build_from_file(contract_path, host)
14
+ expect(contract).to be_a(Contract)
15
+ end
16
+
17
+ context 'deprecated contracts' do
18
+ let(:contracts_path) { %w(spec fixtures deprecated_contracts) }
19
+ let(:contract_name) { 'deprecated_contract' }
20
+ it 'can still be loaded' do
21
+ contract = contract_factory.build_from_file(contract_path, host)
22
+ expect(contract).to be_a(Contract)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -1,3 +1,4 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  require 'spec_helper'
2
3
 
3
4
  describe Pacto do
@@ -11,32 +12,23 @@ describe Pacto do
11
12
  $stdout.string.strip
12
13
  end
13
14
 
14
- def mock_validation(errors)
15
+ def mock_investigation(errors)
15
16
  expect(JSON::Validator).to receive(:fully_validate).with(any_args).and_return errors
16
17
  end
17
18
 
18
19
  describe '.validate_contract' do
19
20
  context 'valid' do
20
- it 'displays a success message and return true' do
21
- mock_validation []
22
- success = Pacto.validate_contract 'my_contract.json'
23
- expect(output).to eq 'Validating my_contract.json'
24
- expect(success).to be_true
21
+ it 'returns true' do
22
+ mock_investigation []
23
+ success = described_class.validate_contract 'my_contract.json'
24
+ expect(success).to be true
25
25
  end
26
26
  end
27
27
 
28
28
  context 'invalid' do
29
- it 'displays one error messages and return false' do
30
- mock_validation ['Error 1']
31
- success = Pacto.validate_contract 'my_contract.json'
32
- expect(output).to match(/error/)
33
- expect(success).to be_false
34
- end
35
-
36
- it 'displays several error messages and return false' do
37
- mock_validation ['Error 1', 'Error 2']
38
- success = Pacto.validate_contract 'my_contract.json'
39
- expect(success).to be_false
29
+ it 'raises an InvalidContract error' do
30
+ mock_investigation ['Error 1']
31
+ expect { described_class.validate_contract 'my_contract.json' }.to raise_error(InvalidContract)
40
32
  end
41
33
  end
42
34
  end
@@ -44,19 +36,12 @@ describe Pacto do
44
36
  describe 'loading contracts' do
45
37
  let(:contracts_path) { 'path/to/dir' }
46
38
  let(:host) { 'localhost' }
47
- let(:contract1) { double }
48
- let(:contract2) { double }
49
- let(:factory) { double(:factory) }
50
-
51
- before do
52
- allow(Pacto::ContractFactory).to receive(:new).and_return(factory)
53
- end
54
39
 
55
40
  it 'instantiates a contract list' do
56
- allow(Pacto::ContractFiles).to receive(:for).with(contracts_path).and_return { %w{file1 file2} }
57
- allow(factory).to receive(:build).with(%w{file1 file2}, host).and_return { [contract1, contract2] }
58
- expect(Pacto::ContractList).to receive(:new).with([contract1, contract2])
59
- Pacto.load_contracts(contracts_path, host)
41
+ expect(Pacto::ContractSet).to receive(:new) do |contracts|
42
+ contracts.each { |contract| expect(contract).to be_a_kind_of(Pacto::Contract) }
43
+ end
44
+ described_class.load_contracts('spec/fixtures/contracts/', host)
60
45
  end
61
46
  end
62
47
  end
@@ -1,39 +1,42 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  module Pacto
2
3
  describe RequestClause do
3
4
  let(:host) { 'http://localhost' }
4
5
  let(:method) { 'GET' }
5
6
  let(:path) { '/hello_world' }
6
- let(:headers) { {'accept' => 'application/json'} }
7
- let(:params) { {'foo' => 'bar'} }
7
+ let(:headers) { { 'accept' => 'application/json' } }
8
+ let(:params) { { 'foo' => 'bar' } }
8
9
  let(:body) { double :body }
9
10
  let(:params_as_json) { "{\"foo\":\"bar\"}" }
10
11
  let(:absolute_uri) { "#{host}#{path}" }
11
12
  subject(:request) do
12
- RequestClause.new(
13
- host,
14
- 'method' => method,
13
+ req_hash = {
14
+ host: host,
15
+ 'http_method' => method,
15
16
  'path' => path,
16
17
  'headers' => headers,
17
- 'params' => params,
18
- 'body' => body
19
- )
18
+ 'params' => params
19
+ }
20
+ # The default test is for missing keys, not explicitly nil keys
21
+ req_hash.merge!('schema' => body) if body
22
+ described_class.new(req_hash)
20
23
  end
21
24
 
22
25
  it 'has a host' do
23
26
  expect(request.host).to eq host
24
27
  end
25
28
 
26
- describe '#method' do
29
+ describe '#http_method' do
27
30
  it 'delegates to definition' do
28
- expect(request.method).to eq :get
31
+ expect(request.http_method).to eq :get
29
32
  end
30
33
 
31
34
  it 'downcases the method' do
32
- expect(request.method).to eq request.method.downcase
35
+ expect(request.http_method).to eq request.http_method.downcase
33
36
  end
34
37
 
35
38
  it 'returns a symbol' do
36
- expect(request.method).to be_kind_of Symbol
39
+ expect(request.http_method).to be_kind_of Symbol
37
40
  end
38
41
  end
39
42
 
@@ -42,7 +45,7 @@ module Pacto
42
45
  expect(request.schema).to eq body
43
46
  end
44
47
 
45
- describe 'when definition does not have body' do
48
+ describe 'when definition does not have a schema' do
46
49
  let(:body) { nil }
47
50
 
48
51
  it 'returns an empty empty hash' do
@@ -68,43 +71,5 @@ module Pacto
68
71
  expect(request.params).to eq params
69
72
  end
70
73
  end
71
-
72
- describe '#execute' do
73
- let(:connection) { double 'connection' }
74
- let(:response) { double 'response' }
75
- let(:adapted_response) { double 'adapted response' }
76
-
77
- before do
78
- WebMock.stub_request(:get, 'http://localhost/hello_world?foo=bar').
79
- to_return(:status => 200, :body => '', :headers => {})
80
- WebMock.stub_request(:post, 'http://localhost/hello_world?foo=bar').
81
- to_return(:status => 200, :body => '', :headers => {})
82
- # TODO: Should we just use WebMock?
83
- end
84
-
85
- context 'for any request' do
86
- it 'returns the a Faraday response' do
87
- expect(request.execute).to be_a Faraday::Response
88
- end
89
- end
90
-
91
- context 'for a GET request' do
92
- it 'makes the request thru the http client' do
93
- request.execute
94
- expect(WebMock).to have_requested(:get, 'http://localhost/hello_world?foo=bar').
95
- with(:headers => headers)
96
- end
97
- end
98
-
99
- context 'for a POST request' do
100
- let(:method) { 'POST' }
101
-
102
- it 'makes the request thru the http client' do
103
- request.execute
104
- expect(WebMock).to have_requested(:post, 'http://localhost/hello_world?foo=bar').
105
- with(:headers => headers)
106
- end
107
- end
108
- end
109
74
  end
110
75
  end
@@ -1,19 +1,20 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  require 'spec_helper'
2
3
 
3
4
  module Pacto
4
5
  describe RequestPattern do
5
- let(:method) { :get }
6
+ let(:http_method) { :get }
6
7
  let(:uri_pattern) { double }
7
8
  let(:request_pattern) { double }
8
- let(:request) { double(method: method) }
9
+ let(:request) { double(http_method: http_method) }
9
10
 
10
- it 'returns a pattern that combines the contracts method and uri_pattern' do
11
+ it 'returns a pattern that combines the contracts http_method and uri_pattern' do
11
12
  expect(UriPattern).to receive(:for).
12
13
  with(request).
13
14
  and_return(uri_pattern)
14
15
 
15
- expect(WebMock::RequestPattern).to receive(:new).
16
- with(method, uri_pattern).
16
+ expect(Pacto::RequestPattern).to receive(:new).
17
+ with(http_method, uri_pattern).
17
18
  and_return(request_pattern)
18
19
 
19
20
  expect(RequestPattern.for(request)).to eq request_pattern
@@ -1,11 +1,8 @@
1
+ # -*- encoding : utf-8 -*-
1
2
  module Pacto
2
3
  describe ResponseClause do
3
4
  let(:body_definition) do
4
- {
5
- :type => 'object',
6
- :required => true,
7
- :properties => double('body definition properties')
8
- }
5
+ Fabricate(:schema)
9
6
  end
10
7
 
11
8
  let(:definition) do
@@ -14,11 +11,11 @@ module Pacto
14
11
  'headers' => {
15
12
  'Content-Type' => 'application/json'
16
13
  },
17
- 'body' => body_definition
14
+ 'schema' => body_definition
18
15
  }
19
16
  end
20
17
 
21
- subject(:response) { ResponseClause.new(definition) }
18
+ subject(:response) { described_class.new(definition) }
22
19
 
23
20
  it 'has a status' do
24
21
  expect(response.status).to eq(200)
@@ -35,20 +32,10 @@ module Pacto
35
32
  end
36
33
 
37
34
  it 'has a default value for the schema' do
38
- response = ResponseClause.new(definition.merge('body' => nil))
35
+ definition.delete 'schema'
36
+ response = described_class.new(definition)
39
37
  expect(response.schema).to eq(Hash.new)
40
38
  end
41
39
 
42
- describe 'the response body' do
43
- let(:generated_body) { double }
44
-
45
- it 'is the json generated from the schema' do
46
- JSON::Generator.should_receive(:generate).
47
- with(definition['body']).
48
- and_return(generated_body)
49
-
50
- expect(response.body).to eq(generated_body)
51
- end
52
- end
53
40
  end
54
41
  end
@@ -1,24 +1,27 @@
1
- module Pacto::Server
2
- describe PlaybackServlet do
3
- let(:request) { double }
4
- let(:response) { double('response', :status= => '', :[]= => '', :body= => '') }
1
+ # -*- encoding : utf-8 -*-
2
+ module Pacto
3
+ module DummyServer
4
+ describe PlaybackServlet do
5
+ let(:request) { double }
6
+ let(:response) { double('response', :status= => '', :[]= => '', :body= => '') }
5
7
 
6
- it 'alters response data with recorded status' do
7
- servlet = PlaybackServlet.new status: 200
8
- servlet.do_GET(request, response)
9
- expect(response).to have_received(:status=).with(200)
10
- end
8
+ it 'alters response data with recorded status' do
9
+ servlet = PlaybackServlet.new status: 200
10
+ servlet.do_GET(request, response)
11
+ expect(response).to have_received(:status=).with(200)
12
+ end
11
13
 
12
- it 'alters reponse data with recorded headers' do
13
- servlet = PlaybackServlet.new headers: {'Content-Type' => 'application/json'}
14
- servlet.do_GET(request, response)
15
- expect(response).to have_received(:[]=).with('Content-Type', 'application/json')
16
- end
14
+ it 'alters reponse data with recorded headers' do
15
+ servlet = PlaybackServlet.new headers: { 'Content-Type' => 'application/json' }
16
+ servlet.do_GET(request, response)
17
+ expect(response).to have_received(:[]=).with('Content-Type', 'application/json')
18
+ end
17
19
 
18
- it 'alters reponse data with recorded ' do
19
- servlet = PlaybackServlet.new body: 'recorded'
20
- servlet.do_GET(request, response)
21
- expect(response).to have_received(:body=).with('recorded')
20
+ it 'alters reponse data with recorded ' do
21
+ servlet = PlaybackServlet.new body: 'recorded'
22
+ servlet.do_GET(request, response)
23
+ expect(response).to have_received(:body=).with('recorded')
24
+ end
22
25
  end
23
26
  end
24
27
  end