mustwin-vcr 2.9.3

Sign up to get free protection for your applications and to get access to all the features.
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,86 @@
1
+ require 'spec_helper'
2
+
3
+ describe VCR::Net::HTTPResponse do
4
+ def self.it_allows_the_body_to_be_read(expected_regex)
5
+ it 'allows the body to be read using #body' do
6
+ expect(response.body.to_s).to match expected_regex
7
+ end
8
+
9
+ it 'allows the body to be read using #read_body' do
10
+ expect(response.read_body.to_s).to match expected_regex
11
+ end
12
+
13
+ it 'allows the body to be read using #read_body with a block' do
14
+ yielded_body = ''
15
+ response { |r| r.read_body { |s| yielded_body << s.to_s } }
16
+ expect(yielded_body).to match expected_regex
17
+ end
18
+
19
+ it 'allows the body to be read by passing a destination string to #read_body' do
20
+ dest = ''
21
+ ret_val = response { |r| r.read_body(dest) }.body
22
+ expect(dest.to_s).to match expected_regex
23
+ expect(ret_val.to_s).to eq(dest)
24
+ end
25
+
26
+ it 'raises an ArgumentError if both a destination string and a block is given to #read_body' do
27
+ dest = ''
28
+ expect { response { |r| r.read_body(dest) { |s| } } }.to raise_error(ArgumentError, 'both arg and block given for HTTP method')
29
+ end
30
+
31
+ it 'raises an IOError when #read_body is called twice with a block' do
32
+ response { |r| r.read_body { |s| } }
33
+ expect { response { |r| r.read_body { |s| } } }.to raise_error(IOError, /read_body called twice/)
34
+ end
35
+
36
+ it 'raises an IOError when #read_body is called twice with a destination string' do
37
+ dest = ''
38
+ response { |r| r.read_body(dest) }
39
+ expect { response { |r| r.read_body(dest) } }.to raise_error(IOError, /read_body called twice/)
40
+ end
41
+ end
42
+
43
+ { :get => /GET to root/, :head => /\A\z/ }.each do |http_verb, expected_body_regex|
44
+ context "for a #{http_verb.to_s.upcase} request" do
45
+ let(:http_verb_method) { :"request_#{http_verb}" }
46
+
47
+ def response(&block)
48
+ if defined?(@response) && block
49
+ block.call(@response)
50
+ return @response
51
+ end
52
+
53
+ @response ||= begin
54
+ http = Net::HTTP.new('localhost', VCR::SinatraApp.port)
55
+ res = http.send(http_verb_method, '/', &block)
56
+ expect(res).not_to be_a(VCR::Net::HTTPResponse)
57
+ expect(res).not_to be_a(::Net::WebMockHTTPResponse)
58
+ res
59
+ end
60
+ end
61
+
62
+ context 'when the body has not already been read' do
63
+ it_allows_the_body_to_be_read(expected_body_regex)
64
+ end
65
+
66
+ context 'when the body has already been read using #read_body and a dest string' do
67
+ before(:each) do
68
+ dest = ''
69
+ response { |res| res.read_body(dest) }
70
+ response.extend VCR::Net::HTTPResponse
71
+ end
72
+
73
+ it_allows_the_body_to_be_read(expected_body_regex)
74
+ end
75
+
76
+ context 'when the body has already been read using #body' do
77
+ before(:each) do
78
+ response.body
79
+ response.extend VCR::Net::HTTPResponse
80
+ end
81
+
82
+ it_allows_the_body_to_be_read(expected_body_regex)
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,104 @@
1
+ require 'spec_helper'
2
+ require 'support/shared_example_groups/excon'
3
+
4
+ describe "Excon hook", :with_monkey_patches => :excon do
5
+ after(:each) do
6
+ ::Excon.stubs.clear
7
+ ::Excon.defaults[:mock] = false
8
+ end
9
+
10
+ def directly_stub_request(method, url, response_body)
11
+ ::Excon.defaults[:mock] = true
12
+ ::Excon.stub({ :method => method, :url => url }, { :body => response_body })
13
+ end
14
+
15
+ it_behaves_like 'a hook into an HTTP library', :excon, 'excon', :status_message_not_exposed
16
+
17
+ context "when the query is specified as a hash option" do
18
+ let(:excon) { ::Excon.new("http://localhost:#{VCR::SinatraApp.port}/search") }
19
+
20
+ it 'properly records and plays back the response' do
21
+ allow(VCR).to receive(:real_http_connections_allowed?).and_return(true)
22
+ recorded, played_back = [1, 2].map do
23
+ VCR.use_cassette('excon_query', :record => :once) do
24
+ excon.request(:method => :get, :query => { :q => 'Tolkien' }).body
25
+ end
26
+ end
27
+
28
+ expect(recorded).to eq(played_back)
29
+ expect(recorded).to eq('query: Tolkien')
30
+ end
31
+ end
32
+
33
+ context "when Excon's expects and idempotent middlewares cause errors to be raised" do
34
+ let(:excon) { ::Excon.new("http://localhost:#{VCR::SinatraApp.port}/404_not_200") }
35
+
36
+ def make_request
37
+ VCR.use_cassette('with_errors', :record => :once) do
38
+ excon.request(:method => :get, :expects => [200], :idempotent => true).body
39
+ end
40
+ end
41
+
42
+ it 'records and plays back properly' do
43
+ expect { make_request }.to raise_error(Excon::Errors::NotFound)
44
+ expect { make_request }.to raise_error(Excon::Errors::NotFound)
45
+ end
46
+ end
47
+
48
+ include_examples "Excon streaming"
49
+
50
+ context 'when Excon raises an error due to an unexpected response status' do
51
+ before(:each) do
52
+ allow(VCR).to receive(:real_http_connections_allowed?).and_return(true)
53
+ end
54
+
55
+ it 'still records properly' do
56
+ expect(VCR).to receive(:record_http_interaction) do |interaction|
57
+ expect(interaction.response.status.code).to eq(404)
58
+ expect(interaction.response.body).to eq('404 not 200')
59
+ end
60
+
61
+ expect {
62
+ Excon.get("http://localhost:#{VCR::SinatraApp.port}/404_not_200", :expects => 200)
63
+ }.to raise_error(Excon::Errors::Error)
64
+ end
65
+
66
+ def error_raised_by
67
+ yield
68
+ rescue => e
69
+ return e
70
+ else
71
+ raise "No error was raised"
72
+ end
73
+
74
+ it 'raises the same error class as excon itself raises' do
75
+ real_error, stubbed_error = 2.times.map do
76
+ error_raised_by do
77
+ VCR.use_cassette('excon_error', :record => :once) do
78
+ Excon.get("http://localhost:#{VCR::SinatraApp.port}/not_found", :expects => 200)
79
+ end
80
+ end
81
+ end
82
+
83
+ expect(stubbed_error.class).to be(real_error.class)
84
+ end
85
+
86
+ it_behaves_like "request hooks", :excon, :recordable do
87
+ undef make_request
88
+ def make_request(disabled = false)
89
+ expect {
90
+ Excon.get(request_url, :expects => 404)
91
+ }.to raise_error(Excon::Errors::Error)
92
+ end
93
+ end
94
+ end
95
+
96
+ describe "VCR.configuration.after_library_hooks_loaded hook" do
97
+ it 'disables the webmock excon adapter so it does not conflict with our typhoeus hook' do
98
+ expect(::WebMock::HttpLibAdapters::ExconAdapter).to respond_to(:disable!)
99
+ expect(::WebMock::HttpLibAdapters::ExconAdapter).to receive(:disable!)
100
+ $excon_after_loaded_hook.conditionally_invoke
101
+ end
102
+ end
103
+ end
104
+
@@ -0,0 +1,169 @@
1
+ require 'spec_helper'
2
+
3
+ describe "FakeWeb hook", :with_monkey_patches => :fakeweb do
4
+ after(:each) do
5
+ ::FakeWeb.clean_registry
6
+ end
7
+
8
+ def disable_real_connections
9
+ ::FakeWeb.allow_net_connect = false
10
+ ::FakeWeb::NetConnectNotAllowedError
11
+ end
12
+
13
+ def enable_real_connections
14
+ ::FakeWeb.allow_net_connect = true
15
+ end
16
+
17
+ def directly_stub_request(method, url, response_body)
18
+ ::FakeWeb.register_uri(method, url, :body => response_body)
19
+ end
20
+
21
+ it_behaves_like 'a hook into an HTTP library', :fakeweb, 'net/http'
22
+
23
+ describe "some specific Net::HTTP edge cases" do
24
+ before(:each) do
25
+ allow(VCR).to receive(:real_http_connections_allowed?).and_return(true)
26
+ end
27
+
28
+ it 'records the request body when using #post_form' do
29
+ expect(VCR).to receive(:record_http_interaction) do |interaction|
30
+ expect(interaction.request.body).to eq("q=ruby")
31
+ end
32
+
33
+ uri = URI("http://localhost:#{VCR::SinatraApp.port}/foo")
34
+ Net::HTTP.post_form(uri, 'q' => 'ruby')
35
+ end
36
+
37
+ it "does not record headers for which Net::HTTP sets defaults near the end of the real request" do
38
+ expect(VCR).to receive(:record_http_interaction) do |interaction|
39
+ expect(interaction.request.headers).not_to have_key('content-type')
40
+ expect(interaction.request.headers).not_to have_key('host')
41
+ end
42
+ Net::HTTP.new('localhost', VCR::SinatraApp.port).send_request('POST', '/', '', { 'x-http-user' => 'me' })
43
+ end
44
+
45
+ it "records headers for which Net::HTTP usually sets defaults when the user manually sets their values" do
46
+ expect(VCR).to receive(:record_http_interaction) do |interaction|
47
+ expect(interaction.request.headers['content-type']).to eq(['foo/bar'])
48
+ expect(interaction.request.headers['host']).to eq(['my-example.com'])
49
+ end
50
+ Net::HTTP.new('localhost', VCR::SinatraApp.port).send_request('POST', '/', '', { 'Content-Type' => 'foo/bar', 'Host' => 'my-example.com' })
51
+ end
52
+
53
+ def perform_get_with_returning_block
54
+ Net::HTTP.new('localhost', VCR::SinatraApp.port).request(Net::HTTP::Get.new('/', {})) do |response|
55
+ return response
56
+ end
57
+ end
58
+
59
+ it 'records the interaction when Net::HTTP#request is called with a block with a return statement' do
60
+ expect(VCR).to receive(:record_http_interaction).once
61
+ expect(perform_get_with_returning_block.body).to eq("GET to root")
62
+ end
63
+
64
+ def make_post_request
65
+ Net::HTTP.new('localhost', VCR::SinatraApp.port).post('/record-and-playback', '')
66
+ end
67
+
68
+ it 'records the interaction only once, even when Net::HTTP internally recursively calls #request' do
69
+ expect(VCR).to receive(:record_http_interaction).once
70
+ make_post_request
71
+ end
72
+
73
+ it 'properly returns the response body for a post request when recording, stubbing or ignoring the request' do
74
+ recorded_body = nil
75
+ VCR.use_cassette("new_cassette", :record => :once) do
76
+ recorded_body = make_post_request.body
77
+ expect(recorded_body).to match(/Response \d+/)
78
+ end
79
+
80
+ VCR.use_cassette("new_cassette", :record => :once) do
81
+ expect(make_post_request.body).to eq(recorded_body)
82
+ end
83
+
84
+ VCR.configuration.ignore_request { |r| true }
85
+ ignored_body = make_post_request.body
86
+ expect(ignored_body).not_to eq(recorded_body)
87
+ expect(ignored_body).to match(/Response \d+/)
88
+ end
89
+
90
+ context 'when the same Net::HTTP request object is used twice' do
91
+ let(:uri) { URI("http://localhost:#{VCR::SinatraApp.port}/foo") }
92
+ let(:http) { Net::HTTP.new(uri.host, uri.port) }
93
+
94
+ it 'raises an UnhandledHTTPRequestError when using a cassette that only recorded one request' do
95
+ VCR.use_cassette("new_cassette", :record => :once) do
96
+ request = Net::HTTP::Get.new(uri.request_uri)
97
+ http.request(request)
98
+ end
99
+
100
+ VCR.use_cassette("new_cassette", :record => :once) do
101
+ request = Net::HTTP::Get.new(uri.request_uri)
102
+ http.request(request)
103
+ http.request(request)
104
+ end
105
+ end
106
+ end
107
+ end
108
+
109
+ describe "VCR.configuration.after_library_hooks_loaded hook" do
110
+ let(:run_hook) { $fakeweb_after_loaded_hook.conditionally_invoke }
111
+
112
+ context 'when WebMock has been loaded' do
113
+ before(:each) do
114
+ expect(defined?(WebMock)).to be_truthy
115
+ end
116
+
117
+ it 'raises an error since FakeWeb and WebMock cannot both be used simultaneously' do
118
+ expect { run_hook }.to raise_error(ArgumentError, /cannot use both/)
119
+ end
120
+ end
121
+
122
+ context 'when WebMock has not been loaded' do
123
+ let!(:orig_webmock_constant) { ::WebMock }
124
+ before(:each) { Object.send(:remove_const, :WebMock) }
125
+ after(:each) { ::WebMock = orig_webmock_constant }
126
+
127
+ it 'does not raise an error' do
128
+ run_hook # should not raise an error
129
+ end
130
+
131
+ it "warns about FakeWeb deprecation" do
132
+ expect(::Kernel).to receive(:warn).with("WARNING: VCR's FakeWeb integration is deprecated and will be removed in VCR 3.0.")
133
+ run_hook
134
+ end
135
+ end
136
+ end
137
+
138
+ describe "when a SocketError occurs" do
139
+ before(:each) do
140
+ VCR.configuration.ignore_request { |r| true }
141
+ end
142
+
143
+ it_behaves_like "request hooks", :fakeweb, :ignored do
144
+ undef assert_expected_response
145
+ def assert_expected_response(response)
146
+ expect(response).to be_nil
147
+ end
148
+
149
+ undef make_request
150
+ def make_request(disabled = false)
151
+ allow_any_instance_of(::Net::HTTP).to receive(:request_without_vcr).and_raise(SocketError)
152
+ expect {
153
+ ::Net::HTTP.get_response(URI(request_url))
154
+ }.to raise_error(SocketError)
155
+ end
156
+ end
157
+ end
158
+
159
+ describe "when VCR is turned off" do
160
+ it 'allows white listed connections' do
161
+ ::FakeWeb.allow_net_connect = %r[localhost]
162
+
163
+ VCR.turn_off!
164
+
165
+ uri = URI("http://localhost:#{VCR::SinatraApp.port}/foo")
166
+ expect(Net::HTTP.get(uri)).to eq("FOO!")
167
+ end
168
+ end
169
+ end
@@ -0,0 +1,68 @@
1
+ require 'spec_helper'
2
+ require 'vcr/library_hooks/faraday'
3
+
4
+ describe "Faraday hook" do
5
+ it 'inserts the VCR middleware just before the adapter' do
6
+ conn = Faraday.new(:url => 'http://sushi.com') do |builder|
7
+ builder.request :url_encoded
8
+ builder.response :logger
9
+ builder.adapter :net_http
10
+ end
11
+
12
+ conn.builder.lock!
13
+ expect(conn.builder.handlers.last(2).map(&:klass)).to eq([
14
+ VCR::Middleware::Faraday,
15
+ Faraday::Adapter::NetHttp
16
+ ])
17
+ end
18
+
19
+ it 'handles the case where no adapter is declared' do
20
+ conn = Faraday.new
21
+
22
+ conn.builder.lock!
23
+ expect(conn.builder.handlers.last(2).map(&:klass)).to eq([
24
+ VCR::Middleware::Faraday,
25
+ Faraday::Adapter::NetHttp
26
+ ])
27
+ end
28
+
29
+ it 'does nothing if the VCR middleware has already been included' do
30
+ conn = Faraday.new(:url => 'http://sushi.com') do |builder|
31
+ builder.use VCR::Middleware::Faraday
32
+ builder.use Faraday::Response::Logger
33
+ builder.use Faraday::Adapter::NetHttp
34
+ end
35
+
36
+ conn.builder.lock!
37
+ expect(conn.builder.handlers.map(&:klass)).to eq([
38
+ VCR::Middleware::Faraday,
39
+ Faraday::Response::Logger,
40
+ Faraday::Adapter::NetHttp
41
+ ])
42
+ end
43
+
44
+ it 'prints a warning if the faraday connection stack contains a middleware after the HTTP adapter' do
45
+ conn = Faraday.new(:url => 'http://sushi.com') do |builder|
46
+ builder.use Faraday::Adapter::NetHttp
47
+ builder.use Faraday::Response::Logger
48
+ end
49
+
50
+ expect(conn.builder).to receive(:warn).with(/Faraday::Response::Logger/)
51
+ conn.builder.lock!
52
+ end
53
+
54
+ it 'gracefully handles the case where there is no explicit HTTP adapter' do
55
+ conn = Faraday.new(:url => 'http://sushi.com') do |builder|
56
+ builder.request :url_encoded
57
+ builder.response :logger
58
+ end
59
+
60
+ conn.builder.lock!
61
+ expect(conn.builder.handlers.map(&:klass)).to eq([
62
+ Faraday::Request::UrlEncoded,
63
+ Faraday::Response::Logger,
64
+ VCR::Middleware::Faraday
65
+ ])
66
+ end
67
+ end
68
+
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Typhoeus 0.4 hook", :with_monkey_patches => :typhoeus_0_4 do
4
+ after(:each) do
5
+ ::Typhoeus::Hydra.clear_stubs
6
+ end
7
+
8
+ def disable_real_connections
9
+ ::Typhoeus::Hydra.allow_net_connect = false
10
+ ::Typhoeus::Hydra::NetConnectNotAllowedError
11
+ end
12
+
13
+ def enable_real_connections
14
+ ::Typhoeus::Hydra.allow_net_connect = true
15
+ end
16
+
17
+ def directly_stub_request(method, url, response_body)
18
+ response = ::Typhoeus::Response.new(:code => 200, :body => response_body)
19
+ ::Typhoeus::Hydra.stub(method, url).and_return(response)
20
+ end
21
+
22
+ it_behaves_like 'a hook into an HTTP library', :typhoeus, 'typhoeus 0.4'
23
+
24
+ describe "VCR.configuration.after_library_hooks_loaded hook" do
25
+ it 'disables the webmock typhoeus adapter so it does not conflict with our typhoeus hook' do
26
+ expect(::WebMock::HttpLibAdapters::TyphoeusAdapter).to receive(:disable!)
27
+ $typhoeus_after_loaded_hook.conditionally_invoke
28
+ end
29
+
30
+ it "warns about Typhoeus 0.4 deprecation" do
31
+ expect(::Kernel).to receive(:warn).with("WARNING: VCR's Typhoeus 0.4 integration is deprecated and will be removed in VCR 3.0.")
32
+ $typhoeus_0_4_after_loaded_hook.conditionally_invoke
33
+ end
34
+ end
35
+ end if RUBY_INTERPRETER == :mri && ::Typhoeus::VERSION.to_f < 0.5
36
+