/
helper_test.go
69 lines (66 loc) · 1.46 KB
/
helper_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
63
64
65
66
67
68
69
package main
import (
"testing"
)
func TestSelectContentType(t *testing.T) {
for _, test := range []struct {
Name string
Filename string
ExpectedType string
ExpectDifference bool
}{
{
Name: "text file",
Filename: "test.txt",
ExpectedType: "text/plain; charset=utf-8",
},
{
Name: "file with no extension",
Filename: "test",
ExpectedType: "text/txt",
ExpectDifference: true,
},
} {
t.Run(test.Name, func(t *testing.T) {
result := selectContentType(test.Filename)
if result != test.ExpectedType && !test.ExpectDifference {
t.Errorf("%+q is expected but %+q is resulting\n", test.ExpectedType, result)
}
})
}
}
func Test_onlyAllowedCharacters(t *testing.T) {
for _, test := range []struct {
Name string
Filename string
Expected string
}{
{
Name: "allowed characters",
Filename: "test.txt",
Expected: "test.txt",
},
{
Name: "not allowed characters",
Filename: "test (1).txt",
Expected: "test1.txt",
},
{
Name: "not allowed characters with spaces",
Filename: "test foo bar.txt",
Expected: "testfoobar.txt",
},
{
Name: "capital letters",
Filename: "Test.txt",
Expected: "Test.txt",
},
} {
t.Run(test.Name, func(t *testing.T) {
result := onlyAllowedCharacters(test.Filename)
if result != test.Expected {
t.Errorf("%+q is expected but %+q is resulting\n", test.Expected, result)
}
})
}
}