Skip to content

Commit 372d471

Browse files
committed
net/http/cookiejar: added simple example test
Partially fixes golang#16360 Change-Id: I01563031a1c105e54499134eed4789f6219f41ec
1 parent 0d23c28 commit 372d471

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2013 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package cookiejar_test
6+
7+
import (
8+
"fmt"
9+
"log"
10+
"net/http"
11+
"net/http/cookiejar"
12+
"net/http/httptest"
13+
)
14+
15+
func ExampleNew() {
16+
17+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
18+
http.SetCookie(w, &http.Cookie{Name: "COOKIE", Value: "NOM NOM NOM"})
19+
}))
20+
defer ts.Close()
21+
22+
// A nil value is valid and may be useful for testing but it is not
23+
// secure: it means that the HTTP server for foo.co.uk can set a cookie
24+
// for bar.co.uk.
25+
//
26+
// Actual implementation in package golang.org/x/net/publicsuffix
27+
// jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
28+
jar, err := cookiejar.New(nil)
29+
if err != nil {
30+
log.Fatal(err)
31+
}
32+
33+
client := http.Client{
34+
Jar: jar,
35+
}
36+
37+
req, err := http.NewRequest("GET", ts.URL, nil)
38+
if _, err = client.Do(req); err != nil {
39+
log.Fatal(err)
40+
}
41+
42+
for _, cookie := range jar.Cookies(req.URL) {
43+
fmt.Printf("%s:%s", cookie.Name, cookie.Value)
44+
}
45+
46+
// Output:
47+
// COOKIE:NOM NOM NOM
48+
}

0 commit comments

Comments
 (0)