Skip to content

Commit

Permalink
handle empty content type
Browse files Browse the repository at this point in the history
Signed-off-by: Lixia (Sylvia) Lei <[email protected]>
  • Loading branch information
Wwwsylvia committed Dec 6, 2024
1 parent b440250 commit cfc7169
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
7 changes: 3 additions & 4 deletions internal/trace/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions internal/trace/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit cfc7169

Please sign in to comment.