From: Volker Dobler Date: Mon, 21 May 2012 17:57:15 +0000 (-0700) Subject: net/http: add cookies from jar to POST request. X-Git-Tag: go1.1rc2~3172 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=b4456df6d237e2f8dc66c3f405d2d79836aa797d;p=gostls13.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 --- diff --git a/src/pkg/net/http/client.go b/src/pkg/net/http/client.go index 5d450258bd..54564e0989 100644 --- a/src/pkg/net/http/client.go +++ b/src/pkg/net/http/client.go @@ -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()) diff --git a/src/pkg/net/http/client_test.go b/src/pkg/net/http/client_test.go index e00b62e590..9b4261b9f6 100644 --- a/src/pkg/net/http/client_test.go +++ b/src/pkg/net/http/client_test.go @@ -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 {