Permalink
Browse files

200...400 are valid response codes. PUT and POST request bodies defau…

…lt to ''.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4887 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
jeremy committed Aug 31, 2006
1 parent c1af2db commit f9b369487046020bcdb92a3985270c9fb9943382
Showing with 54 additions and 21 deletions.
  1. +1 −1 activeresource/CHANGELOG
  2. +17 −15 activeresource/lib/active_resource/connection.rb
  3. +36 −5 activeresource/test/connection_test.rb
View
@@ -1,5 +1,5 @@
*SVN*
-* site= accepts URIs. [Jeremy Kemper]
+* Base.site= accepts URIs. 200...400 are valid response codes. PUT and POST request bodies default to ''. [Jeremy Kemper]
* Initial checkin: object-oriented client for restful HTTP resources which follow the Rails convention. [DHH]
@@ -11,24 +11,24 @@ def initialize(response, message = nil)
@response = response
@message = message
end
-
+
def to_s
"Failed with #{response.code}"
end
end
-
+
class ClientError < ConnectionError
end
class ServerError < ConnectionError
end
-
+
class ResourceNotFound < ClientError
end
class Connection
- attr_accessor :uri
-
+ attr_accessor :site
+
class << self
def requests
@@requests ||= []
@@ -38,29 +38,31 @@ def requests
def initialize(site)
@site = site
end
-
+
def get(path)
Hash.create_from_xml(request(:get, path).body)
end
-
+
def delete(path)
request(:delete, path)
end
-
- def put(path, body)
+
+ def put(path, body = '')
request(:put, path, body)
end
- def post(path, body)
+ def post(path, body = '')
request(:post, path, body)
end
-
+
private
def request(method, *arguments)
- response = http.send(method, *arguments)
+ handle_response(http.send(method, *arguments))
+ end
+ def handle_response(response)
case response.code.to_i
- when 200...300
+ when 200...400
response
when 404
raise(ResourceNotFound.new(response))
@@ -79,8 +81,8 @@ def http
@http.use_ssl = @site.is_a?(URI::HTTPS)
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE if @http.use_ssl
end
-
+
@http
end
end
-end
+end
@@ -1,11 +1,42 @@
require "#{File.dirname(__FILE__)}/abstract_unit"
-require "fixtures/person"
class ConnectionTest < Test::Unit::TestCase
+ Response = Struct.new(:code)
+
def setup
+ @conn = ActiveResource::Connection.new('http://localhost')
end
-
- def test_something
- true
+
+ def test_handle_response
+ # 2xx and 3xx are valid responses.
+ [200, 299, 300, 399].each do |code|
+ expected = Response.new(code)
+ assert_equal expected, @conn.send(:handle_response, expected)
+ end
+
+ # 404 is a missing resource.
+ assert_response_raises ActiveResource::ResourceNotFound, 404
+
+ # 4xx are client errors.
+ [400, 499].each do |code|
+ assert_response_raises ActiveResource::ClientError, code
+ end
+
+ # 5xx are server errors.
+ [500, 599].each do |code|
+ assert_response_raises ActiveResource::ServerError, code
+ end
+
+ # Others are unknown.
+ [199, 600].each do |code|
+ assert_response_raises ActiveResource::ConnectionError, code
+ end
end
-end
+
+ protected
+ def assert_response_raises(klass, code)
+ assert_raise(klass, "Expected response code #{code} to raise #{klass}") do
+ @conn.send(:handle_response, Response.new(code))
+ end
+ end
+end

0 comments on commit f9b3694

Please sign in to comment.