diff --git a/go.mod b/go.mod index b978ac8..0fadade 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,6 @@ module github.com/guestin/go-duface go 1.16 require ( - github.com/guestin/go-requests v0.0.8 + github.com/guestin/go-requests v0.0.9 github.com/guestin/mob v1.0.7 ) diff --git a/go.sum b/go.sum index 0707297..58524f0 100644 --- a/go.sum +++ b/go.sum @@ -10,8 +10,8 @@ github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+ github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE= github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/guestin/go-requests v0.0.8 h1:TKdROouom+nVPdBXIsn4IUXWRWpUMkF570/fL9itWg0= -github.com/guestin/go-requests v0.0.8/go.mod h1:gB+JWM1CotK0tHuHu0DpMTmp7TmeXrxgHTx4IAEl+9c= +github.com/guestin/go-requests v0.0.9 h1:udMlhGrinOVA0synB2PGI5p1zGJs02LNBttqkgeEGTk= +github.com/guestin/go-requests v0.0.9/go.mod h1:gB+JWM1CotK0tHuHu0DpMTmp7TmeXrxgHTx4IAEl+9c= github.com/guestin/mob v1.0.7 h1:EOtNE9L+OYcxkVDGvYGzTHid8W0lXeMFInlZm//JBqM= github.com/guestin/mob v1.0.7/go.mod h1:rIQZ+ImMfZM8E3wyP4mpS8Ux9T92198XtQMFT/l6enI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= diff --git a/internal/opt_dump_response.go b/internal/opt_dump_response.go index cb29144..b44f256 100644 --- a/internal/opt_dump_response.go +++ b/internal/opt_dump_response.go @@ -1,29 +1,27 @@ package internal import ( - "bytes" "fmt" "github.com/guestin/go-requests/opt" "io" ) -func DumpResponse(tag string) opt.Option { - return func(context *opt.RequestContext) error { - old := context.ResponseHandler - context.ResponseHandler = func(statusCode int, stream io.Reader) (interface{}, error) { - fmt.Printf("[%s] status code=%d\n", tag, statusCode) - respBytes, err := io.ReadAll(stream) - if err != nil { - fmt.Printf("[%s] read response error:%s\n", tag, err) - return nil, err - } - fmt.Printf("[%s] response:%s\n", tag, respBytes) - if old != nil { - return old(statusCode, bytes.NewReader(respBytes)) - } - return nil, nil - } +func DumpResponse(tag string, skip bool) opt.Option { + return func(reqCtx *opt.RequestContext) error { + reqCtx.InstallResponseHandler( + func(statusCode int, stream io.Reader, previousValue interface{}) (interface{}, error) { + if skip { + return previousValue, nil + } + fmt.Printf("[%s] status code=%d\n", tag, statusCode) + respBytes, err := io.ReadAll(stream) + if err != nil { + fmt.Printf("[%s] read response error:%s\n", tag, err) + return nil, err + } + fmt.Printf("[%s] response:%s\n", tag, respBytes) + return nil, nil + }, opt.HEAD) return nil } - }