From a0ab6cd6852cec430e280217a9516d6be3c1ef5f Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Tue, 12 Apr 2016 00:22:39 +0000 Subject: [PATCH] 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 Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- src/net/http/clientserver_test.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/net/http/clientserver_test.go b/src/net/http/clientserver_test.go index c2bab378e3..f721382365 100644 --- a/src/net/http/clientserver_test.go +++ b/src/net/http/clientserver_test.go @@ -1123,6 +1123,34 @@ func testBogusStatusWorks(t *testing.T, h2 bool) { } } +func TestInterruptWithPanic_h1(t *testing.T) { testInterruptWithPanic(t, h1Mode) } +func TestInterruptWithPanic_h2(t *testing.T) { testInterruptWithPanic(t, h2Mode) } +func testInterruptWithPanic(t *testing.T, h2 bool) { + log.SetOutput(ioutil.Discard) // is noisy otherwise + defer log.SetOutput(os.Stderr) + + const msg = "hello" + defer afterTest(t) + cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) { + io.WriteString(w, msg) + w.(Flusher).Flush() + panic("no more") + })) + defer cst.close() + res, err := cst.c.Get(cst.ts.URL) + if err != nil { + t.Fatal(err) + } + defer res.Body.Close() + slurp, err := ioutil.ReadAll(res.Body) + if string(slurp) != msg { + t.Errorf("client read %q; want %q", slurp, msg) + } + if err == nil { + t.Errorf("client read all successfully; want some error") + } +} + type noteCloseConn struct { net.Conn closeFunc func() -- 2.48.1