vcr 3.0.3 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/vcr.rb +18 -1
- data/lib/vcr/cassette.rb +11 -3
- data/lib/vcr/cassette/persisters/file_system.rb +1 -1
- data/lib/vcr/configuration.rb +3 -5
- data/lib/vcr/deprecations.rb +0 -62
- data/lib/vcr/errors.rb +16 -0
- data/lib/vcr/library_hooks/typhoeus.rb +37 -8
- data/lib/vcr/middleware/faraday.rb +5 -1
- data/lib/vcr/structs.rb +1 -1
- data/lib/vcr/util/hooks.rb +1 -0
- data/lib/vcr/version.rb +1 -1
- metadata +9 -249
- data/features/CHANGELOG.md +0 -710
- data/features/CONTRIBUTING.md +0 -26
- data/features/LICENSE.md +0 -20
- data/features/README.md +0 -339
- data/features/Upgrade.md +0 -289
- data/features/about_these_examples.md +0 -18
- data/features/cassettes/allow_unused_http_interactions.feature +0 -100
- data/features/cassettes/automatic_re_recording.feature +0 -72
- data/features/cassettes/decompress.feature +0 -74
- data/features/cassettes/dynamic_erb.feature +0 -100
- data/features/cassettes/exclusive.feature +0 -126
- data/features/cassettes/format.feature +0 -411
- data/features/cassettes/freezing_time.feature +0 -68
- data/features/cassettes/naming.feature +0 -28
- data/features/cassettes/no_cassette.feature +0 -152
- data/features/cassettes/update_content_length_header.feature +0 -112
- data/features/configuration/allow_http_connections_when_no_cassette.feature +0 -55
- data/features/configuration/cassette_library_dir.feature +0 -31
- data/features/configuration/debug_logging.feature +0 -58
- data/features/configuration/default_cassette_options.feature +0 -100
- data/features/configuration/filter_sensitive_data.feature +0 -153
- data/features/configuration/hook_into.feature +0 -172
- data/features/configuration/ignore_request.feature +0 -192
- data/features/configuration/preserve_exact_body_bytes.feature +0 -108
- data/features/configuration/query_parser.feature +0 -84
- data/features/configuration/uri_parser.feature +0 -93
- data/features/getting_started.md +0 -82
- data/features/hooks/after_http_request.feature +0 -58
- data/features/hooks/around_http_request.feature +0 -57
- data/features/hooks/before_http_request.feature +0 -63
- data/features/hooks/before_playback.feature +0 -184
- data/features/hooks/before_record.feature +0 -172
- data/features/http_libraries/em_http_request.feature +0 -250
- data/features/http_libraries/net_http.feature +0 -179
- data/features/middleware/faraday.feature +0 -56
- data/features/middleware/rack.feature +0 -92
- data/features/record_modes/all.feature +0 -82
- data/features/record_modes/new_episodes.feature +0 -79
- data/features/record_modes/none.feature +0 -72
- data/features/record_modes/once.feature +0 -95
- data/features/request_matching/README.md +0 -30
- data/features/request_matching/body.feature +0 -91
- data/features/request_matching/body_as_json.feature +0 -90
- data/features/request_matching/custom_matcher.feature +0 -135
- data/features/request_matching/headers.feature +0 -85
- data/features/request_matching/host.feature +0 -95
- data/features/request_matching/identical_request_sequence.feature +0 -89
- data/features/request_matching/method.feature +0 -96
- data/features/request_matching/path.feature +0 -96
- data/features/request_matching/playback_repeats.feature +0 -98
- data/features/request_matching/query.feature +0 -97
- data/features/request_matching/uri.feature +0 -94
- data/features/request_matching/uri_without_param.feature +0 -101
- data/features/step_definitions/cli_steps.rb +0 -199
- data/features/support/env.rb +0 -46
- data/features/support/http_lib_filters.rb +0 -46
- data/features/test_frameworks/cucumber.feature +0 -211
- data/features/test_frameworks/rspec_macro.feature +0 -81
- data/features/test_frameworks/rspec_metadata.feature +0 -150
- data/features/test_frameworks/test_unit.feature +0 -49
- data/lib/vcr/extensions/net_http_response.rb +0 -36
- data/lib/vcr/library_hooks/fakeweb.rb +0 -197
- data/spec/acceptance/concurrency_spec.rb +0 -51
- data/spec/acceptance/threading_spec.rb +0 -34
- data/spec/fixtures/cassette_spec/1_x_cassette.yml +0 -110
- data/spec/fixtures/cassette_spec/empty.yml +0 -0
- data/spec/fixtures/cassette_spec/example.yml +0 -111
- data/spec/fixtures/cassette_spec/with_localhost_requests.yml +0 -111
- data/spec/fixtures/fake_example_responses.yml +0 -110
- data/spec/fixtures/match_requests_on.yml +0 -187
- data/spec/lib/vcr/cassette/erb_renderer_spec.rb +0 -53
- data/spec/lib/vcr/cassette/http_interaction_list_spec.rb +0 -295
- data/spec/lib/vcr/cassette/migrator_spec.rb +0 -196
- data/spec/lib/vcr/cassette/persisters/file_system_spec.rb +0 -75
- data/spec/lib/vcr/cassette/persisters_spec.rb +0 -39
- data/spec/lib/vcr/cassette/serializers_spec.rb +0 -182
- data/spec/lib/vcr/cassette_spec.rb +0 -618
- data/spec/lib/vcr/configuration_spec.rb +0 -326
- data/spec/lib/vcr/deprecations_spec.rb +0 -85
- data/spec/lib/vcr/errors_spec.rb +0 -178
- data/spec/lib/vcr/extensions/net_http_response_spec.rb +0 -86
- data/spec/lib/vcr/library_hooks/excon_spec.rb +0 -104
- data/spec/lib/vcr/library_hooks/fakeweb_spec.rb +0 -169
- data/spec/lib/vcr/library_hooks/faraday_spec.rb +0 -68
- data/spec/lib/vcr/library_hooks/typhoeus_0.4_spec.rb +0 -36
- data/spec/lib/vcr/library_hooks/typhoeus_spec.rb +0 -162
- data/spec/lib/vcr/library_hooks/webmock_spec.rb +0 -117
- data/spec/lib/vcr/library_hooks_spec.rb +0 -51
- data/spec/lib/vcr/middleware/faraday_spec.rb +0 -181
- data/spec/lib/vcr/middleware/rack_spec.rb +0 -115
- data/spec/lib/vcr/request_ignorer_spec.rb +0 -70
- data/spec/lib/vcr/request_matcher_registry_spec.rb +0 -345
- data/spec/lib/vcr/structs_spec.rb +0 -732
- data/spec/lib/vcr/test_frameworks/cucumber_spec.rb +0 -107
- data/spec/lib/vcr/test_frameworks/rspec_spec.rb +0 -94
- data/spec/lib/vcr/util/hooks_spec.rb +0 -158
- data/spec/lib/vcr/util/internet_connection_spec.rb +0 -37
- data/spec/lib/vcr/util/version_checker_spec.rb +0 -31
- data/spec/lib/vcr/version_spec.rb +0 -27
- data/spec/lib/vcr_spec.rb +0 -354
- data/spec/monkey_patches.rb +0 -186
- data/spec/spec_helper.rb +0 -63
- data/spec/support/configuration_stubbing.rb +0 -8
- data/spec/support/cucumber_helpers.rb +0 -39
- data/spec/support/fixnum_extension.rb +0 -10
- data/spec/support/http_library_adapters.rb +0 -289
- data/spec/support/limited_uri.rb +0 -21
- data/spec/support/ruby_interpreter.rb +0 -7
- data/spec/support/shared_example_groups/excon.rb +0 -63
- data/spec/support/shared_example_groups/hook_into_http_library.rb +0 -594
- data/spec/support/shared_example_groups/request_hooks.rb +0 -59
- data/spec/support/sinatra_app.rb +0 -86
- data/spec/support/vcr_localhost_server.rb +0 -76
- data/spec/support/vcr_stub_helpers.rb +0 -17
@@ -1,108 +0,0 @@
|
|
1
|
-
@exclude-18
|
2
|
-
Feature: Preserve Exact Body Bytes
|
3
|
-
|
4
|
-
Some HTTP servers are not well-behaved and respond with invalid data: the response body may
|
5
|
-
not be encoded according to the encoding specified in the HTTP headers, or there may be bytes
|
6
|
-
that are invalid for the given encoding. The YAML and JSON serializers are not generally
|
7
|
-
designed to handle these cases gracefully, and you may get errors when the cassette is serialized
|
8
|
-
or deserialized. Also, the encoding may not be preserved when round-tripped through the
|
9
|
-
serializer.
|
10
|
-
|
11
|
-
VCR provides a configuration option to deal with cases like these. The `preserve_exact_body_bytes`
|
12
|
-
method accepts a block that VCR will use to determine if the body of the given request or response object
|
13
|
-
should be base64 encoded in order to preserve the bytes exactly as-is. VCR does not do this by
|
14
|
-
default, since base64-encoding the string removes the human readibility of the cassette.
|
15
|
-
|
16
|
-
Alternately, if you want to force an entire cassette to preserve the exact body bytes,
|
17
|
-
you can pass the `:preserve_exact_body_bytes => true` cassette option when inserting your
|
18
|
-
cassette.
|
19
|
-
|
20
|
-
Scenario: Preserve exact bytes for response body with invalid encoding
|
21
|
-
Given a file named "preserve.rb" with:
|
22
|
-
"""ruby
|
23
|
-
# encoding: utf-8
|
24
|
-
string = "abc \xFA"
|
25
|
-
puts "Valid encoding: #{string.valid_encoding?}"
|
26
|
-
|
27
|
-
$server = start_sinatra_app do
|
28
|
-
get('/') { string }
|
29
|
-
end
|
30
|
-
|
31
|
-
require 'vcr'
|
32
|
-
|
33
|
-
VCR.configure do |c|
|
34
|
-
c.cassette_library_dir = 'cassettes'
|
35
|
-
c.hook_into :webmock
|
36
|
-
c.preserve_exact_body_bytes do |http_message|
|
37
|
-
http_message.body.encoding.name == 'ASCII-8BIT' ||
|
38
|
-
!http_message.body.valid_encoding?
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
def make_request(label)
|
43
|
-
puts
|
44
|
-
puts label
|
45
|
-
VCR.use_cassette('example', :serialize_with => :json) do
|
46
|
-
body = Net::HTTP.get_response('localhost', '/', $server.port).body
|
47
|
-
puts "Body: #{body.inspect}"
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
make_request("Recording:")
|
52
|
-
make_request("Playback:")
|
53
|
-
"""
|
54
|
-
When I run `ruby preserve.rb`
|
55
|
-
Then the output should contain exactly:
|
56
|
-
"""
|
57
|
-
Valid encoding: false
|
58
|
-
|
59
|
-
Recording:
|
60
|
-
Body: "abc \xFA"
|
61
|
-
|
62
|
-
Playback:
|
63
|
-
Body: "abc \xFA"
|
64
|
-
|
65
|
-
"""
|
66
|
-
And the file "cassettes/example.json" should contain:
|
67
|
-
"""
|
68
|
-
"body":{"encoding":"ASCII-8BIT","base64_string":"YWJjIPo=\n"}
|
69
|
-
"""
|
70
|
-
|
71
|
-
Scenario: Preserve exact bytes for cassette with `:preserve_exact_body_bytes` option
|
72
|
-
Given a file named "preserve.rb" with:
|
73
|
-
"""ruby
|
74
|
-
$server = start_sinatra_app do
|
75
|
-
get('/') { "Hello World" }
|
76
|
-
end
|
77
|
-
|
78
|
-
require 'vcr'
|
79
|
-
|
80
|
-
VCR.configure do |c|
|
81
|
-
c.cassette_library_dir = 'cassettes'
|
82
|
-
c.hook_into :webmock
|
83
|
-
c.default_cassette_options = { :serialize_with => :json }
|
84
|
-
|
85
|
-
c.before_record do |i|
|
86
|
-
# otherwise Ruby 2.0 will default to UTF-8:
|
87
|
-
i.response.body.force_encoding('US-ASCII')
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
VCR.use_cassette('preserve_bytes', :preserve_exact_body_bytes => true) do
|
92
|
-
Net::HTTP.get_response('localhost', '/', $server.port)
|
93
|
-
end
|
94
|
-
|
95
|
-
VCR.use_cassette('dont_preserve_bytes') do
|
96
|
-
Net::HTTP.get_response('localhost', '/', $server.port)
|
97
|
-
end
|
98
|
-
"""
|
99
|
-
When I run `ruby preserve.rb`
|
100
|
-
Then the file "cassettes/preserve_bytes.json" should contain:
|
101
|
-
"""
|
102
|
-
"body":{"encoding":"US-ASCII","base64_string":"SGVsbG8gV29ybGQ=\n"}
|
103
|
-
"""
|
104
|
-
And the file "cassettes/dont_preserve_bytes.json" should contain:
|
105
|
-
"""
|
106
|
-
"body":{"encoding":"US-ASCII","string":"Hello World"}
|
107
|
-
"""
|
108
|
-
|
@@ -1,84 +0,0 @@
|
|
1
|
-
Feature: query_parser
|
2
|
-
|
3
|
-
By default, VCR will parse query strings using `CGI.parse` from the Ruby
|
4
|
-
standard library. This may not be the most optimal or performant library
|
5
|
-
available. You can set the `query_parser` configuration option to use a
|
6
|
-
different parser (such as `Rack::Utils.method(:parse_query)`) to decode,
|
7
|
-
normalize, and/or provide a comparison object for query strings.
|
8
|
-
|
9
|
-
The configured query parser needs to expose a `.call` method that returns an
|
10
|
-
object which is comparable. This instance needs to implement the following
|
11
|
-
API:
|
12
|
-
|
13
|
-
* `#==` => boolean
|
14
|
-
|
15
|
-
Background:
|
16
|
-
Given a file named "cassettes/example.yml" with:
|
17
|
-
"""
|
18
|
-
---
|
19
|
-
http_interactions:
|
20
|
-
- request:
|
21
|
-
method: get
|
22
|
-
uri: http://url.example.com/?bravo=2&alpha=1
|
23
|
-
body:
|
24
|
-
encoding: UTF-8
|
25
|
-
string: ""
|
26
|
-
headers: {}
|
27
|
-
response:
|
28
|
-
status:
|
29
|
-
code: 200
|
30
|
-
message: OK
|
31
|
-
headers:
|
32
|
-
Content-Length:
|
33
|
-
- "5"
|
34
|
-
body:
|
35
|
-
encoding: UTF-8
|
36
|
-
string: Hello
|
37
|
-
http_version: "1.1"
|
38
|
-
recorded_at: Tue, 25 Sep 2012 04:58:44 GMT
|
39
|
-
recorded_with: VCR 2.2.5
|
40
|
-
"""
|
41
|
-
|
42
|
-
Scenario: the VCR query parser gets its value from `query_parser`
|
43
|
-
Given a file named "query_parser.rb" with:
|
44
|
-
"""ruby
|
45
|
-
require 'vcr'
|
46
|
-
require 'rack'
|
47
|
-
|
48
|
-
VCR.configure do |c|
|
49
|
-
c.query_parser = lambda { |query| raise query.inspect }
|
50
|
-
c.default_cassette_options = {:match_requests_on => [:query]}
|
51
|
-
c.hook_into :webmock
|
52
|
-
c.cassette_library_dir = 'cassettes'
|
53
|
-
end
|
54
|
-
|
55
|
-
uri = URI.parse('http://other-url.example.com/?bravo=2&alpha=1')
|
56
|
-
VCR.use_cassette('example') do
|
57
|
-
puts Net::HTTP.get_response(uri).body
|
58
|
-
end
|
59
|
-
"""
|
60
|
-
When I run `ruby query_parser.rb`
|
61
|
-
Then it should fail with an error like:
|
62
|
-
"""
|
63
|
-
"alpha=1&bravo=2"
|
64
|
-
"""
|
65
|
-
|
66
|
-
|
67
|
-
Scenario: the `query_parser` defaults to the standard library's `CGI.parse`
|
68
|
-
Given a file named "query_parser_default.rb" with:
|
69
|
-
"""ruby
|
70
|
-
require 'vcr'
|
71
|
-
|
72
|
-
VCR.configure do |c|
|
73
|
-
c.hook_into :webmock
|
74
|
-
c.default_cassette_options = {:match_requests_on => [:query]}
|
75
|
-
c.cassette_library_dir = 'cassettes'
|
76
|
-
end
|
77
|
-
|
78
|
-
uri = URI.parse('http://other-url.example.com/?bravo=2&alpha=1')
|
79
|
-
VCR.use_cassette('example') do
|
80
|
-
puts Net::HTTP.get_response(uri).body
|
81
|
-
end
|
82
|
-
"""
|
83
|
-
When I run `ruby query_parser_default.rb`
|
84
|
-
Then it should pass with "Hello"
|
@@ -1,93 +0,0 @@
|
|
1
|
-
Feature: uri_parser
|
2
|
-
|
3
|
-
By default, VCR will parse URIs using `URI` from the Ruby standard
|
4
|
-
library. There are some URIs seen out in the wild that `URI` cannot
|
5
|
-
parse properly. You can set the `uri_parser` configuration option
|
6
|
-
to use a different parser (such as `Addressable::URI`) to work with
|
7
|
-
these URIs.
|
8
|
-
|
9
|
-
The configured URI parser needs to expose a `.parse` class method
|
10
|
-
that returns an instance of the uri. This uri instance needs to
|
11
|
-
implement the following API:
|
12
|
-
|
13
|
-
* `#scheme` => a string
|
14
|
-
* `#host` => a string
|
15
|
-
* `#port` => a fixnum
|
16
|
-
* `#path` => a string
|
17
|
-
* `#query` => a string
|
18
|
-
* `#to_s` => a string
|
19
|
-
* `#port=`
|
20
|
-
* `#query=`
|
21
|
-
* `#==` => boolean
|
22
|
-
|
23
|
-
Background:
|
24
|
-
Given a file named "cassettes/example.yml" with:
|
25
|
-
"""
|
26
|
-
---
|
27
|
-
http_interactions:
|
28
|
-
- request:
|
29
|
-
method: get
|
30
|
-
uri: http://example.com/hello
|
31
|
-
body:
|
32
|
-
encoding: UTF-8
|
33
|
-
string: ""
|
34
|
-
headers: {}
|
35
|
-
response:
|
36
|
-
status:
|
37
|
-
code: 200
|
38
|
-
message: OK
|
39
|
-
headers:
|
40
|
-
Content-Length:
|
41
|
-
- "5"
|
42
|
-
body:
|
43
|
-
encoding: UTF-8
|
44
|
-
string: Hello
|
45
|
-
http_version: "1.1"
|
46
|
-
recorded_at: Tue, 25 Sep 2012 04:58:44 GMT
|
47
|
-
recorded_with: VCR 2.2.5
|
48
|
-
"""
|
49
|
-
|
50
|
-
Scenario: the VCR uri parser gets its value from `uri_parser`
|
51
|
-
Given a file named "uri_parser.rb" with:
|
52
|
-
"""ruby
|
53
|
-
require 'vcr'
|
54
|
-
require 'uri'
|
55
|
-
|
56
|
-
module MyURI
|
57
|
-
def self.parse(url)
|
58
|
-
uri = URI.parse(url)
|
59
|
-
uri.host = 'example.com'
|
60
|
-
uri.path = '/hello'
|
61
|
-
uri
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
VCR.configure do |c|
|
66
|
-
c.uri_parser = MyURI
|
67
|
-
c.hook_into :webmock
|
68
|
-
c.cassette_library_dir = 'cassettes'
|
69
|
-
end
|
70
|
-
|
71
|
-
VCR.use_cassette('example') do
|
72
|
-
puts Net::HTTP.get_response('evil.org', '/projects').body
|
73
|
-
end
|
74
|
-
"""
|
75
|
-
When I run `ruby uri_parser.rb`
|
76
|
-
Then it should pass with "Hello"
|
77
|
-
|
78
|
-
Scenario: the `uri_parser` defaults to the standard library's `URI`
|
79
|
-
Given a file named "uri_parser_default.rb" with:
|
80
|
-
"""ruby
|
81
|
-
require 'vcr'
|
82
|
-
|
83
|
-
VCR.configure do |c|
|
84
|
-
c.hook_into :webmock
|
85
|
-
c.cassette_library_dir = 'cassettes'
|
86
|
-
end
|
87
|
-
|
88
|
-
VCR.use_cassette('example') do
|
89
|
-
puts Net::HTTP.get_response('example.com', '/hello').body
|
90
|
-
end
|
91
|
-
"""
|
92
|
-
When I run `ruby uri_parser_default.rb`
|
93
|
-
Then it should pass with "Hello"
|
data/features/getting_started.md
DELETED
@@ -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
|
-
|