]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: add another hijack-after-background-read test
authorBrad Fitzpatrick <bradfitz@golang.org>
Sat, 14 Jan 2017 03:19:54 +0000 (03:19 +0000)
committerBrad Fitzpatrick <bradfitz@golang.org>
Sat, 14 Jan 2017 05:57:07 +0000 (05:57 +0000)
Follow-up test from Ian's comments in https://golang.org/cl/35232
after submit.

Change-Id: Ifa504bd8d09e555c3c7738376199dfc9b99130cf
Reviewed-on: https://go-review.googlesource.com/35234
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
src/net/http/serve_test.go

index 22188ab48365026beb88aa113312188ac12b70c1..4997c9ee019b54aa73419b59dd0732b6e27ada22 100644 (file)
@@ -5240,3 +5240,69 @@ func TestServerHijackGetsBackgroundByte(t *testing.T) {
                t.Error("timeout")
        }
 }
+
+// Like TestServerHijackGetsBackgroundByte above but sending a
+// immediate 1MB of data to the server to fill up the server's 4KB
+// buffer.
+func TestServerHijackGetsBackgroundByte_big(t *testing.T) {
+       setParallel(t)
+       defer afterTest(t)
+       done := make(chan struct{})
+       const size = 8 << 10
+       ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
+               defer close(done)
+
+               // Wait until the HTTP server sees the extra data
+               // after the GET request. The HTTP server fires the
+               // close notifier here, assuming it's a pipelined
+               // request, as documented.
+               select {
+               case <-w.(CloseNotifier).CloseNotify():
+               case <-time.After(5 * time.Second):
+                       t.Error("timeout")
+                       return
+               }
+
+               conn, buf, err := w.(Hijacker).Hijack()
+               if err != nil {
+                       t.Error(err)
+                       return
+               }
+               defer conn.Close()
+               slurp, err := ioutil.ReadAll(buf.Reader)
+               if err != nil {
+                       t.Error("Copy: %v", err)
+               }
+               allX := true
+               for _, v := range slurp {
+                       if v != 'x' {
+                               allX = false
+                       }
+               }
+               if len(slurp) != size {
+                       t.Errorf("read %d; want %d", len(slurp), size)
+               } else if !allX {
+                       t.Errorf("read %q; want %d 'x'", slurp, size)
+               }
+       }))
+       defer ts.Close()
+
+       cn, err := net.Dial("tcp", ts.Listener.Addr().String())
+       if err != nil {
+               t.Fatal(err)
+       }
+       defer cn.Close()
+       if _, err := fmt.Fprintf(cn, "GET / HTTP/1.1\r\nHost: e.com\r\n\r\n%s",
+               strings.Repeat("x", size)); err != nil {
+               t.Fatal(err)
+       }
+       if err := cn.(*net.TCPConn).CloseWrite(); err != nil {
+               t.Fatal(err)
+       }
+
+       select {
+       case <-done:
+       case <-time.After(2 * time.Second):
+               t.Error("timeout")
+       }
+}