Skip to content

Commit

Permalink
Active Resource recognizes 410 as Resource Gone now [#2316 state:reso…
Browse files Browse the repository at this point in the history
…lved] [Jordan Brough, Jatinder Singh]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information
Jordan Brough authored and lifo committed Aug 9, 2009
1 parent 1af9bd5 commit 916b18a
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 1 deletion.
2 changes: 2 additions & 0 deletions activeresource/CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
*Edge*

* Recognizes 410 as Resource Gone. #2316 [Jordan Brough, Jatinder Singh]

* More thorough SSL support. #2370 [Roy Nicholson]

* HTTP proxy support. #2133 [Marshall Huss, Sébastien Dabet]
Expand Down
3 changes: 2 additions & 1 deletion activeresource/lib/active_resource/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ module ActiveResource
# * 404 - ActiveResource::ResourceNotFound
# * 405 - ActiveResource::MethodNotAllowed
# * 409 - ActiveResource::ResourceConflict
# * 410 - ActiveResource::ResourceGone
# * 422 - ActiveResource::ResourceInvalid (rescued by save as validation errors)
# * 401..499 - ActiveResource::ClientError
# * 500..599 - ActiveResource::ServerError
Expand Down Expand Up @@ -626,7 +627,7 @@ def exists?(id, options = {})
response.code.to_i == 200
end
# id && !find_single(id, options).nil?
rescue ActiveResource::ResourceNotFound
rescue ActiveResource::ResourceNotFound, ActiveResource::ResourceGone
false
end

Expand Down
2 changes: 2 additions & 0 deletions activeresource/lib/active_resource/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ def handle_response(response)
raise(MethodNotAllowed.new(response))
when 409
raise(ResourceConflict.new(response))
when 410
raise(ResourceGone.new(response))
when 422
raise(ResourceInvalid.new(response))
when 401...500
Expand Down
3 changes: 3 additions & 0 deletions activeresource/lib/active_resource/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class ResourceNotFound < ClientError; end # :nodoc:
# 409 Conflict
class ResourceConflict < ClientError; end # :nodoc:

# 410 Gone
class ResourceGone < ClientError; end # :nodoc:

# 5xx Server Error
class ServerError < ConnectionError; end # :nodoc:

Expand Down
24 changes: 24 additions & 0 deletions activeresource/test/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,14 @@ def test_destroy_with_custom_prefix
assert_raise(ActiveResource::ResourceNotFound) { StreetAddress.find(1, :params => { :person_id => 1 }) }
end

def test_destroy_with_410_gone
assert Person.find(1).destroy
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/people/1.xml", {}, nil, 410
end
assert_raise(ActiveResource::ResourceGone) { Person.find(1).destroy }
end

def test_delete
assert Person.delete(1)
ActiveResource::HttpMock.respond_to do |mock|
Expand All @@ -914,6 +922,14 @@ def test_delete_with_custom_prefix
end
assert_raise(ActiveResource::ResourceNotFound) { StreetAddress.find(1, :params => { :person_id => 1 }) }
end

def test_delete_with_410_gone
assert Person.delete(1)
ActiveResource::HttpMock.respond_to do |mock|
mock.get "/people/1.xml", {}, nil, 410
end
assert_raise(ActiveResource::ResourceGone) { Person.find(1) }
end

def test_exists
# Class method.
Expand Down Expand Up @@ -974,6 +990,14 @@ def test_exists_without_http_mock
assert Person.exists?('not-mocked')
end

def test_exists_with_410_gone
ActiveResource::HttpMock.respond_to do |mock|
mock.head "/people/1.xml", {}, nil, 410
end

assert !Person.exists?(1)
end

def test_to_xml
matz = Person.find(1)
xml = matz.encode
Expand Down
3 changes: 3 additions & 0 deletions activeresource/test/connection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ def test_handle_response
# 409 is an optimistic locking error
assert_response_raises ActiveResource::ResourceConflict, 409

# 410 is a removed resource
assert_response_raises ActiveResource::ResourceGone, 410

# 422 is a validation error
assert_response_raises ActiveResource::ResourceInvalid, 422

Expand Down

0 comments on commit 916b18a

Please sign in to comment.