-
Notifications
You must be signed in to change notification settings - Fork 1
/
secureform_test.go
169 lines (161 loc) · 3.65 KB
/
secureform_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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package secureform
import (
"net/http"
"net/url"
"reflect"
"strings"
"testing"
)
func NewMockRequest(query string) *http.Request {
req, err := http.NewRequest("POST", "/", strings.NewReader(query))
if err != nil {
panic(err)
}
return req
}
type URL url.URL
func (u *URL) Set(value string) error {
parsed, err := url.Parse(value)
if err != nil {
return err
}
*u = URL(*parsed)
return nil
}
func TestParseForm(t *testing.T) {
tests := []struct {
Name string
Form interface{}
Query string
ExpErrName string
ExpErrVal error
ExpForm interface{}
}{
{
Name: "Simple test w/ typo",
Form: &struct {
Name string
}{},
Query: "name=foobar",
ExpForm: &struct{ Name string }{""},
},
{
Name: "Simple test w/ struct tag",
Form: &struct {
Name string `form:"name"`
}{},
Query: "name=foobar",
ExpForm: &struct {
Name string `form:"name"`
}{"foobar"},
},
{
Name: "Simple test",
Form: &struct {
Name string
}{},
Query: "Name=foobar",
ExpForm: &struct{ Name string }{"foobar"},
},
{
Name: "Test non-pointer struct",
Form: struct{ Name string }{},
Query: "Name=foobar",
ExpErrVal: ErrExpectedStructPtr,
},
{
Name: "Test pointer int",
Form: int(42),
Query: "Name=foobar",
ExpErrVal: ErrExpectedStructPtr,
},
{
Name: "Slice test w/ struct tag",
Form: &struct {
Name string `form:"name"`
Fruits []string `form:"fruits"`
}{},
Query: "name=foobar;fruits=apple;fruits=banana;fruits=tomato",
ExpForm: &struct {
Name string `form:"name"`
Fruits []string `form:"fruits"`
}{"foobar", []string{"apple", "banana", "tomato"}},
},
{
Name: "Integer min",
Form: &struct {
Count int `form:"count?min=1"`
}{},
Query: "count=0",
ExpErrName: "count",
ExpErrVal: ErrValidMin,
},
{
Name: "Tag query param parse error",
Form: &struct {
Name string `form:"name?min=1&foo=%zz"`
}{},
Query: "name=Me",
ExpErrName: "Name",
ExpErrVal: nil,
},
{
Name: "Unsigned",
Form: &struct {
ID uint `form:"id?max=42"`
}{},
Query: "id=43",
ExpErrName: "id",
ExpErrVal: ErrValidMax,
},
{
Name: "Floating (latitude and longitude)",
Form: &struct {
Latitude float32 `form:"lat?min=-90.0;max=90.0"`
Longitude float32 `form:"lng?min=-180.0;max=180.0"`
}{},
Query: "lat=47.6062;lng=122.3321",
ExpForm: &struct {
Latitude float32 `form:"lat?min=-90.0;max=90.0"`
Longitude float32 `form:"lng?min=-180.0;max=180.0"`
}{47.6062, 122.3321},
},
{
Name: "Custom Type (URL)",
Form: &struct {
Page URL `form:"page"`
}{},
Query: "page=https://google.com/%25zz",
ExpErrName: "page",
},
}
for _, test := range tests {
r := NewMockRequest(test.Query)
parser := Parser{maxStringLen: 24}
query, err := url.ParseQuery(test.Query)
if err != nil {
panic(err)
}
r.Form = query
err = parser.loadForm(test.Form, r)
if err != nil {
if ferr, ok := err.(*FieldError); ok {
if ferr.Name != test.ExpErrName {
t.Errorf("Test %q: Expected error with field %q, got %q", test.Name, test.ExpErrName, ferr.Name)
}
if test.ExpErrVal != nil && test.ExpErrVal != ferr.Err {
t.Errorf("Test %q: Got error %q, expected error %q", test.Name, ferr.Err, test.ExpErrVal)
}
continue
}
if err != test.ExpErrVal {
t.Errorf("Test %q: Got error %q, expected error %q", test.Name, err, test.ExpErrVal)
}
continue
}
if !reflect.DeepEqual(test.Form, test.ExpForm) {
t.Errorf("Test %q: Expected %#v, got %#v", test.Name, test.ExpForm, test.Form)
continue
}
}
}