File: spec_multipart.rb

package info (click to toggle)
ruby-rack 2.1.4-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,492 kB
  • sloc: ruby: 14,778; sh: 12; makefile: 6; javascript: 1
file content (762 lines) | stat: -rw-r--r-- 29,338 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
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
# frozen_string_literal: true

require 'minitest/global_expectations/autorun'
require 'rack'
require 'rack/multipart'
require 'rack/multipart/parser'
require 'rack/utils'
require 'rack/mock'
require 'timeout'

describe Rack::Multipart do
  def multipart_fixture(name, boundary = "AaB03x")
    file = multipart_file(name)
    data = File.open(file, 'rb') { |io| io.read }

    type = %(multipart/form-data; boundary=#{boundary})
    length = data.bytesize

    { "CONTENT_TYPE" => type,
      "CONTENT_LENGTH" => length.to_s,
      :input => StringIO.new(data) }
  end

  def multipart_file(name)
    File.join(File.dirname(__FILE__), "multipart", name.to_s)
  end

  it "return nil if content type is not multipart" do
    env = Rack::MockRequest.env_for("/",
            "CONTENT_TYPE" => 'application/x-www-form-urlencoded')
    Rack::Multipart.parse_multipart(env).must_be_nil
  end

  it "parse multipart content when content type present but filename is not" do
    env = Rack::MockRequest.env_for("/", multipart_fixture(:content_type_and_no_filename))
    params = Rack::Multipart.parse_multipart(env)
    params["text"].must_equal "contents"
  end

  it "set US_ASCII encoding based on charset" do
    env = Rack::MockRequest.env_for("/", multipart_fixture(:content_type_and_no_filename))
    params = Rack::Multipart.parse_multipart(env)
    params["text"].encoding.must_equal Encoding::US_ASCII

    # I'm not 100% sure if making the param name encoding match the
    # Content-Type charset is the right thing to do.  We should revisit this.
    params.keys.each do |key|
      key.encoding.must_equal Encoding::US_ASCII
    end
  end

  it "set BINARY encoding on things without content type" do
    env = Rack::MockRequest.env_for("/", multipart_fixture(:none))
    params = Rack::Multipart.parse_multipart(env)
    params["submit-name"].encoding.must_equal Encoding::UTF_8
  end

  it "set UTF8 encoding on names of things without content type" do
    env = Rack::MockRequest.env_for("/", multipart_fixture(:none))
    params = Rack::Multipart.parse_multipart(env)
    params.keys.each do |key|
      key.encoding.must_equal Encoding::UTF_8
    end
  end

  it "default text to UTF8" do
    env = Rack::MockRequest.env_for("/", multipart_fixture(:text))
    params = Rack::Multipart.parse_multipart(env)
    params['submit-name'].encoding.must_equal Encoding::UTF_8
    params['submit-name-with-content'].encoding.must_equal Encoding::UTF_8
    params.keys.each do |key|
      key.encoding.must_equal Encoding::UTF_8
    end
  end

  it "handles quoted encodings" do
    # See #905
    env = Rack::MockRequest.env_for("/", multipart_fixture(:unity3d_wwwform))
    params = Rack::Multipart.parse_multipart(env)
    params['user_sid'].encoding.must_equal Encoding::UTF_8
  end

  it "raise RangeError if the key space is exhausted" do
    env = Rack::MockRequest.env_for("/", multipart_fixture(:content_type_and_no_filename))

    old, Rack::Utils.key_space_limit = Rack::Utils.key_space_limit, 1
    begin
      lambda { Rack::Multipart.parse_multipart(env) }.must_raise(RangeError)
    ensure
      Rack::Utils.key_space_limit = old
    end
  end

  it "parse multipart form webkit style" do
    env = Rack::MockRequest.env_for '/', multipart_fixture(:webkit)
    env['CONTENT_TYPE'] = "multipart/form-data; boundary=----WebKitFormBoundaryWLHCs9qmcJJoyjKR"
    params = Rack::Multipart.parse_multipart(env)
    params['profile']['bio'].must_include 'hello'
    params['profile'].keys.must_include 'public_email'
  end

  it "reject insanely long boundaries" do
    # using a pipe since a tempfile can use up too much space
    rd, wr = IO.pipe

    # we only call rewind once at start, so make sure it succeeds
    # and doesn't hit ESPIPE
    def rd.rewind; end
    wr.sync = true

    # write to a pipe in a background thread, this will write a lot
    # unless Rack (properly) shuts down the read end
    thr = Thread.new do
      begin
        wr.write("--AaB03x")

        # make the initial boundary a few gigs long
        longer = "0123456789" * 1024 * 1024
        (1024 * 1024).times { wr.write(longer) }

        wr.write("\r\n")
        wr.write('Content-Disposition: form-data; name="a"; filename="a.txt"')
        wr.write("\r\n")
        wr.write("Content-Type: text/plain\r\n")
        wr.write("\r\na")
        wr.write("--AaB03x--\r\n")
        wr.close
      rescue => err # this is EPIPE if Rack shuts us down
        err
      end
    end

    fixture = {
      "CONTENT_TYPE" => "multipart/form-data; boundary=AaB03x",
      "CONTENT_LENGTH" => (1024 * 1024 * 8).to_s,
      :input => rd,
    }

    env = Rack::MockRequest.env_for '/', fixture
    lambda {
      Rack::Multipart.parse_multipart(env)
    }.must_raise EOFError
    rd.close

    err = thr.value
    err.must_be_instance_of Errno::EPIPE
    wr.close
  end

  # see https://github.com/rack/rack/pull/1309
  it "parse strange multipart pdf" do
    boundary = '---------------------------932620571087722842402766118'

    data = StringIO.new
    data.write("--#{boundary}")
    data.write("\r\n")
    data.write('Content-Disposition: form-data; name="a"; filename="a.pdf"')
    data.write("\r\n")
    data.write("Content-Type:application/pdf\r\n")
    data.write("\r\n")
    data.write("-" * (1024 * 1024))
    data.write("\r\n")
    data.write("--#{boundary}--\r\n")

    fixture = {
      "CONTENT_TYPE" => "multipart/form-data; boundary=#{boundary}",
      "CONTENT_LENGTH" => data.length.to_s,
      :input => data,
    }

    env = Rack::MockRequest.env_for '/', fixture
    Timeout::timeout(10) { Rack::Multipart.parse_multipart(env) }
  end

  it 'raises an EOF error on content-length mistmatch' do
    env = Rack::MockRequest.env_for("/", multipart_fixture(:empty))
    env['rack.input'] = StringIO.new
    assert_raises(EOFError) do
      Rack::Multipart.parse_multipart(env)
    end
  end

  it "parse multipart upload with text file" do
    env = Rack::MockRequest.env_for("/", multipart_fixture(:text))
    params = Rack::Multipart.parse_multipart(env)
    params["submit-name"].must_equal "Larry"
    params["submit-name-with-content"].must_equal "Berry"
    params["files"][:type].must_equal "text/plain"
    params["files"][:filename].must_equal "file1.txt"
    params["files"][:head].must_equal "Content-Disposition: form-data; " +
      "name=\"files\"; filename=\"file1.txt\"\r\n" +
      "Content-Type: text/plain\r\n"
    params["files"][:name].must_equal "files"
    params["files"][:tempfile].read.must_equal "contents"
  end

  it "accept the params hash class to use for multipart 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
    query_parser = Rack::QueryParser.new c, 65536, 100
    env = Rack::MockRequest.env_for("/", multipart_fixture(:text))
    params = Rack::Multipart.parse_multipart(env, query_parser)
    params[:files][:type].must_equal "text/plain"
  end

  it "preserve extension in the created tempfile" do
    env = Rack::MockRequest.env_for("/", multipart_fixture(:text))
    params = Rack::Multipart.parse_multipart(env)
    File.extname(params["files"][:tempfile].path).must_equal ".txt"
  end

  it "parse multipart upload with text file with no name field" do
    env = Rack::MockRequest.env_for("/", multipart_fixture(:filename_and_no_name))
    params = Rack::Multipart.parse_multipart(env)
    params["file1.txt"][:type].must_equal "text/plain"
    params["file1.txt"][:filename].must_equal "file1.txt"
    params["file1.txt"][:head].must_equal "Content-Disposition: form-data; " +
      "filename=\"file1.txt\"\r\n" +
      "Content-Type: text/plain\r\n"
    params["file1.txt"][:name].must_equal "file1.txt"
    params["file1.txt"][:tempfile].read.must_equal "contents"
  end

  it "parse multipart upload file using custom tempfile class" do
    env = Rack::MockRequest.env_for("/", multipart_fixture(:text))
    my_tempfile = "".dup
    env['rack.multipart.tempfile_factory'] = lambda { |filename, content_type| my_tempfile }
    params = Rack::Multipart.parse_multipart(env)
    params["files"][:tempfile].object_id.must_equal my_tempfile.object_id
    my_tempfile.must_equal "contents"
  end

  it "parse multipart upload with nested parameters" do
    env = Rack::MockRequest.env_for("/", multipart_fixture(:nested))
    params = Rack::Multipart.parse_multipart(env)
    params["foo"]["submit-name"].must_equal "Larry"
    params["foo"]["files"][:type].must_equal "text/plain"
    params["foo"]["files"][:filename].must_equal "file1.txt"
    params["foo"]["files"][:head].must_equal "Content-Disposition: form-data; " +
      "name=\"foo[files]\"; filename=\"file1.txt\"\r\n" +
      "Content-Type: text/plain\r\n"
    params["foo"]["files"][:name].must_equal "foo[files]"
    params["foo"]["files"][:tempfile].read.must_equal "contents"
  end

  it "parse multipart upload with binary file" do
    env = Rack::MockRequest.env_for("/", multipart_fixture(:binary))
    params = Rack::Multipart.parse_multipart(env)
    params["submit-name"].must_equal "Larry"

    params["files"][:type].must_equal "image/png"
    params["files"][:filename].must_equal "rack-logo.png"
    params["files"][:head].must_equal "Content-Disposition: form-data; " +
      "name=\"files\"; filename=\"rack-logo.png\"\r\n" +
      "Content-Type: image/png\r\n"
    params["files"][:name].must_equal "files"
    params["files"][:tempfile].read.length.must_equal 26473
  end

  it "parse multipart upload with empty file" do
    env = Rack::MockRequest.env_for("/", multipart_fixture(:empty))
    params = Rack::Multipart.parse_multipart(env)
    params["submit-name"].must_equal "Larry"
    params["files"][:type].must_equal "text/plain"
    params["files"][:filename].must_equal "file1.txt"
    params["files"][:head].must_equal "Content-Disposition: form-data; " +
      "name=\"files\"; filename=\"file1.txt\"\r\n" +
      "Content-Type: text/plain\r\n"
    params["files"][:name].must_equal "files"
    params["files"][:tempfile].read.must_equal ""
  end

  it "parse multipart upload with filename with semicolons" do
    env = Rack::MockRequest.env_for("/", multipart_fixture(:semicolon))
    params = Rack::Multipart.parse_multipart(env)
    params["files"][:type].must_equal "text/plain"
    params["files"][:filename].must_equal "fi;le1.txt"
    params["files"][:head].must_equal "Content-Disposition: form-data; " +
      "name=\"files\"; filename=\"fi;le1.txt\"\r\n" +
      "Content-Type: text/plain\r\n"
    params["files"][:name].must_equal "files"
    params["files"][:tempfile].read.must_equal "contents"
  end

  it "parse multipart upload with quoted boundary" do
    env = Rack::MockRequest.env_for("/", multipart_fixture(:quoted, %("AaB:03x")))
    params = Rack::Multipart.parse_multipart(env)
    params["submit-name"].must_equal "Larry"
    params["submit-name-with-content"].must_equal "Berry"
    params["files"][:type].must_equal "text/plain"
    params["files"][:filename].must_equal "file1.txt"
    params["files"][:head].must_equal "Content-Disposition: form-data; " +
      "name=\"files\"; filename=\"file1.txt\"\r\n" +
      "Content-Type: text/plain\r\n"
    params["files"][:name].must_equal "files"
    params["files"][:tempfile].read.must_equal "contents"
  end

  it "parse multipart upload with filename with invalid characters" do
    env = Rack::MockRequest.env_for("/", multipart_fixture(:invalid_character))
    params = Rack::Multipart.parse_multipart(env)
    params["files"][:type].must_equal "text/plain"
    params["files"][:filename].must_match(/invalid/)
    head = "Content-Disposition: form-data; " +
      "name=\"files\"; filename=\"invalid\xC3.txt\"\r\n" +
      "Content-Type: text/plain\r\n"
    head = head.force_encoding(Encoding::ASCII_8BIT)
    params["files"][:head].must_equal head
    params["files"][:name].must_equal "files"
    params["files"][:tempfile].read.must_equal "contents"
  end

  it "parse multipart form with an encoded word filename" do
    env = Rack::MockRequest.env_for '/', multipart_fixture(:filename_with_encoded_words)
    params = Rack::Multipart.parse_multipart(env)
    params["files"][:filename].must_equal "файл"
  end

  it "parse multipart form with a single quote in the filename" do
    env = Rack::MockRequest.env_for '/', multipart_fixture(:filename_with_single_quote)
    params = Rack::Multipart.parse_multipart(env)
    params["files"][:filename].must_equal "bob's flowers.jpg"
  end

  it "parse multipart form with a null byte in the filename" do
    env = Rack::MockRequest.env_for '/', multipart_fixture(:filename_with_null_byte)
    params = Rack::Multipart.parse_multipart(env)
    params["files"][:filename].must_equal "flowers.exe\u0000.jpg"
  end

  it "is robust separating Content-Disposition fields" do
    env = Rack::MockRequest.env_for("/", multipart_fixture(:robust_field_separation))
    params = Rack::Multipart.parse_multipart(env)
    params["text"].must_equal "contents"
  end

  it "not include file params if no file was selected" do
    env = Rack::MockRequest.env_for("/", multipart_fixture(:none))
    params = Rack::Multipart.parse_multipart(env)
    params["submit-name"].must_equal "Larry"
    params["files"].must_be_nil
    params.keys.wont_include "files"
  end

  it "parse multipart/mixed" do
    env = Rack::MockRequest.env_for("/", multipart_fixture(:mixed_files))
    params = Rack::Multipart.parse_multipart(env)
    params["foo"].must_equal "bar"
    params["files"].must_be_instance_of String
    params["files"].size.must_equal 252
  end

  it "parse IE multipart upload and clean up filename" do
    env = Rack::MockRequest.env_for("/", multipart_fixture(:ie))
    params = Rack::Multipart.parse_multipart(env)
    params["files"][:type].must_equal "text/plain"
    params["files"][:filename].must_equal "file1.txt"
    params["files"][:head].must_equal "Content-Disposition: form-data; " +
      "name=\"files\"; " +
      'filename="C:\Documents and Settings\Administrator\Desktop\file1.txt"' +
      "\r\nContent-Type: text/plain\r\n"
    params["files"][:name].must_equal "files"
    params["files"][:tempfile].read.must_equal "contents"
  end

  it "parse filename and modification param" do
    env = Rack::MockRequest.env_for("/", multipart_fixture(:filename_and_modification_param))
    params = Rack::Multipart.parse_multipart(env)
    params["files"][:type].must_equal "image/jpeg"
    params["files"][:filename].must_equal "genome.jpeg"
    params["files"][:head].must_equal "Content-Type: image/jpeg\r\n" +
      "Content-Disposition: attachment; " +
      "name=\"files\"; " +
      "filename=genome.jpeg; " +
      "modification-date=\"Wed, 12 Feb 1997 16:29:51 -0500\";\r\n" +
      "Content-Description: a complete map of the human genome\r\n"
    params["files"][:name].must_equal "files"
    params["files"][:tempfile].read.must_equal "contents"
  end

  it "parse filename with escaped quotes" do
    env = Rack::MockRequest.env_for("/", multipart_fixture(:filename_with_escaped_quotes))
    params = Rack::Multipart.parse_multipart(env)
    params["files"][:type].must_equal "application/octet-stream"
    params["files"][:filename].must_equal "escape \"quotes"
    params["files"][:head].must_equal "Content-Disposition: form-data; " +
      "name=\"files\"; " +
      "filename=\"escape \\\"quotes\"\r\n" +
      "Content-Type: application/octet-stream\r\n"
    params["files"][:name].must_equal "files"
    params["files"][:tempfile].read.must_equal "contents"
  end

  it "parse filename with plus character" do
    env = Rack::MockRequest.env_for("/", multipart_fixture(:filename_with_plus))
    params = Rack::Multipart.parse_multipart(env)
    params["files"][:type].must_equal "application/octet-stream"
    params["files"][:filename].must_equal "foo+bar"
    params["files"][:head].must_equal "Content-Disposition: form-data; " +
      "name=\"files\"; " +
      "filename=\"foo+bar\"\r\n" +
      "Content-Type: application/octet-stream\r\n"
    params["files"][:name].must_equal "files"
    params["files"][:tempfile].read.must_equal "contents"
  end

  it "parse filename with percent escaped quotes" do
    env = Rack::MockRequest.env_for("/", multipart_fixture(:filename_with_percent_escaped_quotes))
    params = Rack::Multipart.parse_multipart(env)
    params["files"][:type].must_equal "application/octet-stream"
    params["files"][:filename].must_equal "escape \"quotes"
    params["files"][:head].must_equal "Content-Disposition: form-data; " +
      "name=\"files\"; " +
      "filename=\"escape %22quotes\"\r\n" +
      "Content-Type: application/octet-stream\r\n"
    params["files"][:name].must_equal "files"
    params["files"][:tempfile].read.must_equal "contents"
  end

  it "parse filename with unescaped quotes" do
    env = Rack::MockRequest.env_for("/", multipart_fixture(:filename_with_unescaped_quotes))
    params = Rack::Multipart.parse_multipart(env)
    params["files"][:type].must_equal "application/octet-stream"
    params["files"][:filename].must_equal "escape \"quotes"
    params["files"][:head].must_equal "Content-Disposition: form-data; " +
      "name=\"files\"; " +
      "filename=\"escape \"quotes\"\r\n" +
      "Content-Type: application/octet-stream\r\n"
    params["files"][:name].must_equal "files"
    params["files"][:tempfile].read.must_equal "contents"
  end

  it "parse filename with escaped quotes and modification param" do
    env = Rack::MockRequest.env_for("/", multipart_fixture(:filename_with_escaped_quotes_and_modification_param))
    params = Rack::Multipart.parse_multipart(env)
    params["files"][:type].must_equal "image/jpeg"
    params["files"][:filename].must_equal "\"human\" genome.jpeg"
    params["files"][:head].must_equal "Content-Type: image/jpeg\r\n" +
      "Content-Disposition: attachment; " +
      "name=\"files\"; " +
      "filename=\"\"human\" genome.jpeg\"; " +
      "modification-date=\"Wed, 12 Feb 1997 16:29:51 -0500\";\r\n" +
      "Content-Description: a complete map of the human genome\r\n"
    params["files"][:name].must_equal "files"
    params["files"][:tempfile].read.must_equal "contents"
  end

  it "parse filename with unescaped percentage characters" do
    env = Rack::MockRequest.env_for("/", multipart_fixture(:filename_with_unescaped_percentages, "----WebKitFormBoundary2NHc7OhsgU68l3Al"))
    params = Rack::Multipart.parse_multipart(env)
    files = params["document"]["attachment"]
    files[:type].must_equal "image/jpeg"
    files[:filename].must_equal "100% of a photo.jpeg"
    files[:head].must_equal <<-MULTIPART
Content-Disposition: form-data; name="document[attachment]"; filename="100% of a photo.jpeg"\r
Content-Type: image/jpeg\r
    MULTIPART

    files[:name].must_equal "document[attachment]"
    files[:tempfile].read.must_equal "contents"
  end

  it "parse filename with unescaped percentage characters that look like partial hex escapes" do
    env = Rack::MockRequest.env_for("/", multipart_fixture(:filename_with_unescaped_percentages2, "----WebKitFormBoundary2NHc7OhsgU68l3Al"))
    params = Rack::Multipart.parse_multipart(env)
    files = params["document"]["attachment"]
    files[:type].must_equal "image/jpeg"
    files[:filename].must_equal "100%a"
    files[:head].must_equal <<-MULTIPART
Content-Disposition: form-data; name="document[attachment]"; filename="100%a"\r
Content-Type: image/jpeg\r
    MULTIPART

    files[:name].must_equal "document[attachment]"
    files[:tempfile].read.must_equal "contents"
  end

  it "parse filename with unescaped percentage characters that look like partial hex escapes" do
    env = Rack::MockRequest.env_for("/", multipart_fixture(:filename_with_unescaped_percentages3, "----WebKitFormBoundary2NHc7OhsgU68l3Al"))
    params = Rack::Multipart.parse_multipart(env)
    files = params["document"]["attachment"]
    files[:type].must_equal "image/jpeg"
    files[:filename].must_equal "100%"
    files[:head].must_equal <<-MULTIPART
Content-Disposition: form-data; name="document[attachment]"; filename="100%"\r
Content-Type: image/jpeg\r
    MULTIPART

    files[:name].must_equal "document[attachment]"
    files[:tempfile].read.must_equal "contents"
  end

  it "rewinds input after parsing upload" do
    options = multipart_fixture(:text)
    input = options[:input]
    env = Rack::MockRequest.env_for("/", options)
    params = Rack::Multipart.parse_multipart(env)
    params["submit-name"].must_equal "Larry"
    params["files"][:filename].must_equal "file1.txt"
    input.read.length.must_equal 307
  end

  it "builds multipart body" do
    files = Rack::Multipart::UploadedFile.new(multipart_file("file1.txt"))
    data  = Rack::Multipart.build_multipart("submit-name" => "Larry", "files" => files)

    options = {
      "CONTENT_TYPE" => "multipart/form-data; boundary=AaB03x",
      "CONTENT_LENGTH" => data.length.to_s,
      :input => StringIO.new(data)
    }
    env = Rack::MockRequest.env_for("/", options)
    params = Rack::Multipart.parse_multipart(env)
    params["submit-name"].must_equal "Larry"
    params["files"][:filename].must_equal "file1.txt"
    params["files"][:tempfile].read.must_equal "contents"
  end

  it "builds nested multipart body" do
    files = Rack::Multipart::UploadedFile.new(multipart_file("file1.txt"))
    data  = Rack::Multipart.build_multipart("people" => [{ "submit-name" => "Larry", "files" => files }])

    options = {
      "CONTENT_TYPE" => "multipart/form-data; boundary=AaB03x",
      "CONTENT_LENGTH" => data.length.to_s,
      :input => StringIO.new(data)
    }
    env = Rack::MockRequest.env_for("/", options)
    params = Rack::Multipart.parse_multipart(env)
    params["people"][0]["submit-name"].must_equal "Larry"
    params["people"][0]["files"][:filename].must_equal "file1.txt"
    params["people"][0]["files"][:tempfile].read.must_equal "contents"
  end

  it "can parse fields that end at the end of the buffer" do
    input = File.read(multipart_file("bad_robots"))

    req = Rack::Request.new Rack::MockRequest.env_for("/",
                      "CONTENT_TYPE" => "multipart/form-data, boundary=1yy3laWhgX31qpiHinh67wJXqKalukEUTvqTzmon",
                      "CONTENT_LENGTH" => input.size,
                      :input => input)

    req.POST['file.path'].must_equal "/var/tmp/uploads/4/0001728414"
    req.POST['addresses'].wont_equal nil
  end

  it "builds complete params with the chunk size of 16384 slicing exactly on boundary" do
    begin
      previous_limit = Rack::Utils.multipart_part_limit
      Rack::Utils.multipart_part_limit = 256

      data = File.open(multipart_file("fail_16384_nofile"), 'rb') { |f| f.read }.gsub(/\n/, "\r\n")
      options = {
        "CONTENT_TYPE" => "multipart/form-data; boundary=----WebKitFormBoundaryWsY0GnpbI5U7ztzo",
        "CONTENT_LENGTH" => data.length.to_s,
        :input => StringIO.new(data)
      }
      env = Rack::MockRequest.env_for("/", options)
      params = Rack::Multipart.parse_multipart(env)

      params.wont_equal nil
      params.keys.must_include "AAAAAAAAAAAAAAAAAAA"
      params["AAAAAAAAAAAAAAAAAAA"].keys.must_include "PLAPLAPLA_MEMMEMMEMM_ATTRATTRER"
      params["AAAAAAAAAAAAAAAAAAA"]["PLAPLAPLA_MEMMEMMEMM_ATTRATTRER"].keys.must_include "new"
      params["AAAAAAAAAAAAAAAAAAA"]["PLAPLAPLA_MEMMEMMEMM_ATTRATTRER"]["new"].keys.must_include "-2"
      params["AAAAAAAAAAAAAAAAAAA"]["PLAPLAPLA_MEMMEMMEMM_ATTRATTRER"]["new"]["-2"].keys.must_include "ba_unit_id"
      params["AAAAAAAAAAAAAAAAAAA"]["PLAPLAPLA_MEMMEMMEMM_ATTRATTRER"]["new"]["-2"]["ba_unit_id"].must_equal "1017"
    ensure
      Rack::Utils.multipart_part_limit = previous_limit
    end
  end

  it "not reach a multi-part limit" do
    begin
      previous_limit = Rack::Utils.multipart_part_limit
      Rack::Utils.multipart_part_limit = 4

      env = Rack::MockRequest.env_for '/', multipart_fixture(:three_files_three_fields)
      params = Rack::Multipart.parse_multipart(env)
      params['reply'].must_equal 'yes'
      params['to'].must_equal 'people'
      params['from'].must_equal 'others'
    ensure
      Rack::Utils.multipart_part_limit = previous_limit
    end
  end

  it "reach a multipart limit" do
    begin
      previous_limit = Rack::Utils.multipart_part_limit
      Rack::Utils.multipart_part_limit = 3

      env = Rack::MockRequest.env_for '/', multipart_fixture(:three_files_three_fields)
      lambda { Rack::Multipart.parse_multipart(env) }.must_raise Rack::Multipart::MultipartPartLimitError
    ensure
      Rack::Utils.multipart_part_limit = previous_limit
    end
  end

  it "return nil if no UploadedFiles were used" do
    data = Rack::Multipart.build_multipart("people" => [{ "submit-name" => "Larry", "files" => "contents" }])
    data.must_be_nil
  end

  it "raise ArgumentError if params is not a Hash" do
    lambda {
      Rack::Multipart.build_multipart("foo=bar")
    }.must_raise(ArgumentError).message.must_equal "value must be a Hash"
  end

  it "can parse fields with a content type" do
    data = <<-EOF
--1yy3laWhgX31qpiHinh67wJXqKalukEUTvqTzmon\r
Content-Disposition: form-data; name="description"\r
Content-Type: text/plain"\r
\r
Very very blue\r
--1yy3laWhgX31qpiHinh67wJXqKalukEUTvqTzmon--\r
EOF
    options = {
      "CONTENT_TYPE" => "multipart/form-data; boundary=1yy3laWhgX31qpiHinh67wJXqKalukEUTvqTzmon",
      "CONTENT_LENGTH" => data.length.to_s,
      :input => StringIO.new(data)
    }
    env = Rack::MockRequest.env_for("/", options)
    params = Rack::Multipart.parse_multipart(env)

    params.must_equal "description" => "Very very blue"
  end

  it "parse multipart upload with no content-length header" do
    env = Rack::MockRequest.env_for '/', multipart_fixture(:webkit)
    env['CONTENT_TYPE'] = "multipart/form-data; boundary=----WebKitFormBoundaryWLHCs9qmcJJoyjKR"
    env.delete 'CONTENT_LENGTH'
    params = Rack::Multipart.parse_multipart(env)
    params['profile']['bio'].must_include 'hello'
  end

  it "parse very long unquoted multipart file names" do
    data = <<-EOF
--AaB03x\r
Content-Type: text/plain\r
Content-Disposition: attachment; name=file; filename=#{'long' * 100}\r
\r
contents\r
--AaB03x--\r
    EOF

    options = {
      "CONTENT_TYPE" => "multipart/form-data; boundary=AaB03x",
      "CONTENT_LENGTH" => data.length.to_s,
      :input => StringIO.new(data)
    }
    env = Rack::MockRequest.env_for("/", options)
    params = Rack::Multipart.parse_multipart(env)

    params["file"][:filename].must_equal 'long' * 100
  end

  it "parse unquoted parameter values at end of line" do
    data = <<-EOF
--AaB03x\r
Content-Type: text/plain\r
Content-Disposition: attachment; name=inline\r
\r
true\r
--AaB03x--\r
    EOF

    options = {
      "CONTENT_TYPE" => "multipart/form-data; boundary=AaB03x",
      "CONTENT_LENGTH" => data.length.to_s,
      :input => StringIO.new(data)
    }
    env = Rack::MockRequest.env_for("/", options)
    params = Rack::Multipart.parse_multipart(env)
    params["inline"].must_equal 'true'
  end

  it "parse quoted chars in name parameter" do
    data = <<-EOF
--AaB03x\r
Content-Type: text/plain\r
Content-Disposition: attachment; name="quoted\\\\chars\\"in\rname"\r
\r
true\r
--AaB03x--\r
    EOF

    options = {
      "CONTENT_TYPE" => "multipart/form-data; boundary=AaB03x",
      "CONTENT_LENGTH" => data.length.to_s,
      :input => StringIO.new(data)
    }
    env = Rack::MockRequest.env_for("/", options)
    params = Rack::Multipart.parse_multipart(env)
    params["quoted\\chars\"in\rname"].must_equal 'true'
  end

  it "support mixed case metadata" do
    file = multipart_file(:text)
    data = File.open(file, 'rb') { |io| io.read }

    type = "Multipart/Form-Data; Boundary=AaB03x"
    length = data.bytesize

    e = { "CONTENT_TYPE" => type,
      "CONTENT_LENGTH" => length.to_s,
      :input => StringIO.new(data) }

    env = Rack::MockRequest.env_for("/", e)
    params = Rack::Multipart.parse_multipart(env)
    params["submit-name"].must_equal "Larry"
    params["submit-name-with-content"].must_equal "Berry"
    params["files"][:type].must_equal "text/plain"
    params["files"][:filename].must_equal "file1.txt"
    params["files"][:head].must_equal "Content-Disposition: form-data; " +
      "name=\"files\"; filename=\"file1.txt\"\r\n" +
      "Content-Type: text/plain\r\n"
    params["files"][:name].must_equal "files"
    params["files"][:tempfile].read.must_equal "contents"
  end

  it "fallback to content-type for name" do
    rack_logo = File.read(multipart_file("rack-logo.png"))

    data = <<-EOF.dup
--AaB03x\r
Content-Type: text/plain\r
\r
some text\r
--AaB03x\r
\r
\r
some more text (I didn't specify Content-Type)\r
--AaB03x\r
Content-Type: image/png\r
\r
#{rack_logo}\r
--AaB03x--\r
    EOF

    options = {
      "CONTENT_TYPE" => "multipart/related; boundary=AaB03x",
      "CONTENT_LENGTH" => data.bytesize.to_s,
      :input => StringIO.new(data.dup)
    }
    env = Rack::MockRequest.env_for("/", options)
    params = Rack::Multipart.parse_multipart(env)

    params["text/plain"].must_equal ["some text", "some more text (I didn't specify Content-Type)"]
    params["image/png"].length.must_equal 1

    f = Tempfile.new("rack-logo")
    f.write(params["image/png"][0])
    f.length.must_equal 26473
  end
end