/
adapter_spec.rb
367 lines (280 loc) · 13.9 KB
/
adapter_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
context Restfulie::Client::HTTP do
context "HTTP Base" do
before(:all) do
@host = "http://localhost:4567"
@client = Restfulie.at(@host)
end
it "should get and respond 200 code" do
@client.at("/test").get.response.code.should == 200
end
it "should put and respond 200 code" do
@client.at("/test").as("text/plain").put("<test></test>").response.code.should == 200
end
it "should delete and respond 200 code" do
@client.at("/test").delete.response.code.should == 200
end
it "should head and respond 200 code" do
@client.at("/test").head.response.code.should == 200
end
it "should get! and respond 200 code" do
@client.at("/test").get!.response.code.should == 200
end
it "should post! and respond 201 code" do
Restfulie.using{
recipe
request_marshaller
verb_request
}.at(@host).at("/test/redirect/songs").as("application/xml").post!("<test></text>").should respond_with_status(201)
end
it "should put! and respond 200 code" do
@client.at("/test").as("application/xml").put!("<test></test>").response.code.should == 200
end
it "should delete! and respond 200 code" do
@client.at("/test").delete!.response.code.should == 200
end
it "should head! and respond 200 code" do
@client.at("/test").head!.response.code.should == 200
end
end
context 'HTTP Builder' do
let(:builder) { ::Restfulie::Client::HTTP::RequestBuilderExecutor.new("http://localhost:4567") }
context "On GET" do
it "should respond to 200 code" do
builder.at('/test/200').get.should respond_with_status(200)
end
it "should accepts and respond to 200 code on xml" do
builder.at('/test/200').accepts('application/xml').get.should respond_with_status(200)
end
it "should respond to 200 code as xml" do
builder.at('/test/200').as('application/xml').get.should respond_with_status(200)
end
it "should accept language and respond 200" do
builder.at('/test/200').with('Accept-Language' => 'en').get.should respond_with_status(200)
end
it "should respond 200 code as xml and accept atom and language" do
builder.at('/test/200').as('application/xml').accepts('application/atom+xml').with('Accept-Language' => 'en').get.should respond_with_status(200)
end
end
context "On PUT" do
it "should respond to 200 code" do
builder.at("/test").put("test").should respond_with_status(200)
end
it "should accepts xml and respond to 200 code" do
builder.at('/test').accepts('application/xml').put("test").should respond_with_status(200)
end
it "should respond to 200 code as xml" do
builder.at('/test').as('application/xml').put("test").should respond_with_status(200)
end
it "should include accept language and respond 200" do
builder.at('/test').with('Accept-Language' => 'en').put("test").should respond_with_status(200)
end
it "should respond 200 code as xml and accepts xml with en language" do
builder.at('/test').as('application/xml').accepts('application/atom+xml').with('Accept-Language' => 'en').put("test").should respond_with_status(200)
end
it "should respond 200 code as xml and accept atom and language" do
builder.at('/test').as('application/xml').accepts('application/atom+xml').with('Accept-Language' => 'en').put!("test").should respond_with_status(200)
end
end
context "On DELETE" do
it "should respond 200 code" do
builder.at("/test").delete.should respond_with_status(200)
end
it "should accepts xml and respond 200 code" do
builder.at('/test').accepts('application/xml').delete.should respond_with_status(200)
end
it "as xml should respond 200 code" do
builder.at('/test').as('application/xml').delete.should respond_with_status(200)
end
it "should respond 200 code with accept language en" do
builder.at('/test').with('Accept-Language' => 'en').delete.should respond_with_status(200)
end
it "should respond 200 code as xml accepts atom+xml with accept language en" do
builder.at('/test').as('application/xml').accepts('application/atom+xml').with('Accept-Language' => 'en').delete.should respond_with_status(200)
end
end
context "On HEAD" do
it "should respond 200 code" do
builder.at("/test").head.should respond_with_status(200)
end
it "should respond 200 code and accepts xml" do
builder.at('/test').accepts('application/xml').head.should respond_with_status(200)
end
it "should respond 200 code as xml" do
builder.at('/test').as('application/xml').head.should respond_with_status(200)
end
it "should respond 200 code with accept language en" do
builder.at('/test').with('Accept-Language' => 'en').head.should respond_with_status(200)
end
it "should respond 200 code as xml and accepts atom+xml with accepts language en" do
builder.at('/test').as('application/xml').accepts('application/atom+xml').with('Accept-Language' => 'en').head.should respond_with_status(200)
end
it "should respond 200 code as xml and accepts atom+xml with accepts language en in a destructive method" do
builder.at('/test').as('application/xml').accepts('application/atom+xml').with('Accept-Language' => 'en').head!.should respond_with_status(200)
end
end
end
context 'RequestHistory' do
before(:all) do
@host = "http://localhost:4567"
@builder = ::Restfulie::Client::HTTP::RequestHistoryExecutor.new(@host)
end
it "should remember last requests" do
@builder.at('/test').accepts('application/atom+xml').with('Accept-Language' => 'en').get.should respond_with_status(200)
@builder.at('/test').accepts('text/html').with('Accept-Language' => 'pt-BR').head.should respond_with_status(200)
@builder.at('/test').as('application/xml').with('Accept-Language' => 'en').post!('test').should respond_with_status(201)
@builder.at('/test/500').accepts('application/xml').with('Accept-Language' => 'en').get.should respond_with_status(500)
@builder.history(0).request.should respond_with_status(200)
@builder.path.should == '/test'
@builder.host.to_s.should == @host + "/test"
@builder.headers['Accept'].should == 'application/atom+xml'
@builder.headers['Accept-Language'].should == 'en'
@builder.history(1).request.should respond_with_status(200)
@builder.path.should == '/test'
@builder.host.to_s.should == @host + "/test"
@builder.headers['Accept'].should == 'text/html'
@builder.headers['Accept-Language'].should == 'pt-BR'
@builder.history(2).request.should respond_with_status(201)
@builder.path.should == '/test'
@builder.host.to_s.should == @host + "/test"
@builder.headers['Accept'].should == 'application/xml'
@builder.headers['Accept-Language'].should == 'en'
@builder.headers['Content-Type'].should == 'application/xml'
@builder.history(3).request.should respond_with_status(500)
@builder.path.should == '/test/500'
@builder.host.to_s.should == @host + "/test/500"
@builder.headers['Accept'].should == 'application/xml'
@builder.headers['Accept-Language'].should == 'en'
lambda { @builder.history(10).request }.should raise_error RuntimeError
end
end
context 'Response Handler' do
class FakeResponse < ::Restfulie::Client::HTTP::Response
end
::Restfulie::Client::HTTP::ResponseHandler.register(307,FakeResponse)
let(:client) { Restfulie.at("http://localhost:4567") }
it 'should have FakeResponder as Response Handler to 307' do
::Restfulie::Client::HTTP::ResponseHandler.handlers(307).should equal FakeResponse
end
it 'should respond FakeResponse' do
client.at('/test/307').get.response.should be_kind_of FakeResponse
end
it 'should respond default Response' do
client.at('/test/299').get.response.should be_kind_of ::Restfulie::Client::HTTP::Response
end
end
context "redirection" do
let(:resp) {
Restfulie.at("http://localhost:4567/test_redirection").follow.get!
}
it "should follow redirection" do
resp.response.path.should == "/redirected"
end
it "should set the body as 'OK'" do
resp.response.body.should == "OK"
end
end
context "error conditions" do
before do
@host = "http://localhost:4567"
@client = Restfulie.at(@host)
end
it "receives error when 300..399 code is returned" do
@client.at("/test/302").get.should respond_with_status(302)
end
it "raise Error::Redirection error when 300..399 code is returned" do
lambda { @client.at("/test/302").get! }.should raise_exception ::Restfulie::Client::HTTP::Error::Redirection
end
it "raise Error::BadRequest error when 400 code is returned" do
@client.at("/test/400").get.should respond_with_status(400)
end
it "receives error when 400 code is returned" do
lambda { @client.at("/test/400").get! }.should raise_exception ::Restfulie::Client::HTTP::Error::BadRequest
end
it "raise Error::Unauthorized error when 401 code is returned" do
@client.at("/test/401").get.should respond_with_status(401)
end
it "receives error when 401 code is returned" do
lambda { @client.at("/test/401").get! }.should raise_exception ::Restfulie::Client::HTTP::Error::Unauthorized
end
it "raise Error::Forbidden error when 403 code is returned" do
@client.at("/test/403").get.should respond_with_status(403)
end
it "receives error when 403 code is returned" do
lambda { @client.at!("/test/403").get! }.should raise_exception ::Restfulie::Client::HTTP::Error::Forbidden
end
it "raise Error::NotFound error when 404 code is returned" do
@client.at("/test/404").get.should respond_with_status(404)
end
it "receives error when 404 code is returned" do
lambda { @client.at("/test/404").get! }.should raise_exception ::Restfulie::Client::HTTP::Error::NotFound
end
it "raise Error::MethodNotAllowed error when 405 code is returned" do
@client.at("/test/405").get.should respond_with_status(405)
end
it "receives error when 405 code is returned" do
lambda { @client.at("/test/405").get! }.should raise_exception ::Restfulie::Client::HTTP::Error::MethodNotAllowed
end
it "raise Error::ProxyAuthenticationRequired error when 407 code is returned" do
@client.at("/test/407").get.should respond_with_status(407)
end
it "receives error when 407 code is returned" do
lambda { @client.at("/test/407").get! }.should raise_exception ::Restfulie::Client::HTTP::Error::ProxyAuthenticationRequired
end
it "receives error when 409 code is returned" do
@client.at("/test/409").get.should respond_with_status(409)
end
it "raise Error::Conflict error when 409 code is returned" do
lambda { @client.at("/test/409").get! }.should raise_exception ::Restfulie::Client::HTTP::Error::Conflict
end
it "raise Error::Gone error when 410 code is returned" do
@client.at("/test/410").get.should respond_with_status(410)
end
it "receives error when 410 code is returned" do
lambda { @client.at("/test/410").get! }.should raise_exception ::Restfulie::Client::HTTP::Error::Gone
end
it "raise Error::PreconditionFailed error when 412 code is returned" do
@client.at("/test/412").get.should respond_with_status(412)
end
it "receives error when 412 code is returned" do
lambda { @client.at("/test/412").get! }.should raise_exception ::Restfulie::Client::HTTP::Error::PreconditionFailed
end
it "receives error when 413 code is returned" do
@client.at("/test/413").get.should respond_with_status(413)
end
it "raise Error::ClientError error when 413 code is returned" do
lambda { @client.at("/test/413").get! }.should raise_exception ::Restfulie::Client::HTTP::Error::ClientError
end
it "receives error when 501 code is returned" do
@client.at("/test/501").get.should respond_with_status(501)
end
it "raise Error::NotImplemented error when 501 code is returned" do
lambda { @client.at("/test/501").get! }.should raise_exception ::Restfulie::Client::HTTP::Error::NotImplemented
end
it "receives error when 500 code is returned" do
@client.at("/test/500").get.should respond_with_status(500)
end
it "raise Error::ServerError error when 500 code is returned" do
lambda { @client.at("/test/500").get! }.should raise_exception ::Restfulie::Client::HTTP::Error::ServerError
end
it "receives error when 503 code is returned" do
debugger
Restfulie.at("http://localhost:2222/").get.should respond_with_status(503)
end
it "raise Error::ServerNotAvailableError error when 503 code is returned" do
lambda { Restfulie.at("http://localhost:2222/").get! }.should raise_exception ::Restfulie::Client::HTTP::Error::ServerNotAvailableError
end
it "receives error when 502..599 code is returned" do
@client.at("/test/502").get.should respond_with_status(502)
end
it "raise Error::ServerError error when 502..599 code is returned" do
lambda { @client.at("/test/502").get! }.should raise_exception ::Restfulie::Client::HTTP::Error::ServerError
end
it "receives error when 600 or bigger code is returned" do
@client.at("/test/600").get.should respond_with_status(600)
end
it "raise Error::UnknownError error when 600 or bigger code is returned" do
lambda { @client.at("/test/600").get! }.should raise_exception ::Restfulie::Client::HTTP::Error::UnknownError
end
end
end