2006-06-25 14:44:22 +00:00
|
|
|
require "#{File.dirname(__FILE__)}/abstract_unit"
|
2006-09-29 16:25:49 +00:00
|
|
|
require 'base64'
|
2006-06-25 14:44:22 +00:00
|
|
|
|
|
|
|
class ConnectionTest < Test::Unit::TestCase
|
2006-12-05 19:12:51 +00:00
|
|
|
ResponseCodeStub = Struct.new(:code)
|
2006-08-31 09:31:11 +00:00
|
|
|
|
2006-06-25 14:44:22 +00:00
|
|
|
def setup
|
2006-08-31 09:31:11 +00:00
|
|
|
@conn = ActiveResource::Connection.new('http://localhost')
|
2006-12-12 15:29:54 +00:00
|
|
|
@matz = { :id => 1, :name => 'Matz' }
|
|
|
|
@david = { :id => 2, :name => 'David' }
|
|
|
|
@people = [ @matz, @david ].to_xml(:root => 'people')
|
|
|
|
@people_single = [ @matz ].to_xml(:root => 'people-single-elements')
|
|
|
|
@people_empty = [ ].to_xml(:root => 'people-empty-elements')
|
|
|
|
@matz = @matz.to_xml(:root => 'person')
|
|
|
|
@david = @david.to_xml(:root => 'person')
|
2007-04-29 03:14:36 +00:00
|
|
|
@header = {'key' => 'value'}
|
2006-12-12 15:29:54 +00:00
|
|
|
|
2006-09-29 16:25:49 +00:00
|
|
|
@default_request_headers = { 'Content-Type' => 'application/xml' }
|
|
|
|
ActiveResource::HttpMock.respond_to do |mock|
|
2007-04-29 03:14:36 +00:00
|
|
|
mock.get "/people/2.xml", @header, @david
|
2006-12-12 15:29:54 +00:00
|
|
|
mock.get "/people.xml", {}, @people
|
|
|
|
mock.get "/people_single_elements.xml", {}, @people_single
|
|
|
|
mock.get "/people_empty_elements.xml", {}, @people_empty
|
2006-09-29 16:25:49 +00:00
|
|
|
mock.get "/people/1.xml", {}, @matz
|
|
|
|
mock.put "/people/1.xml", {}, nil, 204
|
2007-04-29 03:14:36 +00:00
|
|
|
mock.put "/people/2.xml", {}, @header, 204
|
2006-09-29 16:25:49 +00:00
|
|
|
mock.delete "/people/1.xml", {}, nil, 200
|
2007-04-29 03:14:36 +00:00
|
|
|
mock.delete "/people/2.xml", @header, nil, 200
|
2006-09-29 16:25:49 +00:00
|
|
|
mock.post "/people.xml", {}, nil, 201, 'Location' => '/people/5.xml'
|
2007-04-29 03:14:36 +00:00
|
|
|
mock.post "/members.xml", {}, @header, 201, 'Location' => '/people/6.xml'
|
2006-09-29 16:25:49 +00:00
|
|
|
end
|
2006-06-25 14:44:22 +00:00
|
|
|
end
|
2006-08-31 09:31:11 +00:00
|
|
|
|
|
|
|
def test_handle_response
|
|
|
|
# 2xx and 3xx are valid responses.
|
|
|
|
[200, 299, 300, 399].each do |code|
|
2006-12-05 19:12:51 +00:00
|
|
|
expected = ResponseCodeStub.new(code)
|
|
|
|
assert_equal expected, handle_response(expected)
|
2006-08-31 09:31:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# 404 is a missing resource.
|
|
|
|
assert_response_raises ActiveResource::ResourceNotFound, 404
|
|
|
|
|
2007-05-26 20:57:08 +00:00
|
|
|
# 405 is a missing not allowed error
|
|
|
|
assert_response_raises ActiveResource::MethodNotAllowed, 405
|
|
|
|
|
2006-09-09 00:11:12 +00:00
|
|
|
# 409 is an optimistic locking error
|
|
|
|
assert_response_raises ActiveResource::ResourceConflict, 409
|
|
|
|
|
2007-01-17 00:46:32 +00:00
|
|
|
# 422 is a validation error
|
|
|
|
assert_response_raises ActiveResource::ResourceInvalid, 422
|
|
|
|
|
2006-08-31 09:31:11 +00:00
|
|
|
# 4xx are client errors.
|
2006-09-08 00:07:30 +00:00
|
|
|
[401, 499].each do |code|
|
2006-08-31 09:31:11 +00:00
|
|
|
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
|
2006-06-25 14:44:22 +00:00
|
|
|
end
|
2006-12-05 19:12:51 +00:00
|
|
|
|
2007-08-09 19:22:04 +00:00
|
|
|
ResponseHeaderStub = Struct.new(:code, :message, 'Allow')
|
2007-05-26 20:57:08 +00:00
|
|
|
def test_should_return_allowed_methods_for_method_no_allowed_exception
|
|
|
|
begin
|
2007-08-09 19:22:04 +00:00
|
|
|
handle_response ResponseHeaderStub.new(405, "HTTP Failed...", "GET, POST")
|
2007-05-26 20:57:08 +00:00
|
|
|
rescue ActiveResource::MethodNotAllowed => e
|
2007-08-09 19:22:04 +00:00
|
|
|
assert_equal "Failed with 405 HTTP Failed...", e.message
|
2007-05-26 20:57:08 +00:00
|
|
|
assert_equal [:get, :post], e.allowed_methods
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-12-05 19:12:51 +00:00
|
|
|
def test_initialize_raises_argument_error_on_missing_site
|
|
|
|
assert_raise(ArgumentError) { ActiveResource::Connection.new(nil) }
|
|
|
|
end
|
|
|
|
|
2006-09-29 16:25:49 +00:00
|
|
|
def test_site_accessor_accepts_uri_or_string_argument
|
|
|
|
site = URI.parse("http://localhost")
|
|
|
|
|
2006-12-05 19:12:51 +00:00
|
|
|
assert_raise(URI::InvalidURIError) { @conn.site = nil }
|
|
|
|
|
2006-09-29 16:25:49 +00:00
|
|
|
assert_nothing_raised { @conn.site = "http://localhost" }
|
2006-12-05 19:12:51 +00:00
|
|
|
assert_equal site, @conn.site
|
2006-08-31 09:31:11 +00:00
|
|
|
|
2006-09-29 16:25:49 +00:00
|
|
|
assert_nothing_raised { @conn.site = site }
|
|
|
|
assert_equal site, @conn.site
|
|
|
|
end
|
2006-12-05 19:12:51 +00:00
|
|
|
|
2006-09-29 16:25:49 +00:00
|
|
|
def test_get
|
|
|
|
matz = @conn.get("/people/1.xml")
|
2006-12-12 15:29:54 +00:00
|
|
|
assert_equal "Matz", matz["name"]
|
|
|
|
end
|
|
|
|
|
2007-04-29 03:14:36 +00:00
|
|
|
def test_get_with_header
|
|
|
|
david = @conn.get("/people/2.xml", @header)
|
|
|
|
assert_equal "David", david["name"]
|
|
|
|
end
|
|
|
|
|
2006-12-12 15:29:54 +00:00
|
|
|
def test_get_collection
|
|
|
|
people = @conn.get("/people.xml")
|
|
|
|
assert_equal "Matz", people[0]["name"]
|
|
|
|
assert_equal "David", people[1]["name"]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_get_collection_single
|
|
|
|
people = @conn.get("/people_single_elements.xml")
|
|
|
|
assert_equal "Matz", people[0]["name"]
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_get_collection_empty
|
|
|
|
people = @conn.get("/people_empty_elements.xml")
|
2007-06-21 15:07:15 +00:00
|
|
|
assert_equal [], people
|
2006-09-29 16:25:49 +00:00
|
|
|
end
|
2006-12-05 19:12:51 +00:00
|
|
|
|
2006-09-29 16:25:49 +00:00
|
|
|
def test_post
|
|
|
|
response = @conn.post("/people.xml")
|
|
|
|
assert_equal "/people/5.xml", response["Location"]
|
|
|
|
end
|
2006-12-05 19:12:51 +00:00
|
|
|
|
2007-04-29 03:14:36 +00:00
|
|
|
def test_post_with_header
|
|
|
|
response = @conn.post("/members.xml", @header)
|
|
|
|
assert_equal "/people/6.xml", response["Location"]
|
|
|
|
end
|
|
|
|
|
2006-09-29 16:25:49 +00:00
|
|
|
def test_put
|
|
|
|
response = @conn.put("/people/1.xml")
|
|
|
|
assert_equal 204, response.code
|
|
|
|
end
|
2006-12-05 19:12:51 +00:00
|
|
|
|
2007-04-29 03:14:36 +00:00
|
|
|
def test_put_with_header
|
|
|
|
response = @conn.put("/people/2.xml", @header)
|
|
|
|
assert_equal 204, response.code
|
|
|
|
end
|
|
|
|
|
2006-09-29 16:25:49 +00:00
|
|
|
def test_delete
|
|
|
|
response = @conn.delete("/people/1.xml")
|
|
|
|
assert_equal 200, response.code
|
|
|
|
end
|
2006-12-05 19:12:51 +00:00
|
|
|
|
2007-04-29 03:14:36 +00:00
|
|
|
def test_delete_with_header
|
|
|
|
response = @conn.delete("/people/2.xml", @header)
|
|
|
|
assert_equal 200, response.code
|
|
|
|
end
|
|
|
|
|
2006-08-31 09:31:11 +00:00
|
|
|
protected
|
|
|
|
def assert_response_raises(klass, code)
|
|
|
|
assert_raise(klass, "Expected response code #{code} to raise #{klass}") do
|
2006-12-05 19:12:51 +00:00
|
|
|
handle_response ResponseCodeStub.new(code)
|
2006-08-31 09:31:11 +00:00
|
|
|
end
|
|
|
|
end
|
2006-12-05 19:12:51 +00:00
|
|
|
|
|
|
|
def handle_response(response)
|
|
|
|
@conn.send(:handle_response, response)
|
|
|
|
end
|
2006-08-31 09:31:11 +00:00
|
|
|
end
|