File: cookiejarparser_test.go

package info (click to toggle)
golang-github-ssgelm-cookiejarparser 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, bullseye-backports, sid
  • size: 72 kB
  • sloc: makefile: 3
file content (96 lines) | stat: -rw-r--r-- 2,632 bytes parent folder | download | duplicates (2)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package cookiejarparser

import (
	"encoding/json"
	"net/http"
	"net/http/cookiejar"
	"net/url"
	"reflect"
	"testing"

	"golang.org/x/net/publicsuffix"
)

func TestParseCookieLine(t *testing.T) {
	// normal
	cookie, err := parseCookieLine("example.com	FALSE	/	FALSE	0	test_cookie	1", 1)
	sampleCookie := &http.Cookie{
		Domain:   "example.com",
		Path:     "/",
		Name:     "test_cookie",
		Value:    "1",
		HttpOnly: false,
		Secure:   false,
	}

	if !reflect.DeepEqual(cookie, sampleCookie) || err != nil {
		c1, _ := json.Marshal(cookie)
		c2, _ := json.Marshal(sampleCookie)

		t.Errorf("Parsing normal cookie failed.  Expected:\n  cookie: %s err: nil,\ngot:\n  cookie: %s err: %s", c2, c1, err)
	}
	// httponly
	cookieHttp, err := parseCookieLine("#HttpOnly_example.com	FALSE	/	FALSE	0	test_cookie_httponly	1", 1)
	sampleCookieHttp := &http.Cookie{
		Domain:   "example.com",
		Path:     "/",
		Name:     "test_cookie_httponly",
		Value:    "1",
		HttpOnly: true,
		Secure:   false,
	}

	if !reflect.DeepEqual(cookieHttp, sampleCookieHttp) || err != nil {
		c1, _ := json.Marshal(cookieHttp)
		c2, _ := json.Marshal(sampleCookieHttp)

		t.Errorf("Parsing httpOnly cookie failed.  Expected:\n  cookie: %s err: nil,\ngot:\n  cookie: %s err: %s", c2, c1, err)
	}

	// comment
	cookieComment, err := parseCookieLine("# This is a comment", 1)
	if cookieComment != nil || err != nil {
		t.Errorf("Parsing comment failed.  Expected cookie: nil err: nil, got cookie: %s err: %s", cookie, err)
	}

	cookieBlank, err := parseCookieLine("", 1)
	if cookieBlank != nil || err != nil {
		t.Errorf("Parsing blank line failed.  Expected cookie: nil err: nil, got cookie: %s err: %s", cookie, err)
	}
}

func TestLoadCookieJarFile(t *testing.T) {
	exampleURL := &url.URL{
		Scheme: "http",
		Host:   "example.com",
	}
	sampleCookies := []*http.Cookie{
		{
			Domain:   "example.com",
			Path:     "/",
			Name:     "test_cookie",
			Value:    "1",
			HttpOnly: false,
			Secure:   false,
		},
		{
			Domain:   "example.com",
			Path:     "/",
			Name:     "test_cookie_httponly",
			Value:    "1",
			HttpOnly: true,
			Secure:   false,
		},
	}
	sampleCookieJar, _ := cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List})
	sampleCookieJar.SetCookies(exampleURL, sampleCookies)

	cookieJar, err := LoadCookieJarFile("data/cookies.txt")

	c1, _ := json.Marshal(cookieJar.Cookies(exampleURL))
	c2, _ := json.Marshal(sampleCookieJar.Cookies(exampleURL))

	if !reflect.DeepEqual(c1, c2) || err != nil {
		t.Errorf("Cookie jar creation failed.  Expected:\n  cookieJar: %s err: nil,\ngot:\n  cookieJar: %s err: %s", c2, c1, err)
	}
}