diff --git a/bench/report.go b/bench/report.go index a501368..f2f9032 100644 --- a/bench/report.go +++ b/bench/report.go @@ -184,7 +184,7 @@ func (r *Report) Process(work chan struct{}) { func (r *Report) WaitAll() { <-r.waitQuit //TODO 处理错误 - r.outputReport() //输出最终报表 + _ = r.outputReport() //输出最终报表 } func (r *Report) calBody(resp *http.Response, bodySize uint64) { diff --git a/dataflow/dataflow.go b/dataflow/dataflow.go index e237b63..d9e499f 100644 --- a/dataflow/dataflow.go +++ b/dataflow/dataflow.go @@ -3,8 +3,6 @@ package dataflow import ( "context" "errors" - "fmt" - "net" "net/http" "net/url" "time" @@ -13,10 +11,10 @@ import ( "github.com/guonaihong/gout/decode" "github.com/guonaihong/gout/encode" "github.com/guonaihong/gout/enjson" + "github.com/guonaihong/gout/hcutil" "github.com/guonaihong/gout/middler" "github.com/guonaihong/gout/middleware/rsp/autodecodebody" "github.com/guonaihong/gout/setting" - "golang.org/x/net/proxy" ) const ( @@ -38,43 +36,43 @@ type DataFlow struct { // GET send HTTP GET method func (df *DataFlow) GET(url string, urlStruct ...interface{}) *DataFlow { - df.Req = reqDef(get, cleanPaths(url), df.out, urlStruct...) + df.Req, df.Err = reqDef(get, cleanPaths(url), df.out, urlStruct...) return df } // POST send HTTP POST method func (df *DataFlow) POST(url string, urlStruct ...interface{}) *DataFlow { - df.Req = reqDef(post, cleanPaths(url), df.out, urlStruct...) + df.Req, df.Err = reqDef(post, cleanPaths(url), df.out, urlStruct...) return df } // PUT send HTTP PUT method func (df *DataFlow) PUT(url string, urlStruct ...interface{}) *DataFlow { - df.Req = reqDef(put, cleanPaths(url), df.out, urlStruct...) + df.Req, df.Err = reqDef(put, cleanPaths(url), df.out, urlStruct...) return df } // DELETE send HTTP DELETE method func (df *DataFlow) DELETE(url string, urlStruct ...interface{}) *DataFlow { - df.Req = reqDef(delete2, cleanPaths(url), df.out, urlStruct...) + df.Req, df.Err = reqDef(delete2, cleanPaths(url), df.out, urlStruct...) return df } // PATCH send HTTP PATCH method func (df *DataFlow) PATCH(url string, urlStruct ...interface{}) *DataFlow { - df.Req = reqDef(patch, cleanPaths(url), df.out, urlStruct...) + df.Req, df.Err = reqDef(patch, cleanPaths(url), df.out, urlStruct...) return df } // HEAD send HTTP HEAD method func (df *DataFlow) HEAD(url string, urlStruct ...interface{}) *DataFlow { - df.Req = reqDef(head, cleanPaths(url), df.out, urlStruct...) + df.Req, df.Err = reqDef(head, cleanPaths(url), df.out, urlStruct...) return df } // OPTIONS send HTTP OPTIONS method func (df *DataFlow) OPTIONS(url string, urlStruct ...interface{}) *DataFlow { - df.Req = reqDef(options, cleanPaths(url), df.out, urlStruct...) + df.Req, df.Err = reqDef(options, cleanPaths(url), df.out, urlStruct...) return df } @@ -135,7 +133,7 @@ func (df *DataFlow) SetURL(url string, urlStruct ...interface{}) *DataFlow { } if df.Req.url == "" && df.Req.req == nil { - df.Req = reqDef(df.method, cleanPaths(url), df.out, urlStruct...) + df.Req, df.Err = reqDef(df.method, cleanPaths(url), df.out, urlStruct...) return df } @@ -224,36 +222,11 @@ func (df *DataFlow) SetProtoBuf(obj interface{}) *DataFlow { return df } -func (df *DataFlow) initTransport() { - if df.out.Client.Transport == nil { - df.out.Client.Transport = &http.Transport{} - } -} - -func (df *DataFlow) getTransport() (*http.Transport, bool) { - // 直接return df.out.Client.Transport.(*http.Transport) 等于下面的写法 - // ts := df.out.Client.Transport.(*http.Transport) - // return ts 编译会报错 - ts, ok := df.out.Client.Transport.(*http.Transport) - return ts, ok -} - // UnixSocket 函数会修改Transport, 请像对待全局变量一样对待UnixSocket // 对于全局变量的解释可看下面的链接 // https://github.com/guonaihong/gout/issues/373 func (df *DataFlow) UnixSocket(path string) *DataFlow { - df.initTransport() - - transport, ok := df.getTransport() - if !ok { - df.Req.Err = fmt.Errorf("UnixSocket:not found http.transport:%T", df.out.Client.Transport) - return df - } - - transport.Dial = func(proto, addr string) (conn net.Conn, err error) { - return net.Dial("unix", path) - } - + df.Err = hcutil.UnixSocket(df.out.Client, path) return df } @@ -261,22 +234,7 @@ func (df *DataFlow) UnixSocket(path string) *DataFlow { // 对于全局变量的解释可看下面的链接 // https://github.com/guonaihong/gout/issues/373 func (df *DataFlow) SetProxy(proxyURL string) *DataFlow { - proxy, err := url.Parse(modifyURL(proxyURL)) - if err != nil { - df.Req.Err = err - return df - } - - df.initTransport() - - transport, ok := df.getTransport() - if !ok { - df.Req.Err = fmt.Errorf("SetProxy:not found http.transport:%T", df.out.Client.Transport) - return df - } - - transport.Proxy = http.ProxyURL(proxy) - + df.Err = hcutil.SetProxy(df.out.Client, proxyURL) return df } @@ -284,21 +242,7 @@ func (df *DataFlow) SetProxy(proxyURL string) *DataFlow { // 对于全局变量的解释可看下面的链接 // https://github.com/guonaihong/gout/issues/373 func (df *DataFlow) SetSOCKS5(addr string) *DataFlow { - dialer, err := proxy.SOCKS5("tcp", addr, nil, proxy.Direct) - if err != nil { - df.Req.Err = err - return df - } - - df.initTransport() - - transport, ok := df.getTransport() - if !ok { - df.Req.Err = fmt.Errorf("SetSOCKS5:not found http.transport:%T", df.out.Client.Transport) - return df - } - - transport.Dial = dialer.Dial + df.Err = hcutil.SetSOCKS5(df.out.Client, addr) return df } @@ -391,8 +335,6 @@ func (df *DataFlow) SetTimeout(d time.Duration) *DataFlow { // WithContext set context, and SetTimeout are mutually exclusive functions func (df *DataFlow) WithContext(c context.Context) *DataFlow { - df.Req.Index++ - df.Req.ctxIndex = df.Req.Index df.Req.c = c return df } diff --git a/dataflow/dataflow_auto_decode_body_test.go b/dataflow/dataflow_auto_decode_body_test.go index 1be7001..c75265c 100644 --- a/dataflow/dataflow_auto_decode_body_test.go +++ b/dataflow/dataflow_auto_decode_body_test.go @@ -46,7 +46,10 @@ func create_AutoDecodeBody() *httptest.Server { c.Header("Content-Encoding", "br") var buf bytes.Buffer w := brotli.NewWriter(&buf) - w.Write([]byte(test_autoDecodeBody_data)) + _, err := w.Write([]byte(test_autoDecodeBody_data)) + if err != nil { + log.Fatal(err) + } w.Flush() w.Close() c.String(200, buf.String()) @@ -56,7 +59,10 @@ func create_AutoDecodeBody() *httptest.Server { var buf bytes.Buffer w := zlib.NewWriter(&buf) - w.Write([]byte(test_autoDecodeBody_data)) + _, err := w.Write([]byte(test_autoDecodeBody_data)) + if err != nil { + log.Fatal(err) + } w.Close() c.Header("Content-Encoding", "deflate") c.String(200, buf.String()) diff --git a/dataflow/dataflow_json_test.go b/dataflow/dataflow_json_test.go index 228171a..803fb0d 100644 --- a/dataflow/dataflow_json_test.go +++ b/dataflow/dataflow_json_test.go @@ -14,10 +14,12 @@ func Test_SetJSONNotEscape(t *testing.T) { fmt.Println("url", ts.URL) var buf bytes.Buffer //POST(ts.URL).Debug(true).SetJSONNotEscape(map[string]any{"url": "http://www.com?a=b&c=d"}).Do() - POST(ts.URL).Debug(debug.ToWriter(&buf, false)).SetJSONNotEscape(map[string]interface{}{"url": "http://www.com?a=b&c=d"}).Do() + err := POST(ts.URL).Debug(debug.ToWriter(&buf, false)).SetJSONNotEscape(map[string]interface{}{"url": "http://www.com?a=b&c=d"}).Do() + assert.NoError(t, err) assert.True(t, bytes.Contains(buf.Bytes(), []byte("&")), buf.String()) buf.Reset() //POST(ts.URL).Debug(true).SetJSON(map[string]any{"url": "http://www.com?a=b&c=d"}).Do() - POST(ts.URL).Debug(debug.ToWriter(&buf, false)).SetJSON(map[string]interface{}{"url": "http://www.com?a=b&c=d"}).Do() + err = POST(ts.URL).Debug(debug.ToWriter(&buf, false)).SetJSON(map[string]interface{}{"url": "http://www.com?a=b&c=d"}).Do() + assert.NoError(t, err) assert.False(t, bytes.Contains(buf.Bytes(), []byte("&")), buf.String()) } diff --git a/dataflow/dataflow_middleware_test.go b/dataflow/dataflow_middleware_test.go index d3398e5..9b9d48b 100644 --- a/dataflow/dataflow_middleware_test.go +++ b/dataflow/dataflow_middleware_test.go @@ -2,14 +2,14 @@ package dataflow import ( "bytes" - "errors" "fmt" - "github.com/guonaihong/gout/json" "io/ioutil" "net/http" "strings" "testing" + "github.com/guonaihong/gout/json" + core "github.com/guonaihong/gout/core" "github.com/guonaihong/gout/middler" @@ -58,7 +58,7 @@ func (d *demoResponseMiddler) ModifyResponse(response *http.Response) error { // Go中json中的数字经过反序列化会成为float64类型 if float64(200) != code { - return errors.New(fmt.Sprintf("请求失败, code %d msg %s", code, msg)) + return fmt.Errorf("请求失败, code %d msg %s", code, msg) } else { byt, _ := json.Marshal(&data) response.Body = ioutil.NopCloser(bytes.NewReader(byt)) diff --git a/dataflow/dataflow_test.go b/dataflow/dataflow_test.go index 44364a0..4c6c9ce 100644 --- a/dataflow/dataflow_test.go +++ b/dataflow/dataflow_test.go @@ -453,7 +453,6 @@ func testWithContextCancel(t *testing.T, ts *httptest.Server) { assert.Error(t, err) } -// func TestWithContext(t *testing.T) { router := setupContext(t) ts := httptest.NewServer(http.HandlerFunc(router.ServeHTTP)) @@ -566,8 +565,7 @@ func Test_DataFlow_Timeout(t *testing.T) { Do() assert.Error(t, err) - // 使用互斥api的原则,后面的覆盖前面的 - // 这里是SetTimeout生效, 超时时间200ms + // ctx, cancel := context.WithTimeout(context.Background(), longTimeout*time.Millisecond) defer cancel() @@ -579,10 +577,9 @@ func Test_DataFlow_Timeout(t *testing.T) { assert.Error(t, err) - assert.LessOrEqual(t, int(time.Since(s)), int(middleTimeout*time.Millisecond)) + assert.LessOrEqual(t, time.Since(s), shortTimeout*time.Millisecond+time.Millisecond*50) - // 使用互斥api的原则,后面的覆盖前面的 - // 这里是WithContext生效, 超时时间400ms + // ctx, cancel = context.WithTimeout(context.Background(), longTimeout*time.Millisecond) defer cancel() s = time.Now() @@ -592,7 +589,7 @@ func Test_DataFlow_Timeout(t *testing.T) { Do() assert.Error(t, err) - assert.GreaterOrEqual(t, int(time.Since(s)), int(middleTimeout*time.Millisecond)) + assert.GreaterOrEqual(t, int(time.Since(s)), int(shortTimeout*time.Millisecond)) } func Test_DataFlow_SetURL(t *testing.T) { diff --git a/dataflow/req.go b/dataflow/req.go index 878267e..7dc3cbe 100644 --- a/dataflow/req.go +++ b/dataflow/req.go @@ -372,8 +372,11 @@ func clearHeader(header http.Header) { // retry模块需要context.Context,所以这里也返回context.Context func (r *Req) GetContext() context.Context { - if r.Timeout > 0 && r.TimeoutIndex > r.ctxIndex { - r.c, r.cancel = context.WithTimeout(context.Background(), r.Timeout) + if r.Timeout > 0 { + if r.c == nil { + r.c = context.TODO() + } + r.c, r.cancel = context.WithTimeout(r.c, r.Timeout) } return r.c } @@ -588,11 +591,14 @@ func modifyURL(url string) string { return fmt.Sprintf("http://%s", url) } -func reqDef(method string, url string, g *Gout, urlStruct ...interface{}) Req { +func reqDef(method string, url string, g *Gout, urlStruct ...interface{}) (Req, error) { if len(urlStruct) > 0 { var out strings.Builder tpl := template.Must(template.New(url).Parse(url)) - tpl.Execute(&out, urlStruct[0]) + err := tpl.Execute(&out, urlStruct[0]) + if err != nil { + return Req{}, err + } url = out.String() } @@ -600,7 +606,7 @@ func reqDef(method string, url string, g *Gout, urlStruct ...interface{}) Req { r.Setting = GlobalSetting - return r + return r, nil } // ReadAll returns the whole response body as bytes. diff --git a/dataflow/req_bench_test.go b/dataflow/req_bench_test.go index ee170bb..85891fd 100644 --- a/dataflow/req_bench_test.go +++ b/dataflow/req_bench_test.go @@ -9,7 +9,10 @@ func Benchmark_URL_Template(b *testing.B) { for n := 0; n < b.N; n++ { code := 0 - New().GET("{{.Host}}/{{.Method}}", tc).Code(&code).Do() + err := New().GET("{{.Host}}/{{.Method}}", tc).Code(&code).Do() + if err != nil { + panic(err) + } if code != 200 { panic("code != 200") } diff --git a/dataflow/req_url_template_test.go b/dataflow/req_url_template_test.go index d63a632..ed7fc02 100644 --- a/dataflow/req_url_template_test.go +++ b/dataflow/req_url_template_test.go @@ -64,25 +64,33 @@ func Test_URL_Template(t *testing.T) { code := 0 switch tc.Method { case "get": - New().GET("{{.Host}}/{{.Method}}", tc).Debug(true).BindBody(&body).Code(&code).Do() + err := New().GET("{{.Host}}/{{.Method}}", tc).Debug(true).BindBody(&body).Code(&code).Do() + assert.NoError(t, err) case "post": - New().POST("{{.Host}}/{{.Method}}", tc).BindBody(&body).Code(&code).Do() + err := New().POST("{{.Host}}/{{.Method}}", tc).BindBody(&body).Code(&code).Do() + assert.NoError(t, err) case "put": - New().PUT("{{.Host}}/{{.Method}}", tc).BindBody(&body).Code(&code).Do() + err := New().PUT("{{.Host}}/{{.Method}}", tc).BindBody(&body).Code(&code).Do() + assert.NoError(t, err) case "patch": - New().PATCH("{{.Host}}/{{.Method}}", tc).BindBody(&body).Code(&code).Do() + err := New().PATCH("{{.Host}}/{{.Method}}", tc).BindBody(&body).Code(&code).Do() + assert.NoError(t, err) case "options": - New().OPTIONS("{{.Host}}/{{.Method}}", tc).BindBody(&body).Code(&code).Do() + err := New().OPTIONS("{{.Host}}/{{.Method}}", tc).BindBody(&body).Code(&code).Do() + assert.NoError(t, err) case "head": code := 0 - New().HEAD("{{.Host}}/{{.Method}}", tc).Debug(true).BindBody(&body).Code(&code).Do() - New().SetMethod(strings.ToUpper(tc.Method)).SetURL("{{.Host}}/{{.Method}}", tc).Debug(true).BindBody(&body2).Code(&code).Do() + err := New().HEAD("{{.Host}}/{{.Method}}", tc).Debug(true).BindBody(&body).Code(&code).Do() + assert.NoError(t, err) + err = New().SetMethod(strings.ToUpper(tc.Method)).SetURL("{{.Host}}/{{.Method}}", tc).Debug(true).BindBody(&body2).Code(&code).Do() + assert.NoError(t, err) assert.Equal(t, code, 200) continue } assert.Equal(t, code, 200) - New().SetMethod(strings.ToUpper(tc.Method)).SetURL("{{.Host}}/{{.Method}}", tc).Debug(true).BindBody(&body2).Do() + err := New().SetMethod(strings.ToUpper(tc.Method)).SetURL("{{.Host}}/{{.Method}}", tc).Debug(true).BindBody(&body2).Do() + assert.NoError(t, err) assert.Equal(t, body, tc.Method) b := assert.Equal(t, body2, tc.Method) if !b { diff --git a/dataflow/validator.go b/dataflow/validator.go index 3253fe5..d2b2194 100644 --- a/dataflow/validator.go +++ b/dataflow/validator.go @@ -46,19 +46,25 @@ func (v *defaultValidator) lazyinit() { v.validate = validator.New() v.validate.SetTagName("valid") v.trans, _ = uni.GetTranslator("en") - en_translations.RegisterDefaultTranslations(v.validate, v.trans) + err := en_translations.RegisterDefaultTranslations(v.validate, v.trans) + if err != nil { + panic(err) + } v.validate.RegisterTagNameFunc(func(fld reflect.StructField) string { return "error: " + fld.Name }) - v.validate.RegisterTranslation("required", v.trans, func(ut ut.Translator) error { + err = v.validate.RegisterTranslation("required", v.trans, func(ut ut.Translator) error { return ut.Add("required", "{0} must have a value!", true) // see universal-translator for details }, func(ut ut.Translator, fe validator.FieldError) string { t, _ := ut.T("required", fe.Field()) return t }) + if err != nil { + panic(err) + } // add any custom validations etc. here }) diff --git a/debug/debug_test.go b/debug/debug_test.go index 2d7a261..e80b394 100644 --- a/debug/debug_test.go +++ b/debug/debug_test.go @@ -7,53 +7,13 @@ import ( "io" "io/ioutil" "net/http" - "net/http/httptest" "testing" - "github.com/gin-gonic/gin" "github.com/guonaihong/gout/color" "github.com/guonaihong/gout/core" "github.com/stretchr/testify/assert" ) -func createGeneralEcho() *httptest.Server { - router := func() *gin.Engine { - router := gin.New() - - router.POST("/", func(c *gin.Context) { - _, err := io.Copy(c.Writer, c.Request.Body) - if err != nil { - fmt.Printf("createGeneralEcho fail:%v\n", err) - } - }) - - return router - }() - - return httptest.NewServer(http.HandlerFunc(router.ServeHTTP)) -} - -func createGeneral(data string) *httptest.Server { - router := func() *gin.Engine { - router := gin.New() - - router.POST("/", func(c *gin.Context) { - if len(data) > 0 { - c.String(200, data) - } - }) - - return router - }() - - return httptest.NewServer(http.HandlerFunc(router.ServeHTTP)) -} - -type data struct { - ID int `json:"id" xml:"id"` - Data string `json:"data" xml:"data"` -} - // 测试resetBodyAndPrint出错 func TestResetBodyAndPrintFail(t *testing.T) { diff --git a/debug/debug_trace_test.go b/debug/debug_trace_test.go deleted file mode 100644 index dd072c1..0000000 --- a/debug/debug_trace_test.go +++ /dev/null @@ -1,35 +0,0 @@ -package debug - -import ( - "bytes" - "reflect" - "strings" -) - -func checkValue(b *bytes.Buffer) bool { - info := &TraceInfo{} - v := reflect.ValueOf(info) - v = v.Elem() - - debugInfo := b.String() - have := false - typ := v.Type() - for i := 0; i < v.NumField(); i++ { - sf := typ.Field(i) - if sf.PkgPath != "" { - continue - } - - name := sf.Name - if !strings.Contains(debugInfo, name) { - return false - } - have = true - } - - if !have { - return have - } - - return true -} diff --git a/gout_global_setdebug_test.go b/gout_global_setdebug_test.go index 08de06e..74f14e1 100644 --- a/gout_global_setdebug_test.go +++ b/gout_global_setdebug_test.go @@ -26,7 +26,8 @@ func Test_Global_SetDebug(t *testing.T) { // copy the output in a separate goroutine so printing can't block indefinitely go func() { var buf bytes.Buffer - io.Copy(&buf, r) + _, err := io.Copy(&buf, r) + assert.NoError(t, err) outC <- buf.String() }() diff --git a/gout_newopt_timeout_and_global_test.go b/gout_newopt_timeout_and_global_test.go index e945845..fe800b4 100644 --- a/gout_newopt_timeout_and_global_test.go +++ b/gout_newopt_timeout_and_global_test.go @@ -50,8 +50,6 @@ func Test_Global_SetTimeout(t *testing.T) { // 期望的结果是返回错误 assert.Error(t, err) - // 使用互斥api的原则,后面的覆盖前面的 - // 这里是WithContext生效, 预期超时时间400ms ctx, cancel := context.WithTimeout(context.Background(), longTimeout*time.Millisecond) defer cancel() @@ -62,7 +60,7 @@ func Test_Global_SetTimeout(t *testing.T) { Do() assert.Error(t, err) - assert.GreaterOrEqual(t, int(time.Since(s)), int(middleTimeout*time.Millisecond)) + assert.LessOrEqual(t, time.Since(s), shortTimeout*time.Millisecond+time.Millisecond*50) SetTimeout(time.Duration(0)) } @@ -96,6 +94,7 @@ func Test_NewWithOpt_Timeout(t *testing.T) { Do() assert.Error(t, err) - assert.GreaterOrEqual(t, int(time.Since(s)), int(middleTimeout*time.Millisecond)) + + assert.LessOrEqual(t, time.Since(s), shortTimeout*time.Millisecond+time.Millisecond*50) } diff --git a/hcutil/hcutil.go b/hcutil/hcutil.go index 849e526..083ab3b 100644 --- a/hcutil/hcutil.go +++ b/hcutil/hcutil.go @@ -1,6 +1,7 @@ package hcutil import ( + "context" "fmt" "net" "net/http" @@ -96,8 +97,9 @@ func UnixSocket(c *http.Client, path string) error { return fmt.Errorf("UnixSocket:not found http.transport:%T", c.Transport) } - transport.Dial = func(proto, addr string) (conn net.Conn, err error) { - return net.Dial("unix", path) + transport.DialContext = func(ctx context.Context, proto, addr string) (conn net.Conn, err error) { + var d net.Dialer + return d.DialContext(ctx, "unix", path) } return nil diff --git a/hcutil/hcutil_test.go b/hcutil/hcutil_test.go index d08fd18..9c381ec 100644 --- a/hcutil/hcutil_test.go +++ b/hcutil/hcutil_test.go @@ -118,10 +118,12 @@ func TestUnixSocket(t *testing.T) { }() c := http.Client{} - UnixSocket(&c, path) + err := UnixSocket(&c, path) + assert.NoError(t, err) s := "" req, err := http.NewRequest("POST", "http://xxx/test/unix/", nil) + assert.NoError(t, err) req.Header.Add("h1", "v1") req.Header.Add("h2", "v2") @@ -139,7 +141,7 @@ func TestUnixSocket(t *testing.T) { // 错误情况1 c.Transport = &TransportFail{} - resp, err = c.Do(req) + _, err = c.Do(req) assert.Error(t, err) } diff --git a/middleware/rsp/autodecodebody/autodecodebody_test.go b/middleware/rsp/autodecodebody/autodecodebody_test.go index 4bfc99a..1ecac72 100644 --- a/middleware/rsp/autodecodebody/autodecodebody_test.go +++ b/middleware/rsp/autodecodebody/autodecodebody_test.go @@ -47,7 +47,8 @@ func TestAutoDecodeBody(t *testing.T) { rv.Header.Set("Content-Encoding", "deflate") var b bytes.Buffer w := zlib.NewWriter(&b) - w.Write([]byte(data)) + _, err := w.Write([]byte(data)) + assert.NoError(t, err) w.Close() rv.Body = ioutil.NopCloser(&b) @@ -59,7 +60,8 @@ func TestAutoDecodeBody(t *testing.T) { rv.Header.Set("Content-Encoding", "br") var b bytes.Buffer w := brotli.NewWriter(&b) - w.Write([]byte(data)) + _, err := w.Write([]byte(data)) + assert.NoError(t, err) w.Flush() w.Close() rv.Body = ioutil.NopCloser(&b) diff --git a/req_url_template_test.go b/req_url_template_test.go index 424a5b1..d4552c2 100644 --- a/req_url_template_test.go +++ b/req_url_template_test.go @@ -64,25 +64,33 @@ func Test_URL_Template(t *testing.T) { code := 0 switch tc.Method { case "get": - GET("{{.Host}}/{{.Method}}", tc).Debug(true).BindBody(&body).Code(&code).Do() + err := GET("{{.Host}}/{{.Method}}", tc).Debug(true).BindBody(&body).Code(&code).Do() + assert.NoError(t, err) case "post": - POST("{{.Host}}/{{.Method}}", tc).BindBody(&body).Code(&code).Do() + err := POST("{{.Host}}/{{.Method}}", tc).BindBody(&body).Code(&code).Do() + assert.NoError(t, err) case "put": - PUT("{{.Host}}/{{.Method}}", tc).BindBody(&body).Code(&code).Do() + err := PUT("{{.Host}}/{{.Method}}", tc).BindBody(&body).Code(&code).Do() + assert.NoError(t, err) case "patch": - PATCH("{{.Host}}/{{.Method}}", tc).BindBody(&body).Code(&code).Do() + err := PATCH("{{.Host}}/{{.Method}}", tc).BindBody(&body).Code(&code).Do() + assert.NoError(t, err) case "options": - OPTIONS("{{.Host}}/{{.Method}}", tc).BindBody(&body).Code(&code).Do() + err := OPTIONS("{{.Host}}/{{.Method}}", tc).BindBody(&body).Code(&code).Do() + assert.NoError(t, err) case "head": code := 0 - HEAD("{{.Host}}/{{.Method}}", tc).Debug(true).BindBody(&body).Code(&code).Do() - New().SetMethod(strings.ToUpper(tc.Method)).SetURL("{{.Host}}/{{.Method}}", tc).Debug(true).BindBody(&body2).Code(&code).Do() + err := HEAD("{{.Host}}/{{.Method}}", tc).Debug(true).BindBody(&body).Code(&code).Do() + assert.NoError(t, err) + err = New().SetMethod(strings.ToUpper(tc.Method)).SetURL("{{.Host}}/{{.Method}}", tc).Debug(true).BindBody(&body2).Code(&code).Do() + assert.NoError(t, err) assert.Equal(t, code, 200) continue } assert.Equal(t, code, 200) - New().SetMethod(strings.ToUpper(tc.Method)).SetURL("{{.Host}}/{{.Method}}", tc).Debug(true).BindBody(&body2).Do() + err := New().SetMethod(strings.ToUpper(tc.Method)).SetURL("{{.Host}}/{{.Method}}", tc).Debug(true).BindBody(&body2).Do() + assert.NoError(t, err) assert.Equal(t, body, tc.Method) b := assert.Equal(t, body2, tc.Method) if !b { diff --git a/setting/setting.go b/setting/setting.go index f5e71fe..230c492 100644 --- a/setting/setting.go +++ b/setting/setting.go @@ -18,14 +18,6 @@ type Setting struct { //超时时间 Timeout time.Duration - // 目前用作SetTimeout 和 WithContext是互斥 - // index是自增id,主要给互斥API定优先级 - // 对于互斥api,后面的会覆盖前面的 - Index int - - //当前time 的index - TimeoutIndex int - UseChunked bool } @@ -35,8 +27,6 @@ func (s *Setting) Chunked() { } func (s *Setting) SetTimeout(d time.Duration) { - s.Index++ - s.TimeoutIndex = s.Index s.Timeout = d } @@ -47,7 +37,6 @@ func (s *Setting) SetDebug(b bool) { func (s *Setting) Reset() { s.NotIgnoreEmpty = false s.NoAutoContentType = false - s.Index = 0 //s.TimeoutIndex = 0 s.Timeout = time.Duration(0) }