Skip to content

Commit

Permalink
fixed failing tests now that non-GET requests are sent with .xml file…
Browse files Browse the repository at this point in the history
… 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
technoweenie committed Sep 20, 2006
1 parent 5e5b87b commit 4d63e01
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
3 changes: 3 additions & 0 deletions activeresource/CHANGELOG
@@ -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
Expand Down
7 changes: 6 additions & 1 deletion activeresource/lib/active_resource/base.rb
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion activeresource/test/base_errors_test.rb
Expand Up @@ -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 => '')
Expand Down
31 changes: 20 additions & 11 deletions activeresource/test/base_test.rb
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 4d63e01

Please sign in to comment.