github.com/ck00004/CobaltStrikeParser-Go@v1.0.14/lib/http/cookiejar/example_test.go (about)

     1  // Copyright 2016 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  
    11  	"github.com/ck00004/CobaltStrikeParser-Go/lib/url"
    12  
    13  	"github.com/ck00004/CobaltStrikeParser-Go/lib/http"
    14  	"github.com/ck00004/CobaltStrikeParser-Go/lib/http/cookiejar"
    15  	"github.com/ck00004/CobaltStrikeParser-Go/lib/http/httptest"
    16  )
    17  
    18  func ExampleNew() {
    19  	// Start a server to give us cookies.
    20  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    21  		if cookie, err := r.Cookie("Flavor"); err != nil {
    22  			http.SetCookie(w, &http.Cookie{Name: "Flavor", Value: "Chocolate Chip"})
    23  		} else {
    24  			cookie.Value = "Oatmeal Raisin"
    25  			http.SetCookie(w, cookie)
    26  		}
    27  	}))
    28  	defer ts.Close()
    29  
    30  	u, err := url.Parse(ts.URL)
    31  	if err != nil {
    32  		log.Fatal(err)
    33  	}
    34  
    35  	// All users of cookiejar should import "golang.org/x/net/publicsuffix"
    36  	jar, err := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
    37  	if err != nil {
    38  		log.Fatal(err)
    39  	}
    40  
    41  	client := &http.Client{
    42  		Jar: jar,
    43  	}
    44  
    45  	if _, err = client.Get(u.String()); err != nil {
    46  		log.Fatal(err)
    47  	}
    48  
    49  	fmt.Println("After 1st request:")
    50  	for _, cookie := range jar.Cookies(u) {
    51  		fmt.Printf("  %s: %s\n", cookie.Name, cookie.Value)
    52  	}
    53  
    54  	if _, err = client.Get(u.String()); err != nil {
    55  		log.Fatal(err)
    56  	}
    57  
    58  	fmt.Println("After 2nd request:")
    59  	for _, cookie := range jar.Cookies(u) {
    60  		fmt.Printf("  %s: %s\n", cookie.Name, cookie.Value)
    61  	}
    62  	// Output:
    63  	// After 1st request:
    64  	//   Flavor: Chocolate Chip
    65  	// After 2nd request:
    66  	//   Flavor: Oatmeal Raisin
    67  }