diff options
| author | Brad Fitzpatrick <bradfitz@golang.org> | 2016-04-05 17:24:23 +0000 |
|---|---|---|
| committer | Brad Fitzpatrick <bradfitz@golang.org> | 2016-04-06 03:20:35 +0000 |
| commit | 870d997ab47fe88c33f4dadef38d7e85eeabf17c (patch) | |
| tree | 7cf599c388f32693f02d0bc2f2e5e46ad5b56a16 /src/net/http/client_test.go | |
| parent | fda831ed3f904c659fe41f253f75fe76528a28ee (diff) | |
| download | go-870d997ab47fe88c33f4dadef38d7e85eeabf17c.tar.xz | |
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>
Diffstat (limited to 'src/net/http/client_test.go')
| -rw-r--r-- | src/net/http/client_test.go | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/net/http/client_test.go b/src/net/http/client_test.go index e4fed26803..b9e17c5270 100644 --- a/src/net/http/client_test.go +++ b/src/net/http/client_test.go @@ -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 { |
