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.
Files changed (166) hide show
  1. data/.gitignore +2 -0
  2. data/.travis.yml +6 -2
  3. data/CHANGELOG.md +49 -1
  4. data/Gemfile +1 -5
  5. data/Guardfile +0 -5
  6. data/README.md +3 -2
  7. data/Rakefile +11 -16
  8. data/cucumber.yml +0 -4
  9. data/features/.nav +14 -2
  10. data/features/cassettes/automatic_re_recording.feature +4 -6
  11. data/features/cassettes/dynamic_erb.feature +6 -8
  12. data/features/cassettes/exclusive.feature +111 -0
  13. data/features/cassettes/format.feature +16 -14
  14. data/features/cassettes/naming.feature +4 -6
  15. data/features/cassettes/no_cassette.feature +25 -28
  16. data/features/cassettes/update_content_length_header.feature +9 -9
  17. data/features/configuration/allow_http_connections_when_no_cassette.feature +6 -8
  18. data/features/configuration/cassette_library_dir.feature +4 -6
  19. data/features/configuration/default_cassette_options.feature +12 -10
  20. data/features/configuration/filter_sensitive_data.feature +12 -17
  21. data/features/configuration/{stub_with.feature → hook_into.feature} +63 -62
  22. data/features/configuration/hooks.feature +23 -33
  23. data/features/configuration/ignore_hosts.feature +16 -17
  24. data/features/configuration/ignore_localhost.feature +33 -42
  25. data/features/http_libraries/em_http_request.feature +7 -8
  26. data/features/http_libraries/net_http.feature +26 -28
  27. data/features/middleware/faraday.feature +17 -56
  28. data/features/middleware/rack.feature +8 -11
  29. data/features/record_modes/all.feature +5 -7
  30. data/features/record_modes/new_episodes.feature +5 -7
  31. data/features/record_modes/none.feature +6 -6
  32. data/features/record_modes/once.feature +6 -8
  33. data/features/request_matching/README.md +28 -0
  34. data/features/request_matching/body.feature +81 -0
  35. data/features/request_matching/custom_matcher.feature +125 -0
  36. data/features/request_matching/headers.feature +85 -0
  37. data/features/request_matching/host.feature +85 -0
  38. data/features/request_matching/identical_request_sequence.feature +79 -0
  39. data/features/request_matching/method.feature +86 -0
  40. data/features/request_matching/path.feature +86 -0
  41. data/features/request_matching/playback_repeats.feature +87 -0
  42. data/features/request_matching/uri.feature +84 -0
  43. data/features/request_matching/uri_without_param.feature +85 -0
  44. data/features/step_definitions/cli_steps.rb +4 -28
  45. data/features/support/env.rb +11 -9
  46. data/features/support/http_lib_filters.rb +2 -11
  47. data/features/support/vcr_cucumber_helpers.rb +4 -5
  48. data/features/test_frameworks/cucumber.feature +17 -18
  49. data/features/test_frameworks/rspec.feature +8 -12
  50. data/features/test_frameworks/shoulda.feature +5 -8
  51. data/features/test_frameworks/test_unit.feature +5 -8
  52. data/lib/vcr.rb +38 -58
  53. data/lib/vcr/cassette.rb +41 -60
  54. data/lib/vcr/cassette/http_interaction_list.rb +56 -0
  55. data/lib/vcr/cassette/reader.rb +29 -31
  56. data/lib/vcr/configuration.rb +76 -0
  57. data/lib/vcr/deprecations.rb +39 -0
  58. data/lib/vcr/errors.rb +22 -0
  59. data/lib/vcr/library_hooks.rb +19 -0
  60. data/lib/vcr/library_hooks/excon.rb +136 -0
  61. data/lib/vcr/library_hooks/fakeweb.rb +110 -0
  62. data/lib/vcr/library_hooks/faraday.rb +3 -0
  63. data/lib/vcr/library_hooks/typhoeus.rb +98 -0
  64. data/lib/vcr/library_hooks/webmock.rb +100 -0
  65. data/lib/vcr/middleware/faraday.rb +43 -36
  66. data/lib/vcr/middleware/rack.rb +28 -4
  67. data/lib/vcr/request_handler.rb +43 -0
  68. data/lib/vcr/request_ignorer.rb +31 -0
  69. data/lib/vcr/request_matcher_registry.rb +86 -0
  70. data/lib/vcr/structs/http_interaction.rb +24 -18
  71. data/lib/vcr/structs/normalizers/body.rb +1 -1
  72. data/lib/vcr/structs/normalizers/header.rb +1 -1
  73. data/lib/vcr/structs/normalizers/status_message.rb +1 -1
  74. data/lib/vcr/structs/normalizers/uri.rb +1 -1
  75. data/lib/vcr/structs/request.rb +0 -13
  76. data/lib/vcr/structs/response.rb +2 -9
  77. data/lib/vcr/structs/response_status.rb +0 -4
  78. data/lib/vcr/test_frameworks/cucumber.rb +1 -1
  79. data/lib/vcr/test_frameworks/rspec.rb +1 -1
  80. data/lib/vcr/util/hooks.rb +28 -19
  81. data/lib/vcr/util/internet_connection.rb +29 -2
  82. data/lib/vcr/util/version_checker.rb +60 -0
  83. data/lib/vcr/version.rb +1 -1
  84. data/script/FullBuildRakeFile +0 -7
  85. data/script/full_build +1 -1
  86. data/spec/capture_warnings.rb +36 -31
  87. data/spec/fixtures/{1.9.1/cassette_spec → cassette_spec}/empty.yml +0 -0
  88. data/spec/fixtures/{not_1.9.1/cassette_spec → cassette_spec}/example.yml +0 -0
  89. data/spec/fixtures/{not_1.9.1/cassette_spec → cassette_spec}/with_localhost_requests.yml +0 -0
  90. data/spec/fixtures/{not_1.9.1/fake_example.com_responses.yml → fake_example.com_responses.yml} +0 -0
  91. data/spec/fixtures/{not_1.9.1/match_requests_on.yml → match_requests_on.yml} +0 -0
  92. data/spec/monkey_patches.rb +40 -11
  93. data/spec/spec_helper.rb +7 -43
  94. data/spec/support/http_library_adapters.rb +3 -13
  95. data/spec/support/shared_example_groups/{http_library.rb → hook_into_http_library.rb} +39 -111
  96. data/spec/support/shared_example_groups/version_checking.rb +9 -9
  97. data/spec/vcr/cassette/http_interaction_list_spec.rb +178 -0
  98. data/spec/vcr/cassette/reader_spec.rb +2 -2
  99. data/spec/vcr/cassette_spec.rb +121 -156
  100. data/spec/vcr/configuration_spec.rb +143 -0
  101. data/spec/vcr/deprecations_spec.rb +91 -0
  102. data/spec/vcr/{http_stubbing_adapters → library_hooks}/excon_spec.rb +6 -9
  103. data/spec/vcr/library_hooks/fakeweb_spec.rb +83 -0
  104. data/spec/vcr/{http_stubbing_adapters → library_hooks}/typhoeus_spec.rb +7 -11
  105. data/spec/vcr/library_hooks/webmock_spec.rb +17 -0
  106. data/spec/vcr/library_hooks_spec.rb +51 -0
  107. data/spec/vcr/middleware/faraday_spec.rb +17 -44
  108. data/spec/vcr/middleware/rack_spec.rb +94 -58
  109. data/spec/vcr/request_ignorer_spec.rb +54 -0
  110. data/spec/vcr/request_matcher_registry_spec.rb +223 -0
  111. data/spec/vcr/structs/request_spec.rb +0 -33
  112. data/spec/vcr/structs/response_spec.rb +0 -24
  113. data/spec/vcr/structs/response_status_spec.rb +0 -9
  114. data/spec/vcr/util/hooks_spec.rb +3 -5
  115. data/spec/vcr/version_spec.rb +1 -1
  116. data/spec/vcr_spec.rb +79 -91
  117. data/vcr.gemspec +1 -1
  118. metadata +83 -103
  119. data/features/cassettes/request_matching.feature +0 -383
  120. data/lib/vcr/config.rb +0 -84
  121. data/lib/vcr/deprecations/cassette.rb +0 -29
  122. data/lib/vcr/deprecations/config.rb +0 -18
  123. data/lib/vcr/deprecations/http_stubbing_adapters/common.rb +0 -9
  124. data/lib/vcr/deprecations/http_stubbing_adapters/fakeweb.rb +0 -11
  125. data/lib/vcr/extensions/net_http.rb +0 -32
  126. data/lib/vcr/http_stubbing_adapters/common.rb +0 -202
  127. data/lib/vcr/http_stubbing_adapters/excon.rb +0 -178
  128. data/lib/vcr/http_stubbing_adapters/fakeweb.rb +0 -107
  129. data/lib/vcr/http_stubbing_adapters/faraday.rb +0 -26
  130. data/lib/vcr/http_stubbing_adapters/multi_object_proxy.rb +0 -43
  131. data/lib/vcr/http_stubbing_adapters/typhoeus.rb +0 -115
  132. data/lib/vcr/http_stubbing_adapters/webmock.rb +0 -120
  133. data/lib/vcr/middleware/cassette_arguments.rb +0 -19
  134. data/lib/vcr/middleware/common.rb +0 -20
  135. data/lib/vcr/request_matcher.rb +0 -94
  136. data/lib/vcr/rspec.rb +0 -2
  137. data/lib/vcr/util/basic_object.rb +0 -43
  138. data/lib/vcr/util/ping.rb +0 -30
  139. data/lib/vcr/util/regexes.rb +0 -37
  140. data/spec/fixtures/1.9.1/0_3_1_cassette.yml +0 -29
  141. data/spec/fixtures/1.9.1/cassette_spec/example.yml +0 -110
  142. data/spec/fixtures/1.9.1/cassette_spec/with_localhost_requests.yml +0 -109
  143. data/spec/fixtures/1.9.1/example_net_http.yml +0 -14
  144. data/spec/fixtures/1.9.1/example_net_http_request.yml +0 -12
  145. data/spec/fixtures/1.9.1/example_net_http_response.yml +0 -25
  146. data/spec/fixtures/1.9.1/fake_example.com_responses.yml +0 -108
  147. data/spec/fixtures/1.9.1/match_requests_on.yml +0 -185
  148. data/spec/fixtures/not_1.9.1/0_3_1_cassette.yml +0 -29
  149. data/spec/fixtures/not_1.9.1/cassette_spec/empty.yml +0 -0
  150. data/spec/fixtures/not_1.9.1/example_net_http.yml +0 -14
  151. data/spec/fixtures/not_1.9.1/example_net_http_request.yml +0 -12
  152. data/spec/fixtures/not_1.9.1/example_net_http_response.yml +0 -25
  153. data/spec/support/shared_example_groups/http_stubbing_adapter.rb +0 -133
  154. data/spec/support/shared_example_groups/ignore_localhost_deprecation.rb +0 -28
  155. data/spec/vcr/config_spec.rb +0 -181
  156. data/spec/vcr/deprecations/cassette_spec.rb +0 -57
  157. data/spec/vcr/deprecations/config_spec.rb +0 -30
  158. data/spec/vcr/deprecations/http_stubbing_adapters/common_spec.rb +0 -7
  159. data/spec/vcr/deprecations/http_stubbing_adapters/fakeweb_spec.rb +0 -16
  160. data/spec/vcr/extensions/net_http_spec.rb +0 -80
  161. data/spec/vcr/http_stubbing_adapters/fakeweb_spec.rb +0 -19
  162. data/spec/vcr/http_stubbing_adapters/faraday_spec.rb +0 -76
  163. data/spec/vcr/http_stubbing_adapters/multi_object_proxy_spec.rb +0 -101
  164. data/spec/vcr/http_stubbing_adapters/webmock_spec.rb +0 -17
  165. data/spec/vcr/middleware/cassette_arguments_spec.rb +0 -32
  166. data/spec/vcr/request_matcher_spec.rb +0 -230
