joebadmo-rack-test 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,489 @@
1
+ require "spec_helper"
2
+
3
+ describe Rack::Test::Session do
4
+ describe "initialization" do
5
+ it "supports being initialized with a Rack::MockSession app" do
6
+ session = Rack::Test::Session.new(Rack::MockSession.new(app))
7
+ session.request("/").should be_ok
8
+ end
9
+
10
+ it "supports being initialized with an app" do
11
+ session = Rack::Test::Session.new(app)
12
+ session.request("/").should be_ok
13
+ end
14
+ end
15
+
16
+ describe "#request" do
17
+ it "requests the URI using GET by default" do
18
+ request "/"
19
+ last_request.should be_get
20
+ last_response.should be_ok
21
+ end
22
+
23
+ it "returns a response" do
24
+ request("/").should be_ok
25
+ end
26
+
27
+ it "uses the provided env" do
28
+ request "/", "X-Foo" => "bar"
29
+ last_request.env["X-Foo"].should == "bar"
30
+ end
31
+
32
+ it "allows HTTP_HOST to be set" do
33
+ request "/", "HTTP_HOST" => "www.example.ua"
34
+ last_request.env['HTTP_HOST'].should == "www.example.ua"
35
+ end
36
+
37
+ it "defaults to GET" do
38
+ request "/"
39
+ last_request.env["REQUEST_METHOD"].should == "GET"
40
+ end
41
+
42
+ it "defaults the REMOTE_ADDR to 127.0.0.1" do
43
+ request "/"
44
+ last_request.env["REMOTE_ADDR"].should == "127.0.0.1"
45
+ end
46
+
47
+ it "sets rack.test to true in the env" do
48
+ request "/"
49
+ last_request.env["rack.test"].should == true
50
+ end
51
+
52
+ it "defaults to port 80" do
53
+ request "/"
54
+ last_request.env["SERVER_PORT"].should == "80"
55
+ end
56
+
57
+ it "defaults to example.org" do
58
+ request "/"
59
+ last_request.env["SERVER_NAME"].should == "example.org"
60
+ end
61
+
62
+ it "yields the response to a given block" do
63
+ request "/" do |response|
64
+ response.should be_ok
65
+ end
66
+ end
67
+
68
+ it "supports sending :params" do
69
+ request "/", :params => { "foo" => "bar" }
70
+ last_request.GET["foo"].should == "bar"
71
+ end
72
+
73
+ it "doesn't follow redirects by default" do
74
+ request "/redirect"
75
+ last_response.should be_redirect
76
+ last_response.body.should be_empty
77
+ end
78
+
79
+ it "allows passing :input in for POSTs" do
80
+ request "/", :method => :post, :input => "foo"
81
+ last_request.env["rack.input"].read.should == "foo"
82
+ end
83
+
84
+ it "converts method names to a uppercase strings" do
85
+ request "/", :method => :put
86
+ last_request.env["REQUEST_METHOD"].should == "PUT"
87
+ end
88
+
89
+ it "prepends a slash to the URI path" do
90
+ request "foo"
91
+ last_request.env["PATH_INFO"].should == "/foo"
92
+ end
93
+
94
+ it "accepts params and builds query strings for GET requests" do
95
+ request "/foo?baz=2", :params => {:foo => {:bar => "1"}}
96
+ last_request.GET.should == { "baz" => "2", "foo" => { "bar" => "1" }}
97
+ end
98
+
99
+ it "parses query strings with repeated variable names correctly" do
100
+ request "/foo?bar=2&bar=3"
101
+ last_request.GET.should == { "bar" => "3" }
102
+ end
103
+
104
+ it "accepts raw input in params for GET requests" do
105
+ request "/foo?baz=2", :params => "foo[bar]=1"
106
+ last_request.GET.should == { "baz" => "2", "foo" => { "bar" => "1" }}
107
+ end
108
+
109
+ it "accepts params and builds url encoded params for POST requests" do
110
+ request "/foo", :method => :post, :params => {:foo => {:bar => "1"}}
111
+ last_request.env["rack.input"].read.should == "foo[bar]=1"
112
+ end
113
+
114
+ it "accepts raw input in params for POST requests" do
115
+ request "/foo", :method => :post, :params => "foo[bar]=1"
116
+ last_request.env["rack.input"].read.should == "foo[bar]=1"
117
+ end
118
+
119
+ context "when the response body responds_to?(:close)" do
120
+ class CloseableBody
121
+ def initialize
122
+ @closed = false
123
+ end
124
+
125
+ def each
126
+ return if @closed
127
+ yield "Hello, World!"
128
+ end
129
+
130
+ def close
131
+ @closed = true
132
+ end
133
+ end
134
+
135
+ it "closes response's body" do
136
+ body = CloseableBody.new
137
+ body.should_receive(:close)
138
+
139
+ app = lambda do |env|
140
+ [200, {"Content-Type" => "text/html", "Content-Length" => "13"}, body]
141
+ end
142
+
143
+ session = Rack::Test::Session.new(Rack::MockSession.new(app))
144
+ session.request("/")
145
+ end
146
+
147
+ it "closes response's body after iteration" do
148
+ app = lambda do |env|
149
+ [200, {"Content-Type" => "text/html", "Content-Length" => "13"}, CloseableBody.new]
150
+ end
151
+
152
+ session = Rack::Test::Session.new(Rack::MockSession.new(app))
153
+ session.request("/")
154
+ session.last_response.body.should == "Hello, World!"
155
+ end
156
+ end
157
+
158
+ context "when input is given" do
159
+ it "sends the input" do
160
+ request "/", :method => "POST", :input => "foo"
161
+ last_request.env["rack.input"].read.should == "foo"
162
+ end
163
+
164
+ it "does not send a multipart request" do
165
+ request "/", :method => "POST", :input => "foo"
166
+ last_request.env["CONTENT_TYPE"].should_not == "application/x-www-form-urlencoded"
167
+ end
168
+ end
169
+
170
+ context "for a POST specified with :method" do
171
+ it "uses application/x-www-form-urlencoded as the CONTENT_TYPE" do
172
+ request "/", :method => "POST"
173
+ last_request.env["CONTENT_TYPE"].should == "application/x-www-form-urlencoded"
174
+ end
175
+ end
176
+
177
+ context "for a POST specified with REQUEST_METHOD" do
178
+ it "uses application/x-www-form-urlencoded as the CONTENT_TYPE" do
179
+ request "/", "REQUEST_METHOD" => "POST"
180
+ last_request.env["CONTENT_TYPE"].should == "application/x-www-form-urlencoded"
181
+ end
182
+ end
183
+
184
+ context "when CONTENT_TYPE is specified in the env" do
185
+ it "does not overwrite the CONTENT_TYPE" do
186
+ request "/", "CONTENT_TYPE" => "application/xml"
187
+ last_request.env["CONTENT_TYPE"].should == "application/xml"
188
+ end
189
+ end
190
+
191
+ context "when the URL is https://" do
192
+ it "sets rack.url_scheme to https" do
193
+ get "https://example.org/"
194
+ last_request.env["rack.url_scheme"].should == "https"
195
+ end
196
+
197
+ it "sets SERVER_PORT to 443" do
198
+ get "https://example.org/"
199
+ last_request.env["SERVER_PORT"].should == "443"
200
+ end
201
+
202
+ it "sets HTTPS to on" do
203
+ get "https://example.org/"
204
+ last_request.env["HTTPS"].should == "on"
205
+ end
206
+ end
207
+
208
+ context "for a XHR" do
209
+ it "sends XMLHttpRequest for the X-Requested-With header" do
210
+ request "/", :xhr => true
211
+ last_request.env["HTTP_X_REQUESTED_WITH"].should == "XMLHttpRequest"
212
+ last_request.should be_xhr
213
+ end
214
+ end
215
+ end
216
+
217
+ describe "#header" do
218
+ it "sets a header to be sent with requests" do
219
+ header "User-Agent", "Firefox"
220
+ request "/"
221
+
222
+ last_request.env["HTTP_USER_AGENT"].should == "Firefox"
223
+ end
224
+
225
+ it "sets a Content-Type to be sent with requests" do
226
+ header "Content-Type", "application/json"
227
+ request "/"
228
+
229
+ last_request.env["CONTENT_TYPE"].should == "application/json"
230
+ end
231
+
232
+ it "sets a Host to be sent with requests" do
233
+ header "Host", "www.example.ua"
234
+ request "/"
235
+
236
+ last_request.env["HTTP_HOST"].should == "www.example.ua"
237
+ end
238
+
239
+ it "persists across multiple requests" do
240
+ header "User-Agent", "Firefox"
241
+ request "/"
242
+ request "/"
243
+
244
+ last_request.env["HTTP_USER_AGENT"].should == "Firefox"
245
+ end
246
+
247
+ it "overwrites previously set headers" do
248
+ header "User-Agent", "Firefox"
249
+ header "User-Agent", "Safari"
250
+ request "/"
251
+
252
+ last_request.env["HTTP_USER_AGENT"].should == "Safari"
253
+ end
254
+
255
+ it "can be used to clear a header" do
256
+ header "User-Agent", "Firefox"
257
+ header "User-Agent", nil
258
+ request "/"
259
+
260
+ last_request.env.should_not have_key("HTTP_USER_AGENT")
261
+ end
262
+
263
+ it "is overridden by headers sent during the request" do
264
+ header "User-Agent", "Firefox"
265
+ request "/", "HTTP_USER_AGENT" => "Safari"
266
+
267
+ last_request.env["HTTP_USER_AGENT"].should == "Safari"
268
+ end
269
+ end
270
+
271
+ describe "#authorize" do
272
+ it "sets the HTTP_AUTHORIZATION header" do
273
+ authorize "bryan", "secret"
274
+ request "/"
275
+
276
+ last_request.env["HTTP_AUTHORIZATION"].should == "Basic YnJ5YW46c2VjcmV0\n"
277
+ end
278
+
279
+ it "includes the header for subsequent requests" do
280
+ basic_authorize "bryan", "secret"
281
+ request "/"
282
+ request "/"
283
+
284
+ last_request.env["HTTP_AUTHORIZATION"].should == "Basic YnJ5YW46c2VjcmV0\n"
285
+ end
286
+ end
287
+
288
+ describe "follow_redirect!" do
289
+ it "follows redirects" do
290
+ get "/redirect"
291
+ follow_redirect!
292
+
293
+ last_response.should_not be_redirect
294
+ last_response.body.should == "You've been redirected"
295
+ last_request.env["HTTP_REFERER"].should eql("http://example.org/redirect")
296
+ end
297
+
298
+ it "does not include params when following the redirect" do
299
+ get "/redirect", { "foo" => "bar" }
300
+ follow_redirect!
301
+
302
+ last_request.GET.should == {}
303
+ end
304
+
305
+ it "raises an error if the last_response is not set" do
306
+ lambda {
307
+ follow_redirect!
308
+ }.should raise_error(Rack::Test::Error)
309
+ end
310
+
311
+ it "raises an error if the last_response is not a redirect" do
312
+ get "/"
313
+
314
+ lambda {
315
+ follow_redirect!
316
+ }.should raise_error(Rack::Test::Error)
317
+ end
318
+ end
319
+
320
+ describe "#last_request" do
321
+ it "returns the most recent request" do
322
+ request "/"
323
+ last_request.env["PATH_INFO"].should == "/"
324
+ end
325
+
326
+ it "raises an error if no requests have been issued" do
327
+ lambda {
328
+ last_request
329
+ }.should raise_error(Rack::Test::Error)
330
+ end
331
+ end
332
+
333
+ describe "#last_response" do
334
+ it "returns the most recent response" do
335
+ request "/"
336
+ last_response["Content-Type"].should == "text/html;charset=utf-8"
337
+ end
338
+
339
+ it "raises an error if no requests have been issued" do
340
+ lambda {
341
+ last_response
342
+ }.should raise_error
343
+ end
344
+ end
345
+
346
+ describe "after_request" do
347
+ it "runs callbacks after each request" do
348
+ ran = false
349
+
350
+ rack_mock_session.after_request do
351
+ ran = true
352
+ end
353
+
354
+ get "/"
355
+ ran.should == true
356
+ end
357
+
358
+ it "runs multiple callbacks" do
359
+ count = 0
360
+
361
+ 2.times do
362
+ rack_mock_session.after_request do
363
+ count += 1
364
+ end
365
+ end
366
+
367
+ get "/"
368
+ count.should == 2
369
+ end
370
+ end
371
+
372
+ describe "#get" do
373
+ it_should_behave_like "any #verb methods"
374
+
375
+ def verb
376
+ "get"
377
+ end
378
+
379
+ it "uses the provided params hash" do
380
+ get "/", :foo => "bar"
381
+ last_request.GET.should == { "foo" => "bar" }
382
+ end
383
+
384
+ it "sends params with parens in names" do
385
+ get "/", "foo(1i)" => "bar"
386
+ last_request.GET["foo(1i)"].should == "bar"
387
+ end
388
+
389
+ it "supports params with encoding sensitive names" do
390
+ get "/", "foo bar" => "baz"
391
+ last_request.GET["foo bar"].should == "baz"
392
+ end
393
+
394
+ it "supports params with nested encoding sensitive names" do
395
+ get "/", "boo" => {"foo bar" => "baz"}
396
+ last_request.GET.should == {"boo" => {"foo bar" => "baz"}}
397
+ end
398
+
399
+ it "accepts params in the path" do
400
+ get "/?foo=bar"
401
+ last_request.GET.should == { "foo" => "bar" }
402
+ end
403
+ end
404
+
405
+ describe "#head" do
406
+ it_should_behave_like "any #verb methods"
407
+
408
+ def verb
409
+ "head"
410
+ end
411
+ end
412
+
413
+ describe "#post" do
414
+ it_should_behave_like "any #verb methods"
415
+
416
+ def verb
417
+ "post"
418
+ end
419
+
420
+ it "uses the provided params hash" do
421
+ post "/", :foo => "bar"
422
+ last_request.POST.should == { "foo" => "bar" }
423
+ end
424
+
425
+ it "supports params with encoding sensitive names" do
426
+ post "/", "foo bar" => "baz"
427
+ last_request.POST["foo bar"].should == "baz"
428
+ end
429
+
430
+ it "uses application/x-www-form-urlencoded as the CONTENT_TYPE" do
431
+ post "/"
432
+ last_request.env["CONTENT_TYPE"].should == "application/x-www-form-urlencoded"
433
+ end
434
+
435
+ it "accepts a body" do
436
+ post "/", "Lobsterlicious!"
437
+ last_request.body.read.should == "Lobsterlicious!"
438
+ end
439
+
440
+ context "when CONTENT_TYPE is specified in the env" do
441
+ it "does not overwrite the CONTENT_TYPE" do
442
+ post "/", {}, { "CONTENT_TYPE" => "application/xml" }
443
+ last_request.env["CONTENT_TYPE"].should == "application/xml"
444
+ end
445
+ end
446
+ end
447
+
448
+ describe "#put" do
449
+ it_should_behave_like "any #verb methods"
450
+
451
+ def verb
452
+ "put"
453
+ end
454
+
455
+ it "accepts a body" do
456
+ put "/", "Lobsterlicious!"
457
+ last_request.body.read.should == "Lobsterlicious!"
458
+ end
459
+ end
460
+
461
+ describe "#patch" do
462
+ it_should_behave_like "any #verb methods"
463
+
464
+ def verb
465
+ "patch"
466
+ end
467
+
468
+ it "accepts a body" do
469
+ patch "/", "Lobsterlicious!"
470
+ last_request.body.read.should == "Lobsterlicious!"
471
+ end
472
+ end
473
+
474
+ describe "#delete" do
475
+ it_should_behave_like "any #verb methods"
476
+
477
+ def verb
478
+ "delete"
479
+ end
480
+ end
481
+
482
+ describe "#options" do
483
+ it_should_behave_like "any #verb methods"
484
+
485
+ def verb
486
+ "options"
487
+ end
488
+ end
489
+ end