Skip to content
Browse files

Added support for Excon's :response_block parameter when webmock retu…

…rns stubbed responses.
  • Loading branch information...
1 parent 6c05e45 commit 5c6330be3cf2341d730ba3de02b4035522d15f00 @bblimke committed
Showing with 28 additions and 3 deletions.
  1. +12 −3 lib/webmock/http_lib_adapters/excon_adapter.rb
  2. +16 −0 spec/acceptance/excon/excon_spec.rb
View
15 lib/webmock/http_lib_adapters/excon_adapter.rb
@@ -81,7 +81,7 @@ def self.perform_callbacks(request, response, options = {})
class ExconConnection < ::Excon::Connection
- def request_kernel(params, &block)
+ def request_kernel(params)
mock_request = ExconAdapter.build_request params.dup
WebMock::RequestRegistry.instance.requested_signatures.put(mock_request)
@@ -91,9 +91,18 @@ def request_kernel(params, &block)
if params.has_key?(:expects) && ![*params[:expects]].include?(response.status)
raise(Excon::Errors.status_error(params, response))
- else
- response
+ elsif params.has_key?(:response_block)
+ content_length = remaining = response.body.bytesize
+ body = response.body
+ response.body = ""
+ i = 0
+ while i < body.length
+ params[:response_block].call(body[i, params[:chunk_size]], [remaining - params[:chunk_size], 0].max, content_length)
+ remaining -= params[:chunk_size]
+ i += params[:chunk_size]
+ end
end
+ response
elsif WebMock.net_connect_allowed?(mock_request.uri)
real_response = super
View
16 spec/acceptance/excon/excon_spec.rb
@@ -16,6 +16,22 @@
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
+
+ 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
+
let(:file) { File.new(__FILE__) }
let(:file_contents) { File.new(__FILE__).read }

0 comments on commit 5c6330b

Please sign in to comment.
Something went wrong with that request. Please try again.