forked from browserutils/kooky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookiejar_example_test.go
48 lines (42 loc) · 1012 Bytes
/
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
package kooky_test
import (
"fmt"
"io"
"log"
"net/http"
"net/url"
"strings"
"github.com/jeremy-cxf/kooky"
_ "github.com/jeremy-cxf/kooky/browser/firefox"
)
func Example_cookieJar() {
stores := kooky.FindAllCookieStores()
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(kooky.Domain(`github.com`))
u, _ := url.Parse(`https://github.com/settings/profile`)
var loggedIn bool
cookies := kooky.FilterCookies(jar.Cookies(u), kooky.Name(`logged_in`))
if len(cookies) > 0 {
loggedIn = true
}
if !loggedIn {
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
}