Skip to content

Commit 93253a8

Browse files
authored
Merge pull request #157 from miclle/ut
Adjust client debug request/response print
2 parents 01628b8 + 94249c5 commit 93253a8

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,6 @@ coverage.txt
3333

3434
# Go workspace file
3535
go.work
36+
37+
# Log files
38+
*.log

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
test:
2-
go test -tags='unit integration' -failfast -count=1 -v -timeout 350m -coverprofile=coverage.txt `go list ./... | egrep -v 'examples|sms'`
2+
go test -tags='unit integration' -failfast -count=1 -v -timeout 350m -coverprofile=coverage.txt `go list ./... | egrep -v 'examples|sms'` | tee -a test.log
33

44
unittest:
55
go test -tags=unit -failfast -count=1 -v -coverprofile=coverage.txt `go list ./... | egrep -v 'examples|sms'`

client/client.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,16 @@ func newRequest(ctx context.Context, method, reqUrl string, headers http.Header,
9393
},
9494
}
9595
req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
96-
bs, bErr := httputil.DumpRequest(req, DeepDebugInfo)
96+
97+
var (
98+
hasBody = DeepDebugInfo
99+
contentType = req.Header.Get("Content-Type")
100+
)
101+
if contentType != conf.CONTENT_TYPE_JSON && contentType != conf.CONTENT_TYPE_FORM {
102+
hasBody = false
103+
}
104+
105+
bs, bErr := httputil.DumpRequest(req, hasBody)
97106
if bErr != nil {
98107
err = bErr
99108
return
@@ -293,7 +302,16 @@ func CallRet(ctx context.Context, ret interface{}, resp *http.Response) (err err
293302
}()
294303

295304
if DebugMode {
296-
bs, dErr := httputil.DumpResponse(resp, DeepDebugInfo)
305+
306+
var hasBody = DeepDebugInfo
307+
switch {
308+
case
309+
resp.Header.Get("Content-Type") == "application/octet-stream",
310+
resp.ContentLength <= 0 || resp.ContentLength > 1024*1024:
311+
hasBody = false
312+
}
313+
314+
bs, dErr := httputil.DumpResponse(resp, hasBody)
297315
if dErr != nil {
298316
err = dErr
299317
return

internal/clientv2/interceptor_debug.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http/httputil"
99

1010
clientV1 "github.com/qiniu/go-sdk/v7/client"
11+
"github.com/qiniu/go-sdk/v7/conf"
1112
"github.com/qiniu/go-sdk/v7/internal/log"
1213
)
1314

@@ -120,7 +121,16 @@ func (interceptor *debugInterceptor) printRequest(label string, req *http.Reques
120121
}
121122

122123
info := label + " request:\n"
123-
d, dErr := httputil.DumpRequest(req, IsPrintRequestBody())
124+
125+
var (
126+
hasBody = IsPrintRequestBody()
127+
contentType = req.Header.Get("Content-Type")
128+
)
129+
if contentType != conf.CONTENT_TYPE_JSON && contentType != conf.CONTENT_TYPE_FORM {
130+
hasBody = false
131+
}
132+
133+
d, dErr := httputil.DumpRequest(req, hasBody)
124134
if dErr != nil {
125135
return dErr
126136
}
@@ -199,7 +209,16 @@ func (interceptor *debugInterceptor) printResponse(label string, resp *http.Resp
199209
}
200210

201211
info := label + " response:\n"
202-
d, dErr := httputil.DumpResponse(resp, IsPrintResponseBody())
212+
213+
var hasBody = IsPrintResponseBody()
214+
switch {
215+
case
216+
resp.Header.Get("Content-Type") == "application/octet-stream",
217+
resp.ContentLength <= 0 || resp.ContentLength > 1024*1024:
218+
hasBody = false
219+
}
220+
221+
d, dErr := httputil.DumpResponse(resp, hasBody)
203222
if dErr != nil {
204223
return dErr
205224
}

0 commit comments

Comments
 (0)