vcr 3.0.3 → 6.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (146) hide show
  1. checksums.yaml +5 -5
  2. data/lib/vcr/cassette/erb_renderer.rb +4 -2
  3. data/lib/vcr/cassette/http_interaction_list.rb +14 -9
  4. data/lib/vcr/cassette/migrator.rb +5 -6
  5. data/lib/vcr/cassette/persisters/file_system.rb +9 -1
  6. data/lib/vcr/cassette/serializers/compressed.rb +2 -2
  7. data/lib/vcr/cassette/serializers/json.rb +14 -8
  8. data/lib/vcr/cassette/serializers/psych.rb +10 -2
  9. data/lib/vcr/cassette/serializers/syck.rb +7 -1
  10. data/lib/vcr/cassette/serializers/yaml.rb +14 -2
  11. data/lib/vcr/cassette/serializers.rb +10 -0
  12. data/lib/vcr/cassette.rb +63 -16
  13. data/lib/vcr/configuration.rb +21 -8
  14. data/lib/vcr/deprecations.rb +0 -62
  15. data/lib/vcr/errors.rb +17 -12
  16. data/lib/vcr/library_hooks/excon.rb +8 -0
  17. data/lib/vcr/library_hooks/typhoeus.rb +91 -79
  18. data/lib/vcr/library_hooks/webmock.rb +2 -11
  19. data/lib/vcr/linked_cassette.rb +4 -4
  20. data/lib/vcr/middleware/excon.rb +1 -1
  21. data/lib/vcr/middleware/faraday.rb +29 -2
  22. data/lib/vcr/request_ignorer.rb +8 -1
  23. data/lib/vcr/request_matcher_registry.rb +3 -3
  24. data/lib/vcr/structs.rb +48 -32
  25. data/lib/vcr/test_frameworks/cucumber.rb +16 -5
  26. data/lib/vcr/test_frameworks/rspec.rb +34 -22
  27. data/lib/vcr/util/hooks.rb +1 -0
  28. data/lib/vcr/util/internet_connection.rb +15 -21
  29. data/lib/vcr/version.rb +2 -2
  30. data/lib/vcr.rb +52 -2
  31. metadata +45 -272
  32. data/features/CHANGELOG.md +0 -710
  33. data/features/CONTRIBUTING.md +0 -26
  34. data/features/LICENSE.md +0 -20
  35. data/features/README.md +0 -339
  36. data/features/Upgrade.md +0 -289
  37. data/features/about_these_examples.md +0 -18
  38. data/features/cassettes/allow_unused_http_interactions.feature +0 -100
  39. data/features/cassettes/automatic_re_recording.feature +0 -72
  40. data/features/cassettes/decompress.feature +0 -74
  41. data/features/cassettes/dynamic_erb.feature +0 -100
  42. data/features/cassettes/exclusive.feature +0 -126
  43. data/features/cassettes/format.feature +0 -411
  44. data/features/cassettes/freezing_time.feature +0 -68
  45. data/features/cassettes/naming.feature +0 -28
  46. data/features/cassettes/no_cassette.feature +0 -152
  47. data/features/cassettes/update_content_length_header.feature +0 -112
  48. data/features/configuration/allow_http_connections_when_no_cassette.feature +0 -55
  49. data/features/configuration/cassette_library_dir.feature +0 -31
  50. data/features/configuration/debug_logging.feature +0 -58
  51. data/features/configuration/default_cassette_options.feature +0 -100
  52. data/features/configuration/filter_sensitive_data.feature +0 -153
  53. data/features/configuration/hook_into.feature +0 -172
  54. data/features/configuration/ignore_request.feature +0 -192
  55. data/features/configuration/preserve_exact_body_bytes.feature +0 -108
  56. data/features/configuration/query_parser.feature +0 -84
  57. data/features/configuration/uri_parser.feature +0 -93
  58. data/features/getting_started.md +0 -82
  59. data/features/hooks/after_http_request.feature +0 -58
  60. data/features/hooks/around_http_request.feature +0 -57
  61. data/features/hooks/before_http_request.feature +0 -63
  62. data/features/hooks/before_playback.feature +0 -184
  63. data/features/hooks/before_record.feature +0 -172
  64. data/features/http_libraries/em_http_request.feature +0 -250
  65. data/features/http_libraries/net_http.feature +0 -179
  66. data/features/middleware/faraday.feature +0 -56
  67. data/features/middleware/rack.feature +0 -92
  68. data/features/record_modes/all.feature +0 -82
  69. data/features/record_modes/new_episodes.feature +0 -79
  70. data/features/record_modes/none.feature +0 -72
  71. data/features/record_modes/once.feature +0 -95
  72. data/features/request_matching/README.md +0 -30
  73. data/features/request_matching/body.feature +0 -91
  74. data/features/request_matching/body_as_json.feature +0 -90
  75. data/features/request_matching/custom_matcher.feature +0 -135
  76. data/features/request_matching/headers.feature +0 -85
  77. data/features/request_matching/host.feature +0 -95
  78. data/features/request_matching/identical_request_sequence.feature +0 -89
  79. data/features/request_matching/method.feature +0 -96
  80. data/features/request_matching/path.feature +0 -96
  81. data/features/request_matching/playback_repeats.feature +0 -98
  82. data/features/request_matching/query.feature +0 -97
  83. data/features/request_matching/uri.feature +0 -94
  84. data/features/request_matching/uri_without_param.feature +0 -101
  85. data/features/step_definitions/cli_steps.rb +0 -199
  86. data/features/support/env.rb +0 -46
  87. data/features/support/http_lib_filters.rb +0 -46
  88. data/features/test_frameworks/cucumber.feature +0 -211
  89. data/features/test_frameworks/rspec_macro.feature +0 -81
  90. data/features/test_frameworks/rspec_metadata.feature +0 -150
  91. data/features/test_frameworks/test_unit.feature +0 -49
  92. data/lib/vcr/extensions/net_http_response.rb +0 -36
  93. data/lib/vcr/library_hooks/fakeweb.rb +0 -197
  94. data/lib/vcr/library_hooks/typhoeus_0.4.rb +0 -103
  95. data/spec/acceptance/concurrency_spec.rb +0 -51
  96. data/spec/acceptance/threading_spec.rb +0 -34
  97. data/spec/fixtures/cassette_spec/1_x_cassette.yml +0 -110
  98. data/spec/fixtures/cassette_spec/empty.yml +0 -0
  99. data/spec/fixtures/cassette_spec/example.yml +0 -111
  100. data/spec/fixtures/cassette_spec/with_localhost_requests.yml +0 -111
  101. data/spec/fixtures/fake_example_responses.yml +0 -110
  102. data/spec/fixtures/match_requests_on.yml +0 -187
  103. data/spec/lib/vcr/cassette/erb_renderer_spec.rb +0 -53
  104. data/spec/lib/vcr/cassette/http_interaction_list_spec.rb +0 -295
  105. data/spec/lib/vcr/cassette/migrator_spec.rb +0 -196
  106. data/spec/lib/vcr/cassette/persisters/file_system_spec.rb +0 -75
  107. data/spec/lib/vcr/cassette/persisters_spec.rb +0 -39
  108. data/spec/lib/vcr/cassette/serializers_spec.rb +0 -182
  109. data/spec/lib/vcr/cassette_spec.rb +0 -618
  110. data/spec/lib/vcr/configuration_spec.rb +0 -326
  111. data/spec/lib/vcr/deprecations_spec.rb +0 -85
  112. data/spec/lib/vcr/errors_spec.rb +0 -178
  113. data/spec/lib/vcr/extensions/net_http_response_spec.rb +0 -86
  114. data/spec/lib/vcr/library_hooks/excon_spec.rb +0 -104
  115. data/spec/lib/vcr/library_hooks/fakeweb_spec.rb +0 -169
  116. data/spec/lib/vcr/library_hooks/faraday_spec.rb +0 -68
  117. data/spec/lib/vcr/library_hooks/typhoeus_0.4_spec.rb +0 -36
  118. data/spec/lib/vcr/library_hooks/typhoeus_spec.rb +0 -162
  119. data/spec/lib/vcr/library_hooks/webmock_spec.rb +0 -117
  120. data/spec/lib/vcr/library_hooks_spec.rb +0 -51
  121. data/spec/lib/vcr/middleware/faraday_spec.rb +0 -181
  122. data/spec/lib/vcr/middleware/rack_spec.rb +0 -115
  123. data/spec/lib/vcr/request_ignorer_spec.rb +0 -70
  124. data/spec/lib/vcr/request_matcher_registry_spec.rb +0 -345
  125. data/spec/lib/vcr/structs_spec.rb +0 -732
  126. data/spec/lib/vcr/test_frameworks/cucumber_spec.rb +0 -107
  127. data/spec/lib/vcr/test_frameworks/rspec_spec.rb +0 -94
  128. data/spec/lib/vcr/util/hooks_spec.rb +0 -158
  129. data/spec/lib/vcr/util/internet_connection_spec.rb +0 -37
  130. data/spec/lib/vcr/util/version_checker_spec.rb +0 -31
  131. data/spec/lib/vcr/version_spec.rb +0 -27
  132. data/spec/lib/vcr_spec.rb +0 -354
  133. data/spec/monkey_patches.rb +0 -186
  134. data/spec/spec_helper.rb +0 -63
  135. data/spec/support/configuration_stubbing.rb +0 -8
  136. data/spec/support/cucumber_helpers.rb +0 -39
  137. data/spec/support/fixnum_extension.rb +0 -10
  138. data/spec/support/http_library_adapters.rb +0 -289
  139. data/spec/support/limited_uri.rb +0 -21
  140. data/spec/support/ruby_interpreter.rb +0 -7
  141. data/spec/support/shared_example_groups/excon.rb +0 -63
  142. data/spec/support/shared_example_groups/hook_into_http_library.rb +0 -594
  143. data/spec/support/shared_example_groups/request_hooks.rb +0 -59
  144. data/spec/support/sinatra_app.rb +0 -86
  145. data/spec/support/vcr_localhost_server.rb +0 -76
  146. data/spec/support/vcr_stub_helpers.rb +0 -17
