-
Notifications
You must be signed in to change notification settings - Fork 2
/
fill_test.go
403 lines (333 loc) · 10.1 KB
/
fill_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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package gosubmit_test
import (
"bytes"
"context"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"reflect"
"regexp"
"strings"
"testing"
. "github.com/jeremija/gosubmit"
)
const (
defaultMaxMemory = 32 << 20 // 32 MB
)
func mustOpen(t *testing.T, filename string) io.ReadCloser {
f, err := os.Open(filename)
if err != nil {
t.Fatalf("Error opening file: %s (reason: %s)", filename, err)
}
return f
}
func TestNewTestRequest_recover(t *testing.T) {
var f Form
_, err := f.NewTestRequest()
if err == nil {
t.Fatal("Expected an error, but got nil")
}
expected := "Caught panic when creating request"
if msg := err.Error(); !strings.Contains(msg, expected) {
t.Errorf("Expected error to contain '%s', but was '%s'", expected, msg)
}
}
func TestNewRequest_no_forms_found(t *testing.T) {
var f Forms
for _, form := range []Form{f.First(), f.Last()} {
_, err := form.NewTestRequest()
if err == nil {
t.Fatal("Expected an error, but got nil")
}
expected := "No forms found"
if msg := err.Error(); msg != expected {
t.Errorf("Expected error to contain '%s', but was '%s'", expected, msg)
}
}
}
func TestNewTestRequest_simple_get(t *testing.T) {
f := mustOpen(t, "./forms/simple.html")
defer f.Close()
form := ParseWithURL(f, "/test").FindForm("name", "simple-get")
if form.URL != "/test" {
t.Fatalf("Expected form url to fallback to /test, but was %s", form.URL)
}
r, err := form.NewTestRequest(
Add("firstName", "John"),
)
if err != nil {
t.Fatalf("Could not fill form and create test request: %s", err)
}
if r.Method != http.MethodGet {
t.Errorf("Expected request method GET but was: %s", r.Method)
}
if r.URL.EscapedPath() != "/test" {
t.Errorf("Expected url path to be /test but was: %s", r.URL.EscapedPath())
}
r.ParseForm()
expected := url.Values{
"firstName": []string{"John"},
}
if !reflect.DeepEqual(expected, r.Form) {
t.Error("Expected form to be", expected, "but was", r.Form)
}
}
func TestFormValidate(t *testing.T) {
f := mustOpen(t, "./forms/simple.html")
defer f.Close()
err := ParseWithURL(f, "/test").FindForm("name", "simple-get").Validate(
Set("a", "b"),
)
expected := "Cannot find input name='a'"
if err == nil || err.Error() != expected {
t.Errorf("Expected an error message: '%s', but got '%s'", expected, err.Error())
}
}
func TestNewTestRequest_simple_post(t *testing.T) {
f := mustOpen(t, "./forms/simple.html")
defer f.Close()
form := ParseWithURL(f, "/test").FindForm("name", "simple-post")
if form.URL != "/mytest" {
t.Fatalf("Expected form url to fallback to /mytest, but was %s", form.URL)
}
r, err := form.NewTestRequest(
Add("firstName", "John"),
)
if err != nil {
t.Fatalf("Could not fill form and create test request: %s", err)
}
if r.Method != http.MethodPost {
t.Errorf("Expected request method POST but was: %s", r.Method)
}
if r.URL.EscapedPath() != "/mytest" {
t.Errorf("Expected url path to be /test but was: %s", r.URL.EscapedPath())
}
if r.Header.Get("Content-Type") != ContentTypeForm {
t.Errorf("Expepcted content type to be %s, but was %s",
ContentTypeForm,
r.Header.Get("Content-Type"),
)
}
r.ParseForm()
expected := url.Values{
"firstName": []string{"John"},
}
if !reflect.DeepEqual(expected, r.Form) {
t.Error("Expected form to be", expected, "but was", r.Form)
}
}
func TestNewTestRequest_multipart(t *testing.T) {
f := mustOpen(t, "./forms/big.html")
defer f.Close()
form := Parse(f).FirstForm()
pictureContents := []byte("test-file")
r, err := form.NewTestRequest(
Set("sel1", form.GetOptionsFor("sel1")[0]),
Add("sel2", "5"),
// Add("chk", form.GetOptionsFor("chk")[0]),
Add("chk", form.GetOptionsFor("chk")[1]),
Set("contact", form.GetOptionsFor("contact")[1]),
Set("email", "[email protected]"),
Set("firstName", "Test"),
Set("age", "33"),
AddFile("profile", "picture.jpg", pictureContents),
Click("Save 1"),
)
if err != nil {
t.Fatalf("Error creating test request: %s", err)
}
if r.Method != http.MethodPost {
t.Fatalf("Expected method to be POST, but was %s", r.Method)
}
if r.URL.EscapedPath() != "/submit" {
t.Errorf("Expected url to be /submit but was %s", r.URL.EscapedPath())
}
err = r.ParseMultipartForm(defaultMaxMemory)
if err != nil {
t.Fatalf("Error parsing multipart form: %s", err)
}
expectedForm := url.Values{
"sel1": []string{"1"},
"sel2": []string{"4", "6", "5"},
"chk": []string{"subscribe-mail", "subscribe-phone"},
"contact": []string{"phone"},
"email": []string{"[email protected]"},
"firstName": []string{"Test"},
"age": []string{"33"},
"lastName": []string{""},
"csrf": []string{"1234"},
"post": []string{"Big Text"},
"action": []string{"Save 1"},
}
if !reflect.DeepEqual(expectedForm, r.PostForm) {
t.Error("Expected form to be:\n", expectedForm, "\nbut was:\n", r.PostForm)
}
file, header, err := r.FormFile("profile")
if err != nil {
t.Errorf("Cannot read profile image: %s", err)
}
defer file.Close()
if header.Filename != "picture.jpg" {
t.Errorf("profile filename expected picture.jpg, but was %s", header.Filename)
}
fileData, err := ioutil.ReadAll(file)
if !bytes.Equal(pictureContents, fileData) {
t.Errorf("Picture contents do not match: %s vs %s", pictureContents, fileData)
}
}
func TestAutoFill(t *testing.T) {
f := mustOpen(t, "./forms/big-empty.html")
defer f.Close()
form := Parse(f).FirstForm()
r, err := form.NewTestRequest(
AutoFill(),
// autofill won't fill up fields with patterns
Set("firstName", "John"),
Click("Save 1"),
)
if err != nil {
t.Fatalf("Error creating test request: %s", err)
}
err = r.ParseMultipartForm(defaultMaxMemory)
if err != nil {
t.Fatalf("Error parsing multipart form: %s", err)
}
randomLastName := r.FormValue("lastName")
randomCaptcha := r.FormValue("captcha")
expectedForm := url.Values{
"sel2": []string{"4", "5", "6"},
"chk": []string{"subscribe-mail", "subscribe-phone"},
"contact": []string{"call"},
"email": []string{AutoFillEmail},
"firstName": []string{"John"},
"age": []string{"18"},
"captcha": []string{randomCaptcha},
"lastName": []string{randomLastName},
"someDate": []string{AutoFillDate},
"website": []string{AutoFillURL},
"other": []string{"other-autofill"},
"csrf": []string{"1234"},
"post": []string{"Big Text"},
"action": []string{"Save 1"},
}
if !reflect.DeepEqual(expectedForm, r.PostForm) {
t.Error("Expected form to be:\n", expectedForm, "\nbut was:\n", r.PostForm)
}
file, header, err := r.FormFile("profile")
if err != nil {
t.Errorf("Cannot read profile image: %s", err)
}
defer file.Close()
if header.Filename != "auto-filename" {
t.Errorf("profile filename expected auto-filename, but was %s", header.Filename)
}
fileData, err := ioutil.ReadAll(file)
if !bytes.Equal(AutoFillFile, fileData) {
t.Errorf("Picture contents do not match: %s vs %s", AutoFillFile, fileData)
}
if len(randomCaptcha) != 5 {
t.Errorf("Expected captcha to be 5 characters long, but was %d", len(randomCaptcha))
}
}
func TestMultipartParams_invalid(t *testing.T) {
f := mustOpen(t, "./forms/big.html")
defer f.Close()
form := Parse(f).FirstForm()
_, _, err := form.MultipartParams(
Set("sel1", form.GetOptionsFor("sel1")[0]),
)
re := regexp.MustCompile("Required field.*has no value")
if err == nil || !re.MatchString(err.Error()) {
t.Errorf("Expected error to match '%s', but was '%s'", re, err.Error())
}
}
func TestNewRequest_invalid(t *testing.T) {
f := mustOpen(t, "./forms/simple.html")
defer f.Close()
_, err := ParseWithURL(f, "/test").FirstForm().NewRequest(
Set("a", "b"),
)
re := regexp.MustCompile("Cannot find input name='a'")
if err == nil || !re.MatchString(err.Error()) {
t.Fatalf("Expected an error %s, but got: %s", re, err)
}
}
func TestNewRequest_missing(t *testing.T) {
f := mustOpen(t, "./forms/simple.html")
defer f.Close()
_, err := ParseWithURL(f, "/test").FirstForm().NewRequest()
re := regexp.MustCompile("Required field 'firstName' has no value")
if err == nil || !re.MatchString(err.Error()) {
t.Fatalf("Expected an error %s, but got: %s", re, err)
}
}
func TestFiller_Reset(t *testing.T) {
f := mustOpen(t, "./forms/simple.html")
defer f.Close()
_, err := ParseWithURL(f, "/test").FirstForm().PostParams(
Set("firstName", "a"),
Reset("firstName"),
)
re := regexp.MustCompile("Required field 'firstName' has no value")
if err == nil || !re.MatchString(err.Error()) {
t.Fatalf("Expected an error %s, but got: %s", re, err)
}
}
func TestFiller_IsRequired(t *testing.T) {
f := mustOpen(t, "./forms/simple.html")
defer f.Close()
isRequired := ParseWithURL(f, "/test").FirstForm().IsRequired("firstName")
if isRequired == false {
t.Fatalf("Field 'firstName' is should be required, but is not")
}
}
func TestFiller_NewRequest(t *testing.T) {
f := mustOpen(t, "./forms/simple.html")
defer f.Close()
ctx := context.WithValue(context.Background(), "a", "b")
r, err := ParseWithURL(f, "/test").FirstForm().NewRequest(
Set("firstName", "John"),
WithContext(ctx),
)
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
if r == nil {
t.Errorf("Request is nil")
}
if c := r.Context(); c != ctx {
t.Errorf("Context should be the same, but is not")
}
}
func TestPostParams_error(t *testing.T) {
f := mustOpen(t, "./forms/simple.html")
defer f.Close()
_, err := Parse(f).FirstForm().PostParams(Set("a", "b"))
expected := "Cannot find input name='a'"
if err == nil || err.Error() != expected {
t.Errorf("Expected an error '%s' but got '%s'", expected, err)
}
}
func TestGetParams_error(t *testing.T) {
f := mustOpen(t, "./forms/simple.html")
defer f.Close()
_, err := Parse(f).FirstForm().GetParams(Set("a", "b"))
expected := "Cannot find input name='a'"
if err == nil || err.Error() != expected {
t.Errorf("Expected an error '%s' but got '%s'", expected, err)
}
}
func TestGetParams_ok(t *testing.T) {
f := mustOpen(t, "./forms/simple.html")
defer f.Close()
params, err := Parse(f).FirstForm().GetParams(Set("firstName", "b"))
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
expected := "firstName=b"
if params != expected {
t.Errorf("Expected params to be '%s' but was '%s'", expected, params)
}
}