mirror of https://github.com/golang/go.git
net/http: add cookies from jar to POST request.
The main content of this CL is a test case checking the reported issue 3511 and a tiny fix for it. A subsequent CL will refactor the fix as proposed issue 3511. Fixes #3511. R=golang-dev, steven.hartland, bradfitz CC=golang-dev https://golang.org/cl/6013049
This commit is contained in:
parent
30c0d2315e
commit
b4456df6d2
|
|
@ -278,6 +278,11 @@ func (c *Client) Post(url string, bodyType string, body io.Reader) (r *Response,
|
|||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", bodyType)
|
||||
if c.Jar != nil {
|
||||
for _, cookie := range c.Jar.Cookies(req.URL) {
|
||||
req.AddCookie(cookie)
|
||||
}
|
||||
}
|
||||
r, err = send(req, c.Transport)
|
||||
if err == nil && c.Jar != nil {
|
||||
c.Jar.SetCookies(req.URL, r.Cookies())
|
||||
|
|
|
|||
|
|
@ -256,6 +256,31 @@ var echoCookiesRedirectHandler = HandlerFunc(func(w ResponseWriter, r *Request)
|
|||
}
|
||||
})
|
||||
|
||||
func TestClientSendsCookieFromJar(t *testing.T) {
|
||||
tr := &recordingTransport{}
|
||||
client := &Client{Transport: tr}
|
||||
client.Jar = &TestJar{perURL: make(map[string][]*Cookie)}
|
||||
us := "http://dummy.faketld/"
|
||||
u, _ := url.Parse(us)
|
||||
client.Jar.SetCookies(u, expectedCookies)
|
||||
|
||||
client.Get(us) // Note: doesn't hit network
|
||||
matchReturnedCookies(t, expectedCookies, tr.req.Cookies())
|
||||
|
||||
client.Head(us) // Note: doesn't hit network
|
||||
matchReturnedCookies(t, expectedCookies, tr.req.Cookies())
|
||||
|
||||
client.Post(us, "text/plain", strings.NewReader("body")) // Note: doesn't hit network
|
||||
matchReturnedCookies(t, expectedCookies, tr.req.Cookies())
|
||||
|
||||
client.PostForm(us, url.Values{}) // Note: doesn't hit network
|
||||
matchReturnedCookies(t, expectedCookies, tr.req.Cookies())
|
||||
|
||||
req, _ := NewRequest("GET", us, nil)
|
||||
client.Do(req) // Note: doesn't hit network
|
||||
matchReturnedCookies(t, expectedCookies, tr.req.Cookies())
|
||||
}
|
||||
|
||||
// Just enough correctness for our redirect tests. Uses the URL.Host as the
|
||||
// scope of all cookies.
|
||||
type TestJar struct {
|
||||
|
|
|
|||
Loading…
Reference in New Issue