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,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'pacto/cli'
3
+
4
+ Pacto::CLI::Main.start
@@ -0,0 +1,30 @@
1
+ ## 0.3.2
2
+
3
+ *New Features:*
4
+ - #105: Add pacto-server for non-ruby tests. Use the pacto-server gem.
5
+
6
+ *Breaking Changes:*
7
+
8
+ - #107: Change default URI pattern to be less greedy.
9
+ /magazine will now not match also /magazine/last_edition.
10
+ query parameters after ? are still a match (ie /magazine?lastest=true)
11
+
12
+ *Bug Fixes:*
13
+
14
+ - #106: Remove dead, undocumented tag feature
15
+
16
+
17
+ ## 0.3.1
18
+
19
+ *Enhancements:*
20
+
21
+ - #103: Display file URI instead of meaningless schema identifier in messages
22
+
23
+ *Bug Fixes:*
24
+
25
+ - #102: - Fix rake pacto:generate task
26
+
27
+
28
+ ## 0.3.0
29
+
30
+ First stable release
@@ -0,0 +1,69 @@
1
+ Just require pacto to add it to your project.
2
+
3
+ ```rb
4
+ require 'pacto'
5
+ ```
6
+
7
+ Pacto will disable live connections, so you will get an error if
8
+ your code unexpectedly calls an service that was not stubbed. If you
9
+ want to re-enable connections, run `WebMock.allow_net_connect!`
10
+
11
+ ```rb
12
+ WebMock.allow_net_connect!
13
+ ```
14
+
15
+ Pacto can be configured via a block:
16
+
17
+ ```rb
18
+ Pacto.configure do |c|
19
+ ```
20
+
21
+ Path for loading/storing contracts.
22
+
23
+ ```rb
24
+ c.contracts_path = 'contracts'
25
+ ```
26
+
27
+ If the request matching should be strict (especially regarding HTTP Headers).
28
+
29
+ ```rb
30
+ c.strict_matchers = true
31
+ ```
32
+
33
+ You can set the Ruby Logger used by Pacto.
34
+
35
+ ```rb
36
+ c.logger = Pacto::Logger::SimpleLogger.instance
37
+ ```
38
+
39
+ (Deprecated) You can specify a callback for post-processing responses. Note that only one hook
40
+ can be active, and specifying your own will disable ERB post-processing.
41
+
42
+ ```rb
43
+ c.register_hook do |_contracts, request, _response|
44
+ puts "Received #{request}"
45
+ end
46
+ ```
47
+
48
+ Options to pass to the [json-schema-generator](https://github.com/maxlinc/json-schema-generator) while generating contracts.
49
+
50
+ ```rb
51
+ c.generator_options = { schema_version: 'draft3' }
52
+ end
53
+ ```
54
+
55
+ You can also do inline configuration. This example tells the json-schema-generator to store default values in the schema.
56
+
57
+ ```rb
58
+ Pacto.configuration.generator_options = { defaults: true }
59
+ ```
60
+
61
+ If you're using Pacto's rspec matchers you might want to configure a reset between each scenario
62
+
63
+ ```rb
64
+ require 'pacto/rspec'
65
+ RSpec.configure do |c|
66
+ c.after(:each) { Pacto.clear! }
67
+ end
68
+ ```
69
+
@@ -0,0 +1,18 @@
1
+
2
+ ```rb
3
+ require 'pacto'
4
+ Pacto.load_contracts 'contracts', 'http://localhost:5000'
5
+ WebMock.allow_net_connect!
6
+
7
+ interactions = Pacto.simulate_consumer :my_client do
8
+ request 'Ping'
9
+ request 'Echo', body: ->(body) { body.reverse },
10
+ headers: (proc do |headers|
11
+ headers['Content-Type'] = 'text/json'
12
+ headers['Accept'] = 'none'
13
+ headers
14
+ end)
15
+ end
16
+ puts interactions
17
+ ```
18
+
@@ -0,0 +1,39 @@
1
+
2
+ ```rb
3
+ require 'pacto'
4
+ Pacto.configure do |c|
5
+ c.contracts_path = 'contracts'
6
+ end
7
+ Pacto.validate!
8
+ ```
9
+
10
+ You can create a custom cop that investigates the request/response and sees if it complies with a
11
+ contract. The cop should return a list of citations if it finds any problems.
12
+
13
+ ```rb
14
+ class MyCustomCop
15
+ def investigate(_request, _response, contract)
16
+ citations = []
17
+ citations << 'Contract must have a request schema' if contract.request.schema.empty?
18
+ citations << 'Contract must have a response schema' if contract.response.schema.empty?
19
+ citations
20
+ end
21
+ end
22
+
23
+ Pacto::Cops.active_cops << MyCustomCop.new
24
+
25
+ contracts = Pacto.load_contracts('contracts', 'http://localhost:5000')
26
+ contracts.stub_providers
27
+ puts contracts.simulate_consumers
28
+ ```
29
+
30
+ Or you can completely replace the default set of validators
31
+
32
+ ```rb
33
+ Pacto::Cops.registered_cops.clear
34
+ Pacto::Cops.register_cop Pacto::Cops::ResponseBodyCop
35
+
36
+ contracts = Pacto.load_contracts('contracts', 'http://localhost:5000')
37
+ puts contracts.simulate_consumers
38
+ ```
39
+
@@ -0,0 +1,66 @@
1
+ Pacto has a few RSpec matchers to help you ensure a **consumer** and **producer** are
2
+ interacting properly. First, let's setup the rspec suite.
3
+
4
+ ```rb
5
+ require 'rspec/autorun' # Not generally needed
6
+ require 'pacto/rspec'
7
+ WebMock.allow_net_connect!
8
+ Pacto.validate!
9
+ Pacto.load_contracts('contracts', 'http://localhost:5000').stub_providers
10
+ ```
11
+
12
+ It's usually a good idea to reset Pacto between each scenario. `Pacto.reset` just clears the
13
+ data and metrics about which services were called. `Pacto.clear!` also resets all configuration
14
+ and plugins.
15
+
16
+ ```rb
17
+ RSpec.configure do |c|
18
+ c.after(:each) { Pacto.reset }
19
+ end
20
+ ```
21
+
22
+ Pacto provides some RSpec matchers related to contract testing, like making sure
23
+ Pacto didn't received any unrecognized requests (`have_unmatched_requests`) and that
24
+ the HTTP requests matched up with the terms of the contract (`have_failed_investigations`).
25
+
26
+ ```rb
27
+ describe Faraday do
28
+ let(:connection) { described_class.new(url: 'http://localhost:5000') }
29
+
30
+ it 'passes contract tests' do
31
+ connection.get '/api/ping'
32
+ expect(Pacto).to_not have_failed_investigations
33
+ expect(Pacto).to_not have_unmatched_requests
34
+ end
35
+ end
36
+ ```
37
+
38
+ There are also some matchers for collaboration testing, so you can make sure each scenario is
39
+ calling the expected services and sending the right type of data.
40
+
41
+ ```rb
42
+ describe Faraday do
43
+ let(:connection) { described_class.new(url: 'http://localhost:5000') }
44
+ before(:each) do
45
+ connection.get '/api/ping'
46
+
47
+ connection.post do |req|
48
+ req.url '/api/echo'
49
+ req.headers['Content-Type'] = 'application/json'
50
+ req.body = '{"foo": "bar"}'
51
+ end
52
+ end
53
+
54
+ it 'calls the ping service' do
55
+ expect(Pacto).to have_validated(:get, 'http://localhost:5000/api/ping').against_contract('Ping')
56
+ end
57
+
58
+ it 'sends data to the echo service' do
59
+ expect(Pacto).to have_investigated('Ping').with_response(body: hash_including('ping' => 'pong - from the example!'))
60
+ expect(Pacto).to have_investigated('Echo').with_request(body: hash_including('foo' => 'bar'))
61
+ echoed_body = { 'foo' => 'bar' }
62
+ expect(Pacto).to have_investigated('Echo').with_request(body: echoed_body).with_response(body: echoed_body)
63
+ end
64
+ end
65
+ ```
66
+
@@ -0,0 +1,65 @@
1
+ Some generation related [configuration](configuration.rb).
2
+
3
+ ```rb
4
+ require 'pacto'
5
+ WebMock.allow_net_connect!
6
+ Pacto.configure do |c|
7
+ c.contracts_path = 'contracts'
8
+ end
9
+ WebMock.allow_net_connect!
10
+ ```
11
+
12
+ Once we call `Pacto.generate!`, Pacto will record contracts for all requests it detects.
13
+
14
+ ```rb
15
+ Pacto.generate!
16
+ ```
17
+
18
+ Now, if we run any code that makes an HTTP call (using an
19
+ [HTTP library supported by WebMock](https://github.com/bblimke/webmock#supported-http-libraries))
20
+ then Pacto will generate a Contract based on the HTTP request/response.
21
+
22
+ This code snippet will generate a Contract and save it to `contracts/samples/contracts/localhost/api/ping.json`.
23
+
24
+ ```rb
25
+ require 'faraday'
26
+ conn = Faraday.new(url: 'http://localhost:5000')
27
+ response = conn.get '/api/ping'
28
+ ```
29
+
30
+ We're getting back real data from GitHub, so this should be the actual file encoding.
31
+
32
+ ```rb
33
+ puts response.body
34
+ ```
35
+
36
+ The generated contract will contain expectations based on the request/response we observed,
37
+ including a best-guess at an appropriate json-schema. Our heuristics certainly aren't foolproof,
38
+ so you might want to customize schema!
39
+ Here's another sample that sends a post request.
40
+
41
+ ```rb
42
+ conn.post do |req|
43
+ req.url '/api/echo'
44
+ req.headers['Content-Type'] = 'application/json'
45
+ req.body = '{"red fish": "blue fish"}'
46
+ end
47
+ ```
48
+
49
+ You can provide hints to Pacto to help it generate contracts. For example, Pacto doesn't have
50
+ a good way to know a good name and correct URI template for the service. That means that Pacto
51
+ will not know if two similar requests are for the same service or two different services, and
52
+ will be forced to give names based on the URI that are not good display names.
53
+ The hint below tells Pacto that requests to http://localhost:5000/album/1/cover and http://localhost:5000/album/2/cover
54
+ are both going to the same service, which is known as "Get Album Cover". This hint will cause Pacto to
55
+ generate a Contract for "Get Album Cover" and save it to `contracts/get_album_cover.json`, rather than two
56
+ contracts that are stored at `contracts/localhost/album/1/cover.json` and `contracts/localhost/album/2/cover.json`.
57
+
58
+ ```rb
59
+ Pacto::Generator.configure do |c|
60
+ c.hint 'Get Album Cover', http_method: :get, host: 'http://localhost:5000', path: '/api/album/{id}/cover'
61
+ end
62
+ conn.get '/api/album/1/cover'
63
+ conn.get '/api/album/2/cover'
64
+ ```
65
+
@@ -0,0 +1,10 @@
1
+ # Rake tasks
2
+ ## This is a test!
3
+ [That](www.google.com) markdown works
4
+
5
+ ```sh
6
+ bundle exec rake pacto:meta_validate['contracts']
7
+
8
+ bundle exec rake pacto:validate['http://localhost:5000','contracts']
9
+ ```
10
+
File without changes
@@ -0,0 +1,133 @@
1
+ # Overview
2
+ Welcome to the Pacto usage samples!
3
+ This document gives a quick overview of the main features.
4
+
5
+ You can browse the Table of Contents (upper right corner) to view additional samples.
6
+
7
+ In addition to this document, here are some highlighted samples:
8
+ <ul>
9
+ <li><a href="configuration">Configuration</a>: Shows all available configuration options</li>
10
+ <li><a href="generation">Generation</a>: More details on generation</li>
11
+ <li><a href="rspec">RSpec</a>: More samples for RSpec expectations</li>
12
+ </ul>
13
+ You can also find other samples using the Table of Content (upper right corner), including sample contracts.
14
+ # Getting started
15
+ Once you've installed the Pacto gem, you just require it. If you want, you can also require the Pacto rspec expectations.
16
+
17
+ ```rb
18
+ require 'pacto'
19
+ require 'pacto/rspec'
20
+ ```
21
+
22
+ Pacto will disable live connections, so you will get an error if
23
+ your code unexpectedly calls an service that was not stubbed. If you
24
+ want to re-enable connections, run `WebMock.allow_net_connect!`
25
+
26
+ ```rb
27
+ WebMock.allow_net_connect!
28
+ ```
29
+
30
+ Pacto can be configured via a block. The `contracts_path` option tells Pacto where it should load or save contracts. See the [Configuration](configuration.html) for all the available options.
31
+
32
+ ```rb
33
+ Pacto.configure do |c|
34
+ c.contracts_path = 'contracts'
35
+ end
36
+ ```
37
+
38
+ # Generating a Contract
39
+ Calling `Pacto.generate!` enables contract generation.
40
+ Pacto.generate!
41
+ Now, if we run any code that makes an HTTP call (using an
42
+ [HTTP library supported by WebMock](https://github.com/bblimke/webmock#supported-http-libraries))
43
+ then Pacto will generate a Contract based on the HTTP request/response.
44
+
45
+ We're using the sample APIs in the sample_apis directory.
46
+
47
+ ```rb
48
+ require 'faraday'
49
+ conn = Faraday.new(url: 'http://localhost:5000')
50
+ response = conn.get '/api/ping'
51
+ ```
52
+
53
+ This is the real request, so you should see {"ping":"pong"}
54
+
55
+ ```rb
56
+ puts response.body
57
+ ```
58
+
59
+ # Testing providers by simulating consumers
60
+ The generated contract will contain expectations based on the request/response we observed,
61
+ including a best-guess at an appropriate json-schema. Our heuristics certainly aren't foolproof,
62
+ so you might want to modify the output!
63
+ We can load the contract and validate it, by sending a new request and making sure
64
+ the response matches the JSON schema. Obviously it will pass since we just recorded it,
65
+ but if the service has made a change, or if you alter the contract with new expectations,
66
+ then you will see a contract investigation message.
67
+
68
+ ```rb
69
+ contracts = Pacto.load_contracts('contracts', 'http://localhost:5000')
70
+ contracts.simulate_consumers
71
+ ```
72
+
73
+ # Stubbing providers for consumer testing
74
+ We can also use Pacto to stub the service based on the contract.
75
+
76
+ ```rb
77
+ contracts.stub_providers
78
+ ```
79
+
80
+ The stubbed data won't be very realistic, the default behavior is to return the simplest data
81
+ that complies with the schema. That basically means that you'll have "bar" for every string.
82
+
83
+ ```rb
84
+ response = conn.get '/api/ping'
85
+ ```
86
+
87
+ You're now getting stubbed data. You should see {"ping":"bar"} unless you recorded with
88
+ the `defaults` option enabled, in which case you will still seee {"ping":"pong"}.
89
+
90
+ ```rb
91
+ puts response.body
92
+ ```
93
+
94
+ # Collaboration tests with RSpec
95
+ Pacto comes with rspec matchers
96
+
97
+ ```rb
98
+ require 'pacto/rspec'
99
+ ```
100
+
101
+ It's probably a good idea to reset Pacto between each rspec scenario
102
+
103
+ ```rb
104
+ RSpec.configure do |c|
105
+ c.after(:each) { Pacto.clear! }
106
+ end
107
+ ```
108
+
109
+ Load your contracts, and stub them if you'd like.
110
+
111
+ ```rb
112
+ Pacto.load_contracts('contracts', 'http://localhost:5000').stub_providers
113
+ ```
114
+
115
+ You can turn on investigation mode so Pacto will detect and validate HTTP requests.
116
+
117
+ ```rb
118
+ Pacto.validate!
119
+
120
+ describe 'my_code' do
121
+ it 'calls a service' do
122
+ conn = Faraday.new(url: 'http://localhost:5000')
123
+ response = conn.get '/api/ping'
124
+ ```
125
+
126
+ The have_validated matcher makes sure that Pacto received and successfully validated a request
127
+
128
+ ```rb
129
+ expect(Pacto).to have_validated(:get, 'http://localhost:5000/api/ping')
130
+ end
131
+ end
132
+ ```
133
+