Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fixed failing tests now that non-GET requests are sent with .xml file…

… ext. Extracted #id_from_response as an entry point for customizing how a created resource gets its own ID.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5153 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 4d63e01fa052749f742e90bcb3889cbae6152c92 1 parent 5e5b87b
risk danger olson technoweenie authored
3  activeresource/CHANGELOG
View
@@ -1,5 +1,8 @@
*SVN*
+* Extracted #id_from_response as an entry point for customizing how a created resource gets its own ID.
+ By default, it extracts from the Location response header.
+
* Optimistic locking: raise ActiveResource::ResourceConflict on 409 Conflict response. [Jeremy Kemper]
# Example controller action
7 activeresource/lib/active_resource/base.rb
View
@@ -164,10 +164,15 @@ def update
def create
returning connection.post(self.class.collection_path(prefix_options), to_xml) do |resp|
- self.id = resp['Location'][/\/([^\/]*?)(\.\w+)?$/, 1]
+ self.id = id_from_response(resp)
end
end
+ # takes a response from a typical create post and pulls the ID out
+ def id_from_response(response)
+ response['Location'][/\/([^\/]*?)(\.\w+)?$/, 1]
+ end
+
private
def find_or_create_resource_for_collection(name)
find_or_create_resource_for(name.to_s.singularize)
2  activeresource/test/base_errors_test.rb
View
@@ -4,7 +4,7 @@
class BaseErrorsTest < Test::Unit::TestCase
def setup
ActiveResource::HttpMock.respond_to do |mock|
- mock.post "/people", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><errors><error>Age can't be blank</error><error>Name can't be blank</error><error>Name must start with a letter</error><error>Person quota full for today.</error></errors>", 400
+ mock.post "/people.xml", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><errors><error>Age can't be blank</error><error>Name can't be blank</error><error>Name must start with a letter</error><error>Person quota full for today.</error></errors>", 400
end
@exception = nil
@person = Person.new(:name => '', :age => '')
31 activeresource/test/base_test.rb
View
@@ -10,22 +10,22 @@ def setup
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/people/1.xml", @matz
mock.get "/people/2.xml", @david
- mock.put "/people/1", nil, 204
- mock.delete "/people/1", nil, 200
- mock.delete "/people/2", nil, 400
- mock.post "/people", nil, 201, 'Location' => '/people/5.xml'
+ mock.put "/people/1.xml", nil, 204
+ mock.delete "/people/1.xml", nil, 200
+ mock.delete "/people/2.xml", nil, 400
+ mock.post "/people.xml", nil, 201, 'Location' => '/people/5.xml'
mock.get "/people/99.xml", nil, 404
mock.get "/people.xml", "<people>#{@matz}#{@david}</people>"
mock.get "/people/1/addresses.xml", "<addresses>#{@addy}</addresses>"
mock.get "/people/1/addresses/1.xml", @addy
- mock.put "/people/1/addresses/1", nil, 204
- mock.delete "/people/1/addresses/1", nil, 200
- mock.post "/people/1/addresses", nil, 201, 'Location' => '/people/1/addresses/5'
+ 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//addresses.xml", nil, 404
mock.get "/people//addresses/1.xml", nil, 404
- mock.put "/people//addresses/1", nil, 404
- mock.delete "/people//addresses/1", nil, 404
- mock.post "/people//addresses", nil, 404
+ mock.put "/people//addresses/1.xml", nil, 404
+ mock.delete "/people//addresses/1.xml", nil, 404
+ mock.post "/people//addresses.xml", nil, 404
end
end
@@ -111,6 +111,15 @@ def test_create
assert_equal '5', rick.id
end
+ def test_id_from_response
+ p = Person.new
+ resp = {'Location' => '/foo/bar/1'}
+ assert_equal '1', p.send(:id_from_response, resp)
+
+ resp['Location'] << '.xml'
+ assert_equal '1', p.send(:id_from_response, resp)
+ end
+
def test_create_with_custom_prefix
matzs_house = StreetAddress.new({}, {:person_id => 1})
matzs_house.save
@@ -136,7 +145,7 @@ def test_update_with_custom_prefix
def test_update_conflict
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/people/2.xml", @david
- mock.put "/people/2", nil, 409
+ mock.put "/people/2.xml", nil, 409
end
assert_raises(ActiveResource::ResourceConflict) { Person.find(2).save }
end
Please sign in to comment.
Something went wrong with that request. Please try again.