From 85217e0d5e01adcd51e6fe5142fa1bda07eaf50b Mon Sep 17 00:00:00 2001 From: Ciprian Dorin Craciun Date: Tue, 13 Aug 2019 12:53:03 +0300 Subject: [PATCH] Correctly handle `NoDefaultContentType` without setting an `Content-Type` value (#628) If `NoDefaultContentType` is set, but no actual `Content-Type` header is set, do not send the wrong `Content-Type: ` header --- header.go | 5 ++++- header_test.go | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/header.go b/header.go index 3dbb980718..45617648af 100644 --- a/header.go +++ b/header.go @@ -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 { diff --git a/header_test.go b/header_test.go index 7ec21afc6a..b6ca26d716 100644 --- a/header_test.go +++ b/header_test.go @@ -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