unblockBackend := make(chan bool)
backend := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, req *Request) {
- io.CopyN(rw, req.Body, bodySize/2)
+ io.CopyN(rw, req.Body, bodySize)
<-unblockBackend
}))
defer backend.Close()
backendRespc := make(chan *Response, 1)
proxy := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, req *Request) {
- if req.RequestURI == "/foo" {
- rw.Write([]byte("bar"))
- return
- }
req2, _ := NewRequest("POST", backend.URL, req.Body)
req2.ContentLength = bodySize
t.Errorf("Proxy outbound request: %v", err)
return
}
- _, err = io.CopyN(ioutil.Discard, bresp.Body, bodySize/4)
+ _, err = io.CopyN(ioutil.Discard, bresp.Body, bodySize/2)
if err != nil {
t.Errorf("Proxy copy error: %v", err)
return
}))
defer proxy.Close()
+ defer close(unblockBackend)
req, _ := NewRequest("POST", proxy.URL, io.LimitReader(neverEnding('a'), bodySize))
res, err := DefaultClient.Do(req)
if err != nil {
// Cleanup, so we don't leak goroutines.
res.Body.Close()
- close(unblockBackend)
- (<-backendRespc).Body.Close()
+ select {
+ case res := <-backendRespc:
+ res.Body.Close()
+ default:
+ // We failed earlier. (e.g. on DefaultClient.Do(req2))
+ }
}
// Test that a hanging Request.Body.Read from another goroutine can't