-
Notifications
You must be signed in to change notification settings - Fork 1
/
mimesniffer_test.go
62 lines (49 loc) · 1.63 KB
/
mimesniffer_test.go
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
package mimesniffer
import "testing"
func TestRegister(t *testing.T) {
if got, want := len(registeredSniffers), 0; got != want {
t.Errorf("got %d, want %d", got, want)
}
Register("", func([]byte) bool { return true })
if got, want := len(registeredSniffers), 0; got != want {
t.Errorf("got %d, want %d", got, want)
}
Register("foobar", func([]byte) bool { return true })
if got, want := len(registeredSniffers), 1; got != want {
t.Errorf("got %d, want %d", got, want)
}
Register("foo/bar", func([]byte) bool { return true })
if got, want := len(registeredSniffers), 2; got != want {
t.Errorf("got %d, want %d", got, want)
}
Register("foo/bar; charset=utf8", func([]byte) bool { return true })
if got, want := len(registeredSniffers), 3; got != want {
t.Errorf("got %d, want %d", got, want)
}
}
func TestSniff(t *testing.T) {
registeredSniffers = map[string]func([]byte) bool{}
mimeType := Sniff(nil)
if want := "application/octet-stream"; mimeType != want {
t.Errorf("got %q, want %q", mimeType, want)
}
Register("foo/bar", func(b []byte) bool {
return len(b) > 0 && b[0] == 0x00
})
mimeType = Sniff([]byte{0x00})
if want := "foo/bar"; mimeType != want {
t.Errorf("got %q, want %q", mimeType, want)
}
mimeType = Sniff([]byte{0x01})
if want := "application/octet-stream"; mimeType != want {
t.Errorf("got %q, want %q", mimeType, want)
}
mimeType = Sniff([]byte{0xff, 0xf1})
if want := "audio/aac"; mimeType != want {
t.Errorf("got %q, want %q", mimeType, want)
}
mimeType = Sniff([]byte("foobar"))
if want := "text/plain; charset=utf-8"; mimeType != want {
t.Errorf("got %q, want %q", mimeType, want)
}
}