diff options
Diffstat (limited to 'activeresource/test')
-rw-r--r-- | activeresource/test/authorization_test.rb | 16 | ||||
-rw-r--r-- | activeresource/test/base/custom_methods_test.rb | 3 | ||||
-rw-r--r-- | activeresource/test/base/load_test.rb | 4 | ||||
-rw-r--r-- | activeresource/test/base_test.rb | 117 | ||||
-rw-r--r-- | activeresource/test/connection_test.rb | 2 | ||||
-rw-r--r-- | activeresource/test/format_test.rb | 34 |
6 files changed, 101 insertions, 75 deletions
diff --git a/activeresource/test/authorization_test.rb b/activeresource/test/authorization_test.rb index 9215227620..ead7f5c12f 100644 --- a/activeresource/test/authorization_test.rb +++ b/activeresource/test/authorization_test.rb @@ -19,7 +19,7 @@ class AuthorizationTest < Test::Unit::TestCase end def test_authorization_header - authorization_header = @authenticated_conn.send!(:authorization_header) + authorization_header = @authenticated_conn.__send__(:authorization_header) assert_equal @authorization_request_header['Authorization'], authorization_header['Authorization'] authorization = authorization_header["Authorization"].to_s.split @@ -29,7 +29,7 @@ class AuthorizationTest < Test::Unit::TestCase def test_authorization_header_with_username_but_no_password @conn = ActiveResource::Connection.new("http://david:@localhost") - authorization_header = @conn.send!(:authorization_header) + authorization_header = @conn.__send__(:authorization_header) authorization = authorization_header["Authorization"].to_s.split assert_equal "Basic", authorization[0] @@ -38,7 +38,7 @@ class AuthorizationTest < Test::Unit::TestCase def test_authorization_header_with_password_but_no_username @conn = ActiveResource::Connection.new("http://:test123@localhost") - authorization_header = @conn.send!(:authorization_header) + authorization_header = @conn.__send__(:authorization_header) authorization = authorization_header["Authorization"].to_s.split assert_equal "Basic", authorization[0] @@ -47,7 +47,7 @@ class AuthorizationTest < Test::Unit::TestCase def test_authorization_header_with_decoded_credentials_from_url @conn = ActiveResource::Connection.new("http://my%40email.com:%31%32%33@localhost") - authorization_header = @conn.send!(:authorization_header) + authorization_header = @conn.__send__(:authorization_header) authorization = authorization_header["Authorization"].to_s.split assert_equal "Basic", authorization[0] @@ -58,7 +58,7 @@ class AuthorizationTest < Test::Unit::TestCase @authenticated_conn = ActiveResource::Connection.new("http://@localhost") @authenticated_conn.user = 'david' @authenticated_conn.password = 'test123' - authorization_header = @authenticated_conn.send!(:authorization_header) + authorization_header = @authenticated_conn.__send__(:authorization_header) assert_equal @authorization_request_header['Authorization'], authorization_header['Authorization'] authorization = authorization_header["Authorization"].to_s.split @@ -69,7 +69,7 @@ class AuthorizationTest < Test::Unit::TestCase def test_authorization_header_explicitly_setting_username_but_no_password @conn = ActiveResource::Connection.new("http://@localhost") @conn.user = "david" - authorization_header = @conn.send!(:authorization_header) + authorization_header = @conn.__send__(:authorization_header) authorization = authorization_header["Authorization"].to_s.split assert_equal "Basic", authorization[0] @@ -79,7 +79,7 @@ class AuthorizationTest < Test::Unit::TestCase def test_authorization_header_explicitly_setting_password_but_no_username @conn = ActiveResource::Connection.new("http://@localhost") @conn.password = "test123" - authorization_header = @conn.send!(:authorization_header) + authorization_header = @conn.__send__(:authorization_header) authorization = authorization_header["Authorization"].to_s.split assert_equal "Basic", authorization[0] @@ -116,7 +116,7 @@ class AuthorizationTest < Test::Unit::TestCase protected def assert_response_raises(klass, code) assert_raise(klass, "Expected response code #{code} to raise #{klass}") do - @conn.send!(:handle_response, Response.new(code)) + @conn.__send__(:handle_response, Response.new(code)) end end end diff --git a/activeresource/test/base/custom_methods_test.rb b/activeresource/test/base/custom_methods_test.rb index 62c33ef9bc..ba5799edfb 100644 --- a/activeresource/test/base/custom_methods_test.rb +++ b/activeresource/test/base/custom_methods_test.rb @@ -10,8 +10,7 @@ class CustomMethodsTest < Test::Unit::TestCase @ryan = { :name => 'Ryan' }.to_xml(:root => 'person') @addy = { :id => 1, :street => '12345 Street' }.to_xml(:root => 'address') @addy_deep = { :id => 1, :street => '12345 Street', :zip => "27519" }.to_xml(:root => 'address') - @default_request_headers = { 'Content-Type' => 'application/xml' } - + ActiveResource::HttpMock.respond_to do |mock| mock.get "/people/1.xml", {}, @matz mock.get "/people/1/shallow.xml", {}, @matz diff --git a/activeresource/test/base/load_test.rb b/activeresource/test/base/load_test.rb index 737afb1748..a475fab34e 100644 --- a/activeresource/test/base/load_test.rb +++ b/activeresource/test/base/load_test.rb @@ -84,7 +84,7 @@ class BaseLoadTest < Test::Unit::TestCase end def test_load_collection_with_unknown_resource - Person.send!(:remove_const, :Address) if Person.const_defined?(:Address) + Person.__send__(:remove_const, :Address) if Person.const_defined?(:Address) assert !Person.const_defined?(:Address), "Address shouldn't exist until autocreated" addresses = silence_warnings { @person.load(:addresses => @addresses).addresses } assert Person.const_defined?(:Address), "Address should have been autocreated" @@ -100,7 +100,7 @@ class BaseLoadTest < Test::Unit::TestCase end def test_load_collection_with_single_unknown_resource - Person.send!(:remove_const, :Address) if Person.const_defined?(:Address) + Person.__send__(:remove_const, :Address) if Person.const_defined?(:Address) assert !Person.const_defined?(:Address), "Address shouldn't exist until autocreated" addresses = silence_warnings { @person.load(:addresses => [ @first_address ]).addresses } assert Person.const_defined?(:Address), "Address should have been autocreated" diff --git a/activeresource/test/base_test.rb b/activeresource/test/base_test.rb index 4addd52636..7460fd45b0 100644 --- a/activeresource/test/base_test.rb +++ b/activeresource/test/base_test.rb @@ -8,7 +8,7 @@ class BaseTest < Test::Unit::TestCase def setup @matz = { :id => 1, :name => 'Matz' }.to_xml(:root => 'person') @david = { :id => 2, :name => 'David' }.to_xml(:root => 'person') - @greg = { :id => 3, :name => 'Greg' }.to_xml(:root => 'person') + @greg = { :id => 3, :name => 'Greg' }.to_xml(:root => 'person') @addy = { :id => 1, :street => '12345 Street' }.to_xml(:root => 'address') @default_request_headers = { 'Content-Type' => 'application/xml' } @rick = { :name => "Rick", :age => 25 }.to_xml(:root => "person") @@ -50,7 +50,7 @@ class BaseTest < Test::Unit::TestCase ActiveResource::HttpMock.respond_to do |mock| mock.get "/people/1.xml", {}, @matz mock.get "/people/2.xml", {}, @david - mock.get "/people/Greg.xml", {}, @greg + mock.get "/people/Greg.xml", {}, @greg mock.get "/people/4.xml", {'key' => 'value'}, nil, 404 mock.put "/people/1.xml", {}, nil, 204 mock.delete "/people/1.xml", {}, nil, 200 @@ -62,7 +62,7 @@ class BaseTest < Test::Unit::TestCase mock.get "/people/1/addresses/1.xml", {}, @addy mock.get "/people/1/addresses/2.xml", {}, nil, 404 mock.get "/people/2/addresses/1.xml", {}, nil, 404 - mock.get "/people/Greg/addresses/1.xml", {}, @addy + mock.get "/people/Greg/addresses/1.xml", {}, @addy mock.put "/people/1/addresses/1.xml", {}, nil, 204 mock.delete "/people/1/addresses/1.xml", {}, nil, 200 mock.post "/people/1/addresses.xml", {}, nil, 201, 'Location' => '/people/1/addresses/5' @@ -101,13 +101,13 @@ class BaseTest < Test::Unit::TestCase assert_equal 'http://foo:bar@beast.caboo.se', Forum.site.to_s assert_equal 'http://foo:bar@beast.caboo.se/forums/:forum_id', Topic.site.to_s end - + def test_site_variable_can_be_reset - actor = Class.new(ActiveResource::Base) + actor = Class.new(ActiveResource::Base) assert_nil actor.site actor.site = 'http://localhost:31337' actor.site = nil - assert_nil actor.site + assert_nil actor.site end def test_should_accept_setting_user @@ -194,18 +194,18 @@ class BaseTest < Test::Unit::TestCase actor.site = 'http://nomad' assert_equal actor.site, jester.site assert jester.site.frozen? - - # Subclasses are always equal to superclass site when not overridden + + # Subclasses are always equal to superclass site when not overridden fruit = Class.new(ActiveResource::Base) apple = Class.new(fruit) - + fruit.site = 'http://market' assert_equal fruit.site, apple.site, 'subclass did not adopt changes from parent class' - + fruit.site = 'http://supermarket' assert_equal fruit.site, apple.site, 'subclass did not adopt changes from parent class' end - + def test_user_reader_uses_superclass_user_until_written # Superclass is Object so returns nil. assert_nil ActiveResource::Base.user @@ -317,14 +317,14 @@ class BaseTest < Test::Unit::TestCase end def test_updating_baseclass_site_object_wipes_descendent_cached_connection_objects - # Subclasses are always equal to superclass site when not overridden + # Subclasses are always equal to superclass site when not overridden fruit = Class.new(ActiveResource::Base) apple = Class.new(fruit) - + fruit.site = 'http://market' assert_equal fruit.connection.site, apple.connection.site first_connection = apple.connection.object_id - + fruit.site = 'http://supermarket' assert_equal fruit.connection.site, apple.connection.site second_connection = apple.connection.object_id @@ -393,34 +393,34 @@ class BaseTest < Test::Unit::TestCase assert_equal '/people.xml?gender=', Person.collection_path(:gender => nil) assert_equal '/people.xml?gender=male', Person.collection_path('gender' => 'male') - + # Use includes? because ordering of param hash is not guaranteed assert Person.collection_path(:gender => 'male', :student => true).include?('/people.xml?') assert Person.collection_path(:gender => 'male', :student => true).include?('gender=male') assert Person.collection_path(:gender => 'male', :student => true).include?('student=true') assert_equal '/people.xml?name%5B%5D=bob&name%5B%5D=your+uncle%2Bme&name%5B%5D=&name%5B%5D=false', Person.collection_path(:name => ['bob', 'your uncle+me', nil, false]) - + assert_equal '/people.xml?struct%5Ba%5D%5B%5D=2&struct%5Ba%5D%5B%5D=1&struct%5Bb%5D=fred', Person.collection_path(:struct => {:a => [2,1], 'b' => 'fred'}) end def test_custom_element_path assert_equal '/people/1/addresses/1.xml', StreetAddress.element_path(1, :person_id => 1) assert_equal '/people/1/addresses/1.xml', StreetAddress.element_path(1, 'person_id' => 1) - assert_equal '/people/Greg/addresses/1.xml', StreetAddress.element_path(1, 'person_id' => 'Greg') + assert_equal '/people/Greg/addresses/1.xml', StreetAddress.element_path(1, 'person_id' => 'Greg') end - + def test_custom_element_path_with_redefined_to_param Person.module_eval do alias_method :original_to_param_element_path, :to_param - def to_param + def to_param name end end # Class method. assert_equal '/people/Greg.xml', Person.element_path('Greg') - + # Protected Instance method. assert_equal '/people/Greg.xml', Person.find('Greg').send(:element_path) @@ -468,23 +468,32 @@ class BaseTest < Test::Unit::TestCase def test_prefix assert_equal "/", Person.prefix - assert_equal Set.new, Person.send!(:prefix_parameters) + assert_equal Set.new, Person.__send__(:prefix_parameters) end - + def test_set_prefix SetterTrap.rollback_sets(Person) do |person_class| person_class.prefix = "the_prefix" assert_equal "the_prefix", person_class.prefix end end - + def test_set_prefix_with_inline_keys SetterTrap.rollback_sets(Person) do |person_class| person_class.prefix = "the_prefix:the_param" assert_equal "the_prefixthe_param_value", person_class.prefix(:the_param => "the_param_value") end end - + + def test_set_prefix_twice_should_clear_params + SetterTrap.rollback_sets(Person) do |person_class| + person_class.prefix = "the_prefix/:the_param1" + assert_equal Set.new([:the_param1]), person_class.prefix_parameters + person_class.prefix = "the_prefix/:the_param2" + assert_equal Set.new([:the_param2]), person_class.prefix_parameters + end + end + def test_set_prefix_with_default_value SetterTrap.rollback_sets(Person) do |person_class| person_class.set_prefix @@ -495,7 +504,7 @@ class BaseTest < Test::Unit::TestCase def test_custom_prefix assert_equal '/people//', StreetAddress.prefix assert_equal '/people/1/', StreetAddress.prefix(:person_id => 1) - assert_equal [:person_id].to_set, StreetAddress.send!(:prefix_parameters) + assert_equal [:person_id].to_set, StreetAddress.__send__(:prefix_parameters) end def test_find_by_id @@ -504,7 +513,7 @@ class BaseTest < Test::Unit::TestCase assert_equal "Matz", matz.name assert matz.name? end - + def test_respond_to matz = Person.find(1) assert matz.respond_to?(:name) @@ -533,6 +542,12 @@ class BaseTest < Test::Unit::TestCase assert_equal "Matz", matz.name end + def test_find_last + david = Person.find(:last) + assert_kind_of Person, david + assert_equal 'David', david.name + end + def test_custom_header Person.headers['key'] = 'value' assert_raises(ActiveResource::ResourceNotFound) { Person.find(4) } @@ -547,7 +562,7 @@ class BaseTest < Test::Unit::TestCase def test_find_all_by_from ActiveResource::HttpMock.respond_to { |m| m.get "/companies/1/people.xml", {}, @people_david } - + people = Person.find(:all, :from => "/companies/1/people.xml") assert_equal 1, people.size assert_equal "David", people.first.name @@ -555,7 +570,7 @@ class BaseTest < Test::Unit::TestCase def test_find_all_by_from_with_options ActiveResource::HttpMock.respond_to { |m| m.get "/companies/1/people.xml", {}, @people_david } - + people = Person.find(:all, :from => "/companies/1/people.xml") assert_equal 1, people.size assert_equal "David", people.first.name @@ -563,7 +578,7 @@ class BaseTest < Test::Unit::TestCase def test_find_all_by_symbol_from ActiveResource::HttpMock.respond_to { |m| m.get "/people/managers.xml", {}, @people_david } - + people = Person.find(:all, :from => :managers) assert_equal 1, people.size assert_equal "David", people.first.name @@ -592,10 +607,10 @@ class BaseTest < Test::Unit::TestCase def test_id_from_response p = Person.new resp = {'Location' => '/foo/bar/1'} - assert_equal '1', p.send!(:id_from_response, resp) - + assert_equal '1', p.__send__(:id_from_response, resp) + resp['Location'] << '.xml' - assert_equal '1', p.send!(:id_from_response, resp) + assert_equal '1', p.__send__(:id_from_response, resp) end def test_create_with_custom_prefix @@ -610,16 +625,16 @@ class BaseTest < Test::Unit::TestCase ryan = Person.new(:id => 1, :name => 'Ryan', :address => address) assert_equal address.prefix_options, ryan.address.prefix_options end - + def test_reload_works_with_prefix_options address = StreetAddress.find(1, :params => { :person_id => 1 }) assert_equal address, address.reload end - + def test_reload_with_redefined_to_param Person.module_eval do alias_method :original_to_param_reload, :to_param - def to_param + def to_param name end end @@ -634,13 +649,13 @@ class BaseTest < Test::Unit::TestCase alias_method :reload_to_param, :to_param alias_method :to_param, :original_to_param_reload end - end - - def test_reload_works_without_prefix_options + end + + def test_reload_works_without_prefix_options person = Person.find(:first) assert_equal person, person.reload end - + def test_create rick = Person.create(:name => 'Rick') @@ -650,11 +665,11 @@ class BaseTest < Test::Unit::TestCase # test additional attribute returned on create assert_equal 25, rick.age - + # Test that save exceptions get bubbled up too ActiveResource::HttpMock.respond_to do |mock| mock.post "/people.xml", {}, nil, 409 - end + end assert_raises(ActiveResource::ResourceConflict) { Person.create(:name => 'Rick') } end @@ -716,7 +731,7 @@ class BaseTest < Test::Unit::TestCase assert_equal "54321 Lane", addy.street addy.save end - + def test_update_conflict ActiveResource::HttpMock.respond_to do |mock| mock.get "/people/2.xml", {}, @david @@ -748,7 +763,7 @@ class BaseTest < Test::Unit::TestCase end assert_raises(ActiveResource::ResourceNotFound) { Person.find(1) } end - + def test_delete_with_custom_prefix assert StreetAddress.delete(1, :person_id => 1) ActiveResource::HttpMock.respond_to do |mock| @@ -778,23 +793,23 @@ class BaseTest < Test::Unit::TestCase assert !StreetAddress.new({:id => 1, :person_id => 2}).exists? assert !StreetAddress.new({:id => 2, :person_id => 1}).exists? end - + def test_exists_with_redefined_to_param Person.module_eval do alias_method :original_to_param_exists, :to_param - def to_param + def to_param name end end # Class method. - assert Person.exists?('Greg') + assert Person.exists?('Greg') # Instance method. - assert Person.find('Greg').exists? + assert Person.find('Greg').exists? # Nested class method. - assert StreetAddress.exists?(1, :params => { :person_id => Person.find('Greg').to_param }) + assert StreetAddress.exists?(1, :params => { :person_id => Person.find('Greg').to_param }) # Nested instance method. assert StreetAddress.find(1, :params => { :person_id => Person.find('Greg').to_param }).exists? @@ -806,11 +821,11 @@ class BaseTest < Test::Unit::TestCase alias_method :exists_to_param, :to_param alias_method :to_param, :original_to_param_exists end - end - + end + def test_to_xml matz = Person.find(1) - xml = matz.to_xml + xml = matz.encode assert xml.starts_with?('<?xml version="1.0" encoding="UTF-8"?>') assert xml.include?('<name>Matz</name>') assert xml.include?('<id type="integer">1</id>') diff --git a/activeresource/test/connection_test.rb b/activeresource/test/connection_test.rb index 8e43e451ff..06f31f1b57 100644 --- a/activeresource/test/connection_test.rb +++ b/activeresource/test/connection_test.rb @@ -185,6 +185,6 @@ class ConnectionTest < Test::Unit::TestCase end def handle_response(response) - @conn.send!(:handle_response, response) + @conn.__send__(:handle_response, response) end end diff --git a/activeresource/test/format_test.rb b/activeresource/test/format_test.rb index 3c81803fce..365576a092 100644 --- a/activeresource/test/format_test.rb +++ b/activeresource/test/format_test.rb @@ -5,14 +5,22 @@ class FormatTest < Test::Unit::TestCase def setup @matz = { :id => 1, :name => 'Matz' } @david = { :id => 2, :name => 'David' } - + @programmers = [ @matz, @david ] end - + + def test_http_format_header_name + header_name = ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[:get] + assert_equal 'Accept', header_name + + headers_names = [ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[:put], ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES[:post]] + headers_names.each{|header_name| assert_equal 'Content-Type', header_name} + end + def test_formats_on_single_element for format in [ :json, :xml ] using_format(Person, format) do - ActiveResource::HttpMock.respond_to.get "/people/1.#{format}", {}, ActiveResource::Formats[format].encode(@david) + ActiveResource::HttpMock.respond_to.get "/people/1.#{format}", {'Accept' => ActiveResource::Formats[format].mime_type}, ActiveResource::Formats[format].encode(@david) assert_equal @david[:name], Person.find(1).name end end @@ -21,7 +29,7 @@ class FormatTest < Test::Unit::TestCase def test_formats_on_collection for format in [ :json, :xml ] using_format(Person, format) do - ActiveResource::HttpMock.respond_to.get "/people.#{format}", {}, ActiveResource::Formats[format].encode(@programmers) + ActiveResource::HttpMock.respond_to.get "/people.#{format}", {'Accept' => ActiveResource::Formats[format].mime_type}, ActiveResource::Formats[format].encode(@programmers) remote_programmers = Person.find(:all) assert_equal 2, remote_programmers.size assert remote_programmers.select { |p| p.name == 'David' } @@ -32,7 +40,7 @@ class FormatTest < Test::Unit::TestCase def test_formats_on_custom_collection_method for format in [ :json, :xml ] using_format(Person, format) do - ActiveResource::HttpMock.respond_to.get "/people/retrieve.#{format}?name=David", {}, ActiveResource::Formats[format].encode([@david]) + ActiveResource::HttpMock.respond_to.get "/people/retrieve.#{format}?name=David", {'Accept' => ActiveResource::Formats[format].mime_type}, ActiveResource::Formats[format].encode([@david]) remote_programmers = Person.get(:retrieve, :name => 'David') assert_equal 1, remote_programmers.size assert_equal @david[:id], remote_programmers[0]['id'] @@ -40,13 +48,13 @@ class FormatTest < Test::Unit::TestCase end end end - + def test_formats_on_custom_element_method for format in [ :json, :xml ] using_format(Person, format) do ActiveResource::HttpMock.respond_to do |mock| - mock.get "/people/2.#{format}", {}, ActiveResource::Formats[format].encode(@david) - mock.get "/people/2/shallow.#{format}", {}, ActiveResource::Formats[format].encode(@david) + mock.get "/people/2.#{format}", {'Accept' => ActiveResource::Formats[format].mime_type}, ActiveResource::Formats[format].encode(@david) + mock.get "/people/2/shallow.#{format}", {'Accept' => ActiveResource::Formats[format].mime_type}, ActiveResource::Formats[format].encode(@david) end remote_programmer = Person.find(2).get(:shallow) assert_equal @david[:id], remote_programmer['id'] @@ -57,20 +65,24 @@ class FormatTest < Test::Unit::TestCase for format in [ :json, :xml ] ryan = ActiveResource::Formats[format].encode({ :name => 'Ryan' }) using_format(Person, format) do - ActiveResource::HttpMock.respond_to.post "/people/new/register.#{format}", {}, ryan, 201, 'Location' => "/people/5.#{format}" remote_ryan = Person.new(:name => 'Ryan') + ActiveResource::HttpMock.respond_to.post "/people.#{format}", {'Content-Type' => ActiveResource::Formats[format].mime_type}, ryan, 201, {'Location' => "/people/5.#{format}"} + remote_ryan.save + + remote_ryan = Person.new(:name => 'Ryan') + ActiveResource::HttpMock.respond_to.post "/people/new/register.#{format}", {'Content-Type' => ActiveResource::Formats[format].mime_type}, ryan, 201, {'Location' => "/people/5.#{format}"} assert_equal ActiveResource::Response.new(ryan, 201, {'Location' => "/people/5.#{format}"}), remote_ryan.post(:register) end end end - + def test_setting_format_before_site resource = Class.new(ActiveResource::Base) resource.format = :json resource.site = 'http://37s.sunrise.i:3000' assert_equal ActiveResource::Formats[:json], resource.connection.format end - + private def using_format(klass, mime_type_reference) previous_format = klass.format |