Commit 31283dd4 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick Committed by Russ Cox

net/http: don't assume Response.Request is populated after redirect errors

Fixes #15577

Change-Id: I5f023790a393b17235db2e66c02c2483773ddc1a
Reviewed-on: https://go-review.googlesource.com/22857Reviewed-by: 's avatarRuss Cox <rsc@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 670a5cda
...@@ -442,7 +442,7 @@ func (c *Client) doFollowingRedirects(req *Request, shouldRedirect func(int) boo ...@@ -442,7 +442,7 @@ func (c *Client) doFollowingRedirects(req *Request, shouldRedirect func(int) boo
req.closeBody() req.closeBody()
method := valueOrDefault(reqs[0].Method, "GET") method := valueOrDefault(reqs[0].Method, "GET")
var urlStr string var urlStr string
if resp != nil { if resp != nil && resp.Request != nil {
urlStr = resp.Request.URL.String() urlStr = resp.Request.URL.String()
} else { } else {
urlStr = req.URL.String() urlStr = req.URL.String()
......
...@@ -1168,3 +1168,26 @@ func TestReferer(t *testing.T) { ...@@ -1168,3 +1168,26 @@ func TestReferer(t *testing.T) {
} }
} }
} }
// issue15577Tripper returns a Response with a redirect response
// header and doesn't populate its Response.Request field.
type issue15577Tripper struct{}
func (issue15577Tripper) RoundTrip(*Request) (*Response, error) {
resp := &Response{
StatusCode: 303,
Header: map[string][]string{"Location": {"http://www.example.com/"}},
Body: ioutil.NopCloser(strings.NewReader("")),
}
return resp, nil
}
// Issue 15577: don't assume the roundtripper's response populates its Request field.
func TestClientRedirectResponseWithoutRequest(t *testing.T) {
c := &Client{
CheckRedirect: func(*Request, []*Request) error { return fmt.Errorf("no redirects!") },
Transport: issue15577Tripper{},
}
// Check that this doesn't crash:
c.Get("http://dummy.tld")
}
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