Commit 870d997a authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

net/http: keep request context during Client redirects

Change-Id: I25c51280ba55120ffeaf08222f5ac5d471632d89
Reviewed-on: https://go-review.googlesource.com/21535Reviewed-by: default avatarAndrew Gerrand <adg@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent fda831ed
......@@ -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 {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment