File: multipart_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 (54 lines) | stat: -rw-r--r-- 1,786 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
require "spec"
require "mime/multipart"

describe MIME::Multipart do
  describe ".parse" do
    it "parses multipart messages" do
      multipart = "--aA40\r\nContent-Type: text/plain\r\n\r\nabcd\r\n--aA40--"
      MIME::Multipart.parse(IO::Memory.new(multipart), "aA40") do |headers, io|
        headers["Content-Type"].should eq("text/plain")
        io.gets_to_end.should eq("abcd")
      end
    end
  end

  describe ".parse_boundary" do
    it "parses unquoted boundaries" do
      content_type = "multipart/mixed; boundary=a_-47HDS"
      MIME::Multipart.parse_boundary(content_type).should eq("a_-47HDS")
    end

    it "parses quoted boundaries" do
      content_type = %{multipart/mixed; boundary="aA_-<>()"}
      MIME::Multipart.parse_boundary(content_type).should eq(%{aA_-<>()})
    end
  end

  describe ".parse" do
    it "parses multipart messages" do
      headers = HTTP::Headers{"Content-Type" => "multipart/mixed; boundary=aA40"}
      body = "--aA40\r\nContent-Type: text/plain\r\n\r\nbody\r\n--aA40--"
      request = HTTP::Request.new("POST", "/", headers, body)

      MIME::Multipart.parse(request) do |headers, io|
        headers["Content-Type"].should eq("text/plain")
        io.gets_to_end.should eq("body")
      end
    end

    it "parses multipart messages from HTTP client responses" do
      headers = HTTP::Headers{"Content-Type" => "multipart/byteranges; boundary=aA40"}
      body = "--aA40\r\nContent-Type: text/plain\r\n\r\nbody\r\n--aA40--"
      response = HTTP::Client::Response.new(
        status: :ok,
        headers: headers,
        body: body,
      )

      MIME::Multipart.parse(response) do |headers, io|
        headers["Content-Type"].should eq("text/plain")
        io.gets_to_end.should eq("body")
      end
    end
  end
end