Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
myronmarston committed Sep 9, 2010
1 parent d7284af commit 9f2c913
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 3 deletions.
4 changes: 2 additions & 2 deletions features/step_definitions/net_http_steps.rb
Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion lib/vcr/extensions/net_http.rb
Expand Up @@ -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?
Expand Down
4 changes: 4 additions & 0 deletions lib/vcr/http_stubbing_adapters/fakeweb.rb
Expand Up @@ -64,6 +64,10 @@ def ignore_localhost?
@ignore_localhost
end

def should_unwind_response?(response)
true
end

private

def version
Expand Down
6 changes: 6 additions & 0 deletions lib/vcr/http_stubbing_adapters/webmock.rb
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions spec/http_stubbing_adapters/fakeweb_spec.rb
Expand Up @@ -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 }
Expand Down
13 changes: 13 additions & 0 deletions spec/http_stubbing_adapters/webmock_spec.rb
Expand Up @@ -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) }

Expand Down

0 comments on commit 9f2c913

Please sign in to comment.