diff --git a/src/net/http/httptest/recorder.go b/src/net/http/httptest/recorder.go index c813cf5021a..7c51af1867a 100644 --- a/src/net/http/httptest/recorder.go +++ b/src/net/http/httptest/recorder.go @@ -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)) } diff --git a/src/net/http/httptest/recorder_test.go b/src/net/http/httptest/recorder_test.go index a5a1725fa98..c29b6d4cf91 100644 --- a/src/net/http/httptest/recorder_test.go +++ b/src/net/http/httptest/recorder_test.go @@ -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, "") + }, + check(hasHeader("Content-Type", "text/html; charset=utf-8")), + }, } r, _ := http.NewRequest("GET", "http://foo.com/", nil) for _, tt := range tests {