vcr 3.0.2 → 6.0.0

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