@@ -2,6 +2,7 @@ package httpx_test
2
2
3
3
import (
4
4
"bytes"
5
+ "context"
5
6
"fmt"
6
7
"net"
7
8
"net/http"
@@ -64,14 +65,16 @@ func newTestHTTPServer(port int) *httptest.Server {
64
65
}
65
66
66
67
func TestDoTrace (t * testing.T ) {
68
+ ctx := context .Background ()
69
+
67
70
defer dates .SetNowFunc (time .Now )
68
71
69
72
dates .SetNowFunc (dates .NewSequentialNow (time .Date (2019 , 10 , 7 , 15 , 21 , 30 , 123456789 , time .UTC ), time .Second ))
70
73
71
74
server := newTestHTTPServer (52025 )
72
75
73
76
// test with a text response
74
- request , err := httpx .NewRequest ("GET" , server .URL + "?cmd=success" , nil , nil )
77
+ request , err := httpx .NewRequest (ctx , "GET" , server .URL + "?cmd=success" , nil , nil )
75
78
require .NoError (t , err )
76
79
77
80
trace , err := httpx .DoTrace (http .DefaultClient , request , nil , nil , - 1 )
@@ -89,7 +92,7 @@ func TestDoTrace(t *testing.T) {
89
92
assert .Equal (t , ">>>>>>>> GET http://127.0.0.1:52025?cmd=success\n GET /?cmd=success HTTP/1.1\r \n Host: 127.0.0.1:52025\r \n User-Agent: Go-http-client/1.1\r \n Accept-Encoding: gzip\r \n \r \n \n <<<<<<<<\n HTTP/1.1 200 OK\r \n Content-Length: 16\r \n Content-Type: text/plain; charset=utf-8\r \n Date: Wed, 11 Apr 2018 18:24:30 GMT\r \n \r \n { \" ok\" : \" true\" }" , trace .String ())
90
93
91
94
// test with a binary response
92
- request , err = httpx .NewRequest ("GET" , server .URL + "?cmd=binary" , nil , nil )
95
+ request , err = httpx .NewRequest (ctx , "GET" , server .URL + "?cmd=binary" , nil , nil )
93
96
require .NoError (t , err )
94
97
95
98
trace , err = httpx .DoTrace (http .DefaultClient , request , nil , nil , - 1 )
@@ -100,7 +103,7 @@ func TestDoTrace(t *testing.T) {
100
103
assert .Equal (t , "HTTP/1.1 200 OK\r \n Content-Length: 1000\r \n Content-Type: application/octet-stream\r \n Date: Wed, 11 Apr 2018 18:24:30 GMT\r \n \r \n ..." , string (trace .SanitizedResponse ("..." )))
101
104
102
105
// test with a response containing null chars
103
- request , err = httpx .NewRequest ("GET" , server .URL + "?cmd=nullchars" , nil , nil )
106
+ request , err = httpx .NewRequest (ctx , "GET" , server .URL + "?cmd=nullchars" , nil , nil )
104
107
require .NoError (t , err )
105
108
106
109
trace , err = httpx .DoTrace (http .DefaultClient , request , nil , nil , - 1 )
@@ -111,7 +114,7 @@ func TestDoTrace(t *testing.T) {
111
114
assert .Equal (t , "HTTP/1.1 200 OK\r \n Content-Length: 7\r \n Content-Type: text/plain; charset=utf-8\r \n Date: Wed, 11 Apr 2018 18:24:30 GMT\r \n \r \n ab�cd��" , string (trace .SanitizedResponse ("..." )))
112
115
113
116
// test with a response containing invalid UTF8 sequences
114
- request , err = httpx .NewRequest ("GET" , server .URL + "?cmd=badutf8" , nil , nil )
117
+ request , err = httpx .NewRequest (ctx , "GET" , server .URL + "?cmd=badutf8" , nil , nil )
115
118
require .NoError (t , err )
116
119
117
120
trace , err = httpx .DoTrace (http .DefaultClient , request , nil , nil , - 1 )
@@ -164,6 +167,8 @@ func TestMaxBodyBytes(t *testing.T) {
164
167
}
165
168
166
169
func TestNonUTF8Request (t * testing.T ) {
170
+ ctx := context .Background ()
171
+
167
172
defer httpx .SetRequestor (httpx .DefaultRequestor )
168
173
169
174
httpx .SetRequestor (httpx .NewMockRequestor (map [string ][]* httpx.MockResponse {
@@ -172,7 +177,7 @@ func TestNonUTF8Request(t *testing.T) {
172
177
},
173
178
}))
174
179
175
- request , err := httpx .NewRequest ("GET" , "https://temba.io" , bytes .NewReader ([]byte {'\xc3' , '\x28' }), map [string ]string {"X-Badness" : string ([]byte {'\xc3' , '\x28' })})
180
+ request , err := httpx .NewRequest (ctx , "GET" , "https://temba.io" , bytes .NewReader ([]byte {'\xc3' , '\x28' }), map [string ]string {"X-Badness" : string ([]byte {'\xc3' , '\x28' })})
176
181
require .NoError (t , err )
177
182
178
183
trace , err := httpx .DoTrace (http .DefaultClient , request , nil , nil , - 1 )
@@ -189,6 +194,8 @@ func TestNonUTF8Request(t *testing.T) {
189
194
}
190
195
191
196
func TestNonUTF8Response (t * testing.T ) {
197
+ ctx := context .Background ()
198
+
192
199
defer httpx .SetRequestor (httpx .DefaultRequestor )
193
200
194
201
httpx .SetRequestor (httpx .NewMockRequestor (map [string ][]* httpx.MockResponse {
@@ -197,7 +204,7 @@ func TestNonUTF8Response(t *testing.T) {
197
204
},
198
205
}))
199
206
200
- request , err := httpx .NewRequest ("GET" , "https://temba.io" , nil , nil )
207
+ request , err := httpx .NewRequest (ctx , "GET" , "https://temba.io" , nil , nil )
201
208
require .NoError (t , err )
202
209
203
210
trace , err := httpx .DoTrace (http .DefaultClient , request , nil , nil , - 1 )
0 commit comments