Repository navigation
Expand file tree
/
Copy pathsanitize_test.go
More file actions
76 lines (72 loc) · 2.37 KB
/
Copy pathsanitize_test.go
File metadata and controls
76 lines (72 loc) · 2.37 KB
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
package main
import (
"strings"
"testing"
)
func TestIsBinaryBody(t *testing.T) {
cases := []struct {
name string
body []byte
contentType string
want bool
}{
{"plain text", []byte("hello world"), "text/plain", false},
{"json", []byte(`{"a":1}`), "application/json", false},
{"image content-type", []byte("anything"), "image/png", true},
{"octet-stream", []byte("anything"), "application/octet-stream", true},
{"pdf", []byte("anything"), "application/pdf; charset=binary", true},
{"nul byte sniff", []byte{0x48, 0x69, 0x00, 0x21}, "", true},
{"protobuf", []byte("\x08\x96\x01"), "application/x-protobuf", true},
{"gzip", []byte("anything"), "application/gzip", true},
{"empty", []byte{}, "text/plain", false},
{"utf8 unicode", []byte("héllo ✓ ☃"), "text/plain", false},
{"invalid utf8", []byte{0xff, 0xfe, 0xfd}, "", true},
{"large text", []byte(strings.Repeat("a", 9000)), "text/plain", false},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := isBinaryBody(tc.body, tc.contentType)
if got != tc.want {
t.Fatalf("isBinaryBody(%q, %q) = %v, want %v",
string(tc.body), tc.contentType, got, tc.want)
}
})
}
}
func TestSanitizeText(t *testing.T) {
cases := []struct {
in, want string
}{
{"plain", "plain"},
{"with\nnewline", "with\nnewline"},
{"with\ttab", "with\ttab"},
{"bell\x07char", "bell\\x07char"},
{"bare esc\x1bseq", "bare esc\\x1bseq"}, // bare ESC with no valid CSI follower is escaped literally
{"ansi\x1b[31mred\x1b[0m more", "ansired more"},
{"osc\x1b]0;title\x07rest", "oscrest"},
{"nul\x00mid", "nul\\x00mid"},
}
for _, tc := range cases {
got := sanitizeText([]byte(tc.in))
if got != tc.want {
t.Errorf("sanitizeText(%q) = %q, want %q", tc.in, got, tc.want)
}
}
}
func TestContentTypeFromHeaders(t *testing.T) {
cases := []struct {
headers, want string
}{
{"HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n", "text/html"},
{"Content-Type:application/json\r\n", "application/json"},
{"content-type: image/png\r\n\r\n", "image/png"},
{"X-Other: foo\r\nContent-Type: application/octet-stream\r\n", "application/octet-stream"},
{"no content type here\r\n", ""},
}
for _, tc := range cases {
got := contentTypeFromHeaders(tc.headers)
if got != tc.want {
t.Errorf("contentTypeFromHeaders(%q) = %q, want %q", tc.headers, got, tc.want)
}
}
}