Skip to content

Commit

Permalink
Added support for Excon's :response_block parameter when webmock retu…
Browse files Browse the repository at this point in the history
…rns stubbed responses.
  • Loading branch information
bblimke committed Feb 17, 2013
1 parent 6c05e45 commit 5c6330b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/webmock/http_lib_adapters/excon_adapter.rb
Expand Up @@ -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)

Expand All @@ -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
Expand Down
16 changes: 16 additions & 0 deletions spec/acceptance/excon/excon_spec.rb
Expand Up @@ -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 }

Expand Down

0 comments on commit 5c6330b

Please sign in to comment.