File: test_spec.rb

package info (click to toggle)
ruby-rack-test 0.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 348 kB
  • ctags: 142
  • sloc: ruby: 1,713; makefile: 3
file content (550 lines) | stat: -rw-r--r-- 14,988 bytes parent folder | download
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
require "spec_helper"

describe Rack::Test::Session do
  describe "initialization" do
    it "supports being initialized with a Rack::MockSession app" do
      session = Rack::Test::Session.new(Rack::MockSession.new(app))
      expect(session.request("/")).to be_ok
    end

    it "supports being initialized with an app" do
      session = Rack::Test::Session.new(app)
      expect(session.request("/")).to be_ok
    end
  end

  describe "#request" do
    it "requests the URI using GET by default" do
      request "/"
      expect(last_request).to be_get
      expect(last_response).to be_ok
    end

    it "returns a response" do
      expect(request("/")).to be_ok
    end

    it "uses the provided env" do
      request "/", "X-Foo" => "bar"
      expect(last_request.env["X-Foo"]).to eq("bar")
    end

    it "allows HTTP_HOST to be set" do
      request "/", "HTTP_HOST" => "www.example.ua"
      expect(last_request.env['HTTP_HOST']).to eq("www.example.ua")
    end

    it "sets HTTP_HOST with port for non-default ports" do
      request "http://foo.com:8080"
      expect(last_request.env["HTTP_HOST"]).to eq("foo.com:8080")
      request "https://foo.com:8443"
      expect(last_request.env["HTTP_HOST"]).to eq("foo.com:8443")
    end

    it "sets HTTP_HOST without port for default ports" do
      request "http://foo.com"
      expect(last_request.env["HTTP_HOST"]).to eq("foo.com")
      request "http://foo.com:80"
      expect(last_request.env["HTTP_HOST"]).to eq("foo.com")
      request "https://foo.com:443"
      expect(last_request.env["HTTP_HOST"]).to eq("foo.com")
    end

    it "defaults to GET" do
      request "/"
      expect(last_request.env["REQUEST_METHOD"]).to eq("GET")
    end

    it "defaults the REMOTE_ADDR to 127.0.0.1" do
      request "/"
      expect(last_request.env["REMOTE_ADDR"]).to eq("127.0.0.1")
    end

    it "sets rack.test to true in the env" do
      request "/"
      expect(last_request.env["rack.test"]).to eq(true)
    end

    it "defaults to port 80" do
      request "/"
      expect(last_request.env["SERVER_PORT"]).to eq("80")
    end

    it "defaults to example.org" do
      request "/"
      expect(last_request.env["SERVER_NAME"]).to eq("example.org")
    end

    it "yields the response to a given block" do
      request "/" do |response|
        expect(response).to be_ok
      end
    end

    it "supports sending :params" do
      request "/", :params => { "foo" => "bar" }
      expect(last_request.GET["foo"]).to eq("bar")
    end

    it "doesn't follow redirects by default" do
      request "/redirect"
      expect(last_response).to be_redirect
      expect(last_response.body).to be_empty
    end

    it "allows passing :input in for POSTs" do
      request "/", :method => :post, :input => "foo"
      expect(last_request.env["rack.input"].read).to eq("foo")
    end

    it "converts method names to a uppercase strings" do
      request "/", :method => :put
      expect(last_request.env["REQUEST_METHOD"]).to eq("PUT")
    end

    it "prepends a slash to the URI path" do
      request "foo"
      expect(last_request.env["PATH_INFO"]).to eq("/foo")
    end

    it "accepts params and builds query strings for GET requests" do
      request "/foo?baz=2", :params => {:foo => {:bar => "1"}}
      expect(last_request.GET).to eq({ "baz" => "2", "foo" => { "bar" => "1" }})
    end

    it "parses query strings with repeated variable names correctly" do
      request "/foo?bar=2&bar=3"
      expect(last_request.GET).to eq({ "bar" => "3" })
    end

    it "accepts raw input in params for GET requests" do
      request "/foo?baz=2", :params => "foo[bar]=1"
      expect(last_request.GET).to eq({ "baz" => "2", "foo" => { "bar" => "1" }})
    end

    it "does not rewrite a GET query string when :params is not supplied" do
      request "/foo?a=1&b=2&c=3&e=4&d=5+%20"
      expect(last_request.query_string).to eq("a=1&b=2&c=3&e=4&d=5+%20")
    end

    it "accepts params and builds url encoded params for POST requests" do
      request "/foo", :method => :post, :params => {:foo => {:bar => "1"}}
      expect(last_request.env["rack.input"].read).to eq("foo[bar]=1")
    end

    it "accepts raw input in params for POST requests" do
      request "/foo", :method => :post, :params => "foo[bar]=1"
      expect(last_request.env["rack.input"].read).to eq("foo[bar]=1")
    end

    context "when the response body responds_to?(:close)" do
      class CloseableBody
        def initialize
          @closed = false
        end

        def each
          return if @closed
          yield "Hello, World!"
        end

        def close
          @closed = true
        end
      end

      it "closes response's body" do
        body = CloseableBody.new
        expect(body).to receive(:close)

        app = lambda do |env|
          [200, {"Content-Type" => "text/html", "Content-Length" => "13"}, body]
        end

        session = Rack::Test::Session.new(Rack::MockSession.new(app))
        session.request("/")
      end

      it "closes response's body after iteration" do
        app = lambda do |env|
          [200, {"Content-Type" => "text/html", "Content-Length" => "13"}, CloseableBody.new]
        end

        session = Rack::Test::Session.new(Rack::MockSession.new(app))
        session.request("/")
        expect(session.last_response.body).to eq("Hello, World!")
      end
    end

    context "when input is given" do
      it "sends the input" do
        request "/", :method => "POST", :input => "foo"
        expect(last_request.env["rack.input"].read).to eq("foo")
      end

      it "does not send a multipart request" do
        request "/", :method => "POST", :input => "foo"
        expect(last_request.env["CONTENT_TYPE"]).not_to eq("application/x-www-form-urlencoded")
      end
    end

    context "for a POST specified with :method" do
      it "uses application/x-www-form-urlencoded as the CONTENT_TYPE" do
        request "/", :method => "POST"
        expect(last_request.env["CONTENT_TYPE"]).to eq("application/x-www-form-urlencoded")
      end
    end

    context "for a POST specified with REQUEST_METHOD" do
      it "uses application/x-www-form-urlencoded as the CONTENT_TYPE" do
        request "/", "REQUEST_METHOD" => "POST"
        expect(last_request.env["CONTENT_TYPE"]).to eq("application/x-www-form-urlencoded")
      end
    end

    context "when CONTENT_TYPE is specified in the env" do
      it "does not overwrite the CONTENT_TYPE" do
        request "/", "CONTENT_TYPE" => "application/xml"
        expect(last_request.env["CONTENT_TYPE"]).to eq("application/xml")
      end
    end

    context "when the URL is https://" do
      it "sets rack.url_scheme to https" do
        get "https://example.org/"
        expect(last_request.env["rack.url_scheme"]).to eq("https")
      end

      it "sets SERVER_PORT to 443" do
        get "https://example.org/"
        expect(last_request.env["SERVER_PORT"]).to eq("443")
      end

      it "sets HTTPS to on" do
        get "https://example.org/"
        expect(last_request.env["HTTPS"]).to eq("on")
      end
    end

    context "for a XHR" do
      it "sends XMLHttpRequest for the X-Requested-With header" do
        request "/", :xhr => true
        expect(last_request.env["HTTP_X_REQUESTED_WITH"]).to eq("XMLHttpRequest")
        expect(last_request).to be_xhr
      end
    end
  end

  describe "#header" do
    it "sets a header to be sent with requests" do
      header "User-Agent", "Firefox"
      request "/"

      expect(last_request.env["HTTP_USER_AGENT"]).to eq("Firefox")
    end

    it "sets a Content-Type to be sent with requests" do
      header "Content-Type", "application/json"
      request "/"

      expect(last_request.env["CONTENT_TYPE"]).to eq("application/json")
    end

    it "sets a Host to be sent with requests" do
      header "Host", "www.example.ua"
      request "/"

      expect(last_request.env["HTTP_HOST"]).to eq("www.example.ua")
    end

    it "persists across multiple requests" do
      header "User-Agent", "Firefox"
      request "/"
      request "/"

      expect(last_request.env["HTTP_USER_AGENT"]).to eq("Firefox")
    end

    it "overwrites previously set headers" do
      header "User-Agent", "Firefox"
      header "User-Agent", "Safari"
      request "/"

      expect(last_request.env["HTTP_USER_AGENT"]).to eq("Safari")
    end

    it "can be used to clear a header" do
      header "User-Agent", "Firefox"
      header "User-Agent", nil
      request "/"

      expect(last_request.env).not_to have_key("HTTP_USER_AGENT")
    end

    it "is overridden by headers sent during the request" do
      header "User-Agent", "Firefox"
      request "/", "HTTP_USER_AGENT" => "Safari"

      expect(last_request.env["HTTP_USER_AGENT"]).to eq("Safari")
    end
  end

  describe "#env" do
    it "sets the env to be sent with requests" do
      env "rack.session", {:csrf => 'token'}
      request "/"

      expect(last_request.env["rack.session"]).to eq({:csrf => 'token'})
    end

    it "persists across multiple requests" do
      env "rack.session", {:csrf => 'token'}
      request "/"
      request "/"

      expect(last_request.env["rack.session"]).to eq({:csrf => 'token'})
    end

    it "overwrites previously set envs" do
      env "rack.session", {:csrf => 'token'}
      env "rack.session", {:some => :thing}
      request "/"

      expect(last_request.env["rack.session"]).to eq({:some => :thing})
    end

    it "can be used to clear a env" do
      env "rack.session", {:csrf => 'token'}
      env "rack.session", nil
      request "/"

      expect(last_request.env).not_to have_key("X_CSRF_TOKEN")
    end

    it "is overridden by envs sent during the request" do
      env "rack.session", {:csrf => 'token'}
      request "/", "rack.session" => {:some => :thing}

      expect(last_request.env["rack.session"]).to eq({:some => :thing})
    end
  end

  describe "#authorize" do
    it "sets the HTTP_AUTHORIZATION header" do
      authorize "bryan", "secret"
      request "/"

      expect(last_request.env["HTTP_AUTHORIZATION"]).to eq("Basic YnJ5YW46c2VjcmV0\n")
    end

    it "includes the header for subsequent requests" do
      basic_authorize "bryan", "secret"
      request "/"
      request "/"

      expect(last_request.env["HTTP_AUTHORIZATION"]).to eq("Basic YnJ5YW46c2VjcmV0\n")
    end
  end

  describe "follow_redirect!" do
    it "follows redirects" do
      get "/redirect"
      follow_redirect!

      expect(last_response).not_to be_redirect
      expect(last_response.body).to eq("You've been redirected")
      expect(last_request.env["HTTP_REFERER"]).to eql("http://example.org/redirect")
    end

    it "does not include params when following the redirect" do
      get "/redirect", { "foo" => "bar" }
      follow_redirect!

      expect(last_request.GET).to eq({})
    end

    it "raises an error if the last_response is not set" do
      expect {
        follow_redirect!
      }.to raise_error(Rack::Test::Error)
    end

    it "raises an error if the last_response is not a redirect" do
      get "/"

      expect {
        follow_redirect!
      }.to raise_error(Rack::Test::Error)
    end
  end

  describe "#last_request" do
    it "returns the most recent request" do
      request "/"
      expect(last_request.env["PATH_INFO"]).to eq("/")
    end

    it "raises an error if no requests have been issued" do
      expect {
        last_request
      }.to raise_error(Rack::Test::Error)
    end
  end

  describe "#last_response" do
    it "returns the most recent response" do
      request "/"
      expect(last_response["Content-Type"]).to eq("text/html;charset=utf-8")
    end

    it "raises an error if no requests have been issued" do
      expect {
        last_response
      }.to raise_error(Rack::Test::Error)
    end
  end

  describe "after_request" do
    it "runs callbacks after each request" do
      ran = false

      rack_mock_session.after_request do
        ran = true
      end

      get "/"
      expect(ran).to eq(true)
    end

    it "runs multiple callbacks" do
      count = 0

      2.times do
        rack_mock_session.after_request do
          count += 1
        end
      end

      get "/"
      expect(count).to eq(2)
    end
  end

  describe "#get" do
    it_should_behave_like "any #verb methods"

    def verb
      "get"
    end

    it "uses the provided params hash" do
      get "/", :foo => "bar"
      expect(last_request.GET).to eq({ "foo" => "bar" })
    end

    it "sends params with parens in names" do
      get "/", "foo(1i)" => "bar"
      expect(last_request.GET["foo(1i)"]).to eq("bar")
    end

    it "supports params with encoding sensitive names" do
      get "/", "foo bar" => "baz"
      expect(last_request.GET["foo bar"]).to eq("baz")
    end

    it "supports params with nested encoding sensitive names" do
      get "/", "boo" => {"foo bar" => "baz"}
      expect(last_request.GET).to eq({"boo" => {"foo bar" => "baz"}})
    end

    it "accepts params in the path" do
      get "/?foo=bar"
      expect(last_request.GET).to eq({ "foo" => "bar" })
    end
  end

  describe "#head" do
    it_should_behave_like "any #verb methods"

    def verb
      "head"
    end
  end

  describe "#post" do
    it_should_behave_like "any #verb methods"

    def verb
      "post"
    end

    it "uses the provided params hash" do
      post "/", :foo => "bar"
      expect(last_request.POST).to eq({ "foo" => "bar" })
    end

    it "supports params with encoding sensitive names" do
      post "/", "foo bar" => "baz"
      expect(last_request.POST["foo bar"]).to eq("baz")
    end

    it "uses application/x-www-form-urlencoded as the CONTENT_TYPE" do
      post "/"
      expect(last_request.env["CONTENT_TYPE"]).to eq("application/x-www-form-urlencoded")
    end

    it "accepts a body" do
      post "/", "Lobsterlicious!"
      expect(last_request.body.read).to eq("Lobsterlicious!")
    end

    context "when CONTENT_TYPE is specified in the env" do
      it "does not overwrite the CONTENT_TYPE" do
        post "/", {}, { "CONTENT_TYPE" => "application/xml" }
        expect(last_request.env["CONTENT_TYPE"]).to eq("application/xml")
      end
    end
  end

  describe "#put" do
    it_should_behave_like "any #verb methods"

    def verb
      "put"
    end

    it "accepts a body" do
      put "/", "Lobsterlicious!"
      expect(last_request.body.read).to eq("Lobsterlicious!")
    end
  end

  describe "#patch" do
    it_should_behave_like "any #verb methods"

    def verb
      "patch"
    end

    it "accepts a body" do
      patch "/", "Lobsterlicious!"
      expect(last_request.body.read).to eq("Lobsterlicious!")
    end
  end

  describe "#delete" do
    it_should_behave_like "any #verb methods"

    def verb
      "delete"
    end
  end

  describe "#options" do
    it_should_behave_like "any #verb methods"

    def verb
      "options"
    end
  end
end