Skip to content

Commit

Permalink
Correctly handle NoDefaultContentType without setting an `Content-T…
Browse files Browse the repository at this point in the history
…ype` value (#628)

If `NoDefaultContentType` is set, but no actual `Content-Type` header is set, do not send the wrong `Content-Type: ` header
  • Loading branch information
cipriancraciun authored and erikdubbelboer committed Aug 13, 2019
1 parent b97bc32 commit 85217e0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 4 additions & 1 deletion header.go
Original file line number Diff line number Diff line change
Expand Up @@ -1499,7 +1499,10 @@ func (h *ResponseHeader) AppendBytes(dst []byte) []byte {
// or if it is explicitly set.
// See https://github.com/valyala/fasthttp/issues/28 .
if h.ContentLength() != 0 || len(h.contentType) > 0 {
dst = appendHeaderLine(dst, strContentType, h.ContentType())
contentType := h.ContentType()
if len(contentType) > 0 {
dst = appendHeaderLine(dst, strContentType, contentType)
}
}

if len(h.contentLengthBytes) > 0 {
Expand Down
13 changes: 13 additions & 0 deletions header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,19 @@ func TestRequestHeaderCopyTo(t *testing.T) {
}
}

func TestResponseContentTypeNoDefaultNotEmpty(t *testing.T) {
var h ResponseHeader

h.noDefaultContentType = true
h.SetContentLength(5)

headers := h.String()

if strings.Index(headers, "Content-Type: \r\n") != -1 {
t.Fatalf("ResponseContentTypeNoDefaultNotEmpty fail, response: \n%+v\noutcome: \n%q\n", h, headers)
}
}

func TestRequestHeaderConnectionClose(t *testing.T) {
var h RequestHeader

Expand Down

0 comments on commit 85217e0

Please sign in to comment.