rack-test 0.6.3 → 2.1.0

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