-
Notifications
You must be signed in to change notification settings - Fork 1
/
cookies_test.go
158 lines (140 loc) · 4.11 KB
/
cookies_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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package spectest
import (
"net/http"
"testing"
"time"
)
func TestApiTestCookiesExpectedCookie(t *testing.T) {
expiry, _ := time.Parse("1/2/2006 15:04:05", "03/01/2017 12:00:00")
cookie := NewCookie("Tom").
Value("LovesBeers").
Path("/at-the-lyric").
Domain("in.london").
Expires(expiry).
MaxAge(10).
Secure(true).
HTTPOnly(false)
ten := 10
boolTrue := true
boolFalse := false
assert.Equal(t, Cookie{
name: toString("Tom"),
value: toString("LovesBeers"),
path: toString("/at-the-lyric"),
domain: toString("in.london"),
expires: &expiry,
maxAge: &ten,
secure: &boolTrue,
httpOnly: &boolFalse,
}, *cookie)
}
func TestApiTestCookiesToHttpCookie(t *testing.T) {
expiry, _ := time.Parse("1/2/2006 15:04:05", "03/01/2017 12:00:00")
httpCookie := NewCookie("Tom").
Value("LovesBeers").
Path("/at-the-lyric").
Domain("in.london").
Expires(expiry).
MaxAge(10).
Secure(true).
HTTPOnly(false).
ToHTTPCookie()
assert.Equal(t, http.Cookie{
Name: "Tom",
Value: "LovesBeers",
Path: "/at-the-lyric",
Domain: "in.london",
Expires: expiry,
MaxAge: 10,
Secure: true,
HttpOnly: false,
}, *httpCookie)
}
func TestApiTestCookiesFromHttpCookie(t *testing.T) {
expiry, _ := time.Parse("1/2/2006 15:04:05", "03/01/2017 12:00:00")
cookie := NewCookie("Tom").
Value("LovesBeers").
Path("/at-the-lyric").
Domain("in.london").
Expires(expiry).
MaxAge(10).
Secure(true).
HTTPOnly(false)
result := FromHTTPCookie(cookie.ToHTTPCookie())
assert.Equal(t, cookie, result)
}
func TestApiTestCookiesToHttpCookiePartiallyCreated(t *testing.T) {
expiry, _ := time.Parse("1/2/2006 15:04:05", "03/01/2017 12:00:00")
httpCookie := NewCookie("Tom").
Value("LovesBeers").
Expires(expiry).
ToHTTPCookie()
assert.Equal(t, http.Cookie{
Name: "Tom",
Value: "LovesBeers",
Expires: expiry,
Secure: false,
HttpOnly: false,
}, *httpCookie)
}
func TestCompareCookies(t *testing.T) {
tests := []struct {
name string
expected *Cookie
actual http.Cookie
mismatches []string
}{
{
name: "mismatches value",
expected: NewCookie("C").Value("A"),
actual: http.Cookie{Name: "C", Value: "V"},
mismatches: []string{"Mismatched field Value. Expected A but received V"},
},
{
name: "mismatches domain",
expected: NewCookie("C").Value("A").Domain("b.com"),
actual: http.Cookie{Name: "C", Value: "A", Domain: "a.com"},
mismatches: []string{"Mismatched field Domain. Expected b.com but received a.com"},
},
{
name: "mismatches path",
expected: NewCookie("C").Value("A").Path("/"),
actual: http.Cookie{Name: "C", Value: "A", Path: "/path"},
mismatches: []string{"Mismatched field Path. Expected / but received /path"},
},
{
name: "mismatches expires",
expected: NewCookie("C").Value("A").Expires(time.Unix(0, 0).UTC()),
actual: http.Cookie{Name: "C", Value: "A", Expires: time.Unix(1, 0).UTC()},
mismatches: []string{"Mismatched field Expires. Expected 1970-01-01 00:00:00 +0000 UTC but received 1970-01-01 00:00:01 +0000 UTC"},
},
{
name: "mismatches max age",
expected: NewCookie("C").Value("A").MaxAge(0),
actual: http.Cookie{Name: "C", Value: "A", MaxAge: 1},
mismatches: []string{"Mismatched field MaxAge. Expected 0 but received 1"},
},
{
name: "mismatches max secure",
expected: NewCookie("C").Value("A").Secure(true),
actual: http.Cookie{Name: "C", Value: "A", Secure: false},
mismatches: []string{"Mismatched field Secure. Expected true but received false"},
},
{
name: "mismatches http only",
expected: NewCookie("C").Value("A").HTTPOnly(true),
actual: http.Cookie{Name: "C", Value: "A", HttpOnly: false},
mismatches: []string{"Mismatched field HttpOnly. Expected true but received false"},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
found, mismatches := compareCookies(test.expected, &test.actual)
assert.True(t, found)
assert.Equal(t, test.mismatches, mismatches)
})
}
}
func toString(str string) *string {
return &str
}