-
Notifications
You must be signed in to change notification settings - Fork 6
/
cookie_test.go
116 lines (90 loc) · 2.59 KB
/
cookie_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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package fetch
import (
"testing"
)
func TestCookieEmpty(t *testing.T) {
cookie := newCookie()
if cookie.Get("") != "" {
t.Error("Expected empty string, got", cookie.Get(""))
}
if err := cookie.Add("", ""); err == nil {
t.Fatal("Expected error, got nil")
}
if items := cookie.Items(); len(items) != 0 {
t.Fatal("Expected 0 items, got", len(items))
}
if err := cookie.Set("key", "value"); err != nil {
t.Fatal("Expected error, got nil")
}
if items := cookie.Items(); len(items) != 1 {
t.Fatal("Expected 1 items, got", len(items))
}
if cookie.Get("key") != "value" {
t.Fatal("Expected value, got", cookie.Get("key"))
}
if cookie.String() != "key=value" {
t.Fatal("Expected key=value, got", cookie.String())
}
if err := cookie.Set("key1", "value1"); err != nil {
t.Fatal("Expected error, got nil")
}
if items := cookie.Items(); len(items) != 2 {
t.Fatal("Expected 2 items, got", len(items))
}
if cookie.Get("key1") != "value1" {
t.Fatal("Expected value, got", cookie.Get("key1"))
}
if cookie.String() != "key=value; key1=value1" {
t.Fatal("Expected string, got", cookie.String())
}
if err := cookie.Remove("key"); err != nil {
t.Fatal("Expected error, got nil")
}
if cookie.Get("key") != "" {
t.Fatal("Expected empty string, got", cookie.Get("key1"))
}
if cookie.Get("key1") != "value1" {
t.Fatal("Expected value, got", cookie.Get("key1"))
}
if cookie.String() != "key1=value1" {
t.Fatal("Expected key1=value1, got", cookie.String())
}
if err := cookie.Clear(); err != nil {
t.Fatal("Expected error, got nil")
}
if cookie.Get("key1") != "" {
t.Fatal("Expected empty string, got", cookie.Get("key1"))
}
if cookie.String() != "" {
t.Fatal("Expected empty string, got", cookie.String())
}
}
func TestCookieParse(t *testing.T) {
cookie := newCookie()
if err := cookie.Parse("key=value; key1=value1"); err != nil {
t.Fatal("Expected error, got nil")
}
if cookie.Get("key") != "value" {
t.Fatal("Expected value, got", cookie.Get("key"))
}
if cookie.Get("key1") != "value1" {
t.Fatal("Expected value, got", cookie.Get("key1"))
}
if cookie.String() != "key=value; key1=value1" {
t.Fatal("Expected string, got", cookie.String())
}
}
func TestCookie(t *testing.T) {
response, err := New().
SetCookie("key", "value").
SetCookie("key1", "value1").
SetURL("https://httpbin.zcorky.com/headers").
Send()
if err != nil {
t.Fatal(err)
}
// fmt.Println(response.Get("headers").String())
if response.Get("headers.cookie").String() != "key=value; key1=value1" {
t.Fatal("Expected cookie, got", response.Get("header.cookie").String())
}
}