]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: remove a stale cookie TODO comment, add a test
authorBrad Fitzpatrick <bradfitz@golang.org>
Wed, 12 Dec 2012 19:36:44 +0000 (11:36 -0800)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 12 Dec 2012 19:36:44 +0000 (11:36 -0800)
Fixes #4528

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/6922054

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

index 2b28b77d1bebae38bb81e090b853c0b66fdf394e..d0e50f6082c124f6f67bdd67dba3ade4d1fc869a 100644 (file)
@@ -231,8 +231,6 @@ func (c *Client) Get(url string) (resp *Response, err error) {
 }
 
 func (c *Client) doFollowingRedirects(ireq *Request, shouldRedirect func(int) bool) (resp *Response, err error) {
-       // TODO: if/when we add cookie support, the redirected request shouldn't
-       // necessarily supply the same cookies as the original.
        var base *url.URL
        redirectChecker := c.CheckRedirect
        if redirectChecker == nil {
index 4bb336f1a9b9a2d4f04ea1312495fe29c6038288..c2a836c948a71b1c86703e02449c8722c6eddeea 100644 (file)
@@ -351,6 +351,9 @@ type TestJar struct {
 func (j *TestJar) SetCookies(u *url.URL, cookies []*Cookie) {
        j.m.Lock()
        defer j.m.Unlock()
+       if j.perURL == nil {
+               j.perURL = make(map[string][]*Cookie)
+       }
        j.perURL[u.Host] = cookies
 }
 
@@ -381,8 +384,9 @@ func TestRedirectCookiesJar(t *testing.T) {
        var ts *httptest.Server
        ts = httptest.NewServer(echoCookiesRedirectHandler)
        defer ts.Close()
-       c := &Client{}
-       c.Jar = &TestJar{perURL: make(map[string][]*Cookie)}
+       c := &Client{
+               Jar: new(TestJar),
+       }
        u, _ := url.Parse(ts.URL)
        c.Jar.SetCookies(u, []*Cookie{expectedCookies[0]})
        resp, err := c.Get(ts.URL)
@@ -411,6 +415,61 @@ 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:]
+               SetCookie(w, &Cookie{Name: "name" + pathSuffix, Value: "val" + pathSuffix})
+               if r.RequestURI == "/" {
+                       Redirect(w, r, "http://secondhost.fake/secondpath", 302)
+               }
+       }))
+       defer ts.Close()
+       jar := new(RecordingJar)
+       c := &Client{
+               Jar: jar,
+               Transport: &Transport{
+                       Dial: func(_ string, _ string) (net.Conn, error) {
+                               return net.Dial("tcp", ts.Listener.Addr().String())
+                       },
+               },
+       }
+       _, err := c.Get("http://firsthost.fake/")
+       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])
+`
+       if got != want {
+               t.Errorf("Got Jar calls:\n%s\nWant:\n%s", got, want)
+       }
+}
+
+// RecordingJar keeps a log of calls made to it, without
+// tracking any cookies.
+type RecordingJar struct {
+       mu  sync.Mutex
+       log bytes.Buffer
+}
+
+func (j *RecordingJar) SetCookies(u *url.URL, cookies []*Cookie) {
+       j.logf("SetCookie(%q, %v)\n", u, cookies)
+}
+
+func (j *RecordingJar) Cookies(u *url.URL) []*Cookie {
+       j.logf("Cookies(%q)\n", u)
+       return nil
+}
+
+func (j *RecordingJar) logf(format string, args ...interface{}) {
+       j.mu.Lock()
+       defer j.mu.Unlock()
+       fmt.Fprintf(&j.log, format, args...)
+}
+
 func TestStreamingGet(t *testing.T) {
        say := make(chan string)
        ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {