File: multipart_spec.cr

package info (click to toggle)
crystal 1.6.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 18,956 kB
  • sloc: javascript: 1,712; sh: 592; cpp: 541; makefile: 243; ansic: 119; python: 105; xml: 32
file content (39 lines) | stat: -rw-r--r-- 1,268 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
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
  end
end