File: mime_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 (209 lines) | stat: -rw-r--r-- 6,623 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
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
require "./spec_helper"
require "mime"

module MIME
  def self.initialized
    @@initialized
  end

  def self.reset!
    @@initialized = false
    @@types = {} of String => String
    @@types_lower = {} of String => String
    @@extensions = {} of String => Set(String)
  end
end

describe MIME do
  before_each do
    MIME.reset!
  end

  after_each do
    MIME.reset!
  end

  it ".from_extension" do
    MIME.init
    MIME.from_extension(".html").partition(';')[0].should eq "text/html"
    MIME.from_extension(".HTML").partition(';')[0].should eq "text/html"

    expect_raises KeyError do
      MIME.from_extension(".fooobar")
    end
    MIME.from_extension(".fooobar", "default/fooobar").should eq "default/fooobar"
    MIME.from_extension(".fooobar") { "default/fooobar" }.should eq "default/fooobar"
  end

  it ".from_extension?" do
    MIME.init
    MIME.from_extension?(".html").should eq MIME.from_extension(".html")
    MIME.from_extension?(".HTML").should eq MIME.from_extension(".HTML")

    MIME.from_extension?(".fooobar").should be_nil
  end

  describe ".from_filename" do
    it String do
      MIME.init
      MIME.from_filename("test.html").should eq MIME.from_extension(".html")
      MIME.from_filename("foo/bar.not-exists", "foo/bar-exist").should eq "foo/bar-exist"
      MIME.from_filename("foo/bar.not-exists") { "foo/bar-exist" }.should eq "foo/bar-exist"
    end

    it Path do
      MIME.init
      MIME.from_filename(Path["test.html"]).should eq MIME.from_extension(".html")
      MIME.from_filename(Path["foo/bar.not-exists"], "foo/bar-exist").should eq "foo/bar-exist"
      MIME.from_filename(Path["foo/bar.not-exists"]) { "foo/bar-exist" }.should eq "foo/bar-exist"
    end
  end

  describe ".from_filename?" do
    it String do
      MIME.init
      MIME.from_filename?("test.html").should eq MIME.from_extension(".html")
    end

    it Path do
      MIME.init
      MIME.from_filename?(Path["test.html"]).should eq MIME.from_extension(".html")
    end
  end

  describe ".register" do
    it "registers new type" do
      MIME.init(load_defaults: false)
      MIME.register(".Custom-Type", "text/custom-type")

      MIME.from_extension(".Custom-Type").should eq "text/custom-type"
      MIME.from_extension(".custom-type").should eq "text/custom-type"
      MIME.extensions("text/custom-type").should eq Set{".Custom-Type"}

      MIME.register(".custom-type2", "text/custom-type")
      MIME.extensions("text/custom-type").should eq Set{".Custom-Type", ".custom-type2"}

      MIME.register(".custom-type", "text/custom-type-lower")
      MIME.from_extension(".custom-type").should eq "text/custom-type-lower"
      MIME.from_extension(".Custom-Type").should eq "text/custom-type"
    end

    it "fails for invalid extension" do
      expect_raises ArgumentError, "Extension does not start with a dot" do
        MIME.register("foo", "text/foo")
      end

      expect_raises ArgumentError, "String contains null byte" do
        MIME.register(".foo\0", "text/foo")
      end
    end
  end

  describe ".extensions" do
    it "lists extensions" do
      MIME.init
      MIME.extensions("text/html").should contain ".htm"
      MIME.extensions("text/html").should contain ".html"
    end

    it "returns empty set" do
      MIME.init(load_defaults: false)
      MIME.extensions("foo/bar").should eq Set(String).new
    end

    it "recognizes overridden types" do
      MIME.init(load_defaults: false)
      MIME.register(".custom-type-overridden", "text/custom-type-overridden")
      MIME.register(".custom-type-overridden", "text/custom-type-override")

      MIME.extensions("text/custom-type-overridden").should eq Set(String).new
    end
  end

  it "parses media types" do
    MIME.init(load_defaults: false)
    MIME.register(".parse-media-type1", "text/html; charset=utf-8")
    MIME.extensions("text/html").should contain(".parse-media-type1")

    MIME.register(".parse-media-type2", "text/html; foo = bar; bar= foo ;")
    MIME.extensions("text/html").should contain(".parse-media-type2")

    MIME.register(".parse-media-type3", "foo/bar")
    MIME.extensions("foo/bar").should contain(".parse-media-type3")

    MIME.register(".parse-media-type4", " form-data ; name=foo")
    MIME.extensions("form-data").should contain(".parse-media-type4")

    MIME.register(".parse-media-type41", %(FORM-DATA;name="foo"))
    MIME.extensions("form-data").should contain(".parse-media-type41")

    MIME.register(".parse-media-type5", %( FORM-DATA ; name="foo"))
    MIME.extensions("form-data").should contain(".parse-media-type5")

    expect_raises ArgumentError, "Invalid media type" do
      MIME.register(".parse-media-type6", ": inline; attachment; filename=foo.html")
    end

    expect_raises ArgumentError, "Invalid media type" do
      MIME.register(".parse-media-type7", "filename=foo.html, filename=bar.html")
    end

    expect_raises ArgumentError, "Invalid media type" do
      MIME.register(".parse-media-type8", %("foo; filename=bar;baz"; filename=qux))
    end

    expect_raises ArgumentError, "Invalid media type" do
      MIME.register(".parse-media-type9", "x=y; filename=foo.html")
    end

    expect_raises ArgumentError, "Invalid media type" do
      MIME.register(".parse-media-type10", "filename=foo.html")
    end
  end

  it ".load_mime_database" do
    MIME.init(load_defaults: false)
    MIME.from_extension?(".bar").should be_nil
    MIME.from_extension?(".fbaz").should be_nil

    MIME.load_mime_database IO::Memory.new <<-EOF
      foo/bar          bar
      foo/baz          baz fbaz #foobaz
      # foo/foo        foo
    EOF

    MIME.from_extension?(".bar").should eq "foo/bar"
    MIME.from_extension?(".fbaz").should eq "foo/baz"
    MIME.from_extension?(".#foobaz").should be_nil
    MIME.from_extension?(".foobaz").should be_nil
    MIME.from_extension?(".foo").should be_nil
  end

  describe ".init" do
    it "loads defaults" do
      MIME.init
      MIME.initialized.should be_true
      MIME.from_extension(".html").partition(';')[0].should eq "text/html"
    end

    it "skips loading defaults" do
      MIME.init(load_defaults: false)
      MIME.initialized.should be_true
      MIME.from_extension?(".html").should be_nil
    end

    it "loads file" do
      MIME.initialized.should be_false
      MIME.init(datapath("mime.types"))
      MIME.from_extension?(".foo").should eq "foo/bar"
    end
  end

  {% if flag?(:win32) %}
    it "loads MIME data from registry" do
      MIME.register(".wma", "non-initialized")
      MIME.init
      MIME.from_extension?(".wma").should eq "audio/x-ms-wma"
    end
  {% end %}
end