|
2 | 2 | //
|
3 | 3 | // Details about the gofight project are found in github page:
|
4 | 4 | //
|
5 |
| -// https://github.com/appleboy/gofight |
| 5 | +// https://github.com/appleboy/gofight |
6 | 6 | //
|
7 | 7 | // Installation:
|
8 | 8 | //
|
9 |
| -// $ go get -u github.com/appleboy/gofight |
| 9 | +// $ go get -u github.com/appleboy/gofight |
10 | 10 | //
|
11 | 11 | // Set Header: You can add custom header via SetHeader func.
|
12 | 12 | //
|
13 |
| -// SetHeader(gofight.H{ |
14 |
| -// "X-Version": version, |
15 |
| -// }) |
| 13 | +// SetHeader(gofight.H{ |
| 14 | +// "X-Version": version, |
| 15 | +// }) |
16 | 16 | //
|
17 | 17 | // Set Cookie: You can add custom cookie via SetCookie func.
|
18 | 18 | //
|
19 |
| -// SetCookie(gofight.H{ |
20 |
| -// "foo": "bar", |
21 |
| -// }) |
| 19 | +// SetCookie(gofight.H{ |
| 20 | +// "foo": "bar", |
| 21 | +// }) |
22 | 22 | //
|
23 | 23 | // Set query string: Using SetQuery to generate query string data.
|
24 | 24 | //
|
25 |
| -// SetQuery(gofight.H{ |
26 |
| -// "a": "1", |
27 |
| -// "b": "2", |
28 |
| -// }) |
| 25 | +// SetQuery(gofight.H{ |
| 26 | +// "a": "1", |
| 27 | +// "b": "2", |
| 28 | +// }) |
29 | 29 | //
|
30 | 30 | // POST FORM Data: Using SetForm to generate form data.
|
31 | 31 | //
|
32 |
| -// SetForm(gofight.H{ |
33 |
| -// "a": "1", |
34 |
| -// "b": "2", |
35 |
| -// }) |
| 32 | +// SetForm(gofight.H{ |
| 33 | +// "a": "1", |
| 34 | +// "b": "2", |
| 35 | +// }) |
36 | 36 | //
|
37 | 37 | // POST JSON Data: Using SetJSON to generate json data.
|
38 | 38 | //
|
39 |
| -// SetJSON(gofight.H{ |
40 |
| -// "a": "1", |
41 |
| -// "b": "2", |
42 |
| -// }) |
| 39 | +// SetJSON(gofight.H{ |
| 40 | +// "a": "1", |
| 41 | +// "b": "2", |
| 42 | +// }) |
43 | 43 | //
|
44 | 44 | // POST RAW Data: Using SetBody to generate raw data.
|
45 | 45 | //
|
46 |
| -// SetBody("a=1&b=1") |
| 46 | +// SetBody("a=1&b=1") |
47 | 47 | //
|
48 | 48 | // For more details, see the documentation and example.
|
49 |
| -// |
50 | 49 | package gofight
|
51 | 50 |
|
52 | 51 | import (
|
53 | 52 | "bytes"
|
| 53 | + "context" |
54 | 54 | "encoding/json"
|
55 | 55 | "io"
|
56 | 56 | "log"
|
@@ -272,23 +272,24 @@ func (rc *RequestConfig) SetPath(str string) *RequestConfig {
|
272 | 272 | }
|
273 | 273 |
|
274 | 274 | // SetQueryD supply query string, support query using string array input.
|
275 |
| -// ex. /reqpath/?Ids[]=E&Ids[]=M usage: IDArray:=[]string{"E","M"} r.GET("reqpath").SetQueryD(gofight.D{`Ids[]`: IDArray}) |
| 275 | +// ex. /reqpath/?Ids[]=E&Ids[]=M usage: |
| 276 | +// IDArray:=[]string{"E","M"} r.GET("reqpath").SetQueryD(gofight.D{`Ids[]`: IDArray}) |
276 | 277 | func (rc *RequestConfig) SetQueryD(query D) *RequestConfig {
|
277 | 278 | var buf strings.Builder
|
278 | 279 | buf.WriteString("?")
|
279 | 280 | for k, v := range query {
|
280 |
| - switch v.(type) { |
| 281 | + switch v := v.(type) { |
281 | 282 | case string:
|
282 |
| - buf.WriteString(k + "=" + v.(string)) |
| 283 | + buf.WriteString(k + "=" + v) |
283 | 284 | buf.WriteString("&")
|
284 | 285 | case []string:
|
285 |
| - for _, info := range v.([]string) { |
| 286 | + for _, info := range v { |
286 | 287 | buf.WriteString(k + "=" + info)
|
287 | 288 | buf.WriteString("&")
|
288 | 289 | }
|
289 | 290 | }
|
290 | 291 | }
|
291 |
| - rc.Path = rc.Path + buf.String()[:len(buf.String())-1] |
| 292 | + rc.Path += buf.String()[:len(buf.String())-1] |
292 | 293 | return rc
|
293 | 294 | }
|
294 | 295 |
|
@@ -337,7 +338,7 @@ func (rc *RequestConfig) initTest() (*http.Request, *httptest.ResponseRecorder)
|
337 | 338 |
|
338 | 339 | body := bytes.NewBufferString(rc.Body)
|
339 | 340 |
|
340 |
| - req, _ := http.NewRequest(rc.Method, rc.Path, body) |
| 341 | + req, _ := http.NewRequestWithContext(context.Background(), rc.Method, rc.Path, body) |
341 | 342 | req.RequestURI = req.URL.RequestURI()
|
342 | 343 |
|
343 | 344 | if len(qs) > 0 {
|
|
0 commit comments