Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

after_request callback receives full body in response object even if …

…Excon's :response_block param is used.
  • Loading branch information...
commit 9903ba8ca3fb9e6ecc1e53abda9232f315830535 1 parent 5c6330b
@bblimke authored
View
15 lib/webmock/http_lib_adapters/excon_adapter.rb
@@ -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
@@ -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)
View
41 spec/acceptance/excon/excon_spec.rb
@@ -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__) }
Please sign in to comment.
Something went wrong with that request. Please try again.