-
Notifications
You must be signed in to change notification settings - Fork 275
/
Copy pathcookiePrefixes.spec.ts
213 lines (195 loc) · 6.18 KB
/
cookiePrefixes.spec.ts
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { PrefixSecurityEnum } from '../cookie/constants'
import { CookieJar } from '../cookie/cookieJar'
let cookieJar: CookieJar
const insecureUrl = 'http://www.example.com'
const secureUrl = 'https://www.example.com'
describe('When `prefixSecurity` is enabled for `CookieJar`', () => {
describe('silent', () => {
beforeEach(() => {
cookieJar = new CookieJar(null, {
prefixSecurity: 'silent',
})
expect(cookieJar.prefixSecurity).toBe(PrefixSecurityEnum.SILENT)
})
describe('__Secure prefix', () => {
it('should fail silently with no Secure attribute', async () => {
await cookieJar.setCookie(
'__Secure-SID=12345; Domain=example.com',
insecureUrl,
{},
)
const cookies = await cookieJar.getCookies(insecureUrl)
expect(cookies).toEqual([])
})
it('should work if cookie has Secure attribute and domain is https', async () => {
await cookieJar.setCookie(
'__Secure-SID=12345; Domain=example.com; Secure',
secureUrl,
{},
)
const cookies = await cookieJar.getCookies(secureUrl)
expect(cookies).toEqual([
expect.objectContaining({
key: '__Secure-SID',
value: '12345',
}),
])
})
it('should fail silently if cookie has Secure attribute but domain is http', async () => {
await cookieJar.setCookie(
'__Secure-SID=12345; Domain=example.com; Secure',
insecureUrl,
{},
)
const cookies = await cookieJar.getCookies(insecureUrl)
expect(cookies).toEqual([])
})
})
describe('__Host prefix', () => {
it('should fail silently when no Secure attribute, Domain, or Path', async () => {
await cookieJar.setCookie('__Host-SID=12345', insecureUrl, {})
const cookies = await cookieJar.getCookies(insecureUrl)
expect(cookies).toEqual([])
})
it('should fail silently when no Domain or Path', async () => {
await cookieJar.setCookie('__Host-SID=12345; Secure', insecureUrl, {})
const cookies = await cookieJar.getCookies(insecureUrl)
expect(cookies).toEqual([])
})
it('should fail silently when no Path', async () => {
await cookieJar.setCookie(
'__Host-SID=12345; Secure; Domain=example.com',
insecureUrl,
{},
)
const cookies = await cookieJar.getCookies(insecureUrl)
expect(cookies).toEqual([])
})
it('should fail silently with Domain', async () => {
await cookieJar.setCookie(
'__Host-SID=12345; Secure; Domain=example.com; Path=/',
insecureUrl,
{},
)
const cookies = await cookieJar.getCookies(insecureUrl)
expect(cookies).toEqual([])
})
it('should work with Secure and Path but no Domain over https', async () => {
await cookieJar.setCookie(
'__Host-SID=12345; Secure; Path=/',
secureUrl,
{},
)
const cookies = await cookieJar.getCookies(secureUrl)
expect(cookies).toEqual([
expect.objectContaining({
key: '__Host-SID',
value: '12345',
}),
])
})
})
})
describe('strict', () => {
beforeEach(() => {
cookieJar = new CookieJar(null, {
prefixSecurity: 'strict',
})
expect(cookieJar.prefixSecurity).toBe(PrefixSecurityEnum.STRICT)
})
describe('__Secure prefix', () => {
it('should work for a valid cookie', async () => {
await cookieJar.setCookie(
'__Secure-SID=12345; Secure; Domain=example.com',
secureUrl,
{},
)
const cookies = await cookieJar.getCookies(secureUrl)
expect(cookies).toEqual([
expect.objectContaining({
key: '__Secure-SID',
value: '12345',
}),
])
})
it('should error for an invalid cookie', async () => {
await expect(
cookieJar.setCookie(
'__Secure-SID=12345; Domain=example.com',
insecureUrl,
{},
),
).rejects.toThrowError(
'Cookie has __Secure prefix but Secure attribute is not set',
)
})
})
describe('__Host prefix', () => {
it('should work for a valid cookie', async () => {
await cookieJar.setCookie(
'___Host-SID=12345; Secure; Path=/',
secureUrl,
{},
)
const cookies = await cookieJar.getCookies(secureUrl)
expect(cookies).toEqual([
expect.objectContaining({
key: '___Host-SID',
value: '12345',
}),
])
})
it('should error for an invalid cookie', async () => {
await expect(
cookieJar.setCookie(
'__Host-SID=12345; Secure; Domain=example.com',
secureUrl,
{},
),
).rejects.toThrowError(
`Cookie has __Host prefix but either Secure or HostOnly attribute is not set or Path is not '/'`,
)
})
})
})
describe('disabled', () => {
beforeEach(() => {
cookieJar = new CookieJar(null, {
prefixSecurity: 'unsafe-disabled',
})
expect(cookieJar.prefixSecurity).toBe(PrefixSecurityEnum.DISABLED)
})
describe('__Secure prefix', () => {
it('does not fail', async () => {
await cookieJar.setCookie(
'__Secure-SID=12345; Domain=example.com',
insecureUrl,
{},
)
const cookies = await cookieJar.getCookies(insecureUrl)
expect(cookies).toEqual([
expect.objectContaining({
key: '__Secure-SID',
value: '12345',
}),
])
})
})
describe('__Host prefix', () => {
it('does not fail', async () => {
await cookieJar.setCookie(
'__Host-SID=12345; Domain=example.com',
insecureUrl,
{},
)
const cookies = await cookieJar.getCookies(insecureUrl)
expect(cookies).toEqual([
expect.objectContaining({
key: '__Host-SID',
value: '12345',
}),
])
})
})
})
})