ActiveResource::Base#create data is not loaded if the transfer-encoding is chunked #2079

Closed
wants to merge 1 commit into
from
@@ -1357,7 +1357,11 @@ def create
end
def load_attributes_from_response(response)
- if !response['Content-Length'].blank? && response['Content-Length'] != "0" && !response.body.nil? && response.body.strip.size > 0
+ has_content_length = !response['Content-Length'].blank? && response['Content-Length'] != "0"
+ has_body = !response.body.nil? && response.body.strip.size > 0
+ is_chunked = response["Transfer-Encoding"] == "chunked"
+
+ if has_body && (has_content_length || is_chunked)
@jreidinger

jreidinger Sep 27, 2011

Contributor

is_chunked is not neccessary as net response object has method chunked?
so it should look like
if has_body && (has_content_length || response.chunked?)

load(self.class.format.decode(response.body), true)
@persisted = true
end
@@ -711,6 +711,44 @@ def test_create_without_location
assert_nil person.id
end
+ def test_create_with_response
+ ActiveResource::HttpMock.respond_to do |mock|
+ mock.post "/people.json", {}, Person.new(:name=>'Rick', :other=>'stuff').to_json
+ end
+ person = Person.create(:name => 'Rick')
+ assert_equal 'Rick', person.name
+ assert_equal 'stuff', person.other
+ end
+
+ def test_create_with_204_response
+ person_json = Person.new(:name=>'Rick', :other=>'stuff').to_json
+ create_request = ActiveResource::Request.new(:post, '/people.json', person_json, {})
+ create_response = ActiveResource::Response.new('', 204, {})
+ create_response.headers.delete('Content-Length')
+ ActiveResource::HttpMock.respond_to({create_request => create_response})
+
+ person = Person.create(:name => 'Rick')
+ assert_equal 'Rick', person.name
+ assert_equal false, person.persisted?
+ end
+
+ def test_create_with_chunked_response
+ person_json = Person.new(:name=>'Rick', :other=>'stuff').to_json
+ create_request = ActiveResource::Request.new(:post, '/people.json', person_json, {})
+
+ # Simulate chunked transfer
+ create_response = ActiveResource::Response.new(person_json, 200, {})
+ create_response["Transfer-Encoding"] = "chunked"
+ create_response.headers.delete('Content-Length')
+
+ ActiveResource::HttpMock.respond_to({create_request => create_response})
+
+ person = Person.create(:name => 'Rick')
+ assert_equal 'Rick', person.name
+ assert_equal 'stuff', person.other
+ end
+
+
def test_clone
matz = Person.find(1)
matz_c = matz.clone