Skip to content

Commit

Permalink
after_request callback receives full body in response object even if …
Browse files Browse the repository at this point in the history
…Excon's :response_block param is used.
  • Loading branch information
bblimke committed Feb 17, 2013
1 parent 5c6330b commit 9903ba8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
15 changes: 12 additions & 3 deletions lib/webmock/http_lib_adapters/excon_adapter.rb
Expand Up @@ -64,11 +64,11 @@ def self.real_response(mock)
:headers => mock.headers
end

def self.mock_response(real)
def self.mock_response(real, response_body_from_chunks)
mock = WebMock::Response.new
mock.status = real.status
mock.headers = real.headers
mock.body = real.body
mock.body = response_body_from_chunks || real.body
mock
end

Expand Down Expand Up @@ -105,8 +105,17 @@ def request_kernel(params)
response

elsif WebMock.net_connect_allowed?(mock_request.uri)
response_body_from_chunks = nil
if params.has_key?(:response_block)
response_body_from_chunks = ""
original_block = params[:response_block]
params[:response_block] = lambda {|chunk, remaining, total|
response_body_from_chunks << chunk
original_block.call(chunk, remaining, total)
}
end
real_response = super
ExconAdapter.perform_callbacks(mock_request, ExconAdapter.mock_response(real_response), :real_request => true)
ExconAdapter.perform_callbacks(mock_request, ExconAdapter.mock_response(real_response, response_body_from_chunks), :real_request => true)
real_response
else
raise WebMock::NetConnectNotAllowedError.new(mock_request)
Expand Down
41 changes: 28 additions & 13 deletions spec/acceptance/excon/excon_spec.rb
Expand Up @@ -16,20 +16,35 @@
lambda { Excon.get('http://example.com', :expects => 204) }.should raise_error(Excon::Errors::OK)
end

it "should support excon response_block for real requests" do
a = []
WebMock.allow_net_connect!
r = Excon.get('http://httpstat.us/200', :response_block => lambda {|e,remaining, total| a << e}, :chunk_size => 1)
a.should == ["2", "0", "0", " ", "O", "K"]
r.body.should == ""
end
context "with response_block" do
it "should support excon response_block for real requests" do
a = []
WebMock.allow_net_connect!
r = Excon.get('http://httpstat.us/200', :response_block => lambda {|e,remaining, total| a << e}, :chunk_size => 1)
a.should == ["2", "0", "0", " ", "O", "K"]
r.body.should == ""
end

it "should support excon response_block" do
a = []
stub_request(:get, "http://example.com/").to_return(:body => "abc")
r = Excon.get('http://example.com', :response_block => lambda {|e, remaining, total| a << e}, :chunk_size => 1)
a.should == ['a', 'b', 'c']
r.body.should == ""
it "should support excon response_block" do
a = []
stub_request(:get, "http://example.com/").to_return(:body => "abc")
r = Excon.get('http://example.com', :response_block => lambda {|e, remaining, total| a << e}, :chunk_size => 1)
a.should == ['a', 'b', 'c']
r.body.should == ""
end

it "should invoke callbacks with response body even if a real request is made" do
a = []
WebMock.allow_net_connect!
response = nil
WebMock.after_request { |_, res|
response = res
}
r = Excon.get('http://httpstat.us/200', :response_block => lambda {|e,remaining, total| a << e}, :chunk_size => 1)
response.body.should == "200 OK"
a.should == ["2", "0", "0", " ", "O", "K"]
r.body.should == ""
end
end

let(:file) { File.new(__FILE__) }
Expand Down

0 comments on commit 9903ba8

Please sign in to comment.