]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: add cookies from jar to POST request.
authorVolker Dobler <dr.volker.dobler@gmail.com>
Mon, 21 May 2012 17:57:15 +0000 (10:57 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Mon, 21 May 2012 17:57:15 +0000 (10:57 -0700)
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

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

index 5d450258bd37798b035e15c891c02429375dbb29..54564e0989ecd624fe157e9bf6e6c89c51a8f449 100644 (file)
@@ -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())
index e00b62e590a2869b13c400b0b6cce3377dcad309..9b4261b9f61e497b7ac41a827aba861d0fcabea3 100644 (file)
@@ -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 {