]> Cypherpunks repositories - gostls13.git/commitdiff
net/http/cookiejar: added simple example test
authorDhaivat Pandit <dhaivatpandit@gmail.com>
Sat, 27 Aug 2016 03:13:44 +0000 (20:13 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Fri, 9 Sep 2016 03:47:00 +0000 (03:47 +0000)
Fixes #16884
Updates #16360

Change-Id: I01563031a1c105e54499134eed4789f6219f41ec
Reviewed-on: https://go-review.googlesource.com/27993
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/net/http/cookiejar/dummy_publicsuffix_test.go [new file with mode: 0644]
src/net/http/cookiejar/example_test.go [new file with mode: 0644]

diff --git a/src/net/http/cookiejar/dummy_publicsuffix_test.go b/src/net/http/cookiejar/dummy_publicsuffix_test.go
new file mode 100644 (file)
index 0000000..9b31173
--- /dev/null
@@ -0,0 +1,21 @@
+// Copyright 2016 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package cookiejar_test
+
+import "net/http/cookiejar"
+
+type dummypsl struct {
+       List cookiejar.PublicSuffixList
+}
+
+func (dummypsl) PublicSuffix(domain string) string {
+       return domain
+}
+
+func (dummypsl) String() string {
+       return "dummy"
+}
+
+var publicsuffix = dummypsl{}
diff --git a/src/net/http/cookiejar/example_test.go b/src/net/http/cookiejar/example_test.go
new file mode 100644 (file)
index 0000000..91728ca
--- /dev/null
@@ -0,0 +1,65 @@
+// Copyright 2016 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package cookiejar_test
+
+import (
+       "fmt"
+       "log"
+       "net/http"
+       "net/http/cookiejar"
+       "net/http/httptest"
+       "net/url"
+)
+
+func ExampleNew() {
+       // Start a server to give us cookies.
+       ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+               if cookie, err := r.Cookie("Flavor"); err != nil {
+                       http.SetCookie(w, &http.Cookie{Name: "Flavor", Value: "Chocolate Chip"})
+               } else {
+                       cookie.Value = "Oatmeal Raisin"
+                       http.SetCookie(w, cookie)
+               }
+       }))
+       defer ts.Close()
+
+       u, err := url.Parse(ts.URL)
+       if err != nil {
+               log.Fatal(err)
+       }
+
+       // All users of cookiejar should import "golang.org/x/net/publicsuffix"
+       jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
+       if err != nil {
+               log.Fatal(err)
+       }
+
+       client := &http.Client{
+               Jar: jar,
+       }
+
+       if _, err = client.Get(u.String()); err != nil {
+               log.Fatal(err)
+       }
+
+       fmt.Println("After 1st request:")
+       for _, cookie := range jar.Cookies(u) {
+               fmt.Printf("  %s: %s\n", cookie.Name, cookie.Value)
+       }
+
+       if _, err = client.Get(u.String()); err != nil {
+               log.Fatal(err)
+       }
+
+       fmt.Println("After 2nd request:")
+       for _, cookie := range jar.Cookies(u) {
+               fmt.Printf("  %s: %s\n", cookie.Name, cookie.Value)
+       }
+       // Output:
+       // After 1st request:
+       //   Flavor: Chocolate Chip
+       // After 2nd request:
+       //   Flavor: Oatmeal Raisin
+}