]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: only call client SetCookie when needed
authorJoakim Sernbrant <serbaut@gmail.com>
Thu, 20 Dec 2012 00:24:38 +0000 (16:24 -0800)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 20 Dec 2012 00:24:38 +0000 (16:24 -0800)
R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/6968049

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

index d0e50f6082c124f6f67bdd67dba3ade4d1fc869a..5ee0804c7da9fc2d3fc4db76d91e28d8688df565 100644 (file)
@@ -98,7 +98,9 @@ func (c *Client) send(req *Request) (*Response, error) {
                return nil, err
        }
        if c.Jar != nil {
-               c.Jar.SetCookies(req.URL, resp.Cookies())
+               if rc := resp.Cookies(); len(rc) > 0 {
+                       c.Jar.SetCookies(req.URL, rc)
+               }
        }
        return resp, err
 }
index c2a836c948a71b1c86703e02449c8722c6eddeea..9514a4b961058ebfc965cf9fe1672fb8c6caa8a0 100644 (file)
@@ -418,6 +418,9 @@ func matchReturnedCookies(t *testing.T, expected, given []*Cookie) {
 func TestJarCalls(t *testing.T) {
        ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
                pathSuffix := r.RequestURI[1:]
+               if r.RequestURI == "/nosetcookie" {
+                       return // dont set cookies for this path
+               }
                SetCookie(w, &Cookie{Name: "name" + pathSuffix, Value: "val" + pathSuffix})
                if r.RequestURI == "/" {
                        Redirect(w, r, "http://secondhost.fake/secondpath", 302)
@@ -437,11 +440,16 @@ func TestJarCalls(t *testing.T) {
        if err != nil {
                t.Fatal(err)
        }
+       _, err = c.Get("http://firsthost.fake/nosetcookie")
+       if err != nil {
+               t.Fatal(err)
+       }
        got := jar.log.String()
        want := `Cookies("http://firsthost.fake/")
 SetCookie("http://firsthost.fake/", [name=val])
 Cookies("http://secondhost.fake/secondpath")
 SetCookie("http://secondhost.fake/secondpath", [namesecondpath=valsecondpath])
+Cookies("http://firsthost.fake/nosetcookie")
 `
        if got != want {
                t.Errorf("Got Jar calls:\n%s\nWant:\n%s", got, want)