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
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
require "codeclimate-test-reporter"
|
2
|
+
CodeClimate::TestReporter.start
|
3
|
+
|
4
|
+
require "pry"
|
5
|
+
require "rspec"
|
6
|
+
require "vcr"
|
7
|
+
require "date"
|
8
|
+
require "forwardable"
|
9
|
+
require "uri"
|
10
|
+
require "vcr/util/internet_connection"
|
11
|
+
require_relative "support/fixnum_extension"
|
12
|
+
require_relative "support/limited_uri"
|
13
|
+
require_relative "support/ruby_interpreter"
|
14
|
+
require_relative "support/shared_example_groups/hook_into_http_library"
|
15
|
+
require_relative "support/shared_example_groups/request_hooks"
|
16
|
+
require_relative "support/vcr_stub_helpers"
|
17
|
+
require_relative "support/vcr_localhost_server"
|
18
|
+
require_relative "support/sinatra_app"
|
19
|
+
require_relative "monkey_patches"
|
20
|
+
require_relative "support/http_library_adapters"
|
21
|
+
|
22
|
+
module VCR
|
23
|
+
SPEC_ROOT = File.dirname(File.expand_path('.', __FILE__))
|
24
|
+
|
25
|
+
def reset!(hook = :fakeweb)
|
26
|
+
instance_variables.each do |ivar|
|
27
|
+
instance_variable_set(ivar, nil)
|
28
|
+
end
|
29
|
+
configuration.hook_into hook if hook
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
RSpec.configure do |config|
|
34
|
+
tmp_dir = File.expand_path('../../tmp/cassette_library_dir', __FILE__)
|
35
|
+
config.before(:each) do |example|
|
36
|
+
unless example.metadata[:skip_vcr_reset]
|
37
|
+
VCR.reset!
|
38
|
+
VCR.configuration.cassette_library_dir = tmp_dir
|
39
|
+
VCR.configuration.uri_parser = LimitedURI
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
config.after(:each) do
|
44
|
+
FileUtils.rm_rf tmp_dir
|
45
|
+
end
|
46
|
+
|
47
|
+
config.before(:all, :disable_warnings => true) do
|
48
|
+
@orig_std_err = $stderr
|
49
|
+
$stderr = StringIO.new
|
50
|
+
end
|
51
|
+
|
52
|
+
config.after(:all, :disable_warnings => true) do
|
53
|
+
$stderr = @orig_std_err
|
54
|
+
end
|
55
|
+
|
56
|
+
config.filter_run :focus => true
|
57
|
+
config.run_all_when_everything_filtered = true
|
58
|
+
|
59
|
+
config.alias_it_should_behave_like_to :it_performs, 'it performs'
|
60
|
+
end
|
61
|
+
|
62
|
+
VCR::SinatraApp.boot
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Gem
|
2
|
+
def self.win_platform?() false end
|
3
|
+
end unless defined?(Gem)
|
4
|
+
|
5
|
+
# pretend we're always on the internet (so that we don't have an
|
6
|
+
# internet connection dependency for our cukes)
|
7
|
+
VCR::InternetConnection.class_eval do
|
8
|
+
def available?; true; end
|
9
|
+
end
|
10
|
+
|
11
|
+
if ENV['DATE_STRING']
|
12
|
+
require 'timecop'
|
13
|
+
Timecop.travel(Date.parse(ENV['DATE_STRING']))
|
14
|
+
end
|
15
|
+
|
16
|
+
def include_http_adapter_for(lib)
|
17
|
+
require((lib =~ /faraday/) ? 'faraday' : lib)
|
18
|
+
require 'typhoeus' if lib.include?('typhoeus') # for faraday-typhoeus
|
19
|
+
require 'support/http_library_adapters'
|
20
|
+
include HTTP_LIBRARY_ADAPTERS[lib]
|
21
|
+
end
|
22
|
+
|
23
|
+
def response_body_for(*args)
|
24
|
+
get_body_string(make_http_request(*args))
|
25
|
+
end
|
26
|
+
|
27
|
+
def start_sinatra_app(&block)
|
28
|
+
require 'sinatra/base'
|
29
|
+
require 'support/vcr_localhost_server'
|
30
|
+
klass = Class.new(Sinatra::Base)
|
31
|
+
klass.disable :protection
|
32
|
+
klass.class_eval(&block)
|
33
|
+
|
34
|
+
VCR::LocalhostServer.new(klass.new)
|
35
|
+
end
|
@@ -0,0 +1,289 @@
|
|
1
|
+
module HeaderDowncaser
|
2
|
+
def downcase_headers(headers)
|
3
|
+
{}.tap do |downcased|
|
4
|
+
headers.each do |k, v|
|
5
|
+
downcased[k.downcase] = v
|
6
|
+
end
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
HTTP_LIBRARY_ADAPTERS = {}
|
12
|
+
|
13
|
+
HTTP_LIBRARY_ADAPTERS['net/http'] = Module.new do
|
14
|
+
include HeaderDowncaser
|
15
|
+
|
16
|
+
def self.http_library_name; 'Net::HTTP'; end
|
17
|
+
|
18
|
+
def get_body_string(response); response.body; end
|
19
|
+
alias get_body_object get_body_string
|
20
|
+
|
21
|
+
def get_header(header_key, response)
|
22
|
+
response.get_fields(header_key)
|
23
|
+
end
|
24
|
+
|
25
|
+
def make_http_request(method, url, body = nil, headers = {})
|
26
|
+
uri = URI.parse(url)
|
27
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
28
|
+
|
29
|
+
if uri.scheme == "https"
|
30
|
+
http.use_ssl = true
|
31
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
32
|
+
end
|
33
|
+
|
34
|
+
http.send_request(method.to_s.upcase, uri.request_uri, body, headers)
|
35
|
+
end
|
36
|
+
|
37
|
+
DEFAULT_REQUEST_HEADERS = { "Accept"=>["*/*"] }
|
38
|
+
DEFAULT_REQUEST_HEADERS['User-Agent'] = ["Ruby"] if RUBY_VERSION.to_f > 1.8
|
39
|
+
DEFAULT_REQUEST_HEADERS['Accept-Encoding'] = ["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"] if RUBY_VERSION.to_f > 1.9
|
40
|
+
|
41
|
+
def normalize_request_headers(headers)
|
42
|
+
defined?(super) ? super :
|
43
|
+
downcase_headers(headers.merge(DEFAULT_REQUEST_HEADERS))
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
HTTP_LIBRARY_ADAPTERS['patron'] = Module.new do
|
48
|
+
def self.http_library_name; 'Patron'; end
|
49
|
+
|
50
|
+
def get_body_string(response); response.body; end
|
51
|
+
alias get_body_object get_body_string
|
52
|
+
|
53
|
+
def get_header(header_key, response)
|
54
|
+
response.headers[header_key]
|
55
|
+
end
|
56
|
+
|
57
|
+
def make_http_request(method, url, body = nil, headers = {})
|
58
|
+
Patron::Session.new.request(method, url, headers, :data => body || '')
|
59
|
+
end
|
60
|
+
|
61
|
+
def normalize_request_headers(headers)
|
62
|
+
headers.merge('Expect' => [''])
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
HTTP_LIBRARY_ADAPTERS['httpclient'] = Module.new do
|
67
|
+
def self.http_library_name; 'HTTP Client'; end
|
68
|
+
|
69
|
+
def get_body_string(response)
|
70
|
+
body = response.body
|
71
|
+
string = body.is_a?(String) ? body : body.content
|
72
|
+
string.respond_to?(:read) ? string.read : string
|
73
|
+
end
|
74
|
+
|
75
|
+
def get_body_object(response)
|
76
|
+
response.body
|
77
|
+
end
|
78
|
+
|
79
|
+
def get_header(header_key, response)
|
80
|
+
response.header[header_key]
|
81
|
+
end
|
82
|
+
|
83
|
+
def make_http_request(method, url, body = nil, headers = {})
|
84
|
+
HTTPClient.new.request(method, url, nil, body, headers)
|
85
|
+
end
|
86
|
+
|
87
|
+
def normalize_request_headers(headers)
|
88
|
+
headers.merge({
|
89
|
+
'Accept' => ["*/*"],
|
90
|
+
'User-Agent' => ["HTTPClient/1.0 (#{HTTPClient::VERSION}, ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}))"],
|
91
|
+
'Date' => [Time.now.httpdate]
|
92
|
+
})
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
HTTP_LIBRARY_ADAPTERS['em-http-request'] = Module.new do
|
97
|
+
def self.http_library_name; 'EM HTTP Request'; end
|
98
|
+
|
99
|
+
def get_body_string(response)
|
100
|
+
response.response
|
101
|
+
end
|
102
|
+
alias get_body_object get_body_string
|
103
|
+
|
104
|
+
def get_header(header_key, response)
|
105
|
+
values = response.response_header[header_key.upcase.gsub('-', '_')]
|
106
|
+
values.is_a?(Array) ? values : values.split(', ')
|
107
|
+
end
|
108
|
+
|
109
|
+
def make_http_request(method, url, body = nil, headers = {})
|
110
|
+
http = nil
|
111
|
+
EventMachine.run do
|
112
|
+
http = EventMachine::HttpRequest.new(url).send(method, :body => body, :head => headers)
|
113
|
+
http.callback { EventMachine.stop }
|
114
|
+
end
|
115
|
+
http
|
116
|
+
end
|
117
|
+
|
118
|
+
def normalize_request_headers(headers)
|
119
|
+
headers
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
HTTP_LIBRARY_ADAPTERS['curb'] = Module.new do
|
124
|
+
def self.http_library_name; "Curb"; end
|
125
|
+
|
126
|
+
def get_body_string(response)
|
127
|
+
response.body_str
|
128
|
+
end
|
129
|
+
alias get_body_object get_body_string
|
130
|
+
|
131
|
+
def get_header(header_key, response)
|
132
|
+
headers = response.header_str.split("\r\n")[1..-1]
|
133
|
+
value = nil
|
134
|
+
headers.each do |h|
|
135
|
+
next unless h =~ /^#{Regexp.escape(header_key)}: (.*)$/
|
136
|
+
new_value = $1.split(', ')
|
137
|
+
value = value ? Array(value) + Array(new_value) : new_value
|
138
|
+
end
|
139
|
+
value
|
140
|
+
end
|
141
|
+
|
142
|
+
def make_http_request(method, url, body = nil, headers = {})
|
143
|
+
Curl::Easy.new(url) do |c|
|
144
|
+
c.headers = headers
|
145
|
+
|
146
|
+
if [:post, :put].include?(method)
|
147
|
+
c.send("http_#{method}", body)
|
148
|
+
else
|
149
|
+
c.send("http_#{method}")
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
def normalize_request_headers(headers)
|
155
|
+
headers
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
HTTP_LIBRARY_ADAPTERS['typhoeus'] = Module.new do
|
160
|
+
def self.http_library_name; "Typhoeus"; end
|
161
|
+
|
162
|
+
def get_body_string(response)
|
163
|
+
response.body
|
164
|
+
end
|
165
|
+
alias get_body_object get_body_string
|
166
|
+
|
167
|
+
def get_header(header_key, response)
|
168
|
+
# Due to https://github.com/typhoeus/typhoeus/commit/256c95473d5d40d7ec2f5db603687323ddd73689
|
169
|
+
# headers are now downcased.
|
170
|
+
# ...except when they're not. I'm not 100% why (I haven't had time to dig into it yet)
|
171
|
+
# but in some situations the headers aren't downcased. I think it has to do with playback; VCR
|
172
|
+
# isn't sending the headers in downcased to typhoeus. It gets complicated with the interaction
|
173
|
+
# w/ WebMock, and the fact that webmock normalizes headers in a different fashion.
|
174
|
+
#
|
175
|
+
# For now this hack works.
|
176
|
+
response.headers.fetch(header_key.downcase) { response.headers[header_key] }
|
177
|
+
end
|
178
|
+
|
179
|
+
def make_http_request(method, url, body = nil, headers = {})
|
180
|
+
request = Typhoeus::Request.new(url, :method => method, :body => body, :headers => headers)
|
181
|
+
request.run
|
182
|
+
request.response
|
183
|
+
end
|
184
|
+
|
185
|
+
def normalize_request_headers(headers)
|
186
|
+
headers.merge("User-Agent"=>["Typhoeus - https://github.com/typhoeus/typhoeus"])
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
HTTP_LIBRARY_ADAPTERS['typhoeus 0.4'] = Module.new do
|
191
|
+
def self.http_library_name; "Typhoeus"; end
|
192
|
+
|
193
|
+
def get_body_string(response)
|
194
|
+
response.body
|
195
|
+
end
|
196
|
+
alias get_body_object get_body_string
|
197
|
+
|
198
|
+
def get_header(header_key, response)
|
199
|
+
response.headers_hash[header_key]
|
200
|
+
end
|
201
|
+
|
202
|
+
def make_http_request(method, url, body = nil, headers = {})
|
203
|
+
Typhoeus::Request.send(method, url, :body => body, :headers => headers)
|
204
|
+
end
|
205
|
+
|
206
|
+
def normalize_request_headers(headers)
|
207
|
+
headers
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
HTTP_LIBRARY_ADAPTERS['excon'] = Module.new do
|
212
|
+
def self.http_library_name; "Excon"; end
|
213
|
+
|
214
|
+
def get_body_string(response)
|
215
|
+
response.body
|
216
|
+
end
|
217
|
+
alias get_body_object get_body_string
|
218
|
+
|
219
|
+
def get_header(header_key, response)
|
220
|
+
response.headers[header_key]
|
221
|
+
end
|
222
|
+
|
223
|
+
def make_http_request(method, url, body = nil, headers = {})
|
224
|
+
# There are multiple ways to use Excon but this is how fog (the main user of excon) uses it:
|
225
|
+
# https://github.com/fog/fog/blob/v1.1.1/lib/fog/aws/rds.rb#L139-147
|
226
|
+
Excon.new(url).request(:method => method.to_s.upcase, :body => body, :headers => headers)
|
227
|
+
end
|
228
|
+
|
229
|
+
def normalize_request_headers(headers)
|
230
|
+
headers.merge('User-Agent' => [Excon::USER_AGENT])
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
%w[ net_http typhoeus patron ].each do |_faraday_adapter|
|
235
|
+
if _faraday_adapter == 'typhoeus' &&
|
236
|
+
defined?(::Typhoeus::VERSION) &&
|
237
|
+
::Typhoeus::VERSION.to_f >= 0.5
|
238
|
+
require 'typhoeus/adapters/faraday'
|
239
|
+
end
|
240
|
+
|
241
|
+
HTTP_LIBRARY_ADAPTERS["faraday (w/ #{_faraday_adapter})"] = Module.new do
|
242
|
+
class << self; self; end.class_eval do
|
243
|
+
define_method(:http_library_name) do
|
244
|
+
"Faraday (#{_faraday_adapter})"
|
245
|
+
end
|
246
|
+
end
|
247
|
+
|
248
|
+
define_method(:faraday_adapter) { _faraday_adapter.to_sym }
|
249
|
+
|
250
|
+
def get_body_string(response)
|
251
|
+
response.body
|
252
|
+
end
|
253
|
+
alias get_body_object get_body_string
|
254
|
+
|
255
|
+
def get_header(header_key, response)
|
256
|
+
value = response.headers[header_key]
|
257
|
+
value.split(', ') if value
|
258
|
+
end
|
259
|
+
|
260
|
+
def make_http_request(method, url, body = nil, headers = {})
|
261
|
+
url_root, url_rest = split_url(url)
|
262
|
+
|
263
|
+
faraday_connection(url_root).send(method) do |req|
|
264
|
+
req.url url_rest
|
265
|
+
headers.each { |k, v| req[k] = v }
|
266
|
+
req.body = body if body
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
270
|
+
def split_url(url)
|
271
|
+
uri = URI.parse(url)
|
272
|
+
url_root = "#{uri.scheme}://#{uri.host}:#{uri.port}"
|
273
|
+
rest = url.sub(url_root, '')
|
274
|
+
|
275
|
+
[url_root, rest]
|
276
|
+
end
|
277
|
+
|
278
|
+
def faraday_connection(url_root)
|
279
|
+
Faraday::Connection.new(:url => url_root) do |builder|
|
280
|
+
builder.adapter faraday_adapter
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
284
|
+
def normalize_request_headers(headers)
|
285
|
+
headers.merge("User-Agent" => ["Faraday v#{Faraday::VERSION}"])
|
286
|
+
end
|
287
|
+
end
|
288
|
+
end
|
289
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class LimitedURI
|
2
|
+
extend Forwardable
|
3
|
+
|
4
|
+
def_delegators :@uri, :scheme, :host, :port, :port=, :path, :query, :query=, :to_s
|
5
|
+
|
6
|
+
def initialize(uri)
|
7
|
+
@uri = uri
|
8
|
+
end
|
9
|
+
|
10
|
+
def ==(other)
|
11
|
+
to_s == other.to_s
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.parse(uri)
|
15
|
+
return uri if uri.is_a? LimitedURI
|
16
|
+
return new(uri) if uri.is_a? URI
|
17
|
+
return new(URI.parse(uri)) if uri.is_a? String
|
18
|
+
|
19
|
+
raise URI::InvalidURIError
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
shared_examples "Excon streaming" do
|
2
|
+
context "when Excon's streaming API is used" do
|
3
|
+
def make_request_to(path)
|
4
|
+
chunks = []
|
5
|
+
|
6
|
+
Excon.get "http://localhost:#{VCR::SinatraApp.port}#{path}", :response_block => lambda { |chunk, remaining_bytes, total_bytes|
|
7
|
+
chunks << chunk
|
8
|
+
}
|
9
|
+
|
10
|
+
chunks.join
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'properly records and plays back the response' do
|
14
|
+
allow(VCR).to receive(:real_http_connections_allowed?).and_return(true)
|
15
|
+
recorded, played_back = [1, 2].map do
|
16
|
+
make_request_to('/foo')
|
17
|
+
end
|
18
|
+
|
19
|
+
expect(recorded).to eq(played_back)
|
20
|
+
expect(recorded).to eq("FOO!")
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'properly records and plays back the response for unexpected status' do
|
24
|
+
allow(VCR).to receive(:real_http_connections_allowed?).and_return(true)
|
25
|
+
recorded, played_back = [1, 2].map do
|
26
|
+
chunks = []
|
27
|
+
|
28
|
+
VCR.use_cassette('excon_streaming_error', :record => :once) do
|
29
|
+
begin
|
30
|
+
Excon.get "http://localhost:#{VCR::SinatraApp.port}/404_not_200", :expects => 200, :response_block => lambda { |chunk, remaining_bytes, total_bytes|
|
31
|
+
chunks << chunk
|
32
|
+
}
|
33
|
+
rescue Excon::Errors::Error => e
|
34
|
+
chunks << e.response.body
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
chunks.join
|
39
|
+
end
|
40
|
+
|
41
|
+
expect(recorded).to eq(played_back)
|
42
|
+
expect(recorded).to eq('404 not 200')
|
43
|
+
end
|
44
|
+
|
45
|
+
context "when a cassette is played back and appended to" do
|
46
|
+
it 'does not allow Excon to mutate the response body in the cassette' do
|
47
|
+
VCR.use_cassette('excon_streaming', :record => :new_episodes) do
|
48
|
+
expect(make_request_to('/')).to eq('GET to root')
|
49
|
+
end
|
50
|
+
|
51
|
+
VCR.use_cassette('excon_streaming', :record => :new_episodes) do
|
52
|
+
expect(make_request_to('/')).to eq('GET to root')
|
53
|
+
expect(make_request_to('/foo')).to eq('FOO!') # so it will save to disk again
|
54
|
+
end
|
55
|
+
|
56
|
+
VCR.use_cassette('excon_streaming', :record => :new_episodes) do
|
57
|
+
expect(make_request_to('/')).to eq('GET to root')
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|