activeresource_csi 2.3.5.p6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,238 @@
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
+ # 410 is a removed resource
60
+ assert_response_raises ActiveResource::ResourceGone, 410
61
+
62
+ # 422 is a validation error
63
+ assert_response_raises ActiveResource::ResourceInvalid, 422
64
+
65
+ # 4xx are client errors.
66
+ [402, 499].each do |code|
67
+ assert_response_raises ActiveResource::ClientError, code
68
+ end
69
+
70
+ # 5xx are server errors.
71
+ [500, 599].each do |code|
72
+ assert_response_raises ActiveResource::ServerError, code
73
+ end
74
+
75
+ # Others are unknown.
76
+ [199, 600].each do |code|
77
+ assert_response_raises ActiveResource::ConnectionError, code
78
+ end
79
+ end
80
+
81
+ ResponseHeaderStub = Struct.new(:code, :message, 'Allow')
82
+ def test_should_return_allowed_methods_for_method_no_allowed_exception
83
+ begin
84
+ handle_response ResponseHeaderStub.new(405, "HTTP Failed...", "GET, POST")
85
+ rescue ActiveResource::MethodNotAllowed => e
86
+ assert_equal "Failed with 405 HTTP Failed...", e.message
87
+ assert_equal [:get, :post], e.allowed_methods
88
+ end
89
+ end
90
+
91
+ def test_initialize_raises_argument_error_on_missing_site
92
+ assert_raise(ArgumentError) { ActiveResource::Connection.new(nil) }
93
+ end
94
+
95
+ def test_site_accessor_accepts_uri_or_string_argument
96
+ site = URI.parse("http://localhost")
97
+
98
+ assert_raise(URI::InvalidURIError) { @conn.site = nil }
99
+
100
+ assert_nothing_raised { @conn.site = "http://localhost" }
101
+ assert_equal site, @conn.site
102
+
103
+ assert_nothing_raised { @conn.site = site }
104
+ assert_equal site, @conn.site
105
+ end
106
+
107
+ def test_proxy_accessor_accepts_uri_or_string_argument
108
+ proxy = URI.parse("http://proxy_user:proxy_password@proxy.local:4242")
109
+
110
+ assert_nothing_raised { @conn.proxy = "http://proxy_user:proxy_password@proxy.local:4242" }
111
+ assert_equal proxy, @conn.proxy
112
+
113
+ assert_nothing_raised { @conn.proxy = proxy }
114
+ assert_equal proxy, @conn.proxy
115
+ end
116
+
117
+ def test_timeout_accessor
118
+ @conn.timeout = 5
119
+ assert_equal 5, @conn.timeout
120
+ end
121
+
122
+ def test_get
123
+ matz = @conn.get("/people/1.xml")
124
+ assert_equal "Matz", matz["name"]
125
+ end
126
+
127
+ def test_head
128
+ response = @conn.head("/people/1.xml")
129
+ assert response.body.blank?
130
+ assert_equal 200, response.code
131
+ end
132
+
133
+ def test_get_with_header
134
+ david = @conn.get("/people/2.xml", @header)
135
+ assert_equal "David", david["name"]
136
+ end
137
+
138
+ def test_get_collection
139
+ people = @conn.get("/people.xml")
140
+ assert_equal "Matz", people[0]["name"]
141
+ assert_equal "David", people[1]["name"]
142
+ end
143
+
144
+ def test_get_collection_single
145
+ people = @conn.get("/people_single_elements.xml")
146
+ assert_equal "Matz", people[0]["name"]
147
+ end
148
+
149
+ def test_get_collection_empty
150
+ people = @conn.get("/people_empty_elements.xml")
151
+ assert_equal [], people
152
+ end
153
+
154
+ def test_post
155
+ response = @conn.post("/people.xml")
156
+ assert_equal "/people/5.xml", response["Location"]
157
+ end
158
+
159
+ def test_post_with_header
160
+ response = @conn.post("/members.xml", @header)
161
+ assert_equal "/people/6.xml", response["Location"]
162
+ end
163
+
164
+ def test_put
165
+ response = @conn.put("/people/1.xml")
166
+ assert_equal 204, response.code
167
+ end
168
+
169
+ def test_put_with_header
170
+ response = @conn.put("/people/2.xml", @header)
171
+ assert_equal 204, response.code
172
+ end
173
+
174
+ def test_delete
175
+ response = @conn.delete("/people/1.xml")
176
+ assert_equal 200, response.code
177
+ end
178
+
179
+ def test_delete_with_header
180
+ response = @conn.delete("/people/2.xml", @header)
181
+ assert_equal 200, response.code
182
+ end
183
+
184
+ def test_timeout
185
+ @http = mock('new Net::HTTP')
186
+ @conn.expects(:http).returns(@http)
187
+ @http.expects(:get).raises(Timeout::Error, 'execution expired')
188
+ assert_raise(ActiveResource::TimeoutError) { @conn.get('/people_timeout.xml') }
189
+ end
190
+
191
+ def test_setting_timeout
192
+ http = Net::HTTP.new('')
193
+
194
+ [10, 20].each do |timeout|
195
+ @conn.timeout = timeout
196
+ @conn.send(:configure_http, http)
197
+ assert_equal timeout, http.open_timeout
198
+ assert_equal timeout, http.read_timeout
199
+ end
200
+ end
201
+
202
+ def test_accept_http_header
203
+ @http = mock('new Net::HTTP')
204
+ @conn.expects(:http).returns(@http)
205
+ path = '/people/1.xml'
206
+ @http.expects(:get).with(path, {'Accept' => 'application/xhtml+xml'}).returns(ActiveResource::Response.new(@matz, 200, {'Content-Type' => 'text/xhtml'}))
207
+ assert_nothing_raised(Mocha::ExpectationError) { @conn.get(path, {'Accept' => 'application/xhtml+xml'}) }
208
+ end
209
+
210
+ def test_ssl_options_get_applied_to_http
211
+ http = Net::HTTP.new('')
212
+ @conn.site="https://secure"
213
+ @conn.ssl_options={:verify_mode => OpenSSL::SSL::VERIFY_PEER}
214
+ @conn.timeout = 10 # prevent warning about uninitialized.
215
+ @conn.send(:configure_http, http)
216
+
217
+ assert http.use_ssl?
218
+ assert_equal http.verify_mode, OpenSSL::SSL::VERIFY_PEER
219
+ end
220
+
221
+ def test_ssl_error
222
+ http = Net::HTTP.new('')
223
+ @conn.expects(:http).returns(http)
224
+ http.expects(:get).raises(OpenSSL::SSL::SSLError, 'Expired certificate')
225
+ assert_raise(ActiveResource::SSLError) { @conn.get('/people/1.xml') }
226
+ end
227
+
228
+ protected
229
+ def assert_response_raises(klass, code)
230
+ assert_raise(klass, "Expected response code #{code} to raise #{klass}") do
231
+ handle_response ResponseCodeStub.new(code)
232
+ end
233
+ end
234
+
235
+ def handle_response(response)
236
+ @conn.__send__(:handle_response, response)
237
+ end
238
+ end