Permalink
Browse files

Merge branch 'master' of github.com:bblimke/webmock

  • Loading branch information...
2 parents 4e3b4f2 + 61819ef commit 02a795caecd02d4d0ad4f330df5221866193553d @bblimke committed Feb 2, 2014
@@ -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)
@@ -46,7 +46,7 @@ def to_webmock
webmock_response = ::WebMock::Response.new
webmock_response.status = [status, reason]
- webmock_response.body = body
+ webmock_response.body = body.to_s
webmock_response.headers = headers
webmock_response
@@ -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 02a795c

Please sign in to comment.