@@ -1,29 +0,0 @@
1
- module VCR
2
- class Cassette
3
- def allow_real_http_requests_to?(uri)
4
- warn "WARNING: VCR::Cassette#allow_real_http_requests_to? is deprecated and should no longer be used."
5
- VCR::Config.uri_should_be_ignored?(uri.to_s)
6
- end
7
-
8
- private
9
-
10
- def deprecate_old_cassette_options(options)
11
- message = "VCR's :allow_real_http cassette option is deprecated. Instead, use the ignore_localhost configuration option."
12
- if options[:allow_real_http] == :localhost
13
- @original_ignored_hosts = VCR::Config.ignored_hosts.dup
14
- VCR::Config.ignored_hosts.clear
15
- VCR::Config.ignore_hosts(*VCR::LOCALHOST_ALIASES)
16
- Kernel.warn "WARNING: #{message}"
17
- elsif options[:allow_real_http]
18
- raise ArgumentError.new(message)
19
- end
20
- end
21
-
22
- def restore_ignore_localhost_for_deprecation
23
- if defined?(@original_ignored_hosts)
24
- VCR::Config.ignored_hosts.clear
25
- VCR::Config.ignore_hosts(*@original_ignored_hosts)
26
- end
27
- end
28
- end
29
- end
@@ -1,18 +0,0 @@
1
- module VCR
2
- module Config
3
- def http_stubbing_library
4
- warn "WARNING: `VCR::Config.http_stubbing_library` is deprecated. Use `VCR::Config.http_stubbing_libraries` instead."
5
- @http_stubbing_libraries && @http_stubbing_libraries.first
6
- end
7
-
8
- def http_stubbing_library=(library)
9
- warn "WARNING: `VCR::Config.http_stubbing_library = #{library.inspect}` is deprecated. Use `VCR::Config.stub_with #{library.inspect}` instead."
10
- stub_with library
11
- end
12
-
13
- def ignore_localhost?
14
- warn "WARNING: `VCR::Config.ignore_localhost?` is deprecated. Check the list of ignored hosts using `VCR::Config.ignored_hosts` instead."
15
- (VCR::LOCALHOST_ALIASES - ignored_hosts).empty?
16
- end
17
- end
18
- end
@@ -1,9 +0,0 @@
1
- module VCR
2
- module HttpStubbingAdapters
3
- module Common
4
- def ignore_localhost?
5
- VCR::Config.ignore_localhost?
6
- end
7
- end
8
- end
9
- end
@@ -1,11 +0,0 @@
1
- module VCR
2
- module HttpStubbingAdapters
3
- module FakeWeb
4
- def self.const_missing(const)
5
- return super unless const == :LOCALHOST_REGEX
6
- warn "WARNING: `VCR::HttpStubbingAdapters::FakeWeb::LOCALHOST_REGEX` is deprecated."
7
- VCR::Regexes.url_regex_for_hosts(VCR::LOCALHOST_ALIASES)
8
- end
9
- end
10
- end
11
- end
@@ -1,32 +0,0 @@
1
- require 'net/http'
2
- require 'vcr/extensions/net_http_response'
3
-
4
- module Net
5
- class HTTP
6
- def request_with_vcr(request, body = nil, &block)
7
- unless VCR::HttpStubbingAdapters::FakeWeb.enabled?
8
- return request_without_vcr(request, body, &block)
9
- end
10
-
11
- vcr_request = VCR::Request.from_net_http_request(self, request)
12
- response = request_without_vcr(request, body)
13
-
14
- match_attributes = if cass = VCR.current_cassette
15
- cass.match_requests_on
16
- else
17
- VCR::RequestMatcher::DEFAULT_MATCH_ATTRIBUTES
18
- end
19
-
20
- if started? && !VCR.http_stubbing_adapter.request_stubbed?(vcr_request, match_attributes)
21
- VCR.record_http_interaction VCR::HTTPInteraction.new(vcr_request, VCR::Response.from_net_http_response(response))
22
- response.extend VCR::Net::HTTPResponse # "unwind" the response
23
- end
24
-
25
- yield response if block_given?
26
- response
27
- end
28
-
29
- alias request_without_vcr request
30
- alias request request_with_vcr
31
- end
32
- end
@@ -1,202 +0,0 @@
1
- module VCR
2
- module HttpStubbingAdapters
3
- autoload :Excon, 'vcr/http_stubbing_adapters/excon'
4
- autoload :FakeWeb, 'vcr/http_stubbing_adapters/fakeweb'
5
- autoload :Faraday, 'vcr/http_stubbing_adapters/faraday'
6
- autoload :MultiObjectProxy, 'vcr/http_stubbing_adapters/multi_object_proxy'
7
- autoload :Typhoeus, 'vcr/http_stubbing_adapters/typhoeus'
8
- autoload :WebMock, 'vcr/http_stubbing_adapters/webmock'
9
-
10
- class UnsupportedRequestMatchAttributeError < ArgumentError; end
11
-
12
- module Common
13
- class << self
14
- attr_accessor :exclusively_enabled_adapter
15
-
16
- def add_vcr_info_to_exception_message(exception_klass)
17
- exception_klass.class_eval do
18
- def initialize(message)
19
- super(message + '. ' + VCR::HttpStubbingAdapters::Common::RECORDING_INSTRUCTIONS)
20
- end
21
- end
22
- end
23
-
24
- def adapters
25
- @adapters ||= []
26
- end
27
-
28
- def included(adapter)
29
- adapters << adapter
30
- end
31
- end
32
-
33
- RECORDING_INSTRUCTIONS = "You can use VCR to automatically record this request and replay it later. " +
34
- "For more details, visit the VCR documentation at: http://relishapp.com/myronmarston/vcr/v/#{VCR.version.gsub('.', '-')}"
35
-
36
- def enabled?
37
- [nil, self].include? VCR::HttpStubbingAdapters::Common.exclusively_enabled_adapter
38
- end
39
-
40
- def after_adapters_loaded
41
- # no-op
42
- end
43
-
44
- def exclusively_enabled
45
- VCR::HttpStubbingAdapters::Common.exclusively_enabled_adapter = self
46
-
47
- begin
48
- yield
49
- ensure
50
- VCR::HttpStubbingAdapters::Common.exclusively_enabled_adapter = nil
51
- end
52
- end
53
-
54
- def check_version!
55
- case compare_version
56
- when :too_low
57
- raise "You are using #{library_name} #{version}. VCR requires version #{version_requirement}."
58
- when :too_high
59
- warn "You are using #{library_name} #{version}. VCR is known to work with #{library_name} #{version_requirement}. It may not work with this version."
60
- end
61
- end
62
-
63
- def library_name
64
- @library_name ||= self.to_s.split('::').last
65
- end
66
-
67
- def set_http_connections_allowed_to_default
68
- self.http_connections_allowed = VCR::Config.allow_http_connections_when_no_cassette?
69
- end
70
-
71
- def raise_no_checkpoint_error(cassette)
72
- raise ArgumentError.new("No checkpoint for #{cassette.inspect} could be found")
73
- end
74
-
75
- attr_writer :http_connections_allowed
76
-
77
- def http_connections_allowed?
78
- defined?(@http_connections_allowed) && !!@http_connections_allowed
79
- end
80
-
81
- def ignored_hosts=(hosts)
82
- @ignored_hosts = hosts
83
- end
84
-
85
- def uri_should_be_ignored?(uri)
86
- uri = URI.parse(uri) unless uri.respond_to?(:host)
87
- ignored_hosts.include?(uri.host)
88
- end
89
-
90
- def stub_requests(http_interactions, match_attributes)
91
- match_attributes_stack << match_attributes
92
- grouped_responses(http_interactions, match_attributes).each do |request_matcher, responses|
93
- request_matcher = request_matcher_with_normalized_uri(request_matcher)
94
- queue = stub_queues[request_matcher]
95
- responses.each { |res| queue << res }
96
- end
97
- end
98
-
99
- def create_stubs_checkpoint(cassette)
100
- checkpoints[cassette] = stub_queue_dup
101
- end
102
-
103
- def restore_stubs_checkpoint(cassette)
104
- match_attributes_stack.pop
105
- @stub_queues = checkpoints.delete(cassette) || raise_no_checkpoint_error(cassette)
106
- end
107
-
108
- def stubbed_response_for(request)
109
- return nil unless match_attributes_stack.any?
110
- request_matcher = request.matcher(match_attributes_stack.last)
111
- queue = stub_queues[request_matcher]
112
- return queue.shift if queue.size > 1
113
- queue.first
114
- end
115
-
116
- def reset!
117
- instance_variables.each do |ivar|
118
- remove_instance_variable(ivar)
119
- end
120
- end
121
-
122
- private
123
-
124
- def ignored_hosts
125
- @ignored_hosts ||= []
126
- end
127
-
128
- def checkpoints
129
- @checkpoints ||= {}
130
- end
131
-
132
- def stub_queues
133
- @stub_queues ||= hash_of_arrays
134
- end
135
-
136
- def match_attributes_stack
137
- @match_attributes_stack ||= []
138
- end
139
-
140
- def stub_queue_dup
141
- dup = hash_of_arrays
142
-
143
- stub_queues.each do |k, v|
144
- dup[k] = v.dup
145
- end
146
-
147
- dup
148
- end
149
-
150
- def hash_of_arrays
151
- Hash.new { |h, k| h[k] = [] }
152
- end
153
-
154
- def compare_version
155
- major, minor, patch = parse_version(version)
156
- min_major, min_minor, min_patch = parse_version(self::MINIMUM_VERSION)
157
- max_major, max_minor = parse_version(self::MAXIMUM_VERSION)
158
-
159
- case
160
- when major < min_major; :too_low
161
- when major > max_major; :too_high
162
- when minor < min_minor; :too_low
163
- when minor > max_minor; :too_high
164
- when patch < min_patch; :too_low
165
- end
166
- end
167
-
168
- def version_requirement
169
- max_major, max_minor = parse_version(self::MAXIMUM_VERSION)
170
- ">= #{self::MINIMUM_VERSION}, < #{max_major}.#{max_minor + 1}"
171
- end
172
-
173
- def parse_version(version)
174
- version.split('.').map { |v| v.to_i }
175
- end
176
-
177
- def grouped_responses(http_interactions, match_attributes)
178
- responses = Hash.new { |h,k| h[k] = [] }
179
-
180
- http_interactions.each do |i|
181
- responses[i.request.matcher(match_attributes)] << i.response
182
- end
183
-
184
- responses
185
- end
186
-
187
- def normalize_uri(uri)
188
- uri # adapters can override this
189
- end
190
-
191
- def request_matcher_with_normalized_uri(matcher)
192
- normalized_uri = normalize_uri(matcher.request.uri)
193
- return matcher unless matcher.request.uri != normalized_uri
194
-
195
- request = matcher.request.dup
196
- request.uri = normalized_uri
197
-
198
- RequestMatcher.new(request, matcher.match_attributes)
199
- end
200
- end
201
- end
202
- end
@@ -1,178 +0,0 @@
1
- require 'excon'
2
-
3
- module VCR
4
- module HttpStubbingAdapters
5
- module Excon
6
- include VCR::HttpStubbingAdapters::Common
7
- extend self
8
-
9
- class HttpConnectionNotAllowedError < StandardError; end
10
-
11
- MINIMUM_VERSION = '0.6.5'
12
- MAXIMUM_VERSION = '0.6'
13
-
14
- private
15
-
16
- def version
17
- ::Excon::VERSION
18
- end
19
-
20
- class RequestHandler
21
- attr_reader :params
22
- def initialize(params)
23
- @params = params
24
- end
25
-
26
- def handle
27
- case
28
- when request_should_be_ignored?
29
- perform_real_request
30
- when stubbed_response
31
- stubbed_response
32
- when http_connections_allowed?
33
- record_interaction
34
- else
35
- raise_connections_disabled_error
36
- end
37
- end
38
-
39
- private
40
-
41
- def request_should_be_ignored?
42
- VCR::HttpStubbingAdapters::Excon.uri_should_be_ignored?(uri)
43
- end
44
-
45
- def stubbed_response
46
- @stubbed_response ||= begin
47
- if stubbed_response = VCR::HttpStubbingAdapters::Excon.stubbed_response_for(vcr_request)
48
- {
49
- :body => stubbed_response.body,
50
- :headers => normalized_headers(stubbed_response.headers || {}),
51
- :status => stubbed_response.status.code
52
- }
53
- end
54
- end
55
- end
56
-
57
- def http_connections_allowed?
58
- VCR::HttpStubbingAdapters::Excon.http_connections_allowed?
59
- end
60
-
61
- def response_from_excon_error(error)
62
- if error.respond_to?(:response)
63
- error.response
64
- elsif error.respond_to?(:socket_error)
65
- response_from_excon_error(error.socket_error)
66
- else
67
- warn "WARNING: VCR could not extract a response from Excon error (#{error.inspect})"
68
- end
69
- end
70
-
71
- def perform_real_request
72
- connection = ::Excon.new(uri)
73
-
74
- response = begin
75
- connection.request(params.merge(:mock => false))
76
- rescue ::Excon::Errors::Error => e
77
- yield response_from_excon_error(e) if block_given?
78
- raise e
79
- end
80
-
81
- yield response if block_given?
82
-
83
- response.attributes
84
- end
85
-
86
- def record_interaction
87
- perform_real_request do |response|
88
- if VCR::HttpStubbingAdapters::Excon.enabled?
89
- http_interaction = http_interaction_for(response)
90
- VCR.record_http_interaction(http_interaction)
91
- end
92
- end
93
- end
94
-
95
- def uri
96
- @uri ||= "#{params[:scheme]}://#{params[:host]}:#{params[:port]}#{params[:path]}#{query}"
97
- end
98
-
99
- def query
100
- @query ||= case params[:query]
101
- when String
102
- "?#{params[:query]}"
103
- when Hash
104
- qry = '?'
105
- for key, values in params[:query]
106
- if values.nil?
107
- qry << key.to_s << '&'
108
- else
109
- for value in [*values]
110
- qry << key.to_s << '=' << CGI.escape(value.to_s) << '&'
111
- end
112
- end
113
- end
114
- qry.chop! # remove trailing '&'
115
- else
116
- ''
117
- end
118
- end
119
-
120
- def http_interaction_for(response)
121
- VCR::HTTPInteraction.new \
122
- vcr_request,
123
- vcr_response(response)
124
- end
125
-
126
- def vcr_request
127
- @vcr_request ||= begin
128
- headers = params[:headers].dup
129
- headers.delete("Host")
130
-
131
- VCR::Request.new \
132
- params[:method],
133
- uri,
134
- params[:body],
135
- headers
136
- end
137
- end
138
-
139
- def vcr_response(response)
140
- VCR::Response.new \
141
- VCR::ResponseStatus.new(response.status, nil),
142
- response.headers,
143
- response.body,
144
- nil
145
- end
146
-
147
- def normalized_headers(headers)
148
- normalized = {}
149
- headers.each do |k, v|
150
- v = v.join(', ') if v.respond_to?(:join)
151
- normalized[normalize_header_key(k)] = v
152
- end
153
- normalized
154
- end
155
-
156
- def normalize_header_key(key)
157
- key.split('-'). # 'user-agent' => %w(user agent)
158
- each { |w| w.capitalize! }. # => %w(User Agent)
159
- join('-')
160
- end
161
-
162
- def raise_connections_disabled_error
163
- raise HttpConnectionNotAllowedError.new(
164
- "Real HTTP connections are disabled. Request: #{params[:method]} #{uri}"
165
- )
166
- end
167
-
168
- ::Excon.stub({}) do |params|
169
- self.new(params).handle
170
- end
171
- end
172
-
173
- end
174
- end
175
- end
176
-
177
- Excon.mock = true
178
- VCR::HttpStubbingAdapters::Common.add_vcr_info_to_exception_message(VCR::HttpStubbingAdapters::Excon::HttpConnectionNotAllowedError)