-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
httpretty_test.go
294 lines (267 loc) · 7.19 KB
/
httpretty_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
package httpretty
import (
"bytes"
_ "embed"
"io"
"net"
"net/http"
"net/url"
"os"
"testing"
"time"
)
// race is a flag that can be usde to detect whether the race detector is on.
// It was added because as of Go 1.13.7 the TestOutgoingConcurrency test is failing because of a bug on the
// net/http standard library package.
// See race_test.go.
// See https://golang.org/issue/30597
var race bool
// sample from http://bastiat.org/fr/petition.html
//
//go:embed testdata/petition.golden
var petition string
func TestPrintRequest(t *testing.T) {
t.Parallel()
var req, err = http.NewRequest(http.MethodPost, "http://wxww.example.com/", nil)
if err != nil {
panic(err)
}
logger := &Logger{
TLS: true,
RequestHeader: true,
RequestBody: true,
ResponseHeader: true,
ResponseBody: true,
}
var buf bytes.Buffer
logger.SetOutput(&buf)
logger.PrintRequest(req)
want := `> POST / HTTP/1.1
> Host: wxww.example.com
`
if got := buf.String(); got != want {
t.Errorf("PrintRequest(req) = %v, wanted %v", got, want)
}
}
func TestPrintRequestWithAlign(t *testing.T) {
t.Parallel()
var req, err = http.NewRequest(http.MethodPost, "http://wxww.example.com/", nil)
if err != nil {
panic(err)
}
req.Header.Set("Header", "foo")
req.Header.Set("Other-Header", "bar")
logger := &Logger{
TLS: true,
RequestHeader: true,
RequestBody: true,
ResponseHeader: true,
ResponseBody: true,
Align: true,
}
var buf bytes.Buffer
logger.SetOutput(&buf)
logger.PrintRequest(req)
want := `> POST / HTTP/1.1
> Host: wxww.example.com
> Header: foo
> Other-Header: bar
`
if got := buf.String(); got != want {
t.Errorf("PrintRequest(req) = %v, wanted %v", got, want)
}
}
func TestPrintRequestWithColors(t *testing.T) {
t.Parallel()
var req, err = http.NewRequest(http.MethodPost, "http://wxww.example.com/", nil)
if err != nil {
panic(err)
}
logger := &Logger{
TLS: true,
RequestHeader: true,
RequestBody: true,
ResponseHeader: true,
ResponseBody: true,
Colors: true,
}
var buf bytes.Buffer
logger.SetOutput(&buf)
logger.PrintRequest(req)
want := "> \x1b[34;1mPOST\x1b[0m \x1b[33m/\x1b[0m \x1b[34mHTTP/1.1\x1b[0m" +
"\n> \x1b[34;1mHost\x1b[0m\x1b[31m:\x1b[0m \x1b[33mwxww.example.com\x1b[0m\n\n"
if got := buf.String(); got != want {
t.Errorf("PrintRequest(req) = %v, wanted %v", got, want)
}
}
func TestEncodingQueryStringParams(t *testing.T) {
// Regression test for verifying query string parameters are being encoded correctly when printing with colors.
// Issue reported by @mislav in https://github.com/henvic/httpretty/issues/9.
t.Parallel()
qs := url.Values{}
qs.Set("a", "b")
qs.Set("i", "j")
qs.Set("x", "y")
qs.Set("z", "+=")
qs.Set("var", "foo&bar")
u := url.URL{
Scheme: "http",
Host: "www.example.com",
Path: "/mypath",
RawQuery: qs.Encode(),
}
var req, err = http.NewRequest(http.MethodPost, u.String(), nil)
if err != nil {
panic(err)
}
logger := &Logger{
TLS: true,
RequestHeader: true,
RequestBody: true,
ResponseHeader: true,
ResponseBody: true,
Colors: true,
}
var buf bytes.Buffer
logger.SetOutput(&buf)
logger.PrintRequest(req)
want := "> \x1b[34;1mPOST\x1b[0m \x1b[33m/mypath?a=b&i=j&var=foo%26bar&x=y&z=%2B%3D\x1b[0m \x1b[34mHTTP/1.1\x1b[0m" +
"\n> \x1b[34;1mHost\x1b[0m\x1b[31m:\x1b[0m \x1b[33mwww.example.com\x1b[0m\n\n"
if got := buf.String(); got != want {
t.Errorf("PrintRequest(req) = %v, wanted %v", got, want)
}
}
func TestEncodingQueryStringParamsNoColors(t *testing.T) {
t.Parallel()
qs := url.Values{}
qs.Set("a", "b")
qs.Set("i", "j")
qs.Set("x", "y")
qs.Set("z", "+=")
qs.Set("var", "foo&bar")
u := url.URL{
Scheme: "http",
Host: "www.example.com",
Path: "/mypath",
RawQuery: qs.Encode(),
}
var req, err = http.NewRequest(http.MethodPost, u.String(), nil)
if err != nil {
panic(err)
}
logger := &Logger{
TLS: true,
RequestHeader: true,
RequestBody: true,
ResponseHeader: true,
ResponseBody: true,
}
var buf bytes.Buffer
logger.SetOutput(&buf)
logger.PrintRequest(req)
want := `> POST /mypath?a=b&i=j&var=foo%26bar&x=y&z=%2B%3D HTTP/1.1
> Host: www.example.com
`
if got := buf.String(); got != want {
t.Errorf("PrintRequest(req) = %v, wanted %v", got, want)
}
}
func TestPrintRequestFiltered(t *testing.T) {
t.Parallel()
var req, err = http.NewRequest(http.MethodPost, "http://wxww.example.com/", nil)
if err != nil {
panic(err)
}
logger := &Logger{
TLS: true,
RequestHeader: true,
RequestBody: true,
ResponseHeader: true,
ResponseBody: true,
}
var buf bytes.Buffer
logger.SetOutput(&buf)
logger.SetFilter(func(req *http.Request) (skip bool, err error) {
return true, nil
})
logger.PrintRequest(req)
if got := buf.Len(); got != 0 {
t.Errorf("got %v from logger, wanted nothing (everything should be filtered)", got)
}
}
func TestPrintRequestNil(t *testing.T) {
t.Parallel()
logger := &Logger{
TLS: true,
RequestHeader: true,
RequestBody: true,
ResponseHeader: true,
ResponseBody: true,
}
var buf bytes.Buffer
logger.SetOutput(&buf)
logger.PrintRequest(nil)
want := "> error: null request\n"
if got := buf.String(); got != want {
t.Errorf("PrintRequest(req) = %v, wanted %v", got, want)
}
}
func TestPrintResponseNil(t *testing.T) {
t.Parallel()
logger := &Logger{
TLS: true,
RequestHeader: true,
RequestBody: true,
ResponseHeader: true,
ResponseBody: true,
}
var buf bytes.Buffer
logger.SetOutput(&buf)
logger.PrintResponse(nil)
want := "< error: null response\n"
if got := buf.String(); got != want {
t.Errorf("PrintResponse(req) = %v, wanted %v", got, want)
}
}
func testBody(t *testing.T, r io.Reader, want []byte) {
t.Helper()
got, err := io.ReadAll(r)
if err != nil {
t.Errorf("expected no error reading response body, got %v instead", err)
}
if !bytes.Equal(got, want) {
if len(got) != len(want) && (len(got) > 100 || len(want) > 100) {
t.Errorf(`got body length = %v, wanted %v`, len(got), len(want))
} else {
t.Errorf(`got body = %v, wanted %v`, string(got), string(want))
}
}
}
func TestJSONFormatterWriterError(t *testing.T) {
// verifies if function doesn't panic if passed writer isn't *bytes.Buffer
f := &JSONFormatter{}
want := "underlying writer for JSONFormatter must be *bytes.Buffer"
if err := f.Format(os.Stdout, []byte(`{}`)); err == nil || err.Error() != want {
t.Errorf("got format error = %v, wanted %v", err, want)
}
}
// newTransport creates a new HTTP Transport.
//
// BUG(henvic): this function is mostly used at this moment because of a data race condition on the standard library.
// See https://github.com/golang/go/issues/30597 for details.
func newTransport() *http.Transport {
// values copied from Go 1.13.7 http.DefaultTransport variable.
return &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
}