Skip to content

Commit

Permalink
Add a api DisableNoDefaultContentType to disable add default conten…
Browse files Browse the repository at this point in the history
…t type. (#859)

* Add api DisableNoDefaultContentType to disable add default contentype if no Content-Type header.

* Update test case.

* Update api name.

* Update header.go

Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com>

Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com>
  • Loading branch information
wangfakang and erikdubbelboer authored Aug 7, 2020
1 parent 2509c12 commit cc8ba4b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
7 changes: 6 additions & 1 deletion header.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,10 +601,15 @@ func (h *ResponseHeader) DisableNormalizing() {
h.disableNormalizing = true
}

// SetNoDefaultContentType allows you to control if a default Content-Type header will be set (false) or not (true).
func (h *ResponseHeader) SetNoDefaultContentType(noDefaultContentType bool) {
h.noDefaultContentType = noDefaultContentType
}

// Reset clears response header.
func (h *ResponseHeader) Reset() {
h.disableNormalizing = false
h.noDefaultContentType = false
h.SetNoDefaultContentType(false)
h.noDefaultDate = false
h.resetSkipNormalize()
}
Expand Down
6 changes: 3 additions & 3 deletions header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1240,7 +1240,7 @@ func TestResponseContentTypeNoDefaultNotEmpty(t *testing.T) {

var h ResponseHeader

h.noDefaultContentType = true
h.SetNoDefaultContentType(true)
h.SetContentLength(5)

headers := h.String()
Expand Down Expand Up @@ -2193,10 +2193,10 @@ func TestResponseHeaderReadSuccess(t *testing.T) {
400, 123, string(defaultContentType), "foiaaa")

// no content-type and no default
h.noDefaultContentType = true
h.SetNoDefaultContentType(true)
testResponseHeaderReadSuccess(t, h, "HTTP/1.1 400 OK\r\nContent-Length: 123\r\n\r\nfoiaaa",
400, 123, "", "foiaaa")
h.noDefaultContentType = false
h.SetNoDefaultContentType(false)

// no headers
testResponseHeaderReadSuccess(t, h, "HTTP/1.1 200 OK\r\n\r\naaaabbb",
Expand Down

0 comments on commit cc8ba4b

Please sign in to comment.