aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activeresource/CHANGELOG2
-rw-r--r--activeresource/lib/active_resource/connection.rb32
-rw-r--r--activeresource/test/connection_test.rb41
3 files changed, 54 insertions, 21 deletions
diff --git a/activeresource/CHANGELOG b/activeresource/CHANGELOG
index 8ac17c24a1..4b870e5db7 100644
--- a/activeresource/CHANGELOG
+++ b/activeresource/CHANGELOG
@@ -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]
diff --git a/activeresource/lib/active_resource/connection.rb b/activeresource/lib/active_resource/connection.rb
index 7318e7256f..cc98645fdc 100644
--- a/activeresource/lib/active_resource/connection.rb
+++ b/activeresource/lib/active_resource/connection.rb
@@ -11,24 +11,24 @@ module ActiveResource
@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 @@ module ActiveResource
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 @@ module ActiveResource
@http.use_ssl = @site.is_a?(URI::HTTPS)
@http.verify_mode = OpenSSL::SSL::VERIFY_NONE if @http.use_ssl
end
-
+
@http
end
end
-end \ No newline at end of file
+end
diff --git a/activeresource/test/connection_test.rb b/activeresource/test/connection_test.rb
index 1c5951ca91..4790c30bed 100644
--- a/activeresource/test/connection_test.rb
+++ b/activeresource/test/connection_test.rb
@@ -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 \ No newline at end of file
+
+ 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