forked from ido50/requests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequests_test.go
350 lines (299 loc) · 10.2 KB
/
requests_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
package requests
import (
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/pkg/errors"
)
func TestPlainTextResponse(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintln(w, "Hello, client")
}))
defer ts.Close()
cli := NewClient(ts.URL).Accept("text/plain")
var text string
var status int
err := cli.NewRequest("GET", "/").
Into(&text).
StatusInto(&status).
Run()
if err != nil {
t.Fatalf("Failed request: %s", err)
}
if text != "Hello, client" {
t.Errorf("Failed reading plain text body: got %+q, expected %+q", text, "Hello, client")
}
if status != 200 {
t.Errorf("Failed to read status: got %d, expected 200", status)
}
}
func TestJSONResponse(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "encoding/json")
fmt.Fprintln(w, `{ "message": "Hello, client" }`)
}))
defer ts.Close()
cli := NewClient(ts.URL)
var response struct {
Message string `json:"message"`
}
err := cli.NewRequest("GET", "/").
Into(&response).
Run()
if err != nil {
t.Fatalf("Failed request: %s", err)
}
if response.Message != "Hello, client" {
t.Errorf("Failed reading JSON body: got %#v, expected %+q", response, "Hello, client")
}
}
func TestResponseHeader(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Custom-Header", "bla")
w.WriteHeader(http.StatusNoContent)
}))
defer ts.Close()
cli := NewClient(ts.URL)
var customHeader string
err := cli.NewRequest("GET", "/").
HeaderInto("Custom-Header", &customHeader).
ExpectedStatus(http.StatusNoContent).
Run()
if err != nil {
t.Fatalf("Failed request: %s", err)
}
if customHeader != "bla" {
t.Errorf("Failed reading custom header: got %+q, expected %+q", customHeader, "bla")
}
}
func TestErrorResponse(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("bad request"))
}))
defer ts.Close()
cli := NewClient(ts.URL)
var status int
err := cli.NewRequest("GET", "/").
StatusInto(&status).
ExpectedStatus(http.StatusBadRequest).
Run()
if err != nil {
t.Fatalf("Failed request: %s", err)
}
if status != http.StatusBadRequest {
t.Errorf("Failed reading non-200 status code: got %d, expected %d", status, http.StatusBadRequest)
}
}
//func TestNoTLSVerify(t *testing.T) {
//cli := NewClient("")
//cli.NewRequest("GET", "/")
//transport := cli.httpCli.Transport.(*http.Transport)
//if transport.TLSClientConfig.InsecureSkipVerify {
//t.Fatal("Transport should verify TLS connections but isn't")
//}
//cli.NoTLSVerify(true).NewRequest("GET", "/")
//transport = cli.httpCli.Transport.(*http.Transport)
//if !transport.TLSClientConfig.InsecureSkipVerify {
//t.Fatal("Transport shouldn't verify TLS connections but is")
//}
//}
func TestResponseSizeLimit(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintln(w, "Lorem ipsum dolor sit amet")
}))
defer ts.Close()
var data string
cli := NewClient(ts.URL)
err := cli.NewRequest("GET", "/").SizeLimit(5).Into(&data).Run()
if err != ErrSizeExceeded {
t.Fatal("Response size was not limited as expected")
}
}
func TestRequestTimeout(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(2 * time.Second)
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintln(w, "Lorem ipsum dolor sit amet")
}))
defer ts.Close()
err := NewClient(ts.URL).
NewRequest("GET", "/").
Timeout(1 * time.Second).
Run()
if err == nil {
t.Fatal("Response succeeded but should have timed out")
} else if err != ErrTimeoutReached {
t.Fatalf("Request failed but not with timeout error: %s", err)
}
}
func TestRequestRetries(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintln(w, "Lorem ipsum dolor sit amet")
}))
ts.Close() // this is on purpose
cli := NewClient(ts.URL)
start := time.Now()
err := cli.NewRequest("GET", "/").RetryLimit(2).Run()
end := time.Now()
if err == nil {
t.Fatal("Request did not fail as expected")
} else if end.Sub(start) < 6*time.Second {
t.Fatal("Request was not retried as expected")
}
}
func TestCustomBodyHandler(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/x.rot13")
fmt.Fprintln(w, "Hello, World")
}))
defer ts.Close()
var data []byte
err := NewClient(ts.URL).
NewRequest("GET", "/").
BodyHandler(func(status int, cType string, reader io.Reader, target interface{}) error {
if cType != "application/x.rot13" {
return errors.Errorf("unexpected content type %s returned", cType)
}
b, err := ioutil.ReadAll(reader)
if err != nil {
return errors.Wrap(err, "failed reading response body")
}
t := target.(*[]byte)
*t = rot13(b[:len(b)-1]) // chomp newline
return nil
}).
Into(&data).
Run()
if err != nil {
t.Fatalf("Failed custom body handler: %s", err)
} else if string(data) != "Uryyb, Jbeyq" {
t.Fatalf("Body handler returned wrong content: %q", string(data))
}
}
func TestTLSConnection(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintln(w, "Lorem ipsum dolor sit amet")
}))
defer ts.Close()
certpem := `-----BEGIN CERTIFICATE-----
MIIC+TCCAeGgAwIBAgIRAJTH5D+Q6YNUn+jnF1TDxikwDQYJKoZIhvcNAQELBQAw
EjEQMA4GA1UEChMHQWNtZSBDbzAeFw0xODA3MTExNDM1MjBaFw0xOTA3MTExNDM1
MjBaMBIxEDAOBgNVBAoTB0FjbWUgQ28wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAw
ggEKAoIBAQCzd/q1zkG6XODPKBVI8gjV12JBIusZHyeF/fefvCzTqOhWUP3+VDOa
3JOQqe06qiXfe3fbWGtqLhVWvo48RMtQCAq+nfbdH5peSfHTHqe3x1WtgVAqPsRx
nP0TZ9MQvMfJDvyMuJtowVTktOSKIEX6DYct3v6OJrdnFq/SB1gxdcjP03Gvr688
PN15llTtPuWxcmim7uTQHJZ4ep1xsD7XiqCfU8nAM9FCi5Nsm+Lu4tPbN8JH6g58
2VnPtsrbHmG7i5t3a3c6MDN11SR7C/76CsjQ09FHCCzPMoXguPCW5Xh813vfYFvE
9Yi8k1rE4ahDQGYTgSmzSdtGqjAXj+LFAgMBAAGjSjBIMA4GA1UdDwEB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okuA8Xs+wU68U0nYsNESzcDRz7erFpDU7ypydvGS4x2X+0NK5hLbjoETN9KZXAG
8ksWM5BAP41ZLMfe+bl02F94u3nWATU3RuFeS9OwLx0i+1T4ll5wwh1yfacP6Egz
Ybqw0EECgYEAzPrjCZuforDiHxHyr0stiTvCmWIbBawwz5rFTJ2FfmKcYfwlnX9+
9FLnDAIwx9nramUbKd4Y9b/PiDfDGN+eROxU5mxDUZF87Ayy8Ejst2n0dEDQ9izJ
XXcafi0UU0ShCSqovya24N+Ul6bLOMwFkvjzwO+/VA7wHJTCtknVArECgYEA4COJ
EZpg8woINisf02xenbmuxs6R/+dQXFClap446IUQaDwHF+8Fv50nUr0/uUEQijbY
wKC9cwN9A8VVSDethXpYLDKQniu3P6hwtsQRiBWha275YAtmLNykpacw8YtIzpz+
cY3p5xAxZuGLyki3HaFJQWMHOKvB38i1AXapXlUCgYB5kg8XgrIiFpB15votVwQR
0VywBcyLB74HUv7TWtVyyN+BCb/xck2EcKrRp3bxAOErv/1lTnE1R2a5noDafr3q
mNQduXYPqZ8SjNGPy2CBw5iVXl/QsW1YPqx6yxez7w8nVaKxhC+QnWoOq4D6FZ70
tSw0cLzkCNwFx4DYBmAMcQKBgB/ISi5p0qeD26g6szeanUwGQWdFcWR1G2sLsHkO
2Ij4HVx6bpMRPKJwGVxdI4UUWdEPd+rQoCyH6Rk4ySAFbSCJOamCvgj/r+th6iGw
ab//OTVvtgLNev6PhvVKYOFPW9KYZmgZtHokTK0G/HiBmR2leirAbQy3JjWiUzBS
8C9FAoGAfG4or+rOZYYcjZgpgORip97ugPzK0OPp1Z6wPF3Nhv3cxhU/DbcprNjM
aUz5HXBcNYdNTwmMhJeR+2PjQLwK8XKQyR/OXPwgpDhYdpI8jx4AV4VakekES2Mx
0soArFGOSvrfeB0pT7L6hzoCS806+qZPiHpag+h2iFnooNQWvC8=
-----END RSA PRIVATE KEY-----`
cli := NewClient(ts.URL).Accept("text/plain").SetTLS([]byte(certpem),
[]byte(keypem), []byte(certpem))
var text string
var status int
err := cli.NewRequest("GET", "/").
Into(&text).
StatusInto(&status).
Run()
if err != nil {
t.Fatalf("Failed request: %s", err)
}
if text != "Lorem ipsum dolor sit amet" {
t.Errorf("Failed reading plain text body: got %+q, expected %+q", text, "Hello, client")
}
if status != 200 {
t.Errorf("Failed to read status: got %d, expected 200", status)
}
}
func rot13(b []byte) []byte {
for i, r := range b {
switch {
case r >= 'A' && r <= 'Z':
b[i] = 'A' + (((r - 'A') + 13) % 26)
case r >= 'a' && r <= 'z':
b[i] = 'a' + (((r - 'a') + 13) % 26)
default:
b[i] = r
}
}
return b
}
func TestCompressedRequest(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
cr, err := gzip.NewReader(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Failed creating gzip reader: %s\n", err)
return
}
defer cr.Close()
content, err := io.ReadAll(cr)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Failed reading request body: %s\n", err)
return
}
fmt.Fprintln(w, string(content))
}))
defer ts.Close()
cli := NewClient(ts.URL).
Accept("text/plain").
CompressWith(CompressionAlgorithmGzip)
for i, input := range []string{"hello world", "bla", "AAABBb"} {
var output string
err := cli.NewRequest("GET", "/").
Into(&output).
Body([]byte(input), "text/plain").
Run()
if err != nil {
t.Fatalf("Failed request: %s", err)
}
if output != input {
t.Errorf("Compression test %d: got %+q, expected %+q", i, output, input)
}
}
}