Commit a8d9c310 authored by Burcu Dogan's avatar Burcu Dogan

net/http: test max request body size against both HTTP/1 and HTTP/2

Change-Id: I009eaa52d03f1c3af33a6e884332f41c7cf48edd
Reviewed-on: https://go-review.googlesource.com/17427Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 1ea31c7b
......@@ -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",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment