diff options
Diffstat (limited to 'activeresource/test/cases')
-rw-r--r-- | activeresource/test/cases/authorization_test.rb | 6 | ||||
-rw-r--r-- | activeresource/test/cases/base/load_test.rb | 19 | ||||
-rw-r--r-- | activeresource/test/cases/base/schema_test.rb | 10 | ||||
-rw-r--r-- | activeresource/test/cases/base_test.rb | 44 | ||||
-rw-r--r-- | activeresource/test/cases/connection_test.rb | 19 |
5 files changed, 87 insertions, 11 deletions
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 0d030148d0..784e7dd036 100644 --- a/activeresource/test/cases/base/load_test.rb +++ b/activeresource/test/cases/base/load_test.rb @@ -51,9 +51,28 @@ class BaseLoadTest < Test::Unit::TestCase :votes => [ true, false, true ], :places => [ "Columbia City", "Unknown" ]}}} + + # List of books formated as [{timestamp_of_publication => name}, ...] + @books = {:books => [ + {1009839600 => "Ruby in a Nutshell"}, + {1199142000 => "The Ruby Programming Language"} + ]} + + @books_date = {:books => [ + {Time.at(1009839600) => "Ruby in a Nutshell"}, + {Time.at(1199142000) => "The Ruby Programming Language"} + ]} @person = Person.new end + def test_load_hash_with_integers_as_keys + assert_nothing_raised{@person.load(@books)} + end + + def test_load_hash_with_dates_as_keys + assert_nothing_raised{@person.load(@books_date)} + end + def test_load_expects_hash assert_raise(ArgumentError) { @person.load nil } assert_raise(ArgumentError) { @person.load '<person id="1"/>' } diff --git a/activeresource/test/cases/base/schema_test.rb b/activeresource/test/cases/base/schema_test.rb index 48fdeb13df..d29eaf5fb6 100644 --- a/activeresource/test/cases/base/schema_test.rb +++ b/activeresource/test/cases/base/schema_test.rb @@ -139,7 +139,7 @@ class SchemaTest < ActiveModel::TestCase assert_nothing_raised { Person.schema = new_schema assert_equal new_schema, Person.schema, "should have saved the schema on the class" - assert_equal new_schema, Person.new.schema, "should have mde the schema available to every instance" + assert_equal new_schema, Person.new.schema, "should have made the schema available to every instance" } end @@ -283,8 +283,8 @@ class SchemaTest < ActiveModel::TestCase new_attr_name_two = :another_new_schema_attribute assert Person.schema.blank?, "sanity check - should have a blank class schema" - assert !Person.new.respond_do?(new_attr_name), "sanity check - should not respond to the brand-new attribute yet" - assert !Person.new.respond_do?(new_attr_name_two), "sanity check - should not respond to the brand-new attribute yet" + assert !Person.new.respond_to?(new_attr_name), "sanity check - should not respond to the brand-new attribute yet" + assert !Person.new.respond_to?(new_attr_name_two), "sanity check - should not respond to the brand-new attribute yet" assert_nothing_raised do Person.schema = {new_attr_name.to_s => 'string'} @@ -301,8 +301,8 @@ class SchemaTest < ActiveModel::TestCase assert Person.schema.blank?, "sanity check - should have a blank class schema" - assert !Person.new.respond_do?(new_attr_name), "sanity check - should not respond to the brand-new attribute yet" - assert !Person.new.respond_do?(new_attr_name_two), "sanity check - should not respond to the brand-new attribute yet" + assert !Person.new.respond_to?(new_attr_name), "sanity check - should not respond to the brand-new attribute yet" + assert !Person.new.respond_to?(new_attr_name_two), "sanity check - should not respond to the brand-new attribute yet" assert_nothing_raised do Person.schema { string new_attr_name_two } 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 diff --git a/activeresource/test/cases/connection_test.rb b/activeresource/test/cases/connection_test.rb index 09df0fb678..535107aeef 100644 --- a/activeresource/test/cases/connection_test.rb +++ b/activeresource/test/cases/connection_test.rb @@ -2,6 +2,7 @@ require 'abstract_unit' class ConnectionTest < Test::Unit::TestCase ResponseCodeStub = Struct.new(:code) + RedirectResponseStub = Struct.new(:code, :Location) def setup @conn = ActiveResource::Connection.new('http://localhost') @@ -38,6 +39,18 @@ class ConnectionTest < Test::Unit::TestCase assert_equal expected, handle_response(expected) end + # 301 is moved permanently (redirect) + assert_redirect_raises 301 + + # 302 is found (redirect) + assert_redirect_raises 302 + + # 303 is see other (redirect) + assert_redirect_raises 303 + + # 307 is temporary redirect + assert_redirect_raises 307 + # 400 is a bad request (e.g. malformed URI or missing request parameter) assert_response_raises ActiveResource::BadRequest, 400 @@ -247,6 +260,12 @@ class ConnectionTest < Test::Unit::TestCase end end + def assert_redirect_raises(code) + assert_raise(ActiveResource::Redirection, "Expected response code #{code} to raise ActiveResource::Redirection") do + handle_response RedirectResponseStub.new(code, 'http://example.com/') + end + end + def handle_response(response) @conn.__send__(:handle_response, response) end |