Skip to content
Browse files

Recognize and raise an exception on 405 Method Not Allowed responses.…

… Closes #7692.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6864 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 404a357 commit 849038ee514b6ddfb0876c293c5b87e3d960f0bf @jeremy jeremy committed May 26, 2007
View
2 activeresource/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Recognize and raise an exception on 405 Method Not Allowed responses. #7692 [Josh Peek]
+
* Handle string and symbol param keys when splitting params into prefix params and query params.
Comment.find(:all, :params => { :article_id => 5, :page => 2 }) or Comment.find(:all, :params => { 'article_id' => 5, :page => 2 })
View
9 activeresource/lib/active_resource/connection.rb
@@ -24,6 +24,13 @@ class ResourceConflict < ClientError; end # 409 Conflict
class ServerError < ConnectionError; end # 5xx Server Error
+ # 405 Method Not Allowed
+ class MethodNotAllowed < ClientError
+ def allowed_methods
+ @response['Allow'].split(',').map { |verb| verb.strip.downcase.to_sym }
+ end
+ end
+
# Class to handle connections to remote services.
class Connection
attr_reader :site
@@ -99,6 +106,8 @@ def handle_response(response)
response
when 404
raise(ResourceNotFound.new(response))
+ when 405
+ raise(MethodNotAllowed.new(response))
when 409
raise(ResourceConflict.new(response))
when 422
View
13 activeresource/test/connection_test.rb
@@ -41,6 +41,9 @@ def test_handle_response
# 404 is a missing resource.
assert_response_raises ActiveResource::ResourceNotFound, 404
+ # 405 is a missing not allowed error
+ assert_response_raises ActiveResource::MethodNotAllowed, 405
+
# 409 is an optimistic locking error
assert_response_raises ActiveResource::ResourceConflict, 409
@@ -63,6 +66,16 @@ def test_handle_response
end
end
+ ResponseHeaderStub = Struct.new(:code, 'Allow')
+ def test_should_return_allowed_methods_for_method_no_allowed_exception
+ begin
+ handle_response ResponseHeaderStub.new(405, "GET, POST")
+ rescue ActiveResource::MethodNotAllowed => e
+ assert_equal "Failed with 405", e.message
+ assert_equal [:get, :post], e.allowed_methods
+ end
+ end
+
def test_initialize_raises_argument_error_on_missing_site
assert_raise(ArgumentError) { ActiveResource::Connection.new(nil) }
end

0 comments on commit 849038e

Please sign in to comment.
Something went wrong with that request. Please try again.