mirror of
https://github.com/golang/go
synced 2024-09-05 07:54:46 +00:00
net/http: keep request context during Client redirects
Change-Id: I25c51280ba55120ffeaf08222f5ac5d471632d89 Reviewed-on: https://go-review.googlesource.com/21535 Reviewed-by: Andrew Gerrand <adg@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
fda831ed3f
commit
870d997ab4
|
@ -475,6 +475,7 @@ func (c *Client) doFollowingRedirects(req *Request, shouldRedirect func(int) boo
|
|||
URL: u,
|
||||
Header: make(Header),
|
||||
Cancel: ireq.Cancel,
|
||||
ctx: ireq.ctx,
|
||||
}
|
||||
if ireq.Method == "POST" || ireq.Method == "PUT" {
|
||||
req.Method = "GET"
|
||||
|
|
|
@ -8,6 +8,7 @@ package http_test
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"encoding/base64"
|
||||
|
@ -290,6 +291,33 @@ func TestClientRedirects(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestClientRedirectContext(t *testing.T) {
|
||||
defer afterTest(t)
|
||||
ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
|
||||
Redirect(w, r, "/", StatusFound)
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
c := &Client{CheckRedirect: func(req *Request, via []*Request) error {
|
||||
cancel()
|
||||
if len(via) > 2 {
|
||||
return errors.New("too many redirects")
|
||||
}
|
||||
return nil
|
||||
}}
|
||||
req, _ := NewRequest("GET", ts.URL, nil)
|
||||
req = req.WithContext(ctx)
|
||||
_, err := c.Do(req)
|
||||
ue, ok := err.(*url.Error)
|
||||
if !ok {
|
||||
t.Fatalf("got error %T; want *url.Error")
|
||||
}
|
||||
if ue.Err != ExportErrRequestCanceled && ue.Err != ExportErrRequestCanceledConn {
|
||||
t.Errorf("url.Error.Err = %v; want errRequestCanceled or errRequestCanceledConn", ue.Err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostRedirects(t *testing.T) {
|
||||
defer afterTest(t)
|
||||
var log struct {
|
||||
|
|
Loading…
Reference in a new issue