-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathforms_test.go
123 lines (115 loc) · 3.42 KB
/
forms_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
// Copyright 2013 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package scrape
import (
"fmt"
"net/http"
"net/url"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"golang.org/x/net/html"
)
func Test_ParseForms(t *testing.T) {
t.Parallel()
tests := []struct {
description string
html string
forms []*htmlForm
}{
{"no forms", `<html></html>`, nil},
{"empty form", `<html><form></form></html>`, []*htmlForm{{Values: url.Values{}}}},
{
"single form with one value",
`<html><form action="a" method="m"><input name="n1" value="v1"></form></html>`,
[]*htmlForm{{Action: "a", Method: "m", Values: url.Values{"n1": {"v1"}}}},
},
{
"two forms",
`<html>
<form action="a1" method="m1"><input name="n1" value="v1"></form>
<form action="a2" method="m2"><input name="n2" value="v2"></form>
</html>`,
[]*htmlForm{
{Action: "a1", Method: "m1", Values: url.Values{"n1": {"v1"}}},
{Action: "a2", Method: "m2", Values: url.Values{"n2": {"v2"}}},
},
},
{
"form with radio buttons (none checked)",
`<html><form>
<input type="radio" name="n1" value="v1">
<input type="radio" name="n1" value="v2">
<input type="radio" name="n1" value="v3">
</form></html>`,
[]*htmlForm{{Values: url.Values{}}},
},
{
"form with radio buttons",
`<html><form>
<input type="radio" name="n1" value="v1">
<input type="radio" name="n1" value="v2">
<input type="radio" name="n1" value="v3" checked>
</form></html>`,
[]*htmlForm{{Values: url.Values{"n1": {"v3"}}}},
},
{
"form with checkboxes",
`<html><form>
<input type="checkbox" name="n1" value="v1" checked>
<input type="checkbox" name="n2" value="v2">
<input type="checkbox" name="n3" value="v3" checked>
</form></html>`,
[]*htmlForm{{Values: url.Values{"n1": {"v1"}, "n3": {"v3"}}}},
},
{
"single form with textarea",
`<html><form><textarea name="n1">v1</textarea></form></html>`,
[]*htmlForm{{Values: url.Values{"n1": {"v1"}}}},
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
t.Parallel()
node, err := html.Parse(strings.NewReader(tt.html))
if err != nil {
t.Fatalf("error parsing html: %v", err)
}
if got, want := parseForms(node), tt.forms; !cmp.Equal(got, want) {
t.Errorf("parseForms(%q) returned %+v, want %+v", tt.html, got, want)
}
})
}
}
func Test_FetchAndSumbitForm(t *testing.T) {
t.Parallel()
client, mux := setup(t)
var submitted bool
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `<html><form action="/submit">
<input type="hidden" name="hidden" value="h">
<input type="text" name="name">
</form></html>`)
})
mux.HandleFunc("/submit", func(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
t.Fatalf("error parsing form: %v", err)
}
want := url.Values{"hidden": {"h"}, "name": {"n"}}
if got := r.Form; !cmp.Equal(got, want) {
t.Errorf("submitted form contained values %v, want %v", got, want)
}
submitted = true
})
setValues := func(values url.Values) { values.Set("name", "n") }
_, err := fetchAndSubmitForm(client.Client, client.baseURL.String()+"/", setValues)
if err != nil {
t.Fatalf("fetchAndSubmitForm returned err: %v", err)
}
if !submitted {
t.Error("form was never submitted")
}
}