readMu sync.Mutex // for TestHandlerBodyClose
readBuf bytes.Buffer
writeBuf bytes.Buffer
- closec chan bool // if non-nil, send value to it on close
+ closec chan bool // 1-buffered; receives true when Close is called
noopConn
}
+func newTestConn() *testConn {
+ return &testConn{closec: make(chan bool, 1)}
+}
+
func (c *testConn) Read(b []byte) (int, error) {
c.readMu.Lock()
defer c.readMu.Unlock()
}
// If a Handler finishes and there's an unread request body,
-// verify the server try to do implicit read on it before replying.
+// verify the server implicitly tries to do a read on it before replying.
func TestHandlerFinishSkipBigContentLengthRead(t *testing.T) {
setParallel(t)
- conn := &testConn{closec: make(chan bool)}
+ conn := newTestConn()
conn.readBuf.Write([]byte(fmt.Sprintf(
"POST / HTTP/1.1\r\n" +
"Host: test\r\n" +
{"GET / HTTP/3.0", "", 505},
}
for _, tt := range tests {
- conn := &testConn{closec: make(chan bool, 1)}
+ conn := newTestConn()
methodTarget := "GET / "
if !strings.HasPrefix(tt.proto, "HTTP/") {
methodTarget = ""
{"foo: foo\xfffoo\r\n", 200}, // non-ASCII high octets in value are fine
}
for _, tt := range tests {
- conn := &testConn{closec: make(chan bool, 1)}
+ conn := newTestConn()
io.WriteString(&conn.readBuf, "GET / HTTP/1.1\r\nHost: foo\r\n"+tt.header+"\r\n")
ln := &oneConnListener{conn}
`)
res := []byte("Hello world!\n")
- conn := &testConn{
- // testConn.Close will not push into the channel
- // if it's full.
- closec: make(chan bool, 1),
- }
+ conn := newTestConn()
handler := HandlerFunc(func(rw ResponseWriter, r *Request) {
rw.Header().Set("Content-Type", "text/html; charset=utf-8")
rw.Write(res)
{"GE(T", 400},
}
for _, tt := range tests {
- conn := &testConn{closec: make(chan bool, 1)}
+ conn := newTestConn()
io.WriteString(&conn.readBuf, tt.method+" / HTTP/1.1\r\nHost: foo.example\r\n\r\n")
ln := &oneConnListener{conn}