Permalink
Browse files

Adds support for chunked responses in Curb

Calls the 'on_body' callback for each 'chunk' of the body of a webmock
response if the response responds to :each and the Transfer-Encoding is
'chunked.'
  • Loading branch information...
1 parent 2919c0e commit f85fc15dfe0cdc875437d39fa9f9ec6bd78086fb @zbelzer zbelzer committed Jan 29, 2014
Showing with 63 additions and 1 deletion.
  1. +14 −1 lib/webmock/http_lib_adapters/curb_adapter.rb
  2. +49 −0 spec/acceptance/curb/curb_spec.rb
@@ -125,6 +125,7 @@ def build_curb_response(webmock_response)
end
@content_type = webmock_response.headers["Content-Type"]
+ @transfer_encoding = webmock_response.headers["Transfer-Encoding"]
end
@last_effective_url ||= self.url
@@ -144,7 +145,15 @@ def webmock_follow_location(location)
def invoke_curb_callbacks
@on_progress.call(0.0,1.0,0.0,1.0) if @on_progress
self.header_str.lines.each { |header_line| @on_header.call header_line } if @on_header
- @on_body.call(self.body_str) if @on_body
+ if @on_body
+ if chunked_response?
+ self.body_str.each do |chunk|
+ @on_body.call(chunk)
+ end
+ else
+ @on_body.call(self.body_str)
+ end
+ end
@on_complete.call(self) if @on_complete
case response_code
@@ -155,6 +164,10 @@ def invoke_curb_callbacks
end
end
+ def chunked_response?
+ @transfer_encoding == 'chunked' && self.body_str.respond_to?(:each)
+ end
+
def build_webmock_response
status, headers =
WebMock::HttpLibAdapters::CurbAdapter.parse_header_string(self.header_str)
@@ -81,6 +81,20 @@
test.should == body
end
+ it "should call on_body for each chunk with chunked response" do
+ body = "on_body fired"
+ stub_request(:any, "example.com").
+ to_return(:body => ["first_chunk", "second_chunk"],
+ :headers => {"Transfer-Encoding" => "chunked"})
+
+ test = []
+ @curl.on_body do |data|
+ test << data
+ end
+ @curl.http_get
+ test.should == ["first_chunk", "second_chunk"]
+ end
+
it "should call on_header when response headers are read" do
stub_request(:any, "example.com").
to_return(:headers => {:one => 1})
@@ -267,6 +281,41 @@
end
end
end
+
+ describe "#chunked_response?" do
+ before(:each) do
+ @curl = Curl::Easy.new
+ @curl.url = "http://example.com"
+ end
+
+ it "is true when Transfer-Encoding is 'chunked' and body responds to each" do
+ stub_request(:any, 'example.com').
+ to_return(:body => ["abc", "def"],
+ :status => 200,
+ :headers => { 'Transfer-Encoding' => 'chunked' })
+
+ @curl.http_get
+ @curl.should be_chunked_response
+ end
+
+ it "is false when Transfer-Encoding is not 'chunked'" do
+ stub_request(:any, 'example.com').
+ to_return(:body => ["abc", "def"],
+ :status => 200)
+
+ @curl.http_get
+ @curl.should_not be_chunked_response
+ end
+
+ it "is false when Transfer-Encoding is 'chunked' but body does not respond to each" do
+ stub_request(:any, 'example.com').
+ to_return(:body => "abc",
+ :status => 200)
+
+ @curl.http_get
+ @curl.should_not be_chunked_response
+ end
+ end
end
describe "Webmock with Curb" do

0 comments on commit f85fc15

Please sign in to comment.