-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrules_test.go
106 lines (88 loc) · 3.67 KB
/
rules_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
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
package filevalidator
import (
"testing"
)
func Test_FileSize(t *testing.T) {
req := setupRequestMultipartForm(&testFile{gopherPNG, "imageupload", "gopher.png"}) // 17668 bytes
for _, fieldSlice := range req.MultipartForm.File { // map[string][]*FileHeader
for _, header := range fieldSlice {
file, err := header.Open()
if err != nil {
t.Fatalf("Failed to open *multipart.FileHeader: %s", err)
}
defer file.Close()
minRule := FileSize(100000, 1000000)
if e, _ := minRule.Validate(file, header.Filename, make(map[string]string)); e == nil {
t.Errorf("Testing minimum size - FileSize(): Should return an error!")
}
maxRule := FileSize(2, 1000)
if e, _ := maxRule.Validate(file, header.Filename, make(map[string]string)); e == nil {
t.Errorf("Testing maximum size - FileSize(): Should return an error!")
}
rightRule := FileSize(17600, 18000)
if e, _ := rightRule.Validate(file, header.Filename, make(map[string]string)); e != nil {
t.Errorf("Testing correct size - FileSize(): Should not return an error!")
}
}
}
}
func Test_MimeTypes(t *testing.T) {
req := setupRequestMultipartForm(&testFile{gopherPNG, "imageupload", "gopher.png"}, &testFile{blueJPG, "imageupload", "blue.jpg"}) // image/png & image/jpeg
for _, fieldSlice := range req.MultipartForm.File { // map[string][]*FileHeader
for _, header := range fieldSlice {
file, err := header.Open()
if err != nil {
t.Fatalf("Failed to open *multipart.FileHeader: %s", err)
}
defer file.Close()
badRule := MimeTypes([]string{"application/zip", "text/html"})
if e, _ := badRule.Validate(file, header.Filename, make(map[string]string)); e == nil {
t.Errorf("Testing bad mime type - MimeTypes(): Should return an error!")
}
goodRule := MimeTypes([]string{"image/png", "image/jpeg"})
if e, _ := goodRule.Validate(file, header.Filename, make(map[string]string)); e != nil {
t.Errorf("Testing good mime type - MimeTypes(): Should not return an error!")
}
}
}
}
func Test_MinPixelSize(t *testing.T) {
req := setupRequestMultipartForm(&testFile{gopherPNG, "imageupload", "gopher.png"}) // 250 x 340 pixels
for _, fieldSlice := range req.MultipartForm.File { // map[string][]*FileHeader
for _, header := range fieldSlice {
file, err := header.Open()
if err != nil {
t.Fatalf("Failed to open *multipart.FileHeader: %s", err)
}
defer file.Close()
badRule := MinPixelSize(400, 400)
if e, _ := badRule.Validate(file, header.Filename, make(map[string]string)); e == nil {
t.Errorf("Testing bad min width and height - MinPixelSize(): Should return an error!")
}
goodRule := MinPixelSize(200, 200)
if e, _ := goodRule.Validate(file, header.Filename, make(map[string]string)); e != nil {
t.Errorf("Testing good min width and height - MinPixelSize(): Should not return an error!")
}
}
}
}
func Test_MaxPixelSize(t *testing.T) {
req := setupRequestMultipartForm(&testFile{gopherPNG, "imageupload", "gopher.png"}) // 250 x 340 pixels
for _, fieldSlice := range req.MultipartForm.File { // map[string][]*FileHeader
for _, header := range fieldSlice {
file, err := header.Open()
if err != nil {
t.Fatalf("Failed to open *multipart.FileHeader: %s", err)
}
defer file.Close()
badRule := MaxPixelSize(100, 100)
if e, _ := badRule.Validate(file, header.Filename, make(map[string]string)); e == nil {
t.Errorf("Testing bad max width and height - MaxPixelSize(): Should return an error!")
}
goodRule := MaxPixelSize(400, 400)
if e, _ := goodRule.Validate(file, header.Filename, make(map[string]string)); e != nil {
t.Errorf("Testing good max width and height - MaxPixelSize(): Should not return an error!")
}
}
}
}