Froze rails gems
[depot.git] / vendor / rails / activeresource / test / connection_test.rb
1 require 'abstract_unit'
2
3 class ConnectionTest < Test::Unit::TestCase
4 ResponseCodeStub = Struct.new(:code)
5
6 def setup
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
16
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
31 end
32 end
33
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)
39 end
40
41 # 400 is a bad request (e.g. malformed URI or missing request parameter)
42 assert_response_raises ActiveResource::BadRequest, 400
43
44 # 401 is an unauthorized request
45 assert_response_raises ActiveResource::UnauthorizedAccess, 401
46
47 # 403 is a forbidden requst (and authorizing will not help)
48 assert_response_raises ActiveResource::ForbiddenAccess, 403
49
50 # 404 is a missing resource.
51 assert_response_raises ActiveResource::ResourceNotFound, 404
52
53 # 405 is a missing not allowed error
54 assert_response_raises ActiveResource::MethodNotAllowed, 405
55
56 # 409 is an optimistic locking error
57 assert_response_raises ActiveResource::ResourceConflict, 409
58
59 # 422 is a validation error
60 assert_response_raises ActiveResource::ResourceInvalid, 422
61
62 # 4xx are client errors.
63 [402, 499].each do |code|
64 assert_response_raises ActiveResource::ClientError, code
65 end
66
67 # 5xx are server errors.
68 [500, 599].each do |code|
69 assert_response_raises ActiveResource::ServerError, code
70 end
71
72 # Others are unknown.
73 [199, 600].each do |code|
74 assert_response_raises ActiveResource::ConnectionError, code
75 end
76 end
77
78 ResponseHeaderStub = Struct.new(:code, :message, 'Allow')
79 def test_should_return_allowed_methods_for_method_no_allowed_exception
80 begin
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
85 end
86 end
87
88 def test_initialize_raises_argument_error_on_missing_site
89 assert_raise(ArgumentError) { ActiveResource::Connection.new(nil) }
90 end
91
92 def test_site_accessor_accepts_uri_or_string_argument
93 site = URI.parse("http://localhost")
94
95 assert_raise(URI::InvalidURIError) { @conn.site = nil }
96
97 assert_nothing_raised { @conn.site = "http://localhost" }
98 assert_equal site, @conn.site
99
100 assert_nothing_raised { @conn.site = site }
101 assert_equal site, @conn.site
102 end
103
104 def test_timeout_accessor
105 @conn.timeout = 5
106 assert_equal 5, @conn.timeout
107 end
108
109 def test_get
110 matz = @conn.get("/people/1.xml")
111 assert_equal "Matz", matz["name"]
112 end
113
114 def test_head
115 response = @conn.head("/people/1.xml")
116 assert response.body.blank?
117 assert_equal 200, response.code
118 end
119
120 def test_get_with_header
121 david = @conn.get("/people/2.xml", @header)
122 assert_equal "David", david["name"]
123 end
124
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"]
129 end
130
131 def test_get_collection_single
132 people = @conn.get("/people_single_elements.xml")
133 assert_equal "Matz", people[0]["name"]
134 end
135
136 def test_get_collection_empty
137 people = @conn.get("/people_empty_elements.xml")
138 assert_equal [], people
139 end
140
141 def test_post
142 response = @conn.post("/people.xml")
143 assert_equal "/people/5.xml", response["Location"]
144 end
145
146 def test_post_with_header
147 response = @conn.post("/members.xml", @header)
148 assert_equal "/people/6.xml", response["Location"]
149 end
150
151 def test_put
152 response = @conn.put("/people/1.xml")
153 assert_equal 204, response.code
154 end
155
156 def test_put_with_header
157 response = @conn.put("/people/2.xml", @header)
158 assert_equal 204, response.code
159 end
160
161 def test_delete
162 response = @conn.delete("/people/1.xml")
163 assert_equal 200, response.code
164 end
165
166 def test_delete_with_header
167 response = @conn.delete("/people/2.xml", @header)
168 assert_equal 200, response.code
169 end
170
171 uses_mocha('test_timeout, test_accept_http_header') do
172 def test_timeout
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') }
177 end
178
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'}) }
185 end
186 end
187
188 protected
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)
192 end
193 end
194
195 def handle_response(response)
196 @conn.__send__(:handle_response, response)
197 end
198 end