vcr 1.11.3 → 2.0.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/.travis.yml +6 -2
- data/CHANGELOG.md +49 -1
- data/Gemfile +1 -5
- data/Guardfile +0 -5
- data/README.md +3 -2
- data/Rakefile +11 -16
- data/cucumber.yml +0 -4
- data/features/.nav +14 -2
- data/features/cassettes/automatic_re_recording.feature +4 -6
- data/features/cassettes/dynamic_erb.feature +6 -8
- data/features/cassettes/exclusive.feature +111 -0
- data/features/cassettes/format.feature +16 -14
- data/features/cassettes/naming.feature +4 -6
- data/features/cassettes/no_cassette.feature +25 -28
- data/features/cassettes/update_content_length_header.feature +9 -9
- data/features/configuration/allow_http_connections_when_no_cassette.feature +6 -8
- data/features/configuration/cassette_library_dir.feature +4 -6
- data/features/configuration/default_cassette_options.feature +12 -10
- data/features/configuration/filter_sensitive_data.feature +12 -17
- data/features/configuration/{stub_with.feature → hook_into.feature} +63 -62
- data/features/configuration/hooks.feature +23 -33
- data/features/configuration/ignore_hosts.feature +16 -17
- data/features/configuration/ignore_localhost.feature +33 -42
- data/features/http_libraries/em_http_request.feature +7 -8
- data/features/http_libraries/net_http.feature +26 -28
- data/features/middleware/faraday.feature +17 -56
- data/features/middleware/rack.feature +8 -11
- data/features/record_modes/all.feature +5 -7
- data/features/record_modes/new_episodes.feature +5 -7
- data/features/record_modes/none.feature +6 -6
- data/features/record_modes/once.feature +6 -8
- data/features/request_matching/README.md +28 -0
- data/features/request_matching/body.feature +81 -0
- data/features/request_matching/custom_matcher.feature +125 -0
- data/features/request_matching/headers.feature +85 -0
- data/features/request_matching/host.feature +85 -0
- data/features/request_matching/identical_request_sequence.feature +79 -0
- data/features/request_matching/method.feature +86 -0
- data/features/request_matching/path.feature +86 -0
- data/features/request_matching/playback_repeats.feature +87 -0
- data/features/request_matching/uri.feature +84 -0
- data/features/request_matching/uri_without_param.feature +85 -0
- data/features/step_definitions/cli_steps.rb +4 -28
- data/features/support/env.rb +11 -9
- data/features/support/http_lib_filters.rb +2 -11
- data/features/support/vcr_cucumber_helpers.rb +4 -5
- data/features/test_frameworks/cucumber.feature +17 -18
- data/features/test_frameworks/rspec.feature +8 -12
- data/features/test_frameworks/shoulda.feature +5 -8
- data/features/test_frameworks/test_unit.feature +5 -8
- data/lib/vcr.rb +38 -58
- data/lib/vcr/cassette.rb +41 -60
- data/lib/vcr/cassette/http_interaction_list.rb +56 -0
- data/lib/vcr/cassette/reader.rb +29 -31
- data/lib/vcr/configuration.rb +76 -0
- data/lib/vcr/deprecations.rb +39 -0
- data/lib/vcr/errors.rb +22 -0
- data/lib/vcr/library_hooks.rb +19 -0
- data/lib/vcr/library_hooks/excon.rb +136 -0
- data/lib/vcr/library_hooks/fakeweb.rb +110 -0
- data/lib/vcr/library_hooks/faraday.rb +3 -0
- data/lib/vcr/library_hooks/typhoeus.rb +98 -0
- data/lib/vcr/library_hooks/webmock.rb +100 -0
- data/lib/vcr/middleware/faraday.rb +43 -36
- data/lib/vcr/middleware/rack.rb +28 -4
- data/lib/vcr/request_handler.rb +43 -0
- data/lib/vcr/request_ignorer.rb +31 -0
- data/lib/vcr/request_matcher_registry.rb +86 -0
- data/lib/vcr/structs/http_interaction.rb +24 -18
- data/lib/vcr/structs/normalizers/body.rb +1 -1
- data/lib/vcr/structs/normalizers/header.rb +1 -1
- data/lib/vcr/structs/normalizers/status_message.rb +1 -1
- data/lib/vcr/structs/normalizers/uri.rb +1 -1
- data/lib/vcr/structs/request.rb +0 -13
- data/lib/vcr/structs/response.rb +2 -9
- data/lib/vcr/structs/response_status.rb +0 -4
- data/lib/vcr/test_frameworks/cucumber.rb +1 -1
- data/lib/vcr/test_frameworks/rspec.rb +1 -1
- data/lib/vcr/util/hooks.rb +28 -19
- data/lib/vcr/util/internet_connection.rb +29 -2
- data/lib/vcr/util/version_checker.rb +60 -0
- data/lib/vcr/version.rb +1 -1
- data/script/FullBuildRakeFile +0 -7
- data/script/full_build +1 -1
- data/spec/capture_warnings.rb +36 -31
- data/spec/fixtures/{1.9.1/cassette_spec → cassette_spec}/empty.yml +0 -0
- data/spec/fixtures/{not_1.9.1/cassette_spec → cassette_spec}/example.yml +0 -0
- data/spec/fixtures/{not_1.9.1/cassette_spec → cassette_spec}/with_localhost_requests.yml +0 -0
- data/spec/fixtures/{not_1.9.1/fake_example.com_responses.yml → fake_example.com_responses.yml} +0 -0
- data/spec/fixtures/{not_1.9.1/match_requests_on.yml → match_requests_on.yml} +0 -0
- data/spec/monkey_patches.rb +40 -11
- data/spec/spec_helper.rb +7 -43
- data/spec/support/http_library_adapters.rb +3 -13
- data/spec/support/shared_example_groups/{http_library.rb → hook_into_http_library.rb} +39 -111
- data/spec/support/shared_example_groups/version_checking.rb +9 -9
- data/spec/vcr/cassette/http_interaction_list_spec.rb +178 -0
- data/spec/vcr/cassette/reader_spec.rb +2 -2
- data/spec/vcr/cassette_spec.rb +121 -156
- data/spec/vcr/configuration_spec.rb +143 -0
- data/spec/vcr/deprecations_spec.rb +91 -0
- data/spec/vcr/{http_stubbing_adapters → library_hooks}/excon_spec.rb +6 -9
- data/spec/vcr/library_hooks/fakeweb_spec.rb +83 -0
- data/spec/vcr/{http_stubbing_adapters → library_hooks}/typhoeus_spec.rb +7 -11
- data/spec/vcr/library_hooks/webmock_spec.rb +17 -0
- data/spec/vcr/library_hooks_spec.rb +51 -0
- data/spec/vcr/middleware/faraday_spec.rb +17 -44
- data/spec/vcr/middleware/rack_spec.rb +94 -58
- data/spec/vcr/request_ignorer_spec.rb +54 -0
- data/spec/vcr/request_matcher_registry_spec.rb +223 -0
- data/spec/vcr/structs/request_spec.rb +0 -33
- data/spec/vcr/structs/response_spec.rb +0 -24
- data/spec/vcr/structs/response_status_spec.rb +0 -9
- data/spec/vcr/util/hooks_spec.rb +3 -5
- data/spec/vcr/version_spec.rb +1 -1
- data/spec/vcr_spec.rb +79 -91
- data/vcr.gemspec +1 -1
- metadata +83 -103
- data/features/cassettes/request_matching.feature +0 -383
- data/lib/vcr/config.rb +0 -84
- data/lib/vcr/deprecations/cassette.rb +0 -29
- data/lib/vcr/deprecations/config.rb +0 -18
- data/lib/vcr/deprecations/http_stubbing_adapters/common.rb +0 -9
- data/lib/vcr/deprecations/http_stubbing_adapters/fakeweb.rb +0 -11
- data/lib/vcr/extensions/net_http.rb +0 -32
- data/lib/vcr/http_stubbing_adapters/common.rb +0 -202
- data/lib/vcr/http_stubbing_adapters/excon.rb +0 -178
- data/lib/vcr/http_stubbing_adapters/fakeweb.rb +0 -107
- data/lib/vcr/http_stubbing_adapters/faraday.rb +0 -26
- data/lib/vcr/http_stubbing_adapters/multi_object_proxy.rb +0 -43
- data/lib/vcr/http_stubbing_adapters/typhoeus.rb +0 -115
- data/lib/vcr/http_stubbing_adapters/webmock.rb +0 -120
- data/lib/vcr/middleware/cassette_arguments.rb +0 -19
- data/lib/vcr/middleware/common.rb +0 -20
- data/lib/vcr/request_matcher.rb +0 -94
- data/lib/vcr/rspec.rb +0 -2
- data/lib/vcr/util/basic_object.rb +0 -43
- data/lib/vcr/util/ping.rb +0 -30
- data/lib/vcr/util/regexes.rb +0 -37
- data/spec/fixtures/1.9.1/0_3_1_cassette.yml +0 -29
- data/spec/fixtures/1.9.1/cassette_spec/example.yml +0 -110
- data/spec/fixtures/1.9.1/cassette_spec/with_localhost_requests.yml +0 -109
- data/spec/fixtures/1.9.1/example_net_http.yml +0 -14
- data/spec/fixtures/1.9.1/example_net_http_request.yml +0 -12
- data/spec/fixtures/1.9.1/example_net_http_response.yml +0 -25
- data/spec/fixtures/1.9.1/fake_example.com_responses.yml +0 -108
- data/spec/fixtures/1.9.1/match_requests_on.yml +0 -185
- data/spec/fixtures/not_1.9.1/0_3_1_cassette.yml +0 -29
- data/spec/fixtures/not_1.9.1/cassette_spec/empty.yml +0 -0
- data/spec/fixtures/not_1.9.1/example_net_http.yml +0 -14
- data/spec/fixtures/not_1.9.1/example_net_http_request.yml +0 -12
- data/spec/fixtures/not_1.9.1/example_net_http_response.yml +0 -25
- data/spec/support/shared_example_groups/http_stubbing_adapter.rb +0 -133
- data/spec/support/shared_example_groups/ignore_localhost_deprecation.rb +0 -28
- data/spec/vcr/config_spec.rb +0 -181
- data/spec/vcr/deprecations/cassette_spec.rb +0 -57
- data/spec/vcr/deprecations/config_spec.rb +0 -30
- data/spec/vcr/deprecations/http_stubbing_adapters/common_spec.rb +0 -7
- data/spec/vcr/deprecations/http_stubbing_adapters/fakeweb_spec.rb +0 -16
- data/spec/vcr/extensions/net_http_spec.rb +0 -80
- data/spec/vcr/http_stubbing_adapters/fakeweb_spec.rb +0 -19
- data/spec/vcr/http_stubbing_adapters/faraday_spec.rb +0 -76
- data/spec/vcr/http_stubbing_adapters/multi_object_proxy_spec.rb +0 -101
- data/spec/vcr/http_stubbing_adapters/webmock_spec.rb +0 -17
- data/spec/vcr/middleware/cassette_arguments_spec.rb +0 -32
- data/spec/vcr/request_matcher_spec.rb +0 -230
data/lib/vcr/cassette/reader.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
module VCR
|
2
2
|
class Cassette
|
3
|
-
class MissingERBVariableError < NameError; end
|
4
|
-
|
5
3
|
class Reader
|
6
4
|
def initialize(file_name, erb)
|
7
5
|
@file_name, @erb = file_name, erb
|
@@ -15,44 +13,44 @@ module VCR
|
|
15
13
|
handle_name_error(e)
|
16
14
|
end
|
17
15
|
|
18
|
-
|
16
|
+
private
|
19
17
|
|
20
|
-
|
21
|
-
|
18
|
+
def handle_name_error(e)
|
19
|
+
example_hash = (erb_variables || {}).merge(e.name => 'some value')
|
22
20
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
21
|
+
raise Errors::MissingERBVariableError.new(
|
22
|
+
"The ERB in the #{@file_name} cassette file references undefined variable #{e.name}. " +
|
23
|
+
"Pass it to the cassette using :erb => #{ example_hash.inspect }."
|
24
|
+
)
|
25
|
+
end
|
28
26
|
|
29
|
-
|
30
|
-
|
31
|
-
|
27
|
+
def use_erb?
|
28
|
+
!!@erb
|
29
|
+
end
|
32
30
|
|
33
|
-
|
34
|
-
|
35
|
-
|
31
|
+
def erb_variables
|
32
|
+
@erb if @erb.is_a?(Hash)
|
33
|
+
end
|
36
34
|
|
37
|
-
|
38
|
-
|
39
|
-
|
35
|
+
def file_content
|
36
|
+
@file_content ||= File.read(@file_name)
|
37
|
+
end
|
40
38
|
|
41
|
-
|
42
|
-
|
43
|
-
|
39
|
+
def template
|
40
|
+
@template ||= ERB.new(file_content)
|
41
|
+
end
|
44
42
|
|
45
|
-
|
46
|
-
|
47
|
-
|
43
|
+
@@struct_cache = Hash.new do |hash, attributes|
|
44
|
+
hash[attributes] = Struct.new(*attributes)
|
45
|
+
end
|
48
46
|
|
49
|
-
|
50
|
-
|
51
|
-
|
47
|
+
def variables_object
|
48
|
+
@variables_object ||= @@struct_cache[erb_variables.keys].new(*erb_variables.values)
|
49
|
+
end
|
52
50
|
|
53
|
-
|
54
|
-
|
55
|
-
|
51
|
+
def binding_for_variables
|
52
|
+
@binding_for_variables ||= variables_object.instance_eval { binding }
|
53
|
+
end
|
56
54
|
end
|
57
55
|
end
|
58
56
|
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'vcr/util/hooks'
|
3
|
+
|
4
|
+
module VCR
|
5
|
+
class Configuration
|
6
|
+
include VCR::Hooks
|
7
|
+
include VCR::VariableArgsBlockCaller
|
8
|
+
|
9
|
+
define_hook :before_record
|
10
|
+
define_hook :before_playback
|
11
|
+
define_hook :after_library_hooks_loaded
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
@allow_http_connections_when_no_cassette = nil
|
15
|
+
@default_cassette_options = {
|
16
|
+
:record => :once,
|
17
|
+
:match_requests_on => RequestMatcherRegistry::DEFAULT_MATCHERS
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
attr_reader :cassette_library_dir
|
22
|
+
def cassette_library_dir=(cassette_library_dir)
|
23
|
+
@cassette_library_dir = cassette_library_dir
|
24
|
+
FileUtils.mkdir_p(cassette_library_dir) if cassette_library_dir
|
25
|
+
end
|
26
|
+
|
27
|
+
attr_reader :default_cassette_options
|
28
|
+
def default_cassette_options=(overrides)
|
29
|
+
@default_cassette_options.merge!(overrides)
|
30
|
+
end
|
31
|
+
|
32
|
+
def hook_into(*hooks)
|
33
|
+
hooks.each { |a| load_library_hook(a) }
|
34
|
+
invoke_hook(:after_library_hooks_loaded)
|
35
|
+
end
|
36
|
+
|
37
|
+
def register_request_matcher(name, &block)
|
38
|
+
VCR.request_matchers.register(name, &block)
|
39
|
+
end
|
40
|
+
|
41
|
+
def ignore_hosts(*hosts)
|
42
|
+
VCR.request_ignorer.ignore_hosts(*hosts)
|
43
|
+
end
|
44
|
+
alias ignore_host ignore_hosts
|
45
|
+
|
46
|
+
def ignore_localhost=(value)
|
47
|
+
VCR.request_ignorer.ignore_localhost = value
|
48
|
+
end
|
49
|
+
|
50
|
+
attr_writer :allow_http_connections_when_no_cassette
|
51
|
+
def allow_http_connections_when_no_cassette?
|
52
|
+
!!@allow_http_connections_when_no_cassette
|
53
|
+
end
|
54
|
+
|
55
|
+
def filter_sensitive_data(placeholder, tag = nil, &block)
|
56
|
+
before_record(tag) do |interaction|
|
57
|
+
interaction.filter!(call_block(block, interaction), placeholder)
|
58
|
+
end
|
59
|
+
|
60
|
+
before_playback(tag) do |interaction|
|
61
|
+
interaction.filter!(placeholder, call_block(block, interaction))
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def load_library_hook(hook)
|
68
|
+
file = "vcr/library_hooks/#{hook}"
|
69
|
+
require file
|
70
|
+
rescue LoadError => e
|
71
|
+
raise e unless e.message.include?(file) # in case FakeWeb/WebMock/etc itself is not available
|
72
|
+
raise ArgumentError.new("#{hook.inspect} is not a supported VCR HTTP library hook.")
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module VCR
|
2
|
+
def config
|
3
|
+
warn "WARNING: `VCR.config` is deprecated. Use VCR.configure instead."
|
4
|
+
configure { |c| yield c }
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.const_missing(const)
|
8
|
+
return super unless const == :Config
|
9
|
+
warn "WARNING: `VCR::Config` is deprecated. Use VCR.configuration instead."
|
10
|
+
configuration
|
11
|
+
end
|
12
|
+
|
13
|
+
def Cassette.const_missing(const)
|
14
|
+
return super unless const == :MissingERBVariableError
|
15
|
+
warn "WARNING: `VCR::Cassette::MissingERBVariableError` is deprecated. Use `VCR::Errors::MissingERBVariableError` instead."
|
16
|
+
Errors::MissingERBVariableError
|
17
|
+
end
|
18
|
+
|
19
|
+
class Configuration
|
20
|
+
def stub_with(*adapters)
|
21
|
+
warn "WARNING: `VCR.config { |c| c.stub_with ... }` is deprecated. Use `VCR.configure { |c| c.hook_into ... }` instead."
|
22
|
+
hook_into(*adapters)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
module Deprecations
|
27
|
+
module Middleware
|
28
|
+
module Faraday
|
29
|
+
def initialize(*args)
|
30
|
+
if block_given?
|
31
|
+
Kernel.warn "WARNING: Passing a block to `VCR::Middleware::Faraday` is deprecated. \n" +
|
32
|
+
"As of VCR 2.0, you need to wrap faraday requests in VCR.use_cassette, just like with any other library hook."
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
data/lib/vcr/errors.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
module VCR
|
2
|
+
module Errors
|
3
|
+
class Error < StandardError; end
|
4
|
+
class CassetteInUseError < Error; end
|
5
|
+
class TurnedOffError < Error; end
|
6
|
+
class MissingERBVariableError < Error; end
|
7
|
+
class LibraryVersionTooLowError < Error; end
|
8
|
+
class UnregisteredMatcherError < Error; end
|
9
|
+
|
10
|
+
|
11
|
+
class HTTPConnectionNotAllowedError < Error
|
12
|
+
def initialize(request)
|
13
|
+
super \
|
14
|
+
"Real HTTP connections are disabled. " +
|
15
|
+
"Request: #{request.method.to_s.upcase} #{request.uri}. " +
|
16
|
+
"You can use VCR to automatically record this request and replay it later. " +
|
17
|
+
"For more details, visit the VCR documentation at: http://relishapp.com/myronmarston/vcr/v/#{VCR.version.gsub('.', '-')}"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module VCR
|
2
|
+
class LibraryHooks
|
3
|
+
def initialize
|
4
|
+
@exclusive_hook = nil
|
5
|
+
end
|
6
|
+
|
7
|
+
def disabled?(hook)
|
8
|
+
![nil, hook].include?(@exclusive_hook)
|
9
|
+
end
|
10
|
+
|
11
|
+
def exclusively_enabled(hook)
|
12
|
+
@exclusive_hook = hook
|
13
|
+
yield
|
14
|
+
ensure
|
15
|
+
@exclusive_hook = nil
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
@@ -0,0 +1,136 @@
|
|
1
|
+
require 'vcr/util/version_checker'
|
2
|
+
require 'vcr/request_handler'
|
3
|
+
require 'excon'
|
4
|
+
|
5
|
+
VCR::VersionChecker.new('Excon', Excon::VERSION, '0.6.5', '0.6').check_version!
|
6
|
+
|
7
|
+
module VCR
|
8
|
+
class LibraryHooks
|
9
|
+
module Excon
|
10
|
+
class RequestHandler < ::VCR::RequestHandler
|
11
|
+
attr_reader :params
|
12
|
+
def initialize(params)
|
13
|
+
@params = params
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def on_stubbed_request
|
19
|
+
{
|
20
|
+
:body => stubbed_response.body,
|
21
|
+
:headers => normalized_headers(stubbed_response.headers || {}),
|
22
|
+
:status => stubbed_response.status.code
|
23
|
+
}
|
24
|
+
end
|
25
|
+
|
26
|
+
def response_from_excon_error(error)
|
27
|
+
if error.respond_to?(:response)
|
28
|
+
error.response
|
29
|
+
elsif error.respond_to?(:socket_error)
|
30
|
+
response_from_excon_error(error.socket_error)
|
31
|
+
else
|
32
|
+
warn "WARNING: VCR could not extract a response from Excon error (#{error.inspect})"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def perform_real_request
|
37
|
+
connection = ::Excon.new(uri)
|
38
|
+
|
39
|
+
response = begin
|
40
|
+
connection.request(params.merge(:mock => false))
|
41
|
+
rescue ::Excon::Errors::Error => e
|
42
|
+
yield response_from_excon_error(e) if block_given?
|
43
|
+
raise e
|
44
|
+
end
|
45
|
+
|
46
|
+
yield response if block_given?
|
47
|
+
|
48
|
+
response.attributes
|
49
|
+
end
|
50
|
+
alias on_ignored_request perform_real_request
|
51
|
+
|
52
|
+
def on_recordable_request
|
53
|
+
perform_real_request do |response|
|
54
|
+
http_interaction = http_interaction_for(response)
|
55
|
+
VCR.record_http_interaction(http_interaction)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def uri
|
60
|
+
@uri ||= "#{params[:scheme]}://#{params[:host]}:#{params[:port]}#{params[:path]}#{query}"
|
61
|
+
end
|
62
|
+
|
63
|
+
def query
|
64
|
+
@query ||= case params[:query]
|
65
|
+
when String
|
66
|
+
"?#{params[:query]}"
|
67
|
+
when Hash
|
68
|
+
qry = '?'
|
69
|
+
for key, values in params[:query]
|
70
|
+
if values.nil?
|
71
|
+
qry << key.to_s << '&'
|
72
|
+
else
|
73
|
+
for value in [*values]
|
74
|
+
qry << key.to_s << '=' << CGI.escape(value.to_s) << '&'
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
qry.chop! # remove trailing '&'
|
79
|
+
else
|
80
|
+
''
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def http_interaction_for(response)
|
85
|
+
VCR::HTTPInteraction.new \
|
86
|
+
vcr_request,
|
87
|
+
vcr_response(response)
|
88
|
+
end
|
89
|
+
|
90
|
+
def vcr_request
|
91
|
+
@vcr_request ||= begin
|
92
|
+
headers = params[:headers].dup
|
93
|
+
headers.delete("Host")
|
94
|
+
|
95
|
+
VCR::Request.new \
|
96
|
+
params[:method],
|
97
|
+
uri,
|
98
|
+
params[:body],
|
99
|
+
headers
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def vcr_response(response)
|
104
|
+
VCR::Response.new \
|
105
|
+
VCR::ResponseStatus.new(response.status, nil),
|
106
|
+
response.headers,
|
107
|
+
response.body,
|
108
|
+
nil
|
109
|
+
end
|
110
|
+
|
111
|
+
def normalized_headers(headers)
|
112
|
+
normalized = {}
|
113
|
+
headers.each do |k, v|
|
114
|
+
v = v.join(', ') if v.respond_to?(:join)
|
115
|
+
normalized[normalize_header_key(k)] = v
|
116
|
+
end
|
117
|
+
normalized
|
118
|
+
end
|
119
|
+
|
120
|
+
def normalize_header_key(key)
|
121
|
+
key.split('-'). # 'user-agent' => %w(user agent)
|
122
|
+
each { |w| w.capitalize! }. # => %w(User Agent)
|
123
|
+
join('-')
|
124
|
+
end
|
125
|
+
|
126
|
+
::Excon.stub({}) do |params|
|
127
|
+
self.new(params).handle
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
Excon.mock = true
|
136
|
+
|
@@ -0,0 +1,110 @@
|
|
1
|
+
require 'vcr/util/version_checker'
|
2
|
+
require 'fakeweb'
|
3
|
+
require 'net/http'
|
4
|
+
require 'vcr/extensions/net_http_response'
|
5
|
+
require 'vcr/request_handler'
|
6
|
+
|
7
|
+
VCR::VersionChecker.new('FakeWeb', FakeWeb::VERSION, '1.3.0', '1.3').check_version!
|
8
|
+
|
9
|
+
module VCR
|
10
|
+
class LibraryHooks
|
11
|
+
module FakeWeb
|
12
|
+
class RequestHandler < ::VCR::RequestHandler
|
13
|
+
attr_reader :net_http, :request, :request_body, :block
|
14
|
+
def initialize(net_http, request, request_body = nil, &block)
|
15
|
+
@net_http, @request, @request_body, @block =
|
16
|
+
net_http, request, request_body, block
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def perform_and_record_request
|
22
|
+
# Net::HTTP calls #request recursively in certain circumstances.
|
23
|
+
# We only want to record hte request when the request is started, as
|
24
|
+
# that is the final time through #request.
|
25
|
+
return perform_request unless net_http.started?
|
26
|
+
|
27
|
+
perform_request do |response|
|
28
|
+
VCR.record_http_interaction VCR::HTTPInteraction.new(vcr_request, vcr_response_from(response))
|
29
|
+
response.extend VCR::Net::HTTPResponse # "unwind" the response
|
30
|
+
block.call(response) if block
|
31
|
+
end
|
32
|
+
end
|
33
|
+
alias on_recordable_request perform_and_record_request
|
34
|
+
|
35
|
+
def perform_stubbed_request
|
36
|
+
with_exclusive_fakeweb_stub(stubbed_response) do
|
37
|
+
perform_request
|
38
|
+
end
|
39
|
+
end
|
40
|
+
alias on_stubbed_request perform_stubbed_request
|
41
|
+
|
42
|
+
def perform_request(&record_block)
|
43
|
+
net_http.request_without_vcr(request, request_body, &(record_block || block))
|
44
|
+
end
|
45
|
+
alias on_ignored_request perform_request
|
46
|
+
|
47
|
+
def uri
|
48
|
+
@uri ||= ::FakeWeb::Utility.request_uri_as_string(net_http, request)
|
49
|
+
end
|
50
|
+
|
51
|
+
def response_hash(response)
|
52
|
+
(response.headers || {}).merge(
|
53
|
+
:body => response.body,
|
54
|
+
:status => [response.status.code.to_s, response.status.message]
|
55
|
+
)
|
56
|
+
end
|
57
|
+
|
58
|
+
def with_exclusive_fakeweb_stub(response)
|
59
|
+
original_map = ::FakeWeb::Registry.instance.uri_map.dup
|
60
|
+
::FakeWeb.clean_registry
|
61
|
+
::FakeWeb.register_uri(:any, /.*/, response_hash(response))
|
62
|
+
|
63
|
+
begin
|
64
|
+
return yield
|
65
|
+
ensure
|
66
|
+
::FakeWeb::Registry.instance.uri_map = original_map
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def vcr_request
|
71
|
+
@vcr_request ||= VCR::Request.new \
|
72
|
+
request.method.downcase.to_sym,
|
73
|
+
uri,
|
74
|
+
request_body,
|
75
|
+
request.to_hash
|
76
|
+
end
|
77
|
+
|
78
|
+
def vcr_response_from(response)
|
79
|
+
VCR::Response.new \
|
80
|
+
VCR::ResponseStatus.new(response.code.to_i, response.message),
|
81
|
+
response.to_hash,
|
82
|
+
response.body,
|
83
|
+
response.http_version
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
module Net
|
91
|
+
class HTTP
|
92
|
+
unless method_defined?(:request_with_vcr)
|
93
|
+
def request_with_vcr(*args, &block)
|
94
|
+
VCR::LibraryHooks::FakeWeb::RequestHandler.new(
|
95
|
+
self, *args, &block
|
96
|
+
).handle
|
97
|
+
end
|
98
|
+
|
99
|
+
alias request_without_vcr request
|
100
|
+
alias request request_with_vcr
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
VCR.configuration.after_library_hooks_loaded do
|
106
|
+
if defined?(WebMock)
|
107
|
+
raise ArgumentError.new("You have configured VCR to hook into both :fakeweb and :webmock. You cannot use both.")
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|