]> Cypherpunks.ru repositories - gostls13.git/commitdiff
net/http: keep request context during Client redirects
authorBrad Fitzpatrick <bradfitz@golang.org>
Tue, 5 Apr 2016 17:24:23 +0000 (17:24 +0000)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 6 Apr 2016 03:20:35 +0000 (03:20 +0000)
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>

src/net/http/client.go
src/net/http/client_test.go

index 10f5684a797addf1edecba0bcb97929ea1aa83d3..ee0fd2cb62a388f73d3809cc32aeda96268544d9 100644 (file)
@@ -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"
index e4fed2680327a12419895ac820064d30df55fe8c..b9e17c52709bba2593ec8245e8179678191ab4b6 100644 (file)
@@ -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 {