Commit 9ccdc4ed authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

net/http/httptest: don't crash in mime sniffing if HeaderMap is nil

Fixes some failing Google tests when run under Go tip (1.6).

Updates #12986

Change-Id: I0ca4d20f6103d10ea9464e45730085401336dada
Reviewed-on: https://go-review.googlesource.com/17698Reviewed-by: 's avatarRuss Cox <rsc@golang.org>
Reviewed-by: 's avatarNodir Turakulov <nodir@google.com>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
parent 8233ecd1
......@@ -65,6 +65,9 @@ func (rw *ResponseRecorder) writeHeader(b []byte, str string) {
if b == nil {
b = []byte(str)
}
if rw.HeaderMap == nil {
rw.HeaderMap = make(http.Header)
}
rw.HeaderMap.Set("Content-Type", http.DetectContentType(b))
}
......
......@@ -119,6 +119,17 @@ func TestRecorder(t *testing.T) {
},
check(hasHeader("Content-Type", "some/type")),
},
{
"Content-Type detection doesn't crash if HeaderMap is nil",
func(w http.ResponseWriter, r *http.Request) {
// Act as if the user wrote new(httptest.ResponseRecorder)
// rather than using NewRecorder (which initializes
// HeaderMap)
w.(*ResponseRecorder).HeaderMap = nil
io.WriteString(w, "<html>")
},
check(hasHeader("Content-Type", "text/html; charset=utf-8")),
},
}
r, _ := http.NewRequest("GET", "http://foo.com/", nil)
for _, tt := range tests {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment