-
Notifications
You must be signed in to change notification settings - Fork 42
/
cookiejar_example_test.go
49 lines (42 loc) · 1.11 KB
/
cookiejar_example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package kooky_test
import (
"context"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strings"
"github.com/browserutils/kooky"
_ "github.com/browserutils/kooky/browser/firefox"
)
func Example_cookieJar() {
ctx := context.TODO()
stores := kooky.FindAllCookieStores(ctx)
var s kooky.CookieStore
for _, store := range stores {
if store.Browser() != `firefox` || !store.IsDefaultProfile() {
continue
}
s = store
break
}
// jar := s
// only store cookies relevant for the target website in the cookie jar
jar, _ := s.SubJar(ctx, kooky.FilterFunc(func(c *kooky.Cookie) bool {
return kooky.Domain(`github.com`).Filter(c) || kooky.Domain(`.github.com`).Filter(c)
}))
u, _ := url.Parse(`https://github.com/settings/profile`)
cookies := kooky.FilterCookies(ctx, jar.Cookies(u), kooky.Name(`logged_in`)).Collect(ctx)
if len(cookies) == 0 {
log.Fatal(`not logged in`)
}
client := http.Client{Jar: jar}
resp, _ := client.Get(u.String())
body, _ := io.ReadAll(resp.Body)
if !strings.Contains(string(body), `id="user_profile_name"`) {
fmt.Print("not ")
}
fmt.Println("logged in")
// Output: logged in
}