Permalink
Browse files

Fix excon adapter so that it records a response even when excon raise…

…s an error due to an unexpected response.


Closes #77.
  • Loading branch information...
1 parent 00f50c2 commit ca1e54a6a29ae684ee6510e0b4f02236b2134eb4 @myronmarston committed Jul 16, 2011
Showing with 33 additions and 1 deletion.
  1. +2 −0 CHANGELOG.md
  2. +17 −1 lib/vcr/http_stubbing_adapters/excon.rb
  3. +14 −0 spec/vcr/http_stubbing_adapters/excon_spec.rb
View
@@ -6,6 +6,8 @@
* Fix excon adapter to properly handle queries specified as a hash.
* Fix excon adapter to stub a response with a hash as excon expects
[Wesley Beary](https://github.com/geemus).
+* Fix excon adapter so that it records a response even when excon raises
+ an error due to an unexpected response.
## 1.10.0 (May 18, 2011)
@@ -134,9 +134,25 @@ def http_connections_allowed?
VCR::HttpStubbingAdapters::Excon.http_connections_allowed?
end
+ def response_from_excon_error(error)
+ if error.respond_to?(:response)
+ error.response
+ elsif error.respond_to?(:socket_error)
+ response_from_excon_error(error.socket_error)
+ else
+ warn "WARNING: VCR could not extract a response from Excon error (#{error.inspect})"
+ end
+ end
+
def perform_real_request
connection = ::Excon.new(uri)
- response = connection.request(params.merge(:mock => false))
+
+ response = begin
+ connection.request(params.merge(:mock => false))
+ rescue ::Excon::Errors::Error => e
+ yield response_from_excon_error(e) if block_given?
+ raise e
+ end
yield response if block_given?
@@ -55,5 +55,19 @@
recorded.should == "FOO!"
end
end
+
+ context 'when Excon raises an error due to an unexpected response status' do
+ it 'still records properly' do
+ described_class.http_connections_allowed = true
+
+ VCR.should_receive(:record_http_interaction) do |interaction|
+ interaction.response.status.code.should == 404
+ end
+
+ expect {
+ Excon.get("http://localhost:#{VCR::SinatraApp.port}/not_found", :expects => 200)
+ }.to raise_error(Excon::Errors::Error)
+ end
+ end
end

0 comments on commit ca1e54a

Please sign in to comment.