aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activeresource/CHANGELOG2
-rw-r--r--activeresource/lib/active_resource/connection.rb9
-rw-r--r--activeresource/test/connection_test.rb13
3 files changed, 24 insertions, 0 deletions
diff --git a/activeresource/CHANGELOG b/activeresource/CHANGELOG
index cd1cb557c9..fcef27535e 100644
--- a/activeresource/CHANGELOG
+++ b/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 })
diff --git a/activeresource/lib/active_resource/connection.rb b/activeresource/lib/active_resource/connection.rb
index bb3993512e..ef4d92451c 100644
--- a/activeresource/lib/active_resource/connection.rb
+++ b/activeresource/lib/active_resource/connection.rb
@@ -24,6 +24,13 @@ module ActiveResource
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 @@ module ActiveResource
response
when 404
raise(ResourceNotFound.new(response))
+ when 405
+ raise(MethodNotAllowed.new(response))
when 409
raise(ResourceConflict.new(response))
when 422
diff --git a/activeresource/test/connection_test.rb b/activeresource/test/connection_test.rb
index 67d5553e72..13b518d782 100644
--- a/activeresource/test/connection_test.rb
+++ b/activeresource/test/connection_test.rb
@@ -41,6 +41,9 @@ class ConnectionTest < Test::Unit::TestCase
# 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 @@ class ConnectionTest < Test::Unit::TestCase
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