-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcase.go
More file actions
203 lines (187 loc) · 5.03 KB
/
case.go
File metadata and controls
203 lines (187 loc) · 5.03 KB
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
// This package is a modified version of the webtest package available at
// https://github.com/cespare/webtest, and is under the same license as the
// original package. This version has a reusable http.Client that allows the
// tested handler to set and remove secure cookies as needed.
package webtest
import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"
"regexp"
"strings"
)
// A case_ is a single test case (GET/HEAD/POST/PUT/PATCH/DELETE) in a script.
type case_ struct {
file string
line int
method string
url string
headers [][2]string
cookies [][2]string
postbody string
postquery string
posttype string
hint string
checks []*cmpCheck
}
// A cmp is a single comparison (check) made against a test case.
type cmpCheck struct {
file string
line int
what string
whatArg string
op string
want string
wantRE *regexp.Regexp
}
// runHandler runs a test case against the handler h.
func (c *case_) runHandler(base *url.URL, client *http.Client, h http.Handler) error {
url := fmt.Sprintf("%s%s", base, c.url)
r, err := c.newRequest(url)
if err != nil {
return err
}
for _, cookie := range client.Jar.Cookies(base) {
r.AddCookie(cookie)
}
res, err := client.Do(r)
if err != nil {
return err
}
body, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
return err
}
return c.check(res, string(body))
}
// newRequest creates a new request for the case c,
// using the URL u.
func (c *case_) newRequest(u string) (*http.Request, error) {
body := c.requestBody()
r, err := http.NewRequest(c.method, u, body)
if err != nil {
return nil, err
}
typ := c.posttype
if body != nil && typ == "" {
typ = "application/x-www-form-urlencoded"
}
if typ != "" {
r.Header.Set("Content-Type", typ)
}
for _, kv := range c.headers {
r.Header.Set(kv[0], kv[1])
}
for _, kv := range c.cookies {
r.AddCookie(&http.Cookie{Name: kv[0], Value: kv[1]})
}
return r, nil
}
// requestBody returns the body for the case's request.
func (c *case_) requestBody() io.Reader {
if c.postbody == "" {
return nil
}
return strings.NewReader(c.postbody)
}
// check checks the response against the comparisons for the case.
func (c *case_) check(resp *http.Response, body string) error {
var msg bytes.Buffer
for _, chk := range c.checks {
what := chk.what
if chk.whatArg != "" {
what += " " + chk.whatArg
}
var value string
switch chk.what {
default:
value = "unknown what: " + chk.what
case "body":
value = body
case "trimbody":
value = trim(body)
case "code":
value = fmt.Sprint(resp.StatusCode)
case "cookie", "rawcookie":
for _, ck := range resp.Cookies() {
if ck.Name == chk.whatArg {
if chk.what == "cookie" {
value = ck.Value
} else {
value = ck.String()
}
break
}
}
case "header":
value = resp.Header.Get(chk.whatArg)
case "redirect":
if resp.StatusCode/10 == 30 {
value = resp.Header.Get("Location")
}
}
switch chk.op {
default:
fmt.Fprintf(&msg, "%s:%d: unknown operator %s\n", chk.file, chk.line, chk.op)
case "==":
if value != chk.want {
fmt.Fprintf(&msg, "%s:%d: %s = %q, want %q\n", chk.file, chk.line, what, value, chk.want)
}
case "!=":
if value == chk.want {
fmt.Fprintf(&msg, "%s:%d: %s == %q (but want !=)\n", chk.file, chk.line, what, value)
}
case "~":
if !chk.wantRE.MatchString(value) {
fmt.Fprintf(&msg, "%s:%d: %s does not match %#q (but should)\n\t%s\n", chk.file, chk.line, what, chk.want, indent(value))
}
case "!~":
if chk.wantRE.MatchString(value) {
fmt.Fprintf(&msg, "%s:%d: %s matches %#q (but should not)\n\t%s\n", chk.file, chk.line, what, chk.want, indent(value))
}
case "contains":
if !strings.Contains(value, chk.want) {
fmt.Fprintf(&msg, "%s:%d: %s does not contain %#q (but should)\n\t%s\n", chk.file, chk.line, what, chk.want, indent(value))
}
case "!contains":
if strings.Contains(value, chk.want) {
fmt.Fprintf(&msg, "%s:%d: %s contains %#q (but should not)\n\t%s\n", chk.file, chk.line, what, chk.want, indent(value))
}
}
}
if msg.Len() > 0 && c.hint != "" {
fmt.Fprintf(&msg, "hint: %s\n", indent(c.hint))
}
if msg.Len() > 0 {
return fmt.Errorf("%s:%d: %s %s\n%s", c.file, c.line, c.method, c.url, msg.String())
}
return nil
}
// trim returns a trimming of s, in which all runs of spaces and tabs have
// been collapsed to a single space, leading and trailing spaces have been
// removed from each line, and blank lines are removed entirely.
func trim(s string) string {
s = regexp.MustCompile(`[ \t]+`).ReplaceAllString(s, " ")
s = regexp.MustCompile(`(?m)(^ | $)`).ReplaceAllString(s, "")
s = strings.TrimLeft(s, "\n")
s = regexp.MustCompile(`\n\n+`).ReplaceAllString(s, "\n")
return s
}
// indent indents text for formatting in a message.
func indent(text string) string {
if text == "" {
return "(empty)"
}
if text == "\n" {
return "(blank line)"
}
text = strings.TrimRight(text, "\n")
if text == "" {
return "(blank lines)"
}
text = strings.ReplaceAll(text, "\n", "\n\t")
return text
}