diff options
Diffstat (limited to 'activeresource')
-rw-r--r-- | activeresource/CHANGELOG | 2 | ||||
-rw-r--r-- | activeresource/lib/active_resource/base.rb | 14 | ||||
-rw-r--r-- | activeresource/lib/active_resource/connection.rb | 5 | ||||
-rw-r--r-- | activeresource/test/cases/authorization_test.rb | 6 | ||||
-rw-r--r-- | activeresource/test/cases/base/load_test.rb | 4 | ||||
-rw-r--r-- | activeresource/test/cases/base_test.rb | 44 |
6 files changed, 64 insertions, 11 deletions
diff --git a/activeresource/CHANGELOG b/activeresource/CHANGELOG index 25f9242b98..fe356d7691 100644 --- a/activeresource/CHANGELOG +++ b/activeresource/CHANGELOG @@ -1,4 +1,4 @@ -*Rails 3.1.0 (unreleased)* +*Rails 3.1.0 (August 30, 2011)* * The default format has been changed to JSON for all requests. If you want to continue to use XML you will need to set `self.format = :xml` in the class. eg. diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index 990d9a38dd..03c4cc5b9e 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -1357,7 +1357,9 @@ module ActiveResource end def load_attributes_from_response(response) - if !response['Content-Length'].blank? && response['Content-Length'] != "0" && !response.body.nil? && response.body.strip.size > 0 + if (response_code_allows_body?(response.code) && + (response['Content-Length'].nil? || response['Content-Length'] != "0") && + !response.body.nil? && response.body.strip.size > 0) load(self.class.format.decode(response.body), true) @persisted = true end @@ -1381,6 +1383,16 @@ module ActiveResource end private + + def read_attribute_for_serialization(n) + attributes[n] + end + + # Determine whether the response is allowed to have a body per HTTP 1.1 spec section 4.4.1 + def response_code_allows_body?(c) + !((100..199).include?(c) || [204,304].include?(c)) + end + # Tries to find a resource for a given collection name; if it fails, then the resource is created def find_or_create_resource_for_collection(name) find_or_create_resource_for(ActiveSupport::Inflector.singularize(name.to_s)) diff --git a/activeresource/lib/active_resource/connection.rb b/activeresource/lib/active_resource/connection.rb index d923204dde..592fca96a4 100644 --- a/activeresource/lib/active_resource/connection.rb +++ b/activeresource/lib/active_resource/connection.rb @@ -238,8 +238,11 @@ module ActiveResource def digest_auth_header(http_method, uri) params = extract_params_from_response + request_uri = uri.path + request_uri << "?#{uri.query}" if uri.query + ha1 = Digest::MD5.hexdigest("#{@user}:#{params['realm']}:#{@password}") - ha2 = Digest::MD5.hexdigest("#{http_method.to_s.upcase}:#{uri.path}") + ha2 = Digest::MD5.hexdigest("#{http_method.to_s.upcase}:#{request_uri}") params.merge!('cnonce' => client_nonce) request_digest = Digest::MD5.hexdigest([ha1, params['nonce'], "0", params['cnonce'], params['qop'], ha2].join(":")) diff --git a/activeresource/test/cases/authorization_test.rb b/activeresource/test/cases/authorization_test.rb index 69ef9a2821..17cd9b30fc 100644 --- a/activeresource/test/cases/authorization_test.rb +++ b/activeresource/test/cases/authorization_test.rb @@ -131,6 +131,12 @@ class AuthorizationTest < Test::Unit::TestCase assert_equal blank_digest_auth_header("/people/2.json", "fad396f6a34aeba28e28b9b96ddbb671"), authorization_header['Authorization'] end + def test_authorization_header_with_query_string_if_auth_type_is_digest + @authenticated_conn.auth_type = :digest + authorization_header = @authenticated_conn.__send__(:authorization_header, :get, URI.parse('/people/2.json?only=name')) + assert_equal blank_digest_auth_header("/people/2.json?only=name", "f8457b0b5d21b6b80737a386217afb24"), authorization_header['Authorization'] + end + def test_get david = decode(@authenticated_conn.get("/people/2.json")) assert_equal "David", david["name"] diff --git a/activeresource/test/cases/base/load_test.rb b/activeresource/test/cases/base/load_test.rb index 0bbd3ab5f5..784e7dd036 100644 --- a/activeresource/test/cases/base/load_test.rb +++ b/activeresource/test/cases/base/load_test.rb @@ -66,11 +66,11 @@ class BaseLoadTest < Test::Unit::TestCase end def test_load_hash_with_integers_as_keys - assert_nothing_raised{person = @person.load(@books)} + assert_nothing_raised{@person.load(@books)} end def test_load_hash_with_dates_as_keys - assert_nothing_raised{person = @person.load(@books_date)} + assert_nothing_raised{@person.load(@books_date)} end def test_load_expects_hash diff --git a/activeresource/test/cases/base_test.rb b/activeresource/test/cases/base_test.rb index f45652d988..7b42f64a35 100644 --- a/activeresource/test/cases/base_test.rb +++ b/activeresource/test/cases/base_test.rb @@ -636,13 +636,37 @@ class BaseTest < Test::Unit::TestCase assert_nil p.__send__(:id_from_response, resp) end - def test_load_attributes_from_response - p = Person.new + def test_not_persisted_with_no_body_and_positive_content_length resp = ActiveResource::Response.new(nil) resp['Content-Length'] = "100" - assert_nil p.__send__(:load_attributes_from_response, resp) + Person.connection.expects(:post).returns(resp) + assert !Person.create.persisted? + end + + def test_not_persisted_with_body_and_zero_content_length + resp = ActiveResource::Response.new(@rick) + resp['Content-Length'] = "0" + Person.connection.expects(:post).returns(resp) + assert !Person.create.persisted? end + # These response codes aren't allowed to have bodies per HTTP spec + def test_not_persisted_with_empty_response_codes + [100,101,204,304].each do |status_code| + resp = ActiveResource::Response.new(@rick, status_code) + Person.connection.expects(:post).returns(resp) + assert !Person.create.persisted? + end + end + + # Content-Length is not required by HTTP 1.1, so we should read + # the body anyway in its absence. + def test_persisted_with_no_content_length + resp = ActiveResource::Response.new(@rick) + resp['Content-Length'] = nil + Person.connection.expects(:post).returns(resp) + assert Person.create.persisted? + end def test_create_with_custom_prefix matzs_house = StreetAddress.new(:person_id => 1) @@ -980,9 +1004,17 @@ class BaseTest < Test::Unit::TestCase def test_to_xml_with_private_method_name_as_attribute Person.format = :xml - assert_nothing_raised(ArgumentError) { - Customer.new(:test => true).to_xml - } + + customer = Customer.new(:foo => "foo") + customer.singleton_class.class_eval do + def foo + "bar" + end + private :foo + end + + assert !customer.to_xml.include?("<foo>bar</foo>") + assert customer.to_xml.include?("<foo>foo</foo>") ensure Person.format = :json end |