Skip to content

Commit a0ab6cd

Browse files
committed
net/http: add test that panic in a handler signals an error to the client
Change-Id: Iba40edc9ddad62534b06c5af20bbc3dd3dc14d0a Reviewed-on: https://go-review.googlesource.com/21881 Reviewed-by: Andrew Gerrand <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 982274c commit a0ab6cd

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/net/http/clientserver_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,34 @@ func testBogusStatusWorks(t *testing.T, h2 bool) {
11231123
}
11241124
}
11251125

1126+
func TestInterruptWithPanic_h1(t *testing.T) { testInterruptWithPanic(t, h1Mode) }
1127+
func TestInterruptWithPanic_h2(t *testing.T) { testInterruptWithPanic(t, h2Mode) }
1128+
func testInterruptWithPanic(t *testing.T, h2 bool) {
1129+
log.SetOutput(ioutil.Discard) // is noisy otherwise
1130+
defer log.SetOutput(os.Stderr)
1131+
1132+
const msg = "hello"
1133+
defer afterTest(t)
1134+
cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
1135+
io.WriteString(w, msg)
1136+
w.(Flusher).Flush()
1137+
panic("no more")
1138+
}))
1139+
defer cst.close()
1140+
res, err := cst.c.Get(cst.ts.URL)
1141+
if err != nil {
1142+
t.Fatal(err)
1143+
}
1144+
defer res.Body.Close()
1145+
slurp, err := ioutil.ReadAll(res.Body)
1146+
if string(slurp) != msg {
1147+
t.Errorf("client read %q; want %q", slurp, msg)
1148+
}
1149+
if err == nil {
1150+
t.Errorf("client read all successfully; want some error")
1151+
}
1152+
}
1153+
11261154
type noteCloseConn struct {
11271155
net.Conn
11281156
closeFunc func()

0 commit comments

Comments
 (0)