44 "context"
55 "fmt"
66 "net/http"
7+ "net/url"
78 "strconv"
89 "sync/atomic"
910 "testing"
@@ -14,8 +15,6 @@ import (
1415 "github.com/stretchr/testify/assert"
1516)
1617
17- const testURL = "https://api.github.com"
18-
1918var _port = int64 (10086 )
2019
2120func nextAddr () string {
@@ -159,6 +158,21 @@ func TestRequest_SetQuery(t *testing.T) {
159158 resp := Get ("http://%s" , addr ).SetQuery (nil ).Send (nil )
160159 assert .Error (t , resp .Err ())
161160 })
161+
162+ t .Run ("" , func (t * testing.T ) {
163+ req := Get ("http://%s" , addr ).SetQuery (url.Values {
164+ "name" : []string {"xxx" },
165+ })
166+ assert .Equal (t , req .url , "http://" + addr + "?name=xxx" )
167+ })
168+
169+ t .Run ("" , func (t * testing.T ) {
170+ type Req struct {
171+ Name string `form:"name"`
172+ }
173+ req := Get ("http://%s" , addr ).SetQuery (Req {Name : "xxx" })
174+ assert .Equal (t , req .url , "http://" + addr + "?name=xxx" )
175+ })
162176}
163177
164178func TestRequest_Send (t * testing.T ) {
@@ -215,24 +229,40 @@ func TestMiddleware(t *testing.T) {
215229 time .Sleep (100 * time .Millisecond )
216230
217231 t .Run ("before" , func (t * testing.T ) {
218- opt := WithBefore ( func (ctx context.Context , request * http.Request ) (context.Context , error ) {
232+ before := func (ctx context.Context , request * http.Request ) (context.Context , error ) {
219233 return ctx , errors .New ("status error" )
220- })
221- cli , _ := NewClient (opt )
222- resp := cli .Post ("http://%s/404" , addr ).Send (nil )
223- assert .Error (t , resp .Err ())
234+ }
235+
236+ {
237+ cli , _ := NewClient (WithBefore (before ))
238+ resp := cli .Post ("http://%s/404" , addr ).Send (nil )
239+ assert .Error (t , resp .Err ())
240+ }
241+
242+ {
243+ resp := Post ("http://%s/404" , addr ).SetBefore (before ).Send (nil )
244+ assert .Error (t , resp .Err ())
245+ }
224246 })
225247
226248 t .Run ("after" , func (t * testing.T ) {
227- opt := WithAfter ( func (ctx context.Context , response * http.Response ) (context.Context , error ) {
249+ after := func (ctx context.Context , response * http.Response ) (context.Context , error ) {
228250 if response .StatusCode != http .StatusOK {
229251 return ctx , errors .New ("status error" )
230252 }
231253 return ctx , nil
232- })
233- cli , _ := NewClient (opt )
234- resp := cli .Post ("http://%s/404" , addr ).Send (nil )
235- assert .Error (t , resp .Err ())
254+ }
255+
256+ {
257+ cli , _ := NewClient (WithAfter (after ))
258+ resp := cli .Post ("http://%s/404" , addr ).Send (nil )
259+ assert .Error (t , resp .Err ())
260+ }
261+
262+ {
263+ resp := Post ("http://%s/404" , addr ).SetAfter (after ).Send (nil )
264+ assert .Error (t , resp .Err ())
265+ }
236266 })
237267
238268 t .Run ("latency" , func (t * testing.T ) {
0 commit comments