From a8d9c3103d6ed1b867f9e49bd2a157a195f86d9f Mon Sep 17 00:00:00 2001 From: Burcu Dogan Date: Fri, 4 Dec 2015 11:12:39 -0800 Subject: [PATCH] net/http: test max request body size against both HTTP/1 and HTTP/2 Change-Id: I009eaa52d03f1c3af33a6e884332f41c7cf48edd Reviewed-on: https://go-review.googlesource.com/17427 Reviewed-by: Brad Fitzpatrick Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- src/net/http/serve_test.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/net/http/serve_test.go b/src/net/http/serve_test.go index 15e26a59df..91eab137b7 100644 --- a/src/net/http/serve_test.go +++ b/src/net/http/serve_test.go @@ -2027,10 +2027,13 @@ func (cr countReader) Read(p []byte) (n int, err error) { return } -func TestRequestBodyLimit(t *testing.T) { +func TestRequestBodyLimit_h1(t *testing.T) { testRequestBodyLimit(t, false) } +func TestRequestBodyLimit_h2(t *testing.T) { testRequestBodyLimit(t, true) } + +func testRequestBodyLimit(t *testing.T, h2 bool) { defer afterTest(t) const limit = 1 << 20 - ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) { + cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) { r.Body = MaxBytesReader(w, r.Body, limit) n, err := io.Copy(ioutil.Discard, r.Body) if err == nil { @@ -2040,10 +2043,10 @@ func TestRequestBodyLimit(t *testing.T) { t.Errorf("io.Copy = %d, want %d", n, limit) } })) - defer ts.Close() + defer cst.close() nWritten := new(int64) - req, _ := NewRequest("POST", ts.URL, io.LimitReader(countReader{neverEnding('a'), nWritten}, limit*200)) + req, _ := NewRequest("POST", cst.ts.URL, io.LimitReader(countReader{neverEnding('a'), nWritten}, limit*200)) // Send the POST, but don't care it succeeds or not. The // remote side is going to reply and then close the TCP @@ -2054,7 +2057,7 @@ func TestRequestBodyLimit(t *testing.T) { // // But that's okay, since what we're really testing is that // the remote side hung up on us before we wrote too much. - _, _ = DefaultClient.Do(req) + _, _ = cst.c.Do(req) if atomic.LoadInt64(nWritten) > limit*100 { t.Errorf("handler restricted the request body to %d bytes, but client managed to write %d", -- 2.50.0