aboutsummaryrefslogtreecommitdiffstats
path: root/activeresource/test
diff options
context:
space:
mode:
Diffstat (limited to 'activeresource/test')
-rw-r--r--activeresource/test/abstract_unit.rb7
-rw-r--r--activeresource/test/cases/authorization_test.rb12
-rw-r--r--activeresource/test/cases/base/custom_methods_test.rb2
-rw-r--r--activeresource/test/cases/base_test.rb68
-rw-r--r--activeresource/test/cases/finder_test.rb2
-rw-r--r--activeresource/test/connection_test.rb14
-rw-r--r--activeresource/test/fixtures/address.rb19
-rw-r--r--activeresource/test/fixtures/sound.rb6
-rw-r--r--activeresource/test/fixtures/subscription_plan.rb5
9 files changed, 123 insertions, 12 deletions
diff --git a/activeresource/test/abstract_unit.rb b/activeresource/test/abstract_unit.rb
index 129efeb879..195f93f2a6 100644
--- a/activeresource/test/abstract_unit.rb
+++ b/activeresource/test/abstract_unit.rb
@@ -75,6 +75,10 @@ def setup_response
</person>
eof
+ @startup_sound = {
+ :name => "Mac Startup Sound", :author => { :name => "Jim Reekes" }
+ }.to_xml(:root => 'sound')
+
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/people/1.xml", {}, @matz
mock.get "/people/2.xml", {}, @david
@@ -97,6 +101,7 @@ def setup_response
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'
+ mock.get "/people/1/addresses/99.xml", {}, nil, 404
mock.get "/people//addresses.xml", {}, nil, 404
mock.get "/people//addresses/1.xml", {}, nil, 404
mock.put "/people//addresses/1.xml", {}, nil, 404
@@ -111,6 +116,8 @@ def setup_response
mock.head "/people/Greg/addresses/1.xml", {}, nil, 200
# customer
mock.get "/customers/1.xml", {}, @luis
+ # sound
+ mock.get "/sounds/1.xml", {}, @startup_sound
end
Person.user = nil
diff --git a/activeresource/test/cases/authorization_test.rb b/activeresource/test/cases/authorization_test.rb
index 1a7c9ec8a4..a6797643e1 100644
--- a/activeresource/test/cases/authorization_test.rb
+++ b/activeresource/test/cases/authorization_test.rb
@@ -132,7 +132,7 @@ class AuthorizationTest < Test::Unit::TestCase
end
def test_get
- david = @authenticated_conn.get("/people/2.xml")
+ david = decode(@authenticated_conn.get("/people/2.xml"))
assert_equal "David", david["name"]
end
@@ -159,7 +159,7 @@ class AuthorizationTest < Test::Unit::TestCase
def test_get_with_digest_auth_handles_initial_401_response_and_retries
@authenticated_conn.auth_type = :digest
response = @authenticated_conn.get("/people/2.xml")
- assert_equal "David", response["name"]
+ assert_equal "David", decode(response)["name"]
end
def test_post_with_digest_auth_handles_initial_401_response_and_retries
@@ -190,11 +190,11 @@ class AuthorizationTest < Test::Unit::TestCase
def test_get_with_digest_auth_caches_nonce
@authenticated_conn.auth_type = :digest
response = @authenticated_conn.get("/people/2.xml")
- assert_equal "David", response["name"]
+ assert_equal "David", decode(response)["name"]
# There is no mock for this request with a non-cached nonce.
response = @authenticated_conn.get("/people/1.xml")
- assert_equal "Matz", response["name"]
+ assert_equal "Matz", decode(response)["name"]
end
def test_retry_on_401_only_happens_with_digest_auth
@@ -241,4 +241,8 @@ class AuthorizationTest < Test::Unit::TestCase
def response_digest_auth_header
%Q(Digest realm="RailsTestApp", qop="auth", algorithm=MD5, nonce="#{@nonce}", opaque="ef6dfb078ba22298d366f99567814ffb")
end
+
+ def decode(response)
+ @authenticated_conn.format.decode(response.body)
+ end
end
diff --git a/activeresource/test/cases/base/custom_methods_test.rb b/activeresource/test/cases/base/custom_methods_test.rb
index 459d33c24f..0fbf94bc0e 100644
--- a/activeresource/test/cases/base/custom_methods_test.rb
+++ b/activeresource/test/cases/base/custom_methods_test.rb
@@ -91,7 +91,7 @@ class CustomMethodsTest < Test::Unit::TestCase
201, {'Location' => '/people/1/addresses/2.xml'}),
addy.post(:link)
- matz = Person.new(:id => 1, :name => 'Matz')
+ matz = Person.find(1)
assert_equal ActiveResource::Response.new(@matz, 201), matz.post(:register)
end
diff --git a/activeresource/test/cases/base_test.rb b/activeresource/test/cases/base_test.rb
index b5914683e9..abf4259a54 100644
--- a/activeresource/test/cases/base_test.rb
+++ b/activeresource/test/cases/base_test.rb
@@ -5,6 +5,8 @@ require "fixtures/street_address"
require "fixtures/sound"
require "fixtures/beast"
require "fixtures/proxy"
+require "fixtures/address"
+require "fixtures/subscription_plan"
require 'active_support/json'
require 'active_support/ordered_hash'
require 'active_support/core_ext/hash/conversions'
@@ -473,6 +475,12 @@ class BaseTest < Test::Unit::TestCase
assert_equal '/people/ann%20mary/addresses/ann%20mary.xml', StreetAddress.element_path(:'ann mary', 'person_id' => 'ann mary')
end
+ def test_custom_element_path_without_required_prefix_param
+ assert_raise ActiveResource::MissingPrefixParam do
+ StreetAddress.element_path(1)
+ end
+ end
+
def test_module_element_path
assert_equal '/sounds/1.xml', Asset::Sound.element_path(1)
end
@@ -511,6 +519,12 @@ class BaseTest < Test::Unit::TestCase
assert_equal '/people/1/addresses/1.xml?type=work', StreetAddress.element_path(1, {:person_id => 1}, {:type => 'work'})
end
+ def test_custom_collection_path_without_required_prefix_param
+ assert_raise ActiveResource::MissingPrefixParam do
+ StreetAddress.collection_path
+ end
+ end
+
def test_custom_collection_path
assert_equal '/people/1/addresses.xml', StreetAddress.collection_path(:person_id => 1)
assert_equal '/people/1/addresses.xml', StreetAddress.collection_path('person_id' => 1)
@@ -558,6 +572,8 @@ class BaseTest < Test::Unit::TestCase
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
+ person_class.prefix = "the_prefix/:the_param1/other_prefix/:the_param2"
+ assert_equal Set.new([:the_param2, :the_param1]), person_class.prefix_parameters
end
end
@@ -1034,4 +1050,56 @@ class BaseTest < Test::Unit::TestCase
end
end
end
+
+ def test_with_custom_formatter
+ @addresses = [{:id => "1", :street => "1 Infinite Loop", :city => "Cupertino", :state => "CA"}].to_xml(:root => 'addresses')
+
+ ActiveResource::HttpMock.respond_to do |mock|
+ mock.get "/addresses.xml", {}, @addresses, 200
+ end
+
+ # late bind the site
+ AddressResource.site = "http://localhost"
+ addresses = AddressResource.find(:all)
+
+ assert_equal "Cupertino, CA", addresses.first.city_state
+ end
+
+ def test_create_with_custom_primary_key
+ silver_plan = {:code => "silver", :price => 5.00}.to_xml(:root => "plan")
+
+ ActiveResource::HttpMock.respond_to do |mock|
+ mock.post "/plans.xml", {}, silver_plan, 201, 'Location' => '/plans/silver.xml'
+ end
+
+ plan = SubscriptionPlan.new(:code => "silver", :price => 5.00)
+ assert plan.new?
+
+ plan.save!
+ assert !plan.new?
+ end
+
+ def test_update_with_custom_primary_key
+ silver_plan = {:code => "silver", :price => 5.00}.to_xml(:root => "plan")
+ silver_plan_updated = {:code => "silver", :price => 10.00}.to_xml(:root => "plan")
+
+ ActiveResource::HttpMock.respond_to do |mock|
+ mock.get "/plans/silver.xml", {}, silver_plan
+ mock.put "/plans/silver.xml", {}, silver_plan_updated, 201, 'Location' => '/plans/silver.xml'
+ end
+
+ plan = SubscriptionPlan.find("silver")
+ assert !plan.new?
+ assert_equal 5.00, plan.price
+
+ # update price
+ plan.price = 10.00
+ plan.save!
+ assert_equal 10.00, plan.price
+ end
+
+ def test_namespacing
+ sound = Asset::Sound.find(1)
+ assert_equal "Asset::Sound::Author", sound.author.class.to_s
+ end
end
diff --git a/activeresource/test/cases/finder_test.rb b/activeresource/test/cases/finder_test.rb
index fd09ef46d7..ebb783996d 100644
--- a/activeresource/test/cases/finder_test.rb
+++ b/activeresource/test/cases/finder_test.rb
@@ -84,7 +84,7 @@ class FinderTest < Test::Unit::TestCase
def test_find_by_id_not_found
assert_raise(ActiveResource::ResourceNotFound) { Person.find(99) }
- assert_raise(ActiveResource::ResourceNotFound) { StreetAddress.find(1) }
+ assert_raise(ActiveResource::ResourceNotFound) { StreetAddress.find(99, :params => {:person_id => 1}) }
end
def test_find_all_sub_objects
diff --git a/activeresource/test/connection_test.rb b/activeresource/test/connection_test.rb
index 1b4b61899d..fe80cdf2e5 100644
--- a/activeresource/test/connection_test.rb
+++ b/activeresource/test/connection_test.rb
@@ -120,7 +120,7 @@ class ConnectionTest < Test::Unit::TestCase
end
def test_get
- matz = @conn.get("/people/1.xml")
+ matz = decode(@conn.get("/people/1.xml"))
assert_equal "Matz", matz["name"]
end
@@ -131,23 +131,23 @@ class ConnectionTest < Test::Unit::TestCase
end
def test_get_with_header
- david = @conn.get("/people/2.xml", @header)
+ david = decode(@conn.get("/people/2.xml", @header))
assert_equal "David", david["name"]
end
def test_get_collection
- people = @conn.get("/people.xml")
+ people = decode(@conn.get("/people.xml"))
assert_equal "Matz", people[0]["name"]
assert_equal "David", people[1]["name"]
end
def test_get_collection_single
- people = @conn.get("/people_single_elements.xml")
+ people = decode(@conn.get("/people_single_elements.xml"))
assert_equal "Matz", people[0]["name"]
end
def test_get_collection_empty
- people = @conn.get("/people_empty_elements.xml")
+ people = decode(@conn.get("/people_empty_elements.xml"))
assert_equal [], people
end
@@ -250,4 +250,8 @@ class ConnectionTest < Test::Unit::TestCase
def handle_response(response)
@conn.__send__(:handle_response, response)
end
+
+ def decode(response)
+ @conn.format.decode(response.body)
+ end
end
diff --git a/activeresource/test/fixtures/address.rb b/activeresource/test/fixtures/address.rb
new file mode 100644
index 0000000000..fe921e1595
--- /dev/null
+++ b/activeresource/test/fixtures/address.rb
@@ -0,0 +1,19 @@
+# turns everyting into the same object
+class AddressXMLFormatter
+ include ActiveResource::Formats::XmlFormat
+
+ def decode(xml)
+ data = ActiveResource::Formats::XmlFormat.decode(xml)
+ # process address fields
+ data.each do |address|
+ address['city_state'] = "#{address['city']}, #{address['state']}"
+ end
+ data
+ end
+
+end
+
+class AddressResource < ActiveResource::Base
+ self.element_name = "address"
+ self.format = AddressXMLFormatter.new
+end \ No newline at end of file
diff --git a/activeresource/test/fixtures/sound.rb b/activeresource/test/fixtures/sound.rb
index 5c0ef5d55c..d9d2b99fcd 100644
--- a/activeresource/test/fixtures/sound.rb
+++ b/activeresource/test/fixtures/sound.rb
@@ -1,5 +1,9 @@
-module Asset
+module Asset
class Sound < ActiveResource::Base
self.site = "http://37s.sunrise.i:3000"
end
end
+
+# to test namespacing in a module
+class Author
+end \ No newline at end of file
diff --git a/activeresource/test/fixtures/subscription_plan.rb b/activeresource/test/fixtures/subscription_plan.rb
new file mode 100644
index 0000000000..e3c2dd9a74
--- /dev/null
+++ b/activeresource/test/fixtures/subscription_plan.rb
@@ -0,0 +1,5 @@
+class SubscriptionPlan < ActiveResource::Base
+ self.site = "http://37s.sunrise.i:3000"
+ self.element_name = 'plan'
+ self.primary_key = :code
+end