1
0
mirror of https://github.com/golang/go synced 2024-11-23 17:50:06 -07:00

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/17698
Reviewed-by: Russ Cox <rsc@golang.org>
Reviewed-by: Nodir Turakulov <nodir@google.com>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Brad Fitzpatrick 2015-12-10 10:11:15 -08:00
parent 8233ecd1b2
commit 9ccdc4edac
2 changed files with 14 additions and 0 deletions

View File

@ -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))
}

View File

@ -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 {