@@ -1,82 +0,0 @@
1
- ### Install it
2
-
3
- [sudo] gem install vcr
4
- [sudo] gem install webmock
5
-
6
- ### Configure it
7
-
8
- Create a file named `vcr_setup.rb` with content like:
9
-
10
- require 'vcr'
11
-
12
- VCR.configure do |c|
13
- c.cassette_library_dir = 'vcr_cassettes'
14
- c.hook_into :webmock
15
- end
16
-
17
- Ensure this file is required by your test suite before any
18
- of the tests are run.
19
-
20
- ### Use it
21
-
22
- Run your tests. Any tests that make HTTP requests using Net::HTTP will
23
- raise errors like:
24
-
25
- ================================================================================
26
- An HTTP request has been made that VCR does not know how to handle:
27
- GET http://example.com/
28
-
29
- There is currently no cassette in use. There are a few ways
30
- you can configure VCR to handle this request:
31
-
32
- * If you want VCR to record this request and play it back during future test
33
- runs, you should wrap your test (or this portion of your test) in a
34
- `VCR.use_cassette` block [1].
35
- * If you only want VCR to handle requests made while a cassette is in use,
36
- configure `allow_http_connections_when_no_cassette = true`. VCR will
37
- ignore this request since it is made when there is no cassette [2].
38
- * If you want VCR to ignore this request (and others like it), you can
39
- set an `ignore_request` callback [3].
40
-
41
- [1] https://www.relishapp.com/myronmarston/vcr/v/2-0-0/docs/getting-started
42
- [2] https://www.relishapp.com/myronmarston/vcr/v/2-0-0/docs/configuration/allow-http-connections-when-no-cassette
43
- [3] https://www.relishapp.com/myronmarston/vcr/v/2-0-0/docs/configuration/ignore-request
44
- ================================================================================
45
-
46
- Find one of these tests (preferably one that uses the same HTTP method and
47
- request URL every time--if not, you'll have to configure the request matcher).
48
- Wrap the body of it (or at least the code that makes the HTTP request) in a
49
- `VCR.use_cassette` block:
50
-
51
- VCR.use_cassette('whatever cassette name you want') do
52
- # the body of the test would go here...
53
- end
54
-
55
- Run this test. It will record the HTTP request to disk as a cassette (a
56
- test fixture), with content like:
57
-
58
- ---
59
- http_interactions:
60
- - request:
61
- method: get
62
- uri: http://example.com/
63
- body: ''
64
- headers: {}
65
- response:
66
- status:
67
- code: 200
68
- message: OK
69
- headers:
70
- Content-Type:
71
- - text/html;charset=utf-8
72
- Content-Length:
73
- - '26'
74
- body: This is the response body
75
- http_version: '1.1'
76
- recorded_at: Tue, 01 Nov 2011 04:58:44 GMT
77
- recorded_with: VCR 2.0.0
78
-
79
- Disconnect your computer from the internet. Run the test again.
80
- It should pass since VCR is automatically replaying the recorded
81
- response when the request is made.
82
-
@@ -1,58 +0,0 @@
1
- Feature: after_http_request hook
2
-
3
- The `after_http_request` hook gets called with each request and response
4
- just after a request has completed. It can be used for many things:
5
-
6
- * globally logging requests and responses
7
- * ejecting the current cassette (i.e. if you inserted it in a
8
- `before_http_request` hook)
9
-
10
- You can also pass one or more "filters" to `after_http_request`, to make
11
- the hook only be called for some requests. Any object that responds to `#to_proc`
12
- can be a filter. Here are some simple examples:
13
-
14
- * `:real?` -- only real requests
15
- * `:stubbed?` -- only stubbed requests
16
- * `:ignored?` -- only ignored requests
17
- * `:recordable?` -- only requests that are being recorded
18
- * `lambda { |req| URI(req.uri).host == 'amazon.com' }` -- only requests to amazon.com.
19
-
20
- Scenario Outline: log all requests and responses using after_http_request hook
21
- Given a file named "after_http_request.rb" with:
22
- """ruby
23
- include_http_adapter_for("<http_lib>")
24
-
25
- $server = start_sinatra_app do
26
- get('/foo') { "Hello World (foo)" }
27
- get('/bar') { "Hello World (bar)" }
28
- end
29
-
30
- require 'vcr'
31
-
32
- VCR.configure do |c|
33
- <configuration>
34
- c.cassette_library_dir = 'cassettes'
35
- c.ignore_localhost = true
36
- c.after_http_request(:ignored?, lambda { |req| req.uri =~ /foo/ }) do |request, response|
37
- uri = request.uri.sub(/:\d+/, ":7777")
38
- puts "Response for #{request.method} #{uri}: #{response.body}"
39
- end
40
- end
41
-
42
- make_http_request(:get, "http://localhost:#{$server.port}/foo")
43
- make_http_request(:get, "http://localhost:#{$server.port}/bar")
44
- """
45
- When I run `ruby after_http_request.rb`
46
- Then the output should contain "Response for get http://localhost:7777/foo: Hello World (foo)"
47
- But the output should not contain "bar"
48
-
49
- Examples:
50
- | configuration | http_lib |
51
- | c.hook_into :fakeweb | net/http |
52
- | c.hook_into :webmock | net/http |
53
- | c.hook_into :webmock | httpclient |
54
- | c.hook_into :webmock | curb |
55
- | c.hook_into :typhoeus | typhoeus |
56
- | c.hook_into :excon | excon |
57
- | c.hook_into :faraday | faraday (w/ net_http) |
58
-
@@ -1,57 +0,0 @@
1
- @exclude-1.9.3p327
2
- Feature: around_http_request hook
3
-
4
- The `around_http_request` hook wraps each HTTP request. It can be used
5
- rather than separate `before_http_request` and `after_http_request` hooks
6
- to simplify wrapping/transactional logic (such as using a VCR cassette).
7
-
8
- In your block, call `#proceed` on the yielded request to cause it to continue.
9
- Alternately, you can treat the request as a proc and pass it on to a method that
10
- expects a block by prefixing it with an ampersand (`&request`).
11
-
12
- Note that `around_http_request` will not work on Ruby 1.8. It uses a fiber
13
- under the covers and thus is only available on interpreters that support fibers.
14
- On 1.8, you can use separate `before_http_request` and `after_http_request` hooks.
15
-
16
- Scenario Outline: globally handle requests using an around_http_request hook
17
- Given a file named "globally_handle_requests.rb" with:
18
- """ruby
19
- include_http_adapter_for("<http_lib>")
20
-
21
- request_count = 0
22
- $server = start_sinatra_app do
23
- get('/') { "Response #{request_count += 1 }" }
24
- end
25
-
26
- require 'vcr'
27
-
28
- VCR.configure do |c|
29
- <configuration>
30
- c.cassette_library_dir = 'cassettes'
31
- c.default_cassette_options = { :serialize_with => :syck }
32
- c.around_http_request do |request|
33
- VCR.use_cassette('global', :record => :new_episodes, &request)
34
- end
35
- end
36
-
37
- puts "Response for request 1: " + response_body_for(:get, "http://localhost:#{$server.port}/")
38
- puts "Response for request 2: " + response_body_for(:get, "http://localhost:#{$server.port}/")
39
- """
40
- When I run `ruby globally_handle_requests.rb`
41
- Then it should pass with:
42
- """
43
- Response for request 1: Response 1
44
- Response for request 2: Response 1
45
- """
46
- And the file "cassettes/global.yml" should contain "Response 1"
47
-
48
- Examples:
49
- | configuration | http_lib |
50
- | c.hook_into :fakeweb | net/http |
51
- | c.hook_into :webmock | net/http |
52
- | c.hook_into :webmock | httpclient |
53
- | c.hook_into :webmock | curb |
54
- | c.hook_into :typhoeus | typhoeus |
55
- | c.hook_into :excon | excon |
56
- | c.hook_into :faraday | faraday (w/ net_http) |
57
-
@@ -1,63 +0,0 @@
1
- Feature: before_http_request hook
2
-
3
- The `before_http_request` hook gets called with each request
4
- just before it proceeds. It can be used for many things:
5
-
6
- * globally logging requests
7
- * inserting a particular cassette based on the request URI host
8
- * raising a timeout error
9
-
10
- You can also pass one or more "filters" to `before_http_request`, to make
11
- the hook only be called for some requests. Any object that responds to `#to_proc`
12
- can be a filter. Here are some simple examples:
13
-
14
- * `:real?` -- only real requests
15
- * `:stubbed?` -- only stubbed requests
16
- * `:ignored?` -- only ignored requests
17
- * `:recordable?` -- only requests that are being recorded
18
- * `lambda { |r| URI(r.uri).host == 'amazon.com' }` -- only requests to amazon.com.
19
-
20
- Scenario Outline: log all requests using a before_http_request hook
21
- Given a file named "before_http_request.rb" with:
22
- """ruby
23
- include_http_adapter_for("<http_lib>")
24
-
25
- if ARGV.include?('--with-server')
26
- $server = start_sinatra_app do
27
- get('/') { "Hello World" }
28
- end
29
- end
30
-
31
- require 'vcr'
32
-
33
- VCR.configure do |c|
34
- <configuration>
35
- c.cassette_library_dir = 'cassettes'
36
- c.before_http_request(:real?) do |request|
37
- File.open(ARGV.first, 'w') do |f|
38
- f.write("before real request: #{request.method} #{request.uri}")
39
- end
40
- end
41
- end
42
-
43
- VCR.use_cassette('hook_example') do
44
- port = $server ? $server.port : 0
45
- make_http_request(:get, "http://localhost:#{port}/")
46
- end
47
- """
48
- When I run `ruby before_http_request.rb run1.log --with-server`
49
- Given that port numbers in "run1.log" are normalized to "7777"
50
- Then the file "run1.log" should contain "before real request: get http://localhost:7777/"
51
- When I run `ruby before_http_request.rb run2.log`
52
- Then the file "run2.log" should not exist
53
-
54
- Examples:
55
- | configuration | http_lib |
56
- | c.hook_into :fakeweb | net/http |
57
- | c.hook_into :webmock | net/http |
58
- | c.hook_into :webmock | httpclient |
59
- | c.hook_into :webmock | curb |
60
- | c.hook_into :typhoeus | typhoeus |
61
- | c.hook_into :excon | excon |
62
- | c.hook_into :faraday | faraday (w/ net_http) |
63
-
@@ -1,184 +0,0 @@
1
- Feature: before_playback hook
2
-
3
- The `before_playback` hook is called before a cassette sets up its
4
- stubs for playback.
5
-
6
- Your block should accept up to 2 arguments. The first argument will be
7
- the HTTP interaction that is about to be used for play back. The second
8
- argument will be the current cassette.
9
-
10
- You can also call `#ignore!` on the HTTP interaction to prevent VCR
11
- from playing it back.
12
-
13
- You can use tags to specify a cassette, otherwise your hook will apply to all cassettes. Consider this code:
14
-
15
- VCR.configure do |c|
16
- c.before_playback(:twitter) { ... } # modify the interactions somehow
17
- end
18
-
19
- VCR.use_cassette('cassette_1', :tag => :twitter) { ... }
20
- VCR.use_cassette('cassette_2') { ... }
21
-
22
- In this example, the hook would apply to the first cassette but not the
23
- second cassette.
24
-
25
- Background:
26
- Given a previously recorded cassette file "cassettes/example.yml" with:
27
- """
28
- ---
29
- http_interactions:
30
- - request:
31
- method: get
32
- uri: http://localhost:7777/
33
- body:
34
- encoding: UTF-8
35
- string: ""
36
- headers: {}
37
- response:
38
- status:
39
- code: 200
40
- message: OK
41
- headers:
42
- Content-Type:
43
- - text/html;charset=utf-8
44
- Content-Length:
45
- - "20"
46
- body:
47
- encoding: UTF-8
48
- string: previously recorded response
49
- http_version: "1.1"
50
- recorded_at: Tue, 01 Nov 2011 04:58:44 GMT
51
- recorded_with: VCR 2.0.0
52
- """
53
-
54
- Scenario: Modify played back response
55
- Given a file named "before_playback_example.rb" with:
56
- """ruby
57
- require 'vcr'
58
-
59
- VCR.configure do |c|
60
- c.hook_into :webmock
61
- c.cassette_library_dir = 'cassettes'
62
-
63
- c.before_playback do |interaction|
64
- interaction.response.body = 'response from before_playback'
65
- end
66
- end
67
-
68
- VCR.use_cassette('example') do
69
- response = Net::HTTP.get_response('localhost', '/', 7777)
70
- puts "Response: #{response.body}"
71
- end
72
- """
73
- When I run `ruby before_playback_example.rb`
74
- Then it should pass with "Response: response from before_playback"
75
-
76
- Scenario: Modify played back response based on the cassette
77
- Given a file named "before_playback_example.rb" with:
78
- """ruby
79
- require 'vcr'
80
-
81
- VCR.configure do |c|
82
- c.hook_into :webmock
83
- c.cassette_library_dir = 'cassettes'
84
-
85
- c.before_playback do |interaction, cassette|
86
- interaction.response.body = "response for #{cassette.name} cassette"
87
- end
88
- end
89
-
90
- VCR.use_cassette('example') do
91
- response = Net::HTTP.get_response('localhost', '/', 7777)
92
- puts "Response: #{response.body}"
93
- end
94
- """
95
- When I run `ruby before_playback_example.rb`
96
- Then it should pass with "Response: response for example cassette"
97
-
98
- Scenario: Prevent playback by ignoring interaction in before_playback hook
99
- Given a file named "before_playback_ignore.rb" with:
100
- """ruby
101
- $server = start_sinatra_app do
102
- get('/') { "sinatra response" }
103
- end
104
-
105
- require 'vcr'
106
-
107
- VCR.configure do |c|
108
- c.hook_into :webmock
109
- c.cassette_library_dir = 'cassettes'
110
- c.before_playback { |i| i.ignore! }
111
- end
112
-
113
- VCR.use_cassette('localhost', :record => :new_episodes, :match_requests_on => [:method, :host, :path]) do
114
- response = Net::HTTP.get_response('localhost', '/', $server.port)
115
- puts "Response: #{response.body}"
116
- end
117
- """
118
- When I run `ruby before_playback_ignore.rb`
119
- Then it should pass with "Response: sinatra response"
120
-
121
- Scenario: Multiple hooks are run in order
122
- Given a file named "multiple_hooks.rb" with:
123
- """ruby
124
- require 'vcr'
125
-
126
- VCR.configure do |c|
127
- c.hook_into :webmock
128
- c.cassette_library_dir = 'cassettes'
129
-
130
- c.before_playback { puts "In before_playback hook 1" }
131
- c.before_playback { puts "In before_playback hook 2" }
132
- end
133
-
134
- VCR.use_cassette('example', :record => :new_episodes) do
135
- response = Net::HTTP.get_response('localhost', '/', 7777)
136
- puts "Response: #{response.body}"
137
- end
138
- """
139
- When I run `ruby multiple_hooks.rb`
140
- Then it should pass with:
141
- """
142
- In before_playback hook 1
143
- In before_playback hook 2
144
- Response: previously recorded response
145
- """
146
-
147
- Scenario: Use tagging to apply hooks to only certain cassettes
148
- Given a file named "tagged_hooks.rb" with:
149
- """ruby
150
- require 'vcr'
151
-
152
- VCR.configure do |c|
153
- c.hook_into :webmock
154
- c.cassette_library_dir = 'cassettes'
155
-
156
- c.before_playback(:tag_2) do |i|
157
- puts "In before_playback hook for tag_2"
158
- end
159
- end
160
-
161
- [:tag_1, :tag_2, nil].each do |tag|
162
- puts
163
- puts "Using tag: #{tag.inspect}"
164
-
165
- VCR.use_cassette('example', :record => :new_episodes, :tag => tag) do
166
- response = Net::HTTP.get_response('localhost', '/', 7777)
167
- puts "Response: #{response.body}"
168
- end
169
- end
170
- """
171
- When I run `ruby tagged_hooks.rb`
172
- Then it should pass with:
173
- """
174
- Using tag: :tag_1
175
- Response: previously recorded response
176
-
177
- Using tag: :tag_2
178
- In before_playback hook for tag_2
179
- Response: previously recorded response
180
-
181
- Using tag: nil
182
- Response: previously recorded response
183
- """
184
-
@@ -1,172 +0,0 @@
1
- Feature: before_record hook
2
-
3
- The `before_record` hook is called before a cassette is written to disk.
4
- This can be used to modify the HTTP interaction before it is recorded.
5
-
6
- Your block should accept up to 2 arguments. The first argument will be
7
- the HTTP interaction that is about to be written to disk. The second
8
- argument will be the current cassette.
9
-
10
- If you wish to prevent VCR from recording the HTTP interaction you can call
11
- `#ignore!` on the interaction.
12
-
13
- If you don't want your hook to apply to all cassettes, you can use tags to
14
- select which cassettes a given hook applies to. Consider this code:
15
-
16
- VCR.configure do |c|
17
- c.before_record(:twitter) { ... } # modify the interactions somehow
18
- end
19
-
20
- VCR.use_cassette('cassette_1', :tag => :twitter) { ... }
21
- VCR.use_cassette('cassette_2') { ... }
22
-
23
- In this example, the hook would apply to the first cassette but not the
24
- second cassette.
25
-
26
- Scenario: Modify recorded response
27
- Given a file named "before_record_example.rb" with:
28
- """ruby
29
- $server = start_sinatra_app do
30
- get('/') { "Hello Earth" }
31
- end
32
-
33
- require 'vcr'
34
-
35
- VCR.configure do |c|
36
- c.hook_into :webmock
37
- c.cassette_library_dir = 'cassettes'
38
-
39
- c.before_record do |i|
40
- i.response.body.sub!('Earth', 'World')
41
- end
42
- end
43
-
44
- VCR.use_cassette('recording_example') do
45
- Net::HTTP.get_response('localhost', '/', $server.port)
46
- end
47
- """
48
- When I run `ruby before_record_example.rb`
49
- Then the file "cassettes/recording_example.yml" should contain "Hello World"
50
- And the file "cassettes/recording_example.yml" should not contain "Earth"
51
-
52
- Scenario: Modify recorded response based on the cassette
53
- Given a file named "before_record_example.rb" with:
54
- """ruby
55
- $server = start_sinatra_app do
56
- get('/') { "Hello Earth" }
57
- end
58
-
59
- require 'vcr'
60
-
61
- VCR.configure do |c|
62
- c.hook_into :webmock
63
- c.cassette_library_dir = 'cassettes'
64
-
65
- c.before_record do |interaction, cassette|
66
- interaction.response.body << " (#{cassette.name})"
67
- end
68
- end
69
-
70
- VCR.use_cassette('recording_example') do
71
- Net::HTTP.get_response('localhost', '/', $server.port)
72
- end
73
- """
74
- When I run `ruby before_record_example.rb`
75
- Then the file "cassettes/recording_example.yml" should contain "Hello Earth (recording_example)"
76
-
77
- Scenario: Prevent recording by ignoring interaction in before_record hook
78
- Given a file named "before_record_ignore.rb" with:
79
- """ruby
80
- $server = start_sinatra_app do
81
- get('/') { "Hello World" }
82
- end
83
-
84
- require 'vcr'
85
-
86
- VCR.configure do |c|
87
- c.hook_into :webmock
88
- c.cassette_library_dir = 'cassettes'
89
- c.before_record { |i| i.ignore! }
90
- end
91
-
92
- VCR.use_cassette('recording_example') do
93
- response = Net::HTTP.get_response('localhost', '/', $server.port)
94
- puts "Response: #{response.body}"
95
- end
96
- """
97
- When I run `ruby before_record_ignore.rb`
98
- Then it should pass with "Response: Hello World"
99
- And the file "cassettes/recording_example.yml" should not exist
100
-
101
- Scenario: Multiple hooks are run in order
102
- Given a file named "multiple_hooks.rb" with:
103
- """ruby
104
- $server = start_sinatra_app do
105
- get('/') { "Hello World" }
106
- end
107
-
108
- require 'vcr'
109
-
110
- VCR.configure do |c|
111
- c.hook_into :webmock
112
- c.cassette_library_dir = 'cassettes'
113
-
114
- c.before_record { puts "In before_record hook 1" }
115
- c.before_record { puts "In before_record hook 2" }
116
- end
117
-
118
- VCR.use_cassette('example', :record => :new_episodes) do
119
- response = Net::HTTP.get_response('localhost', '/', $server.port)
120
- puts "Response: #{response.body}"
121
- end
122
- """
123
- When I run `ruby multiple_hooks.rb`
124
- Then it should pass with:
125
- """
126
- Response: Hello World
127
- In before_record hook 1
128
- In before_record hook 2
129
- """
130
-
131
- Scenario: Use tagging to apply hook to only certain cassettes
132
- Given a file named "tagged_hooks.rb" with:
133
- """ruby
134
- $server = start_sinatra_app do
135
- get('/') { "Hello World" }
136
- end
137
-
138
- require 'vcr'
139
-
140
- VCR.configure do |c|
141
- c.hook_into :webmock
142
- c.cassette_library_dir = 'cassettes'
143
-
144
- c.before_record(:tag_1) do
145
- puts "In before_record hook for tag_1"
146
- end
147
- end
148
-
149
- [:tag_1, :tag_2, nil].each do |tag|
150
- puts
151
- puts "Using tag: #{tag.inspect}"
152
-
153
- VCR.use_cassette('example', :record => :new_episodes, :tag => tag) do
154
- response = Net::HTTP.get_response('localhost', '/', $server.port)
155
- puts "Response: #{response.body}"
156
- end
157
- end
158
- """
159
- When I run `ruby tagged_hooks.rb`
160
- Then it should pass with:
161
- """
162
- Using tag: :tag_1
163
- Response: Hello World
164
- In before_record hook for tag_1
165
-
166
- Using tag: :tag_2
167
- Response: Hello World
168
-
169
- Using tag: nil
170
- Response: Hello World
171
- """
172
-