mustwin-vcr 2.9.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/features/about_these_examples.md +18 -0
- data/features/cassettes/allow_unused_http_interactions.feature +100 -0
- data/features/cassettes/automatic_re_recording.feature +72 -0
- data/features/cassettes/decompress.feature +74 -0
- data/features/cassettes/dynamic_erb.feature +100 -0
- data/features/cassettes/exclusive.feature +126 -0
- data/features/cassettes/format.feature +323 -0
- data/features/cassettes/freezing_time.feature +68 -0
- data/features/cassettes/naming.feature +28 -0
- data/features/cassettes/no_cassette.feature +152 -0
- data/features/cassettes/update_content_length_header.feature +112 -0
- data/features/configuration/allow_http_connections_when_no_cassette.feature +55 -0
- data/features/configuration/cassette_library_dir.feature +31 -0
- data/features/configuration/debug_logging.feature +59 -0
- data/features/configuration/default_cassette_options.feature +100 -0
- data/features/configuration/filter_sensitive_data.feature +153 -0
- data/features/configuration/hook_into.feature +172 -0
- data/features/configuration/ignore_request.feature +192 -0
- data/features/configuration/preserve_exact_body_bytes.feature +108 -0
- data/features/configuration/query_parser.feature +84 -0
- data/features/configuration/uri_parser.feature +89 -0
- data/features/getting_started.md +82 -0
- data/features/hooks/after_http_request.feature +58 -0
- data/features/hooks/around_http_request.feature +57 -0
- data/features/hooks/before_http_request.feature +63 -0
- data/features/hooks/before_playback.feature +184 -0
- data/features/hooks/before_record.feature +172 -0
- data/features/http_libraries/em_http_request.feature +250 -0
- data/features/http_libraries/net_http.feature +179 -0
- data/features/middleware/faraday.feature +56 -0
- data/features/middleware/rack.feature +92 -0
- data/features/record_modes/all.feature +82 -0
- data/features/record_modes/new_episodes.feature +79 -0
- data/features/record_modes/none.feature +72 -0
- data/features/record_modes/once.feature +95 -0
- data/features/request_matching/README.md +30 -0
- data/features/request_matching/body.feature +91 -0
- data/features/request_matching/body_as_json.feature +90 -0
- data/features/request_matching/custom_matcher.feature +135 -0
- data/features/request_matching/headers.feature +85 -0
- data/features/request_matching/host.feature +95 -0
- data/features/request_matching/identical_request_sequence.feature +89 -0
- data/features/request_matching/method.feature +96 -0
- data/features/request_matching/path.feature +96 -0
- data/features/request_matching/playback_repeats.feature +98 -0
- data/features/request_matching/query.feature +97 -0
- data/features/request_matching/uri.feature +94 -0
- data/features/request_matching/uri_without_param.feature +101 -0
- data/features/step_definitions/cli_steps.rb +193 -0
- data/features/support/env.rb +44 -0
- data/features/support/http_lib_filters.rb +53 -0
- data/features/test_frameworks/cucumber.feature +211 -0
- data/features/test_frameworks/rspec_macro.feature +81 -0
- data/features/test_frameworks/rspec_metadata.feature +150 -0
- data/features/test_frameworks/test_unit.feature +49 -0
- data/lib/vcr.rb +347 -0
- data/lib/vcr/cassette.rb +291 -0
- data/lib/vcr/cassette/erb_renderer.rb +55 -0
- data/lib/vcr/cassette/http_interaction_list.rb +108 -0
- data/lib/vcr/cassette/migrator.rb +118 -0
- data/lib/vcr/cassette/persisters.rb +42 -0
- data/lib/vcr/cassette/persisters/file_system.rb +64 -0
- data/lib/vcr/cassette/serializers.rb +57 -0
- data/lib/vcr/cassette/serializers/json.rb +48 -0
- data/lib/vcr/cassette/serializers/psych.rb +48 -0
- data/lib/vcr/cassette/serializers/syck.rb +61 -0
- data/lib/vcr/cassette/serializers/yaml.rb +50 -0
- data/lib/vcr/configuration.rb +555 -0
- data/lib/vcr/deprecations.rb +109 -0
- data/lib/vcr/errors.rb +266 -0
- data/lib/vcr/extensions/net_http_response.rb +36 -0
- data/lib/vcr/library_hooks.rb +18 -0
- data/lib/vcr/library_hooks/excon.rb +27 -0
- data/lib/vcr/library_hooks/fakeweb.rb +196 -0
- data/lib/vcr/library_hooks/faraday.rb +51 -0
- data/lib/vcr/library_hooks/typhoeus.rb +120 -0
- data/lib/vcr/library_hooks/typhoeus_0.4.rb +103 -0
- data/lib/vcr/library_hooks/webmock.rb +164 -0
- data/lib/vcr/middleware/excon.rb +221 -0
- data/lib/vcr/middleware/excon/legacy_methods.rb +33 -0
- data/lib/vcr/middleware/faraday.rb +118 -0
- data/lib/vcr/middleware/rack.rb +79 -0
- data/lib/vcr/request_handler.rb +114 -0
- data/lib/vcr/request_ignorer.rb +43 -0
- data/lib/vcr/request_matcher_registry.rb +149 -0
- data/lib/vcr/structs.rb +578 -0
- data/lib/vcr/tasks/vcr.rake +9 -0
- data/lib/vcr/test_frameworks/cucumber.rb +64 -0
- data/lib/vcr/test_frameworks/rspec.rb +47 -0
- data/lib/vcr/util/hooks.rb +61 -0
- data/lib/vcr/util/internet_connection.rb +43 -0
- data/lib/vcr/util/logger.rb +59 -0
- data/lib/vcr/util/variable_args_block_caller.rb +13 -0
- data/lib/vcr/util/version_checker.rb +48 -0
- data/lib/vcr/version.rb +34 -0
- data/spec/acceptance/threading_spec.rb +34 -0
- data/spec/fixtures/cassette_spec/1_x_cassette.yml +110 -0
- data/spec/fixtures/cassette_spec/empty.yml +0 -0
- data/spec/fixtures/cassette_spec/example.yml +111 -0
- data/spec/fixtures/cassette_spec/with_localhost_requests.yml +111 -0
- data/spec/fixtures/fake_example_responses.yml +110 -0
- data/spec/fixtures/match_requests_on.yml +187 -0
- data/spec/lib/vcr/cassette/erb_renderer_spec.rb +53 -0
- data/spec/lib/vcr/cassette/http_interaction_list_spec.rb +295 -0
- data/spec/lib/vcr/cassette/migrator_spec.rb +195 -0
- data/spec/lib/vcr/cassette/persisters/file_system_spec.rb +69 -0
- data/spec/lib/vcr/cassette/persisters_spec.rb +39 -0
- data/spec/lib/vcr/cassette/serializers_spec.rb +176 -0
- data/spec/lib/vcr/cassette_spec.rb +618 -0
- data/spec/lib/vcr/configuration_spec.rb +326 -0
- data/spec/lib/vcr/deprecations_spec.rb +85 -0
- data/spec/lib/vcr/errors_spec.rb +162 -0
- data/spec/lib/vcr/extensions/net_http_response_spec.rb +86 -0
- data/spec/lib/vcr/library_hooks/excon_spec.rb +104 -0
- data/spec/lib/vcr/library_hooks/fakeweb_spec.rb +169 -0
- data/spec/lib/vcr/library_hooks/faraday_spec.rb +68 -0
- data/spec/lib/vcr/library_hooks/typhoeus_0.4_spec.rb +36 -0
- data/spec/lib/vcr/library_hooks/typhoeus_spec.rb +162 -0
- data/spec/lib/vcr/library_hooks/webmock_spec.rb +118 -0
- data/spec/lib/vcr/library_hooks_spec.rb +51 -0
- data/spec/lib/vcr/middleware/faraday_spec.rb +182 -0
- data/spec/lib/vcr/middleware/rack_spec.rb +115 -0
- data/spec/lib/vcr/request_ignorer_spec.rb +70 -0
- data/spec/lib/vcr/request_matcher_registry_spec.rb +345 -0
- data/spec/lib/vcr/structs_spec.rb +732 -0
- data/spec/lib/vcr/test_frameworks/cucumber_spec.rb +107 -0
- data/spec/lib/vcr/test_frameworks/rspec_spec.rb +83 -0
- data/spec/lib/vcr/util/hooks_spec.rb +158 -0
- data/spec/lib/vcr/util/internet_connection_spec.rb +37 -0
- data/spec/lib/vcr/util/version_checker_spec.rb +31 -0
- data/spec/lib/vcr/version_spec.rb +27 -0
- data/spec/lib/vcr_spec.rb +349 -0
- data/spec/monkey_patches.rb +182 -0
- data/spec/spec_helper.rb +62 -0
- data/spec/support/configuration_stubbing.rb +8 -0
- data/spec/support/cucumber_helpers.rb +35 -0
- data/spec/support/fixnum_extension.rb +10 -0
- data/spec/support/http_library_adapters.rb +289 -0
- data/spec/support/limited_uri.rb +21 -0
- data/spec/support/ruby_interpreter.rb +7 -0
- data/spec/support/shared_example_groups/excon.rb +63 -0
- data/spec/support/shared_example_groups/hook_into_http_library.rb +594 -0
- data/spec/support/shared_example_groups/request_hooks.rb +59 -0
- data/spec/support/sinatra_app.rb +86 -0
- data/spec/support/vcr_localhost_server.rb +76 -0
- data/spec/support/vcr_stub_helpers.rb +17 -0
- metadata +677 -0
@@ -0,0 +1,349 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe VCR do
|
4
|
+
def insert_cassette(name = :cassette_test)
|
5
|
+
VCR.insert_cassette(name)
|
6
|
+
end
|
7
|
+
|
8
|
+
describe '.insert_cassette' do
|
9
|
+
it 'creates a new cassette' do
|
10
|
+
expect(insert_cassette).to be_instance_of(VCR::Cassette)
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'takes over as the #current_cassette' do
|
14
|
+
orig_cassette = VCR.current_cassette
|
15
|
+
new_cassette = insert_cassette
|
16
|
+
expect(new_cassette).not_to eq(orig_cassette)
|
17
|
+
expect(VCR.current_cassette).to eq(new_cassette)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'raises an error if the stack of inserted cassettes already contains a cassette with the same name' do
|
21
|
+
insert_cassette(:foo)
|
22
|
+
expect {
|
23
|
+
insert_cassette(:foo)
|
24
|
+
}.to raise_error(/There is already a cassette with the same name/)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe '.eject_cassette' do
|
29
|
+
it 'ejects the current cassette' do
|
30
|
+
cassette = insert_cassette
|
31
|
+
expect(cassette).to receive(:eject)
|
32
|
+
VCR.eject_cassette
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'forwards the given options to `Cassette#eject`' do
|
36
|
+
cassette = insert_cassette
|
37
|
+
expect(cassette).to receive(:eject).with(:some => :options)
|
38
|
+
VCR.eject_cassette(:some => :options)
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'returns the ejected cassette' do
|
42
|
+
cassette = insert_cassette
|
43
|
+
expect(VCR.eject_cassette).to eq(cassette)
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'returns the #current_cassette to the previous one' do
|
47
|
+
cassette1, cassette2 = insert_cassette(:foo1), insert_cassette(:foo2)
|
48
|
+
expect { VCR.eject_cassette }.to change(VCR, :current_cassette).from(cassette2).to(cassette1)
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'keeps the cassette as the current one until after #eject has finished' do
|
52
|
+
cassette = insert_cassette
|
53
|
+
current = nil
|
54
|
+
allow(cassette).to receive(:eject) { current = VCR.current_cassette }
|
55
|
+
|
56
|
+
VCR.eject_cassette
|
57
|
+
|
58
|
+
expect(current).to be(cassette)
|
59
|
+
expect(VCR.current_cassette).not_to be(cassette)
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'properly pops the cassette off the stack even if an error occurs' do
|
63
|
+
cassette = insert_cassette
|
64
|
+
allow(cassette).to receive(:eject) { raise "boom" }
|
65
|
+
expect { VCR.eject_cassette }.to raise_error("boom")
|
66
|
+
expect(VCR.current_cassette).to be_nil
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
describe '.use_cassette' do
|
71
|
+
it 'inserts a new cassette' do
|
72
|
+
new_cassette = VCR::Cassette.new(:use_cassette_test)
|
73
|
+
expect(VCR).to receive(:insert_cassette).and_return(new_cassette)
|
74
|
+
VCR.use_cassette(:cassette_test) { }
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'yields' do
|
78
|
+
yielded = false
|
79
|
+
VCR.use_cassette(:cassette_test, &lambda { yielded = true })
|
80
|
+
expect(yielded).to be true
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'yields the cassette instance if the block expects an argument' do
|
84
|
+
VCR.use_cassette('name', :record => :new_episodes, &lambda do |cassette|
|
85
|
+
expect(cassette).to equal(VCR.current_cassette)
|
86
|
+
end)
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'yields the cassette instance if the block expects a variable number of args' do
|
90
|
+
VCR.use_cassette('name', :record => :new_episodes) do |*args|
|
91
|
+
expect(args.size).to eq(1)
|
92
|
+
expect(args.first).to equal(VCR.current_cassette)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'ejects the cassette' do
|
97
|
+
expect(VCR).to receive(:eject_cassette)
|
98
|
+
VCR.use_cassette(:cassette_test) { }
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'ejects the cassette even if there is an error' do
|
102
|
+
expect(VCR).to receive(:eject_cassette)
|
103
|
+
expect { VCR.use_cassette(:cassette_test) { raise StandardError } }.to raise_error
|
104
|
+
end
|
105
|
+
|
106
|
+
it 'does not eject a cassette if there was an error inserting it' do
|
107
|
+
expect(VCR).to receive(:insert_cassette).and_raise(StandardError.new('Boom!'))
|
108
|
+
expect(VCR).not_to receive(:eject_cassette)
|
109
|
+
expect { VCR.use_cassette(:test) { } }.to raise_error(StandardError, 'Boom!')
|
110
|
+
end
|
111
|
+
|
112
|
+
it 'raises a helpful error if no block is given' do
|
113
|
+
expect {
|
114
|
+
VCR.use_cassette(:test)
|
115
|
+
}.to raise_error(/requires a block/)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
describe '.http_interactions' do
|
120
|
+
it 'returns the current_cassette.http_interactions when there is a current cassette' do
|
121
|
+
cassette = VCR.insert_cassette("a cassette")
|
122
|
+
expect(VCR.http_interactions).to be(cassette.http_interactions)
|
123
|
+
end
|
124
|
+
|
125
|
+
it 'returns a null list when there is no current cassette' do
|
126
|
+
expect(VCR.current_cassette).to be_nil
|
127
|
+
expect(VCR.http_interactions).to be(VCR::Cassette::HTTPInteractionList::NullList)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
describe '.real_http_connections_allowed?' do
|
132
|
+
context 'when a cassette is inserted' do
|
133
|
+
it 'returns true if the cassette is recording' do
|
134
|
+
VCR.insert_cassette('foo', :record => :all)
|
135
|
+
expect(VCR.current_cassette).to be_recording
|
136
|
+
expect(VCR.real_http_connections_allowed?).to be true
|
137
|
+
end
|
138
|
+
|
139
|
+
it 'returns false if the cassette is not recording' do
|
140
|
+
VCR.insert_cassette('foo', :record => :none)
|
141
|
+
expect(VCR.current_cassette).not_to be_recording
|
142
|
+
expect(VCR.real_http_connections_allowed?).to be false
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
context 'when no cassette is inserted' do
|
147
|
+
before(:each) do
|
148
|
+
expect(VCR.current_cassette).to be_nil
|
149
|
+
end
|
150
|
+
|
151
|
+
it 'returns true if the allow_http_connections_when_no_cassette option is set to true' do
|
152
|
+
expect(VCR).to be_turned_on
|
153
|
+
VCR.configure { |c| c.allow_http_connections_when_no_cassette = true }
|
154
|
+
expect(VCR.real_http_connections_allowed?).to be true
|
155
|
+
end
|
156
|
+
|
157
|
+
it 'returns true if VCR is turned off' do
|
158
|
+
VCR.turn_off!
|
159
|
+
VCR.configure { |c| c.allow_http_connections_when_no_cassette = false }
|
160
|
+
expect(VCR.real_http_connections_allowed?).to be true
|
161
|
+
end
|
162
|
+
|
163
|
+
it 'returns false if the allow_http_connections_when_no_cassette option is set to false and VCR is turned on' do
|
164
|
+
expect(VCR).to be_turned_on
|
165
|
+
VCR.configure { |c| c.allow_http_connections_when_no_cassette = false }
|
166
|
+
expect(VCR.real_http_connections_allowed?).to be false
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
describe '.request_matchers' do
|
172
|
+
it 'always returns the same memoized request matcher registry instance' do
|
173
|
+
expect(VCR.request_matchers).to be_a(VCR::RequestMatcherRegistry)
|
174
|
+
expect(VCR.request_matchers).to be(VCR.request_matchers)
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
describe '.request_ignorer' do
|
179
|
+
it 'always returns the same memoized request ignorer instance' do
|
180
|
+
expect(VCR.request_ignorer).to be_a(VCR::RequestIgnorer)
|
181
|
+
expect(VCR.request_ignorer).to be(VCR.request_ignorer)
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
describe '.library_hooks' do
|
186
|
+
it 'always returns the same memoized LibraryHooks instance' do
|
187
|
+
expect(VCR.library_hooks).to be_a(VCR::LibraryHooks)
|
188
|
+
expect(VCR.library_hooks).to be(VCR.library_hooks)
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
describe '.cassette_serializers' do
|
193
|
+
it 'always returns the same memoized cassette serializers instance' do
|
194
|
+
expect(VCR.cassette_serializers).to be_a(VCR::Cassette::Serializers)
|
195
|
+
expect(VCR.cassette_serializers).to be(VCR.cassette_serializers)
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
describe ".cassette_persisters" do
|
200
|
+
it "always returns the same memoized Cassette::Persisters instance" do
|
201
|
+
expect(VCR.cassette_persisters).to be_a(VCR::Cassette::Persisters)
|
202
|
+
expect(VCR.cassette_persisters).to be(VCR.cassette_persisters)
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
describe '.configuration' do
|
207
|
+
it 'returns the configuration object' do
|
208
|
+
expect(VCR.configuration).to be_a(VCR::Configuration)
|
209
|
+
end
|
210
|
+
|
211
|
+
it 'memoizes the instance' do
|
212
|
+
expect(VCR.configuration).to be(VCR.configuration)
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
describe '.configure' do
|
217
|
+
it 'yields the configuration object' do
|
218
|
+
yielded_object = nil
|
219
|
+
VCR.configure do |obj|
|
220
|
+
yielded_object = obj
|
221
|
+
end
|
222
|
+
expect(yielded_object).to eq(VCR.configuration)
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
describe '.cucumber_tags' do
|
227
|
+
it 'yields a cucumber tags object' do
|
228
|
+
yielded_object = nil
|
229
|
+
VCR.cucumber_tags do |obj|
|
230
|
+
yielded_object = obj
|
231
|
+
end
|
232
|
+
expect(yielded_object).to be_instance_of(VCR::CucumberTags)
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
describe '.record_http_interaction' do
|
237
|
+
before(:each) { allow(VCR).to receive(:current_cassette).and_return(current_cassette) }
|
238
|
+
let(:interaction) { double(:request => double) }
|
239
|
+
|
240
|
+
context 'when there is not a current cassette' do
|
241
|
+
let(:current_cassette) { nil }
|
242
|
+
|
243
|
+
it 'does not record a request' do
|
244
|
+
# we can't set a message expectation on nil, but there is no place to record it to...
|
245
|
+
# this mostly tests that there is no error.
|
246
|
+
VCR.record_http_interaction(interaction)
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
context 'when there is a current cassette' do
|
251
|
+
let(:current_cassette) { double('current cassette') }
|
252
|
+
|
253
|
+
it 'records the request when it should not be ignored' do
|
254
|
+
allow(VCR.request_ignorer).to receive(:ignore?).with(interaction.request).and_return(false)
|
255
|
+
expect(current_cassette).to receive(:record_http_interaction).with(interaction)
|
256
|
+
VCR.record_http_interaction(interaction)
|
257
|
+
end
|
258
|
+
|
259
|
+
it 'does not record the request when it should be ignored' do
|
260
|
+
allow(VCR.request_ignorer).to receive(:ignore?).with(interaction.request).and_return(true)
|
261
|
+
expect(current_cassette).not_to receive(:record_http_interaction)
|
262
|
+
VCR.record_http_interaction(interaction)
|
263
|
+
end
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
267
|
+
describe '.turn_off!' do
|
268
|
+
it 'indicates it is turned off' do
|
269
|
+
VCR.turn_off!
|
270
|
+
expect(VCR).not_to be_turned_on
|
271
|
+
end
|
272
|
+
|
273
|
+
it 'raises an error if a cassette is in use' do
|
274
|
+
VCR.insert_cassette('foo')
|
275
|
+
expect {
|
276
|
+
VCR.turn_off!
|
277
|
+
}.to raise_error(VCR::CassetteInUseError, /foo/)
|
278
|
+
end
|
279
|
+
|
280
|
+
it 'causes an error to be raised if you insert a cassette while VCR is turned off' do
|
281
|
+
VCR.turn_off!
|
282
|
+
expect {
|
283
|
+
VCR.insert_cassette('foo')
|
284
|
+
}.to raise_error(VCR::TurnedOffError)
|
285
|
+
end
|
286
|
+
|
287
|
+
it 'raises an ArgumentError when given an invalid option' do
|
288
|
+
expect {
|
289
|
+
VCR.turn_off!(:invalid_option => true)
|
290
|
+
}.to raise_error(ArgumentError)
|
291
|
+
end
|
292
|
+
|
293
|
+
context 'when `:ignore_cassettes => true` is passed' do
|
294
|
+
before(:each) { VCR.turn_off!(:ignore_cassettes => true) }
|
295
|
+
|
296
|
+
it 'ignores cassette insertions' do
|
297
|
+
VCR.insert_cassette('foo')
|
298
|
+
expect(VCR.current_cassette).to be_nil
|
299
|
+
end
|
300
|
+
|
301
|
+
it 'still runs a block passed to use_cassette' do
|
302
|
+
yielded = false
|
303
|
+
|
304
|
+
VCR.use_cassette('foo') do
|
305
|
+
yielded = true
|
306
|
+
expect(VCR.current_cassette).to be_nil
|
307
|
+
end
|
308
|
+
|
309
|
+
expect(yielded).to be true
|
310
|
+
end
|
311
|
+
end
|
312
|
+
end
|
313
|
+
|
314
|
+
describe '.turn_on!' do
|
315
|
+
before(:each) { VCR.turn_off! }
|
316
|
+
|
317
|
+
it 'indicates it is turned on' do
|
318
|
+
VCR.turn_on!
|
319
|
+
expect(VCR).to be_turned_on
|
320
|
+
end
|
321
|
+
end
|
322
|
+
|
323
|
+
describe '.turned_off' do
|
324
|
+
it 'yields with VCR turned off' do
|
325
|
+
expect(VCR).to be_turned_on
|
326
|
+
yielded = false
|
327
|
+
|
328
|
+
VCR.turned_off do
|
329
|
+
yielded = true
|
330
|
+
expect(VCR).not_to be_turned_on
|
331
|
+
end
|
332
|
+
|
333
|
+
expect(yielded).to eq(true)
|
334
|
+
expect(VCR).to be_turned_on
|
335
|
+
end
|
336
|
+
|
337
|
+
it 'passes options through to .turn_off!' do
|
338
|
+
expect(VCR).to receive(:turn_off!).with(:ignore_cassettes => true)
|
339
|
+
VCR.turned_off(:ignore_cassettes => true) { }
|
340
|
+
end
|
341
|
+
end
|
342
|
+
|
343
|
+
describe '.turned_on?' do
|
344
|
+
it 'is on by default' do
|
345
|
+
VCR.send(:initialize_ivars) # clear internal state
|
346
|
+
expect(VCR).to be_turned_on
|
347
|
+
end
|
348
|
+
end
|
349
|
+
end
|
@@ -0,0 +1,182 @@
|
|
1
|
+
require 'typhoeus'
|
2
|
+
|
3
|
+
module MonkeyPatches
|
4
|
+
extend self
|
5
|
+
|
6
|
+
NET_HTTP_SINGLETON = class << Net::HTTP; self; end
|
7
|
+
|
8
|
+
NET_HTTP_MONKEY_PATCHES = [
|
9
|
+
[Net::BufferedIO, :initialize],
|
10
|
+
[Net::HTTP, :request],
|
11
|
+
[Net::HTTP, :connect],
|
12
|
+
[NET_HTTP_SINGLETON, :socket_type]
|
13
|
+
]
|
14
|
+
|
15
|
+
ALL_MONKEY_PATCHES = NET_HTTP_MONKEY_PATCHES.dup
|
16
|
+
|
17
|
+
def enable!(scope)
|
18
|
+
case scope
|
19
|
+
when :fakeweb
|
20
|
+
realias_net_http :with_fakeweb
|
21
|
+
enable!(:vcr) # fakeweb hook relies upon VCR's Net::HTTP monkey patch
|
22
|
+
when :webmock
|
23
|
+
::WebMock.reset!
|
24
|
+
::WebMock::HttpLibAdapters::NetHttpAdapter.enable!
|
25
|
+
::WebMock::HttpLibAdapters::TyphoeusAdapter.enable! if defined?(::Typhoeus)
|
26
|
+
::WebMock::HttpLibAdapters::ExconAdapter.enable! if defined?(::Excon)
|
27
|
+
$original_webmock_callbacks.each do |cb|
|
28
|
+
::WebMock::CallbackRegistry.add_callback(cb[:options], cb[:block])
|
29
|
+
end
|
30
|
+
when :typhoeus
|
31
|
+
$original_typhoeus_global_hooks.each do |hook|
|
32
|
+
::Typhoeus.on_complete << hook
|
33
|
+
end
|
34
|
+
::Typhoeus.before.clear
|
35
|
+
$original_typhoeus_before_hooks.each do |hook|
|
36
|
+
::Typhoeus.before << hook
|
37
|
+
end
|
38
|
+
when :typhoeus_0_4
|
39
|
+
::Typhoeus::Hydra.global_hooks = $original_typhoeus_global_hooks
|
40
|
+
::Typhoeus::Hydra.stub_finders.clear
|
41
|
+
$original_typhoeus_stub_finders.each do |finder|
|
42
|
+
::Typhoeus::Hydra.stub_finders << finder
|
43
|
+
end
|
44
|
+
when :excon
|
45
|
+
VCR::LibraryHooks::Excon.configure_middleware
|
46
|
+
when :vcr
|
47
|
+
realias Net::HTTP, :request, :with_vcr
|
48
|
+
else raise ArgumentError.new("Unexpected scope: #{scope}")
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def disable_all!
|
53
|
+
realias_all :without_monkeypatches
|
54
|
+
|
55
|
+
if defined?(::WebMock::HttpLibAdapters)
|
56
|
+
::WebMock::HttpLibAdapters::NetHttpAdapter.disable!
|
57
|
+
::WebMock::HttpLibAdapters::TyphoeusAdapter.disable! if defined?(::Typhoeus)
|
58
|
+
::WebMock::HttpLibAdapters::ExconAdapter.disable! if defined?(::Excon)
|
59
|
+
::WebMock::CallbackRegistry.reset
|
60
|
+
::WebMock::StubRegistry.instance.request_stubs = []
|
61
|
+
end
|
62
|
+
|
63
|
+
if defined?(::Typhoeus.before)
|
64
|
+
::Typhoeus.on_complete.clear
|
65
|
+
::Typhoeus.before.clear
|
66
|
+
elsif defined?(::Typhoeus::Hydra)
|
67
|
+
::Typhoeus::Hydra.clear_global_hooks
|
68
|
+
::Typhoeus::Hydra.stub_finders.clear
|
69
|
+
end
|
70
|
+
|
71
|
+
if defined?(::Excon)
|
72
|
+
::Excon.defaults[:middlewares].delete(VCR::Middleware::Excon::Request)
|
73
|
+
::Excon.defaults[:middlewares].delete(VCR::Middleware::Excon::Response)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def init
|
78
|
+
# capture the monkey patched definitions so we can realias to them in the future
|
79
|
+
ALL_MONKEY_PATCHES.each do |mp|
|
80
|
+
capture_method_definition(mp.first, mp.last, false)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
private
|
85
|
+
|
86
|
+
def capture_method_definition(klass, method, original)
|
87
|
+
klass.class_eval do
|
88
|
+
monkeypatch_methods = [:vcr, :fakeweb].select { |m| method_defined?(:"#{method}_with_#{m}") }
|
89
|
+
|
90
|
+
if original
|
91
|
+
if monkeypatch_methods.size > 0
|
92
|
+
raise "The following monkeypatch methods have already been defined #{method}: #{monkey_patch_methods.inspect}"
|
93
|
+
end
|
94
|
+
alias_name = :"#{method}_without_monkeypatches"
|
95
|
+
else
|
96
|
+
if monkeypatch_methods.size == 0
|
97
|
+
raise "No monkey patch methods have been defined for #{method}"
|
98
|
+
end
|
99
|
+
alias_name = :"#{method}_with_monkeypatches"
|
100
|
+
end
|
101
|
+
|
102
|
+
alias_method alias_name, method
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
# capture the original method definitions before the monkey patches have been defined
|
107
|
+
# so we can realias to the originals in the future
|
108
|
+
ALL_MONKEY_PATCHES.each do |mp|
|
109
|
+
capture_method_definition(mp.first, mp.last, true)
|
110
|
+
end
|
111
|
+
|
112
|
+
def realias(klass, method, alias_extension)
|
113
|
+
klass.class_eval do
|
114
|
+
old_verbose, $VERBOSE = $VERBOSE, nil
|
115
|
+
alias_method method, :"#{method}_#{alias_extension}"
|
116
|
+
$VERBOSE = old_verbose
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def realias_all(alias_extension)
|
121
|
+
ALL_MONKEY_PATCHES.each do |mp|
|
122
|
+
realias mp.first, mp.last, alias_extension
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
def realias_net_http(alias_extension)
|
127
|
+
NET_HTTP_MONKEY_PATCHES.each do |mp|
|
128
|
+
realias mp.first, mp.last, alias_extension
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
# Require all the HTTP libraries--these must be required before WebMock
|
134
|
+
# for WebMock to work with them.
|
135
|
+
require 'httpclient'
|
136
|
+
|
137
|
+
unless RUBY_INTERPRETER == :jruby
|
138
|
+
require 'patron'
|
139
|
+
require 'em-http-request'
|
140
|
+
require 'curb'
|
141
|
+
end
|
142
|
+
|
143
|
+
if defined?(::Typhoeus.before)
|
144
|
+
require 'vcr/library_hooks/typhoeus'
|
145
|
+
$typhoeus_after_loaded_hook = VCR.configuration.hooks[:after_library_hooks_loaded].last
|
146
|
+
$original_typhoeus_global_hooks = Typhoeus.on_complete.dup
|
147
|
+
$original_typhoeus_before_hooks = Typhoeus.before.dup
|
148
|
+
elsif defined?(::Typhoeus::Hydra.global_hooks)
|
149
|
+
require 'vcr/library_hooks/typhoeus'
|
150
|
+
$typhoeus_0_4_after_loaded_hook = VCR.configuration.hooks[:after_library_hooks_loaded].first
|
151
|
+
$typhoeus_after_loaded_hook = VCR.configuration.hooks[:after_library_hooks_loaded].last
|
152
|
+
$original_typhoeus_global_hooks = Typhoeus::Hydra.global_hooks.dup
|
153
|
+
$original_typhoeus_stub_finders = Typhoeus::Hydra.stub_finders.dup
|
154
|
+
end
|
155
|
+
|
156
|
+
require 'vcr/library_hooks/fakeweb'
|
157
|
+
$fakeweb_after_loaded_hook = VCR.configuration.hooks[:after_library_hooks_loaded].last
|
158
|
+
|
159
|
+
# All Net::HTTP monkey patches have now been loaded, so capture the
|
160
|
+
# appropriate method definitions so we can disable them later.
|
161
|
+
MonkeyPatches.init
|
162
|
+
|
163
|
+
# Disable FakeWeb/VCR Net::HTTP patches before WebMock
|
164
|
+
# subclasses Net::HTTP and inherits them...
|
165
|
+
MonkeyPatches.disable_all!
|
166
|
+
|
167
|
+
require 'vcr/library_hooks/webmock'
|
168
|
+
$original_webmock_callbacks = ::WebMock::CallbackRegistry.callbacks
|
169
|
+
|
170
|
+
require 'vcr/library_hooks/excon'
|
171
|
+
$excon_after_loaded_hook = VCR.configuration.hooks[:after_library_hooks_loaded].last
|
172
|
+
|
173
|
+
# disable all by default; we'll enable specific ones when we need them
|
174
|
+
MonkeyPatches.disable_all!
|
175
|
+
|
176
|
+
RSpec.configure do |config|
|
177
|
+
[:fakeweb, :webmock, :vcr, :typhoeus, :typhoeus_0_4, :excon].each do |scope|
|
178
|
+
config.before(:all, :with_monkey_patches => scope) { MonkeyPatches.enable!(scope) }
|
179
|
+
config.after(:all, :with_monkey_patches => scope) { MonkeyPatches.disable_all! }
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|