File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments