diff --git a/internal/trace/transport.go b/internal/trace/transport.go index 19e96a68c..fa02f30d0 100644 --- a/internal/trace/transport.go +++ b/internal/trace/transport.go @@ -103,6 +103,9 @@ func logResponseBody(resp *http.Response) string { // non-applicable body is not printed and remains untouched for subsequent processing contentType := resp.Header.Get("Content-Type") + if contentType == "" { + return " Response body without a content type is not printed" + } if !isPrintableContentType(contentType) { return fmt.Sprintf(" Response body of content type %q is not printed", contentType) } @@ -137,10 +140,6 @@ func logResponseBody(resp *http.Response) string { // isPrintableContentType returns true if the content of contentType is printable. func isPrintableContentType(contentType string) bool { - if len(contentType) == 0 { - return false - } - mediaType, _, err := mime.ParseMediaType(contentType) if err != nil { return false diff --git a/internal/trace/transport_test.go b/internal/trace/transport_test.go index dd79c949a..764f216bf 100644 --- a/internal/trace/transport_test.go +++ b/internal/trace/transport_test.go @@ -178,6 +178,25 @@ func Test_logResponseBody(t *testing.T) { }, want: "whatever", }, + { + name: "Missing content type header", + wantData: []byte("whatever"), + resp: &http.Response{ + Body: io.NopCloser(bytes.NewReader([]byte("whatever"))), + ContentLength: 8, + }, + want: " Response body without a content type is not printed", + }, + { + name: "Empty content type header", + wantData: []byte("whatever"), + resp: &http.Response{ + Body: io.NopCloser(bytes.NewReader([]byte("whatever"))), + ContentLength: 8, + Header: http.Header{"Content-Type": []string{""}}, + }, + want: " Response body without a content type is not printed", + }, { name: "Non-printable content type", wantData: []byte("binary data"),