/
spec_request.rb
1566 lines (1314 loc) · 51.9 KB
/
spec_request.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
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# frozen_string_literal: true
require_relative 'helper'
require 'cgi'
require 'forwardable'
require 'securerandom'
class RackRequestTest < Minitest::Spec
it "copies the env when duping" do
req = make_request(Rack::MockRequest.env_for("http://example.com:8080/"))
refute_same req.env, req.dup.env
end
it 'can check if something has been set' do
req = make_request(Rack::MockRequest.env_for("http://example.com:8080/"))
refute req.has_header?("FOO")
end
it "can get a key from the env" do
req = make_request(Rack::MockRequest.env_for("http://example.com:8080/"))
assert_equal "example.com", req.get_header("SERVER_NAME")
end
it 'can calculate the authority' do
req = make_request(Rack::MockRequest.env_for("http://example.com:8080/"))
assert_equal "example.com:8080", req.authority
end
it 'can calculate the authority without a port' do
req = make_request(Rack::MockRequest.env_for("http://example.com/"))
assert_equal "example.com:80", req.authority
end
it 'can calculate the authority without a port on ssl' do
req = make_request(Rack::MockRequest.env_for("https://example.com/"))
assert_equal "example.com:443", req.authority
end
it 'yields to the block if no value has been set' do
req = make_request(Rack::MockRequest.env_for("http://example.com:8080/"))
yielded = false
req.fetch_header("FOO") do
yielded = true
req.set_header "FOO", 'bar'
end
assert yielded
assert_equal "bar", req.get_header("FOO")
end
it 'can iterate over values' do
req = make_request(Rack::MockRequest.env_for("http://example.com:8080/"))
req.set_header 'foo', 'bar'
hash = {}
req.each_header do |k, v|
hash[k] = v
end
assert_equal 'bar', hash['foo']
end
it 'can set values in the env' do
req = make_request(Rack::MockRequest.env_for("http://example.com:8080/"))
req.set_header("FOO", "BAR")
assert_equal "BAR", req.get_header("FOO")
end
it 'can add to multivalued headers in the env' do
req = make_request(Rack::MockRequest.env_for('http://example.com:8080/'))
assert_equal '1', req.add_header('FOO', '1')
assert_equal '1', req.get_header('FOO')
assert_equal '1,2', req.add_header('FOO', '2')
assert_equal '1,2', req.get_header('FOO')
assert_equal '1,2', req.add_header('FOO', nil)
assert_equal '1,2', req.get_header('FOO')
end
it 'can delete env values' do
req = make_request(Rack::MockRequest.env_for("http://example.com:8080/"))
req.set_header 'foo', 'bar'
assert req.has_header? 'foo'
req.delete_header 'foo'
refute req.has_header? 'foo'
end
it "wrap the rack variables" do
req = make_request(Rack::MockRequest.env_for("http://example.com:8080/"))
req.body.must_respond_to :gets
req.scheme.must_equal "http"
req.request_method.must_equal "GET"
req.must_be :get?
req.wont_be :post?
req.wont_be :put?
req.wont_be :delete?
req.wont_be :head?
req.wont_be :patch?
req.script_name.must_equal ""
req.path_info.must_equal "/"
req.query_string.must_equal ""
req.host.must_equal "example.com"
req.port.must_equal 8080
req.content_length.must_equal "0"
req.content_type.must_be_nil
end
it "figure out the correct host" do
req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "www2.example.org")
req.host.must_equal "www2.example.org"
req.hostname.must_equal "www2.example.org"
req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "123foo.example.com")
req.host.must_equal "123foo.example.com"
req.hostname.must_equal "123foo.example.com"
req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "♡.com")
req.host.must_equal "♡.com"
req.hostname.must_equal "♡.com"
req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "♡.com:80")
req.host.must_equal "♡.com"
req.hostname.must_equal "♡.com"
req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "nic.谷歌")
req.host.must_equal "nic.谷歌"
req.hostname.must_equal "nic.谷歌"
req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "nic.谷歌:80")
req.host.must_equal "nic.谷歌"
req.hostname.must_equal "nic.谷歌"
req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "technically_invalid.example.com")
req.host.must_equal "technically_invalid.example.com"
req.hostname.must_equal "technically_invalid.example.com"
req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "technically_invalid.example.com:80")
req.host.must_equal "technically_invalid.example.com"
req.hostname.must_equal "technically_invalid.example.com"
req = make_request \
Rack::MockRequest.env_for("/", "SERVER_NAME" => "example.org", "SERVER_PORT" => "9292")
req.host.must_equal "example.org"
req.hostname.must_equal "example.org"
req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "localhost:81", "HTTP_X_FORWARDED_HOST" => "example.org:9292")
req.host.must_equal "example.org"
req.hostname.must_equal "example.org"
req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "localhost:81", "HTTP_X_FORWARDED_HOST" => "[2001:db8:cafe::17]:47011")
req.host.must_equal "[2001:db8:cafe::17]"
req.hostname.must_equal "2001:db8:cafe::17"
req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "localhost:81", "HTTP_X_FORWARDED_HOST" => "2001:db8:cafe::17")
req.host.must_equal "[2001:db8:cafe::17]"
req.hostname.must_equal "2001:db8:cafe::17"
env = Rack::MockRequest.env_for("/")
env.delete("SERVER_NAME")
req = make_request(env)
req.host.must_be_nil
end
it "figure out the correct port" do
req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "www2.example.org")
req.port.must_equal 80
req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "www2.example.org:81")
req.port.must_equal 81
req = make_request \
Rack::MockRequest.env_for("/", "SERVER_NAME" => "example.org", "SERVER_PORT" => "9292")
req.port.must_equal 9292
req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "localhost:81", "HTTP_X_FORWARDED_HOST" => "example.org:9292")
req.port.must_equal 9292
req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "localhost:81", "HTTP_X_FORWARDED_HOST" => "[2001:db8:cafe::17]:47011")
req.port.must_equal 47011
req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "localhost:81", "HTTP_X_FORWARDED_HOST" => "2001:db8:cafe::17")
req.port.must_equal 80
req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "localhost:81", "HTTP_X_FORWARDED_HOST" => "example.org")
req.port.must_equal 80
req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "localhost:81", "HTTP_X_FORWARDED_HOST" => "example.org", "HTTP_X_FORWARDED_SSL" => "on")
req.port.must_equal 443
req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "localhost:81", "HTTP_X_FORWARDED_HOST" => "example.org", "HTTP_X_FORWARDED_PROTO" => "https")
req.port.must_equal 443
req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "localhost:81", "HTTP_X_FORWARDED_HOST" => "example.org", "HTTP_X_FORWARDED_PORT" => "9393")
req.port.must_equal 9393
req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "localhost:81", "HTTP_X_FORWARDED_HOST" => "example.org:9393", "SERVER_PORT" => "80")
req.port.must_equal 9393
req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "localhost:81", "HTTP_X_FORWARDED_HOST" => "example.org", "SERVER_PORT" => "9393")
req.port.must_equal 80
req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "localhost", "HTTP_X_FORWARDED_PROTO" => "https", "SERVER_PORT" => "80")
req.port.must_equal 443
req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "localhost", "HTTP_X_FORWARDED_PROTO" => "https,https", "SERVER_PORT" => "80")
req.port.must_equal 443
end
it "figure out the correct host with port" do
req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "www2.example.org")
req.host_with_port.must_equal "www2.example.org"
req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "localhost:81")
req.host_with_port.must_equal "localhost:81"
req = make_request \
Rack::MockRequest.env_for("/", "SERVER_NAME" => "example.org", "SERVER_PORT" => "9292")
req.host_with_port.must_equal "example.org:9292"
req = make_request \
Rack::MockRequest.env_for("/", "SERVER_NAME" => "example.org")
req.host_with_port.must_equal "example.org"
req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "localhost:81", "HTTP_X_FORWARDED_HOST" => "example.org:9292")
req.host_with_port.must_equal "example.org:9292"
req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "localhost:81", "HTTP_X_FORWARDED_HOST" => "[2001:db8:cafe::17]:47011")
req.host_with_port.must_equal "[2001:db8:cafe::17]:47011"
req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "localhost:81", "HTTP_X_FORWARDED_HOST" => "2001:db8:cafe::17")
req.host_with_port.must_equal "[2001:db8:cafe::17]"
req = make_request \
Rack::MockRequest.env_for("/", "HTTP_HOST" => "localhost:81", "HTTP_X_FORWARDED_HOST" => "example.org", "SERVER_PORT" => "9393")
req.host_with_port.must_equal "example.org"
end
it "parse the query string" do
req = make_request(Rack::MockRequest.env_for("/?foo=bar&quux=bla"))
req.query_string.must_equal "foo=bar&quux=bla"
req.GET.must_equal "foo" => "bar", "quux" => "bla"
req.POST.must_be :empty?
req.params.must_equal "foo" => "bar", "quux" => "bla"
end
it "not truncate query strings containing semi-colons #543 only in POST" do
mr = Rack::MockRequest.env_for("/",
"REQUEST_METHOD" => 'POST',
:input => "foo=bar&quux=b;la")
req = make_request mr
req.query_string.must_equal ""
req.GET.must_be :empty?
req.POST.must_equal "foo" => "bar", "quux" => "b;la"
req.params.must_equal req.GET.merge(req.POST)
end
it "should use the query_parser for query parsing" do
c = Class.new(Rack::QueryParser::Params) do
def initialize(*)
super
@params = Hash.new{|h, k| h[k.to_s] if k.is_a?(Symbol)}
end
end
parser = Rack::QueryParser.new(c, 65536, 100)
c = Class.new(Rack::Request) do
define_method(:query_parser) do
parser
end
end
req = c.new(Rack::MockRequest.env_for("/?foo=bar&quux=bla"))
req.GET[:foo].must_equal "bar"
req.GET[:quux].must_equal "bla"
req.params[:foo].must_equal "bar"
req.params[:quux].must_equal "bla"
end
it "does not use semi-colons as separators for query strings in GET" do
req = make_request(Rack::MockRequest.env_for("/?foo=bar&quux=b;la;wun=duh"))
req.query_string.must_equal "foo=bar&quux=b;la;wun=duh"
req.GET.must_equal "foo" => "bar", "quux" => "b;la;wun=duh"
req.POST.must_be :empty?
req.params.must_equal "foo" => "bar", "quux" => "b;la;wun=duh"
end
it "limit the keys from the GET query string" do
env = Rack::MockRequest.env_for("/?foo=bar")
old, Rack::Utils.key_space_limit = Rack::Utils.key_space_limit, 1
begin
req = make_request(env)
lambda { req.GET }.must_raise RangeError
ensure
Rack::Utils.key_space_limit = old
end
end
it "limit the key size per nested params hash" do
nested_query = Rack::MockRequest.env_for("/?foo%5Bbar%5D%5Bbaz%5D%5Bqux%5D=1")
plain_query = Rack::MockRequest.env_for("/?foo_bar__baz__qux_=1")
old, Rack::Utils.key_space_limit = Rack::Utils.key_space_limit, 3
begin
exp = { "foo" => { "bar" => { "baz" => { "qux" => "1" } } } }
make_request(nested_query).GET.must_equal exp
lambda { make_request(plain_query).GET }.must_raise RangeError
ensure
Rack::Utils.key_space_limit = old
end
end
it "limit the allowed parameter depth when parsing parameters" do
env = Rack::MockRequest.env_for("/?a#{'[a]' * 40}=b")
req = make_request(env)
lambda { req.GET }.must_raise RangeError
env = Rack::MockRequest.env_for("/?a#{'[a]' * 30}=b")
req = make_request(env)
params = req.GET
30.times { params = params['a'] }
params['a'].must_equal 'b'
old, Rack::Utils.param_depth_limit = Rack::Utils.param_depth_limit, 3
begin
env = Rack::MockRequest.env_for("/?a[a][a]=b")
req = make_request(env)
req.GET['a']['a']['a'].must_equal 'b'
env = Rack::MockRequest.env_for("/?a[a][a][a]=b")
req = make_request(env)
lambda { make_request(env).GET }.must_raise RangeError
ensure
Rack::Utils.param_depth_limit = old
end
end
it "not unify GET and POST when calling params" do
mr = Rack::MockRequest.env_for("/?foo=quux",
"REQUEST_METHOD" => 'POST',
:input => "foo=bar&quux=bla"
)
req = make_request mr
req.params
req.GET.must_equal "foo" => "quux"
req.POST.must_equal "foo" => "bar", "quux" => "bla"
req.params.must_equal req.GET.merge(req.POST)
end
it "use the query_parser's params_class for multipart params" do
c = Class.new(Rack::QueryParser::Params) do
def initialize(*)
super
@params = Hash.new{|h, k| h[k.to_s] if k.is_a?(Symbol)}
end
end
parser = Rack::QueryParser.new(c, 65536, 100)
c = Class.new(Rack::Request) do
define_method(:query_parser) do
parser
end
end
mr = Rack::MockRequest.env_for("/?foo=quux",
"REQUEST_METHOD" => 'POST',
:input => "foo=bar&quux=bla"
)
req = c.new mr
req.params
req.GET[:foo].must_equal "quux"
req.POST[:foo].must_equal "bar"
req.POST[:quux].must_equal "bla"
req.params[:foo].must_equal "bar"
req.params[:quux].must_equal "bla"
end
it "raise if input params has invalid %-encoding" do
mr = Rack::MockRequest.env_for("/?foo=quux",
"REQUEST_METHOD" => 'POST',
:input => "a%=1"
)
req = make_request mr
lambda { req.POST }.must_raise(Rack::Utils::InvalidParameterError).
message.must_equal "invalid %-encoding (a%)"
end
it "raise if rack.input is missing" do
req = make_request({})
lambda { req.POST }.must_raise RuntimeError
end
it "parse POST data when method is POST and no Content-Type given" do
req = make_request \
Rack::MockRequest.env_for("/?foo=quux",
"REQUEST_METHOD" => 'POST',
:input => "foo=bar&quux=bla")
req.content_type.must_be_nil
req.media_type.must_be_nil
req.query_string.must_equal "foo=quux"
req.GET.must_equal "foo" => "quux"
req.POST.must_equal "foo" => "bar", "quux" => "bla"
req.params.must_equal "foo" => "bar", "quux" => "bla"
end
it "limit the keys from the POST form data" do
env = Rack::MockRequest.env_for("",
"REQUEST_METHOD" => 'POST',
:input => "foo=bar&quux=bla")
old, Rack::Utils.key_space_limit = Rack::Utils.key_space_limit, 1
begin
req = make_request(env)
lambda { req.POST }.must_raise RangeError
ensure
Rack::Utils.key_space_limit = old
end
end
it "parse POST data with explicit content type regardless of method" do
req = make_request \
Rack::MockRequest.env_for("/",
"CONTENT_TYPE" => 'application/x-www-form-urlencoded;foo=bar',
:input => "foo=bar&quux=bla")
req.content_type.must_equal 'application/x-www-form-urlencoded;foo=bar'
req.media_type.must_equal 'application/x-www-form-urlencoded'
req.media_type_params['foo'].must_equal 'bar'
req.POST.must_equal "foo" => "bar", "quux" => "bla"
req.params.must_equal "foo" => "bar", "quux" => "bla"
end
it "not parse POST data when media type is not form-data" do
req = make_request \
Rack::MockRequest.env_for("/?foo=quux",
"REQUEST_METHOD" => 'POST',
"CONTENT_TYPE" => 'text/plain;charset=utf-8',
:input => "foo=bar&quux=bla")
req.content_type.must_equal 'text/plain;charset=utf-8'
req.media_type.must_equal 'text/plain'
req.media_type_params['charset'].must_equal 'utf-8'
req.content_charset.must_equal 'utf-8'
req.POST.must_be :empty?
req.params.must_equal "foo" => "quux"
req.body.read.must_equal "foo=bar&quux=bla"
end
it "parse POST data on PUT when media type is form-data" do
req = make_request \
Rack::MockRequest.env_for("/?foo=quux",
"REQUEST_METHOD" => 'PUT',
"CONTENT_TYPE" => 'application/x-www-form-urlencoded',
:input => "foo=bar&quux=bla")
req.POST.must_equal "foo" => "bar", "quux" => "bla"
req.body.read.must_equal "foo=bar&quux=bla"
end
it "rewind input after parsing POST data" do
input = StringIO.new("foo=bar&quux=bla")
req = make_request \
Rack::MockRequest.env_for("/",
"CONTENT_TYPE" => 'application/x-www-form-urlencoded;foo=bar',
:input => input)
req.params.must_equal "foo" => "bar", "quux" => "bla"
input.read.must_equal "foo=bar&quux=bla"
end
it "safely accepts POST requests with empty body" do
mr = Rack::MockRequest.env_for("/",
"REQUEST_METHOD" => "POST",
"CONTENT_TYPE" => "multipart/form-data, boundary=AaB03x",
"CONTENT_LENGTH" => '0',
:input => nil)
req = make_request mr
req.query_string.must_equal ""
req.GET.must_be :empty?
req.POST.must_be :empty?
req.params.must_equal({})
end
it "clean up Safari's ajax POST body" do
req = make_request \
Rack::MockRequest.env_for("/",
'REQUEST_METHOD' => 'POST', :input => "foo=bar&quux=bla\0")
req.POST.must_equal "foo" => "bar", "quux" => "bla"
end
it "get value by key from params with #[]" do
req = make_request \
Rack::MockRequest.env_for("?foo=quux")
assert_output(nil, /deprecated/) do
req['foo'].must_equal 'quux'
req[:foo].must_equal 'quux'
end
next if self.class == TestProxyRequest
verbose = $VERBOSE
warn_arg = nil
req.define_singleton_method(:warn) do |*args|
warn_arg = args
end
begin
$VERBOSE = true
req['foo'].must_equal 'quux'
warn_arg.must_equal ["Request#[] is deprecated and will be removed in a future version of Rack. Please use request.params[] instead", { uplevel: 1 }]
ensure
$VERBOSE = verbose
end
end
it "set value to key on params with #[]=" do
req = make_request \
Rack::MockRequest.env_for("?foo=duh")
assert_output(nil, /deprecated/) do
req['foo'].must_equal 'duh'
req[:foo].must_equal 'duh'
end
req.params.must_equal 'foo' => 'duh'
if req.delegate?
skip "delegate requests don't cache params, so mutations have no impact"
end
assert_output(nil, /deprecated/) do
req['foo'] = 'bar'
end
req.params.must_equal 'foo' => 'bar'
assert_output(nil, /deprecated/) do
req['foo'].must_equal 'bar'
req[:foo].must_equal 'bar'
end
assert_output(nil, /deprecated/) do
req[:foo] = 'jaz'
end
req.params.must_equal 'foo' => 'jaz'
assert_output(nil, /deprecated/) do
req['foo'].must_equal 'jaz'
req[:foo].must_equal 'jaz'
end
verbose = $VERBOSE
warn_arg = nil
req.define_singleton_method(:warn) do |*args|
warn_arg = args
end
begin
$VERBOSE = true
req['foo'] = 'quux'
warn_arg.must_equal ["Request#[]= is deprecated and will be removed in a future version of Rack. Please use request.params[]= instead", { uplevel: 1 }]
req.params['foo'].must_equal 'quux'
ensure
$VERBOSE = verbose
end
end
it "return values for the keys in the order given from values_at" do
req = make_request \
Rack::MockRequest.env_for("?foo=baz&wun=der&bar=ful")
req.values_at('foo').must_equal ['baz']
req.values_at('foo', 'wun').must_equal ['baz', 'der']
req.values_at('bar', 'foo', 'wun').must_equal ['ful', 'baz', 'der']
end
it "extract referrer correctly" do
req = make_request \
Rack::MockRequest.env_for("/", "HTTP_REFERER" => "/some/path")
req.referer.must_equal "/some/path"
req = make_request \
Rack::MockRequest.env_for("/")
req.referer.must_be_nil
end
it "extract user agent correctly" do
req = make_request \
Rack::MockRequest.env_for("/", "HTTP_USER_AGENT" => "Mozilla/4.0 (compatible)")
req.user_agent.must_equal "Mozilla/4.0 (compatible)"
req = make_request \
Rack::MockRequest.env_for("/")
req.user_agent.must_be_nil
end
it "treat missing content type as nil" do
req = make_request \
Rack::MockRequest.env_for("/")
req.content_type.must_be_nil
end
it "treat empty content type as nil" do
req = make_request \
Rack::MockRequest.env_for("/", "CONTENT_TYPE" => "")
req.content_type.must_be_nil
end
it "return nil media type for empty content type" do
req = make_request \
Rack::MockRequest.env_for("/", "CONTENT_TYPE" => "")
req.media_type.must_be_nil
end
it "cache, but invalidates the cache" do
req = make_request \
Rack::MockRequest.env_for("/?foo=quux",
"CONTENT_TYPE" => "application/x-www-form-urlencoded",
:input => "foo=bar&quux=bla")
req.GET.must_equal "foo" => "quux"
req.GET.must_equal "foo" => "quux"
req.set_header("QUERY_STRING", "bla=foo")
req.GET.must_equal "bla" => "foo"
req.GET.must_equal "bla" => "foo"
req.POST.must_equal "foo" => "bar", "quux" => "bla"
req.POST.must_equal "foo" => "bar", "quux" => "bla"
req.set_header("rack.input", StringIO.new("foo=bla&quux=bar"))
req.POST.must_equal "foo" => "bla", "quux" => "bar"
req.POST.must_equal "foo" => "bla", "quux" => "bar"
end
it "figure out if called via XHR" do
req = make_request(Rack::MockRequest.env_for(""))
req.wont_be :xhr?
req = make_request \
Rack::MockRequest.env_for("", "HTTP_X_REQUESTED_WITH" => "XMLHttpRequest")
req.must_be :xhr?
end
it "ssl detection" do
request = make_request(Rack::MockRequest.env_for("/"))
request.scheme.must_equal "http"
request.wont_be :ssl?
request = make_request(Rack::MockRequest.env_for("/", 'HTTPS' => 'on'))
request.scheme.must_equal "https"
request.must_be :ssl?
request = make_request(Rack::MockRequest.env_for("/", 'rack.url_scheme' => 'https'))
request.scheme.must_equal "https"
request.must_be :ssl?
request = make_request(Rack::MockRequest.env_for("/", 'rack.url_scheme' => 'wss'))
request.scheme.must_equal "wss"
request.must_be :ssl?
request = make_request(Rack::MockRequest.env_for("/", 'HTTP_HOST' => 'www.example.org:8080'))
request.scheme.must_equal "http"
request.wont_be :ssl?
request = make_request(Rack::MockRequest.env_for("/", 'HTTP_HOST' => 'www.example.org:8443', 'HTTPS' => 'on'))
request.scheme.must_equal "https"
request.must_be :ssl?
request = make_request(Rack::MockRequest.env_for("/", 'HTTP_HOST' => 'www.example.org:8443', 'HTTP_X_FORWARDED_SSL' => 'on'))
request.scheme.must_equal "https"
request.must_be :ssl?
request = make_request(Rack::MockRequest.env_for("/", 'HTTP_X_FORWARDED_SCHEME' => 'https'))
request.scheme.must_equal "https"
request.must_be :ssl?
request = make_request(Rack::MockRequest.env_for("/", 'HTTP_X_FORWARDED_PROTO' => 'https'))
request.scheme.must_equal "https"
request.must_be :ssl?
request = make_request(Rack::MockRequest.env_for("/", 'HTTP_X_FORWARDED_PROTO' => 'https, http, http'))
request.scheme.must_equal "https"
request.must_be :ssl?
end
it "prevents scheme abuse" do
request = make_request(Rack::MockRequest.env_for("/", 'HTTP_X_FORWARDED_SCHEME' => 'a."><script>alert(1)</script>'))
request.scheme.must_equal 'http'
end
it "parse cookies" do
req = make_request \
Rack::MockRequest.env_for("", "HTTP_COOKIE" => "foo=bar;quux=h&m")
req.cookies.must_equal "foo" => "bar", "quux" => "h&m"
req.delete_header("HTTP_COOKIE")
req.cookies.must_equal({})
end
it "always return the same hash object" do
req = make_request \
Rack::MockRequest.env_for("", "HTTP_COOKIE" => "foo=bar;quux=h&m")
hash = req.cookies
req.env.delete("HTTP_COOKIE")
req.cookies.must_equal hash
req.env["HTTP_COOKIE"] = "zoo=m"
req.cookies.must_equal hash
end
it "modify the cookies hash in place" do
req = make_request(Rack::MockRequest.env_for(""))
req.cookies.must_equal({})
req.cookies['foo'] = 'bar'
req.cookies.must_equal 'foo' => 'bar'
end
it "not modify the params hash in place" do
e = Rack::MockRequest.env_for("")
req1 = make_request(e)
if req1.delegate?
skip "delegate requests don't cache params, so mutations have no impact"
end
req1.params.must_equal({})
req1.params['foo'] = 'bar'
req1.params.must_equal 'foo' => 'bar'
req2 = make_request(e)
req2.params.must_equal({})
end
it "modify params hash if param is in GET" do
e = Rack::MockRequest.env_for("?foo=duh")
req1 = make_request(e)
req1.params.must_equal 'foo' => 'duh'
req1.update_param 'foo', 'bar'
req1.params.must_equal 'foo' => 'bar'
req2 = make_request(e)
req2.params.must_equal 'foo' => 'bar'
end
it "modify params hash if param is in POST" do
e = Rack::MockRequest.env_for("", "REQUEST_METHOD" => 'POST', :input => 'foo=duh')
req1 = make_request(e)
req1.params.must_equal 'foo' => 'duh'
req1.update_param 'foo', 'bar'
req1.params.must_equal 'foo' => 'bar'
req2 = make_request(e)
req2.params.must_equal 'foo' => 'bar'
end
it "modify params hash, even if param didn't exist before" do
e = Rack::MockRequest.env_for("")
req1 = make_request(e)
req1.params.must_equal({})
req1.update_param 'foo', 'bar'
req1.params.must_equal 'foo' => 'bar'
req2 = make_request(e)
req2.params.must_equal 'foo' => 'bar'
end
it "modify params hash by changing only GET" do
e = Rack::MockRequest.env_for("?foo=duhget")
req = make_request(e)
req.GET.must_equal 'foo' => 'duhget'
req.POST.must_equal({})
req.update_param 'foo', 'bar'
req.GET.must_equal 'foo' => 'bar'
req.POST.must_equal({})
end
it "modify params hash by changing only POST" do
e = Rack::MockRequest.env_for("", "REQUEST_METHOD" => 'POST', :input => "foo=duhpost")
req = make_request(e)
req.GET.must_equal({})
req.POST.must_equal 'foo' => 'duhpost'
req.update_param 'foo', 'bar'
req.GET.must_equal({})
req.POST.must_equal 'foo' => 'bar'
end
it "modify params hash, even if param is defined in both POST and GET" do
e = Rack::MockRequest.env_for("?foo=duhget", "REQUEST_METHOD" => 'POST', :input => "foo=duhpost")
req1 = make_request(e)
req1.GET.must_equal 'foo' => 'duhget'
req1.POST.must_equal 'foo' => 'duhpost'
req1.params.must_equal 'foo' => 'duhpost'
req1.update_param 'foo', 'bar'
req1.GET.must_equal 'foo' => 'bar'
req1.POST.must_equal 'foo' => 'bar'
req1.params.must_equal 'foo' => 'bar'
req2 = make_request(e)
req2.GET.must_equal 'foo' => 'bar'
req2.POST.must_equal 'foo' => 'bar'
req2.params.must_equal 'foo' => 'bar'
req2.params.must_equal 'foo' => 'bar'
end
it "allow deleting from params hash if param is in GET" do
e = Rack::MockRequest.env_for("?foo=bar")
req1 = make_request(e)
req1.params.must_equal 'foo' => 'bar'
req1.delete_param('foo').must_equal 'bar'
req1.params.must_equal({})
req2 = make_request(e)
req2.params.must_equal({})
end
it "allow deleting from params hash if param is in POST" do
e = Rack::MockRequest.env_for("", "REQUEST_METHOD" => 'POST', :input => 'foo=bar')
req1 = make_request(e)
req1.params.must_equal 'foo' => 'bar'
req1.delete_param('foo').must_equal 'bar'
req1.params.must_equal({})
req2 = make_request(e)
req2.params.must_equal({})
end
it "pass through non-uri escaped cookies as-is" do
req = make_request Rack::MockRequest.env_for("", "HTTP_COOKIE" => "foo=%")
req.cookies["foo"].must_equal "%"
end
it "parse cookies according to RFC 2109" do
req = make_request \
Rack::MockRequest.env_for('', 'HTTP_COOKIE' => 'foo=bar;foo=car')
req.cookies.must_equal 'foo' => 'bar'
end
it 'parse cookies with quotes' do
req = make_request Rack::MockRequest.env_for('', {
'HTTP_COOKIE' => '$Version="1"; Customer="WILE_E_COYOTE"; $Path="/acme"; Part_Number="Rocket_Launcher_0001"; $Path="/acme"'
})
req.cookies.must_equal({
'$Version' => '"1"',
'Customer' => '"WILE_E_COYOTE"',
'$Path' => '"/acme"',
'Part_Number' => '"Rocket_Launcher_0001"',
})
end
it "provide setters" do
req = make_request(e = Rack::MockRequest.env_for(""))
req.script_name.must_equal ""
req.script_name = "/foo"
req.script_name.must_equal "/foo"
e["SCRIPT_NAME"].must_equal "/foo"
req.path_info.must_equal "/"
req.path_info = "/foo"
req.path_info.must_equal "/foo"
e["PATH_INFO"].must_equal "/foo"
end
it "provide the original env" do
req = make_request(e = Rack::MockRequest.env_for(""))
req.env.must_equal e
end
it "restore the base URL" do
make_request(Rack::MockRequest.env_for("")).base_url.
must_equal "http://example.org"
make_request(Rack::MockRequest.env_for("", "SCRIPT_NAME" => "/foo")).base_url.
must_equal "http://example.org"
end
it "restore the URL" do
make_request(Rack::MockRequest.env_for("")).url.
must_equal "http://example.org/"
make_request(Rack::MockRequest.env_for("", "SCRIPT_NAME" => "/foo")).url.
must_equal "http://example.org/foo/"
make_request(Rack::MockRequest.env_for("/foo")).url.
must_equal "http://example.org/foo"
make_request(Rack::MockRequest.env_for("?foo")).url.
must_equal "http://example.org/?foo"
make_request(Rack::MockRequest.env_for("http://example.org:8080/")).url.
must_equal "http://example.org:8080/"
make_request(Rack::MockRequest.env_for("https://example.org/")).url.
must_equal "https://example.org/"
make_request(Rack::MockRequest.env_for("coffee://example.org/")).url.
must_equal "coffee://example.org/"
make_request(Rack::MockRequest.env_for("coffee://example.org:443/")).url.
must_equal "coffee://example.org:443/"
make_request(Rack::MockRequest.env_for("https://example.com:8080/foo?foo")).url.
must_equal "https://example.com:8080/foo?foo"
end
it "restore the full path" do
make_request(Rack::MockRequest.env_for("")).fullpath.
must_equal "/"
make_request(Rack::MockRequest.env_for("", "SCRIPT_NAME" => "/foo")).fullpath.
must_equal "/foo/"
make_request(Rack::MockRequest.env_for("/foo")).fullpath.
must_equal "/foo"
make_request(Rack::MockRequest.env_for("?foo")).fullpath.
must_equal "/?foo"
make_request(Rack::MockRequest.env_for("http://example.org:8080/")).fullpath.
must_equal "/"
make_request(Rack::MockRequest.env_for("https://example.org/")).fullpath.
must_equal "/"
make_request(Rack::MockRequest.env_for("https://example.com:8080/foo?foo")).fullpath.
must_equal "/foo?foo"
end
it "handle multiple media type parameters" do
req = make_request \
Rack::MockRequest.env_for("/",
"CONTENT_TYPE" => 'text/plain; foo=BAR,baz=bizzle dizzle;BLING=bam;blong="boo";zump="zoo\"o";weird=lol"')
req.wont_be :form_data?
req.media_type_params.must_include 'foo'
req.media_type_params['foo'].must_equal 'BAR'
req.media_type_params.must_include 'baz'
req.media_type_params['baz'].must_equal 'bizzle dizzle'
req.media_type_params.wont_include 'BLING'
req.media_type_params.must_include 'bling'
req.media_type_params['bling'].must_equal 'bam'
req.media_type_params['blong'].must_equal 'boo'
req.media_type_params['zump'].must_equal 'zoo\"o'
req.media_type_params['weird'].must_equal 'lol"'
end
it "parse with junk before boundary" do
# Adapted from RFC 1867.
input = <<EOF
blah blah\r
\r
--AaB03x\r
content-disposition: form-data; name="reply"\r
\r
yes\r
--AaB03x\r
content-disposition: form-data; name="fileupload"; filename="dj.jpg"\r
Content-Type: image/jpeg\r
Content-Transfer-Encoding: base64\r
\r
/9j/4AAQSkZJRgABAQAAAQABAAD//gA+Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcg\r
--AaB03x--\r
EOF
req = make_request Rack::MockRequest.env_for("/",
"CONTENT_TYPE" => "multipart/form-data, boundary=AaB03x",
"CONTENT_LENGTH" => input.size,
:input => input)
req.POST.must_include "fileupload"
req.POST.must_include "reply"
req.must_be :form_data?
req.content_length.must_equal input.size
req.media_type.must_equal 'multipart/form-data'
req.media_type_params.must_include 'boundary'
req.media_type_params['boundary'].must_equal 'AaB03x'
req.POST["reply"].must_equal "yes"
f = req.POST["fileupload"]
f.must_be_kind_of Hash
f[:type].must_equal "image/jpeg"
f[:filename].must_equal "dj.jpg"
f.must_include :tempfile
f[:tempfile].size.must_equal 76
end
it "not infinite loop with a malformed HTTP request" do
# Adapted from RFC 1867.
input = <<EOF
--AaB03x
content-disposition: form-data; name="reply"
yes
--AaB03x
content-disposition: form-data; name="fileupload"; filename="dj.jpg"
Content-Type: image/jpeg
Content-Transfer-Encoding: base64
/9j/4AAQSkZJRgABAQAAAQABAAD//gA+Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcg
--AaB03x--
EOF
req = make_request Rack::MockRequest.env_for("/",
"CONTENT_TYPE" => "multipart/form-data, boundary=AaB03x",
"CONTENT_LENGTH" => input.size,
:input => input)