Permalink
Browse files

Raise ActiveResource::Redirection on 301,302 http code

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7297 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent d4bf5e9 commit 18a4cc1bd2d260c6905b58dee61e7b3ac88f0a6b Tobias Lütke committed Aug 9, 2007
Showing with 11 additions and 4 deletions.
  1. +8 −1 activeresource/lib/active_resource/connection.rb
  2. +3 −3 activeresource/test/connection_test.rb
@@ -14,10 +14,15 @@ def initialize(response, message = nil)
end
def to_s
- "Failed with #{response.code}"
+ "Failed with #{response.code} #{response.message if response.respond_to?(:message)}"
end
end
+ # 3xx Redirection
+ class Redirection < ConnectionError # :nodoc:
+ def to_s; response['Location'] ? "#{super} => #{response['Location']}" : super; end
+ end
+
# 4xx Client Error
class ClientError < ConnectionError; end # :nodoc:
@@ -107,6 +112,8 @@ def request(method, path, *arguments)
# Handles response and error codes from remote service.
def handle_response(response)
case response.code.to_i
+ when 301,302
+ raise(Redirection.new(response))
when 200...400
response
when 404
@@ -66,12 +66,12 @@ def test_handle_response
end
end
- ResponseHeaderStub = Struct.new(:code, 'Allow')
+ ResponseHeaderStub = Struct.new(:code, :message, 'Allow')
def test_should_return_allowed_methods_for_method_no_allowed_exception
begin
- handle_response ResponseHeaderStub.new(405, "GET, POST")
+ handle_response ResponseHeaderStub.new(405, "HTTP Failed...", "GET, POST")
rescue ActiveResource::MethodNotAllowed => e
- assert_equal "Failed with 405", e.message
+ assert_equal "Failed with 405 HTTP Failed...", e.message
assert_equal [:get, :post], e.allowed_methods
end
end

0 comments on commit 18a4cc1

Please sign in to comment.