mustwin-vcr 2.9.3

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.
Files changed (148) hide show
  1. checksums.yaml +7 -0
  2. data/features/about_these_examples.md +18 -0
  3. data/features/cassettes/allow_unused_http_interactions.feature +100 -0
  4. data/features/cassettes/automatic_re_recording.feature +72 -0
  5. data/features/cassettes/decompress.feature +74 -0
  6. data/features/cassettes/dynamic_erb.feature +100 -0
  7. data/features/cassettes/exclusive.feature +126 -0
  8. data/features/cassettes/format.feature +323 -0
  9. data/features/cassettes/freezing_time.feature +68 -0
  10. data/features/cassettes/naming.feature +28 -0
  11. data/features/cassettes/no_cassette.feature +152 -0
  12. data/features/cassettes/update_content_length_header.feature +112 -0
  13. data/features/configuration/allow_http_connections_when_no_cassette.feature +55 -0
  14. data/features/configuration/cassette_library_dir.feature +31 -0
  15. data/features/configuration/debug_logging.feature +59 -0
  16. data/features/configuration/default_cassette_options.feature +100 -0
  17. data/features/configuration/filter_sensitive_data.feature +153 -0
  18. data/features/configuration/hook_into.feature +172 -0
  19. data/features/configuration/ignore_request.feature +192 -0
  20. data/features/configuration/preserve_exact_body_bytes.feature +108 -0
  21. data/features/configuration/query_parser.feature +84 -0
  22. data/features/configuration/uri_parser.feature +89 -0
  23. data/features/getting_started.md +82 -0
  24. data/features/hooks/after_http_request.feature +58 -0
  25. data/features/hooks/around_http_request.feature +57 -0
  26. data/features/hooks/before_http_request.feature +63 -0
  27. data/features/hooks/before_playback.feature +184 -0
  28. data/features/hooks/before_record.feature +172 -0
  29. data/features/http_libraries/em_http_request.feature +250 -0
  30. data/features/http_libraries/net_http.feature +179 -0
  31. data/features/middleware/faraday.feature +56 -0
  32. data/features/middleware/rack.feature +92 -0
  33. data/features/record_modes/all.feature +82 -0
  34. data/features/record_modes/new_episodes.feature +79 -0
  35. data/features/record_modes/none.feature +72 -0
  36. data/features/record_modes/once.feature +95 -0
  37. data/features/request_matching/README.md +30 -0
  38. data/features/request_matching/body.feature +91 -0
  39. data/features/request_matching/body_as_json.feature +90 -0
  40. data/features/request_matching/custom_matcher.feature +135 -0
  41. data/features/request_matching/headers.feature +85 -0
  42. data/features/request_matching/host.feature +95 -0
  43. data/features/request_matching/identical_request_sequence.feature +89 -0
  44. data/features/request_matching/method.feature +96 -0
  45. data/features/request_matching/path.feature +96 -0
  46. data/features/request_matching/playback_repeats.feature +98 -0
  47. data/features/request_matching/query.feature +97 -0
  48. data/features/request_matching/uri.feature +94 -0
  49. data/features/request_matching/uri_without_param.feature +101 -0
  50. data/features/step_definitions/cli_steps.rb +193 -0
  51. data/features/support/env.rb +44 -0
  52. data/features/support/http_lib_filters.rb +53 -0
  53. data/features/test_frameworks/cucumber.feature +211 -0
  54. data/features/test_frameworks/rspec_macro.feature +81 -0
  55. data/features/test_frameworks/rspec_metadata.feature +150 -0
  56. data/features/test_frameworks/test_unit.feature +49 -0
  57. data/lib/vcr.rb +347 -0
  58. data/lib/vcr/cassette.rb +291 -0
  59. data/lib/vcr/cassette/erb_renderer.rb +55 -0
  60. data/lib/vcr/cassette/http_interaction_list.rb +108 -0
  61. data/lib/vcr/cassette/migrator.rb +118 -0
  62. data/lib/vcr/cassette/persisters.rb +42 -0
  63. data/lib/vcr/cassette/persisters/file_system.rb +64 -0
  64. data/lib/vcr/cassette/serializers.rb +57 -0
  65. data/lib/vcr/cassette/serializers/json.rb +48 -0
  66. data/lib/vcr/cassette/serializers/psych.rb +48 -0
  67. data/lib/vcr/cassette/serializers/syck.rb +61 -0
  68. data/lib/vcr/cassette/serializers/yaml.rb +50 -0
  69. data/lib/vcr/configuration.rb +555 -0
  70. data/lib/vcr/deprecations.rb +109 -0
  71. data/lib/vcr/errors.rb +266 -0
  72. data/lib/vcr/extensions/net_http_response.rb +36 -0
  73. data/lib/vcr/library_hooks.rb +18 -0
  74. data/lib/vcr/library_hooks/excon.rb +27 -0
  75. data/lib/vcr/library_hooks/fakeweb.rb +196 -0
  76. data/lib/vcr/library_hooks/faraday.rb +51 -0
  77. data/lib/vcr/library_hooks/typhoeus.rb +120 -0
  78. data/lib/vcr/library_hooks/typhoeus_0.4.rb +103 -0
  79. data/lib/vcr/library_hooks/webmock.rb +164 -0
  80. data/lib/vcr/middleware/excon.rb +221 -0
  81. data/lib/vcr/middleware/excon/legacy_methods.rb +33 -0
  82. data/lib/vcr/middleware/faraday.rb +118 -0
  83. data/lib/vcr/middleware/rack.rb +79 -0
  84. data/lib/vcr/request_handler.rb +114 -0
  85. data/lib/vcr/request_ignorer.rb +43 -0
  86. data/lib/vcr/request_matcher_registry.rb +149 -0
  87. data/lib/vcr/structs.rb +578 -0
  88. data/lib/vcr/tasks/vcr.rake +9 -0
  89. data/lib/vcr/test_frameworks/cucumber.rb +64 -0
  90. data/lib/vcr/test_frameworks/rspec.rb +47 -0
  91. data/lib/vcr/util/hooks.rb +61 -0
  92. data/lib/vcr/util/internet_connection.rb +43 -0
  93. data/lib/vcr/util/logger.rb +59 -0
  94. data/lib/vcr/util/variable_args_block_caller.rb +13 -0
  95. data/lib/vcr/util/version_checker.rb +48 -0
  96. data/lib/vcr/version.rb +34 -0
  97. data/spec/acceptance/threading_spec.rb +34 -0
  98. data/spec/fixtures/cassette_spec/1_x_cassette.yml +110 -0
  99. data/spec/fixtures/cassette_spec/empty.yml +0 -0
  100. data/spec/fixtures/cassette_spec/example.yml +111 -0
  101. data/spec/fixtures/cassette_spec/with_localhost_requests.yml +111 -0
  102. data/spec/fixtures/fake_example_responses.yml +110 -0
  103. data/spec/fixtures/match_requests_on.yml +187 -0
  104. data/spec/lib/vcr/cassette/erb_renderer_spec.rb +53 -0
  105. data/spec/lib/vcr/cassette/http_interaction_list_spec.rb +295 -0
  106. data/spec/lib/vcr/cassette/migrator_spec.rb +195 -0
  107. data/spec/lib/vcr/cassette/persisters/file_system_spec.rb +69 -0
  108. data/spec/lib/vcr/cassette/persisters_spec.rb +39 -0
  109. data/spec/lib/vcr/cassette/serializers_spec.rb +176 -0
  110. data/spec/lib/vcr/cassette_spec.rb +618 -0
  111. data/spec/lib/vcr/configuration_spec.rb +326 -0
  112. data/spec/lib/vcr/deprecations_spec.rb +85 -0
  113. data/spec/lib/vcr/errors_spec.rb +162 -0
  114. data/spec/lib/vcr/extensions/net_http_response_spec.rb +86 -0
  115. data/spec/lib/vcr/library_hooks/excon_spec.rb +104 -0
  116. data/spec/lib/vcr/library_hooks/fakeweb_spec.rb +169 -0
  117. data/spec/lib/vcr/library_hooks/faraday_spec.rb +68 -0
  118. data/spec/lib/vcr/library_hooks/typhoeus_0.4_spec.rb +36 -0
  119. data/spec/lib/vcr/library_hooks/typhoeus_spec.rb +162 -0
  120. data/spec/lib/vcr/library_hooks/webmock_spec.rb +118 -0
  121. data/spec/lib/vcr/library_hooks_spec.rb +51 -0
  122. data/spec/lib/vcr/middleware/faraday_spec.rb +182 -0
  123. data/spec/lib/vcr/middleware/rack_spec.rb +115 -0
  124. data/spec/lib/vcr/request_ignorer_spec.rb +70 -0
  125. data/spec/lib/vcr/request_matcher_registry_spec.rb +345 -0
  126. data/spec/lib/vcr/structs_spec.rb +732 -0
  127. data/spec/lib/vcr/test_frameworks/cucumber_spec.rb +107 -0
  128. data/spec/lib/vcr/test_frameworks/rspec_spec.rb +83 -0
  129. data/spec/lib/vcr/util/hooks_spec.rb +158 -0
  130. data/spec/lib/vcr/util/internet_connection_spec.rb +37 -0
  131. data/spec/lib/vcr/util/version_checker_spec.rb +31 -0
  132. data/spec/lib/vcr/version_spec.rb +27 -0
  133. data/spec/lib/vcr_spec.rb +349 -0
  134. data/spec/monkey_patches.rb +182 -0
  135. data/spec/spec_helper.rb +62 -0
  136. data/spec/support/configuration_stubbing.rb +8 -0
  137. data/spec/support/cucumber_helpers.rb +35 -0
  138. data/spec/support/fixnum_extension.rb +10 -0
  139. data/spec/support/http_library_adapters.rb +289 -0
  140. data/spec/support/limited_uri.rb +21 -0
  141. data/spec/support/ruby_interpreter.rb +7 -0
  142. data/spec/support/shared_example_groups/excon.rb +63 -0
  143. data/spec/support/shared_example_groups/hook_into_http_library.rb +594 -0
  144. data/spec/support/shared_example_groups/request_hooks.rb +59 -0
  145. data/spec/support/sinatra_app.rb +86 -0
  146. data/spec/support/vcr_localhost_server.rb +76 -0
  147. data/spec/support/vcr_stub_helpers.rb +17 -0
  148. 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
+