File: parser_spec.cr

package info (click to toggle)
crystal 1.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,384 kB
  • sloc: javascript: 6,400; sh: 695; makefile: 269; ansic: 121; python: 105; cpp: 77; xml: 32
file content (52 lines) | stat: -rw-r--r-- 1,629 bytes parent folder | download | duplicates (2)
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
require "spec"
require "http/formdata"

describe HTTP::FormData::Parser do
  it "parses formdata" do
    formdata = <<-FORMDATA
      -----------------------------735323031399963166993862150
      Content-Disposition: form-data; name="text"

      text
      -----------------------------735323031399963166993862150
      Content-Disposition: form-data; name="file"; filename="a.txt"
      Content-Type: text/plain

      Content of a.txt.

      -----------------------------735323031399963166993862150
      Content-Disposition: form-data; name="file2"; filename="a.html"
      Content-Type: text/html

      <!DOCTYPE html><title>Content of a.html.</title>

      -----------------------------735323031399963166993862150--
      FORMDATA

    parser = HTTP::FormData::Parser.new IO::Memory.new(formdata.gsub('\n', "\r\n")), "---------------------------735323031399963166993862150"

    runs = 0
    while parser.has_next?
      parser.next do |part|
        case part.name
        when "text"
          part.body.gets_to_end.should eq("text")
          runs += 1
        when "file"
          part.body.gets_to_end.should eq("Content of a.txt.\r\n")
          part.filename.should eq("a.txt")
          part.headers["Content-Type"].should eq("text/plain")
          runs += 1
        when "file2"
          part.body.gets_to_end.should eq("<!DOCTYPE html><title>Content of a.html.</title>\r\n")
          part.filename.should eq("a.html")
          part.headers["Content-Type"].should eq("text/html")
          runs += 1
        else
          raise "extra field"
        end
      end
    end
    runs.should eq(3)
  end
end