1 require 'abstract_unit'
3 class ConnectionTest
< Test
::Unit::TestCase
4 ResponseCodeStub
= Struct
.new(:code)
7 @conn = ActiveResource
::Connection.new('http://localhost')
8 @matz = { :id => 1, :name => 'Matz' }
9 @david = { :id => 2, :name => 'David' }
10 @people = [ @matz, @david ].to_xml(:root => 'people')
11 @people_single = [ @matz ].to_xml(:root => 'people-single-elements')
12 @people_empty = [ ].to_xml(:root => 'people-empty-elements')
13 @matz = @matz.to_xml(:root => 'person')
14 @david = @david.to_xml(:root => 'person')
15 @header = {'key' => 'value'}.freeze
17 @default_request_headers = { 'Content-Type' => 'application/xml' }
18 ActiveResource
::HttpMock.respond_to
do |mock
|
19 mock
.get
"/people/2.xml", @header, @david
20 mock
.get
"/people.xml", {}, @people
21 mock
.get
"/people_single_elements.xml", {}, @people_single
22 mock
.get
"/people_empty_elements.xml", {}, @people_empty
23 mock
.get
"/people/1.xml", {}, @matz
24 mock
.put
"/people/1.xml", {}, nil, 204
25 mock
.put
"/people/2.xml", {}, @header, 204
26 mock
.delete
"/people/1.xml", {}, nil, 200
27 mock
.delete
"/people/2.xml", @header, nil, 200
28 mock
.post
"/people.xml", {}, nil, 201, 'Location' => '/people/5.xml'
29 mock
.post
"/members.xml", {}, @header, 201, 'Location' => '/people/6.xml'
30 mock
.head
"/people/1.xml", {}, nil, 200
34 def test_handle_response
35 # 2xx and 3xx are valid responses.
36 [200, 299, 300, 399].each
do |code
|
37 expected
= ResponseCodeStub
.new(code
)
38 assert_equal expected
, handle_response(expected
)
41 # 400 is a bad request (e.g. malformed URI or missing request parameter)
42 assert_response_raises ActiveResource
::BadRequest, 400
44 # 401 is an unauthorized request
45 assert_response_raises ActiveResource
::UnauthorizedAccess, 401
47 # 403 is a forbidden requst (and authorizing will not help)
48 assert_response_raises ActiveResource
::ForbiddenAccess, 403
50 # 404 is a missing resource.
51 assert_response_raises ActiveResource
::ResourceNotFound, 404
53 # 405 is a missing not allowed error
54 assert_response_raises ActiveResource
::MethodNotAllowed, 405
56 # 409 is an optimistic locking error
57 assert_response_raises ActiveResource
::ResourceConflict, 409
59 # 422 is a validation error
60 assert_response_raises ActiveResource
::ResourceInvalid, 422
62 # 4xx are client errors.
63 [402, 499].each
do |code
|
64 assert_response_raises ActiveResource
::ClientError, code
67 # 5xx are server errors.
68 [500, 599].each
do |code
|
69 assert_response_raises ActiveResource
::ServerError, code
73 [199, 600].each
do |code
|
74 assert_response_raises ActiveResource
::ConnectionError, code
78 ResponseHeaderStub
= Struct
.new(:code, :message, 'Allow')
79 def test_should_return_allowed_methods_for_method_no_allowed_exception
81 handle_response ResponseHeaderStub
.new(405, "HTTP Failed...", "GET, POST")
82 rescue ActiveResource
::MethodNotAllowed => e
83 assert_equal
"Failed with 405 HTTP Failed...", e
.message
84 assert_equal
[:get, :post], e
.allowed_methods
88 def test_initialize_raises_argument_error_on_missing_site
89 assert_raise(ArgumentError
) { ActiveResource
::Connection.new(nil) }
92 def test_site_accessor_accepts_uri_or_string_argument
93 site
= URI
.parse("http://localhost")
95 assert_raise(URI
::InvalidURIError) { @conn.site
= nil }
97 assert_nothing_raised
{ @conn.site
= "http://localhost" }
98 assert_equal site
, @conn.site
100 assert_nothing_raised
{ @conn.site
= site
}
101 assert_equal site
, @conn.site
104 def test_timeout_accessor
106 assert_equal
5, @conn.timeout
110 matz
= @conn.get("/people/1.xml")
111 assert_equal
"Matz", matz
["name"]
115 response
= @conn.head("/people/1.xml")
116 assert response
.body
.blank
?
117 assert_equal
200, response
.code
120 def test_get_with_header
121 david
= @conn.get("/people/2.xml", @header)
122 assert_equal
"David", david
["name"]
125 def test_get_collection
126 people
= @conn.get("/people.xml")
127 assert_equal
"Matz", people
[0]["name"]
128 assert_equal
"David", people
[1]["name"]
131 def test_get_collection_single
132 people
= @conn.get("/people_single_elements.xml")
133 assert_equal
"Matz", people
[0]["name"]
136 def test_get_collection_empty
137 people
= @conn.get("/people_empty_elements.xml")
138 assert_equal
[], people
142 response
= @conn.post("/people.xml")
143 assert_equal
"/people/5.xml", response
["Location"]
146 def test_post_with_header
147 response
= @conn.post("/members.xml", @header)
148 assert_equal
"/people/6.xml", response
["Location"]
152 response
= @conn.put("/people/1.xml")
153 assert_equal
204, response
.code
156 def test_put_with_header
157 response
= @conn.put("/people/2.xml", @header)
158 assert_equal
204, response
.code
162 response
= @conn.delete("/people/1.xml")
163 assert_equal
200, response
.code
166 def test_delete_with_header
167 response
= @conn.delete("/people/2.xml", @header)
168 assert_equal
200, response
.code
171 uses_mocha('test_timeout, test_accept_http_header') do
173 @http = mock('new Net::HTTP')
174 @conn.expects(:http).returns(@http)
175 @http.expects(:get).raises(Timeout
::Error, 'execution expired')
176 assert_raise(ActiveResource
::TimeoutError) { @conn.get('/people_timeout.xml') }
179 def test_accept_http_header
180 @http = mock('new Net::HTTP')
181 @conn.expects(:http).returns(@http)
182 path
= '/people/1.xml'
183 @http.expects(:get).with(path
, {'Accept' => 'application/xhtml+xml'}).returns(ActiveResource
::Response.new(@matz, 200, {'Content-Type' => 'text/xhtml'}))
184 assert_nothing_raised(Mocha
::ExpectationError) { @conn.get(path
, {'Accept' => 'application/xhtml+xml'}) }
189 def assert_response_raises(klass
, code
)
190 assert_raise(klass
, "Expected response code #{code} to raise #{klass}") do
191 handle_response ResponseCodeStub
.new(code
)
195 def handle_response(response
)
196 @conn.__send__(:handle_response, response
)