Permalink
Browse files

Don't extend a Net::HTTP response with our module when WebMock has al…

…ready extended it with its own module.

These modules duplicate the same behavior and stomp on each other's toes.
  • Loading branch information...
1 parent d7284af commit 9f2c913cfaed6f904530f8da82160e7012e8cc85 @myronmarston committed Sep 9, 2010
View
4 features/step_definitions/net_http_steps.rb
@@ -19,8 +19,8 @@ def perform_net_http_get_with_returning_block(uri, path)
capture_response(url) do |uri, path|
result_body = ''
result = Net::HTTP.new(uri.host, uri.port).request_get(path) { |r| r.read_body { |fragment| result_body << fragment } }
- result.body.should == result_body
- result
+ def result_body.body; self; end # make the string a fake response (so response.body can be called on it)
+ result_body
end
end
View
5 lib/vcr/extensions/net_http.rb
@@ -11,7 +11,10 @@ def request(request, body = nil, &block)
match_attributes = (cass = VCR.current_cassette) ? cass.match_requests_on : VCR::RequestMatcher::DEFAULT_MATCH_ATTRIBUTES
if started? && !VCR.http_stubbing_adapter.request_stubbed?(vcr_request, match_attributes)
VCR.record_http_interaction VCR::HTTPInteraction.new(vcr_request, VCR::Response.from_net_http_response(response))
- response.extend VCR::Net::HTTPResponse # "unwind" the response
+
+ if VCR.http_stubbing_adapter.should_unwind_response?(response)
+ response.extend VCR::Net::HTTPResponse # "unwind" the response
+ end
end
yield response if block_given?
View
4 lib/vcr/http_stubbing_adapters/fakeweb.rb
@@ -64,6 +64,10 @@ def ignore_localhost?
@ignore_localhost
end
+ def should_unwind_response?(response)
+ true
+ end
+
private
def version
View
6 lib/vcr/http_stubbing_adapters/webmock.rb
@@ -59,6 +59,12 @@ def ignore_localhost?
::WebMock::Config.instance.allow_localhost
end
+ def should_unwind_response?(response)
+ class << response
+ !ancestors.include?(::WebMock::Net::HTTPResponse)
+ end
+ end
+
private
def version
View
8 spec/http_stubbing_adapters/fakeweb_spec.rb
@@ -3,6 +3,14 @@
describe VCR::HttpStubbingAdapters::FakeWeb do
it_should_behave_like 'an http stubbing adapter', ['net/http'], [:method, :uri, :host, :path]
+ describe '#should_unwind_response?' do
+ let(:response) { ::Net::HTTPOK.new('1.1', 200, 'OK') }
+
+ it 'returns true' do
+ described_class.should_unwind_response?(response).should be_true
+ end
+ end
+
describe '#check_version!' do
disable_warnings
before(:each) { @orig_version = FakeWeb::VERSION }
View
13 spec/http_stubbing_adapters/webmock_spec.rb
@@ -5,6 +5,19 @@
%w[net/http patron httpclient em-http-request],
[:method, :uri, :host, :path, :body, :headers]
+ describe '#should_unwind_response?' do
+ let(:response) { ::Net::HTTPOK.new('1.1', 200, 'OK') }
+
+ it 'returns true when the response has not been extended with WebMock::Net::HTTPResponse' do
+ described_class.should_unwind_response?(response).should be_true
+ end
+
+ it 'returns false when the response has been extended with WebMock::Net::HTTPResponse' do
+ response.extend WebMock::Net::HTTPResponse
+ described_class.should_unwind_response?(response).should be_false
+ end
+ end
+
describe '#check_version!' do
before(:each) { WebMock.should respond_to(:version) }

0 comments on commit 9f2c913

Please sign in to comment.