forked from thedevsaddam/govalidator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate_file_test.go
141 lines (122 loc) · 2.92 KB
/
validate_file_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
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
package govalidator
import (
"bytes"
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"testing"
)
// buildMocFormReq prepare a moc form data request with a test file
func buildMocFormReq() (*http.Request, error) {
fPath := "doc/BENCHMARK.md"
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
file, err := os.Open(fPath)
if err != nil {
return nil, err
}
part, err := writer.CreateFormFile("file", filepath.Base(fPath))
if err != nil {
return nil, err
}
io.Copy(part, file)
file.Close()
err = writer.Close()
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", "www.example.com", body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", writer.FormDataContentType())
return req, nil
}
func Test_validateFiles(t *testing.T) {
req, err := buildMocFormReq()
if err != nil {
t.Error("request failed", err)
}
rules := MapData{
"file:file": []string{"ext:jpg,pdf", "size:10", "mime:application/pdf", "required"},
}
opts := Options{
Request: req,
Rules: rules,
}
vd := New(opts)
validationErr := vd.Validate()
if len(validationErr) != 1 {
t.Error("file validation failed!")
}
}
func Test_validateFiles_message(t *testing.T) {
req, err := buildMocFormReq()
if err != nil {
t.Error("request failed", err)
}
rules := MapData{
"file:file": []string{"ext:jpg,pdf", "size:10", "mime:application/pdf", "required"},
}
msgs := MapData{
"file:file": []string{"ext:custom_message"},
}
opts := Options{
Request: req,
Rules: rules,
Messages: msgs,
}
vd := New(opts)
validationErr := vd.Validate()
if len(validationErr) != 1 {
t.Error("file validation failed!")
}
if validationErr.Get("file") != "custom_message" {
t.Log(validationErr)
t.Error("failed custom message for file validation")
}
}
func Test_validateFiles_CustomRule(t *testing.T) {
req, err := buildMocFormReq()
if err != nil {
t.Error("request failed", err)
}
customRule1WasExecuted := false
isMultipartFile := false
AddCustomRule("customRule1", func(field string, rule string, message string, value interface{}) error {
customRule1WasExecuted = true
_, isMultipartFile = value.(multipart.File)
return nil
})
customRule2WasExecuted := false
isValueNil := false
AddCustomRule("customRule2", func(field string, rule string, message string, value interface{}) error {
customRule2WasExecuted = true
isValueNil = value == nil
return nil
})
rules := MapData{
"file:file": []string{"customRule1"},
"file:avatar": []string{"customRule2"},
}
opts := Options{
Request: req,
Rules: rules,
}
vd := New(opts)
vd.Validate()
if !customRule1WasExecuted {
t.Error("file validation performed without custom rule!")
}
if !isMultipartFile {
t.Error("passed to custom rule value is not file!")
}
if !customRule2WasExecuted {
t.Error("file validation performed without custom rule!")
}
if !isValueNil {
t.Error("passed to custom rule value is not nil!")
}
}