mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
net/http: make Transport return proper error on cancel before response headers
Fixes #11020 Change-Id: I52760a01420a11f3c979f678812b3775a3af61e4 Reviewed-on: https://go-review.googlesource.com/12545 Reviewed-by: Russ Cox <rsc@golang.org>
This commit is contained in:
parent
0c2d3f7346
commit
9d56c1813e
2 changed files with 44 additions and 0 deletions
|
|
@ -1205,6 +1205,9 @@ WaitResponse:
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
re = responseAndError{err: errClosed}
|
re = responseAndError{err: errClosed}
|
||||||
|
if pc.isCanceled() {
|
||||||
|
re = responseAndError{err: errRequestCanceled}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break WaitResponse
|
break WaitResponse
|
||||||
case <-respHeaderTimer:
|
case <-respHeaderTimer:
|
||||||
|
|
|
||||||
|
|
@ -1552,6 +1552,47 @@ func TestCancelRequestWithChannelBeforeDo(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Issue 11020. The returned error message should be errRequestCanceled
|
||||||
|
func TestTransportCancelBeforeResponseHeaders(t *testing.T) {
|
||||||
|
defer afterTest(t)
|
||||||
|
|
||||||
|
serverConnCh := make(chan net.Conn, 1)
|
||||||
|
tr := &Transport{
|
||||||
|
Dial: func(network, addr string) (net.Conn, error) {
|
||||||
|
cc, sc := net.Pipe()
|
||||||
|
serverConnCh <- sc
|
||||||
|
return cc, nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
defer tr.CloseIdleConnections()
|
||||||
|
errc := make(chan error, 1)
|
||||||
|
req, _ := NewRequest("GET", "http://example.com/", nil)
|
||||||
|
go func() {
|
||||||
|
_, err := tr.RoundTrip(req)
|
||||||
|
errc <- err
|
||||||
|
}()
|
||||||
|
|
||||||
|
sc := <-serverConnCh
|
||||||
|
verb := make([]byte, 3)
|
||||||
|
if _, err := io.ReadFull(sc, verb); err != nil {
|
||||||
|
t.Errorf("Error reading HTTP verb from server: %v", err)
|
||||||
|
}
|
||||||
|
if string(verb) != "GET" {
|
||||||
|
t.Errorf("server received %q; want GET", verb)
|
||||||
|
}
|
||||||
|
defer sc.Close()
|
||||||
|
|
||||||
|
tr.CancelRequest(req)
|
||||||
|
|
||||||
|
err := <-errc
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("unexpected success from RoundTrip")
|
||||||
|
}
|
||||||
|
if err != ExportErrRequestCanceled {
|
||||||
|
t.Errorf("RoundTrip error = %v; want ExportErrRequestCanceled", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// golang.org/issue/3672 -- Client can't close HTTP stream
|
// golang.org/issue/3672 -- Client can't close HTTP stream
|
||||||
// Calling Close on a Response.Body used to just read until EOF.
|
// Calling Close on a Response.Body used to just read until EOF.
|
||||||
// Now it actually closes the TCP connection.
|
// Now it actually closes the TCP connection.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue