mirror of
https://github.com/golang/go
synced 2024-11-22 05:14:40 -07:00
ensure that a superfluous WriteHeader call panics in httptest
This commit is contained in:
parent
8a4b439ee6
commit
562ba1c461
@ -142,6 +142,7 @@ func checkWriteHeaderCode(code int) {
|
|||||||
// WriteHeader implements [http.ResponseWriter].
|
// WriteHeader implements [http.ResponseWriter].
|
||||||
func (rw *ResponseRecorder) WriteHeader(code int) {
|
func (rw *ResponseRecorder) WriteHeader(code int) {
|
||||||
if rw.wroteHeader {
|
if rw.wroteHeader {
|
||||||
|
panic(fmt.Sprintf("superfluous response.WriteHeader call"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -138,7 +138,6 @@ func TestRecorder(t *testing.T) {
|
|||||||
"first code only",
|
"first code only",
|
||||||
func(w http.ResponseWriter, r *http.Request) {
|
func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(201)
|
w.WriteHeader(201)
|
||||||
w.WriteHeader(202)
|
|
||||||
w.Write([]byte("hi"))
|
w.Write([]byte("hi"))
|
||||||
},
|
},
|
||||||
check(hasStatus(201), hasContents("hi")),
|
check(hasStatus(201), hasContents("hi")),
|
||||||
@ -147,8 +146,6 @@ func TestRecorder(t *testing.T) {
|
|||||||
"write sends 200",
|
"write sends 200",
|
||||||
func(w http.ResponseWriter, r *http.Request) {
|
func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write([]byte("hi first"))
|
w.Write([]byte("hi first"))
|
||||||
w.WriteHeader(201)
|
|
||||||
w.WriteHeader(202)
|
|
||||||
},
|
},
|
||||||
check(hasStatus(200), hasContents("hi first"), hasFlush(false)),
|
check(hasStatus(200), hasContents("hi first"), hasFlush(false)),
|
||||||
},
|
},
|
||||||
@ -168,7 +165,6 @@ func TestRecorder(t *testing.T) {
|
|||||||
"flush",
|
"flush",
|
||||||
func(w http.ResponseWriter, r *http.Request) {
|
func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.(http.Flusher).Flush() // also sends a 200
|
w.(http.Flusher).Flush() // also sends a 200
|
||||||
w.WriteHeader(201)
|
|
||||||
},
|
},
|
||||||
check(hasStatus(200), hasFlush(true), hasContentLength(-1)),
|
check(hasStatus(200), hasFlush(true), hasContentLength(-1)),
|
||||||
},
|
},
|
||||||
@ -369,3 +365,20 @@ func TestRecorderPanicsOnNonXXXStatusCode(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ensure that httptest.Recorder panics when using WriteHeader twice.
|
||||||
|
func TestRecorderPanicsOnSuperfluousWriteHeader(t *testing.T) {
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r == nil {
|
||||||
|
t.Fatal("Expected a panic")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
handler := func(rw http.ResponseWriter, _ *http.Request) {
|
||||||
|
rw.WriteHeader(200)
|
||||||
|
rw.WriteHeader(201)
|
||||||
|
}
|
||||||
|
r, _ := http.NewRequest("GET", "http://example.org/", nil)
|
||||||
|
rw := NewRecorder()
|
||||||
|
handler(rw, r)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user