-
-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathcookie.test.ts
More file actions
175 lines (172 loc) · 4.96 KB
/
cookie.test.ts
File metadata and controls
175 lines (172 loc) · 4.96 KB
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
import { describe, expect, it } from 'vitest'
import * as cookie from '../../packages/cookie/src/index'
describe('Cookie parsing', () => {
it('should parse basic cookies', () => {
expect(cookie.parse('foo=bar')).toStrictEqual({
foo: 'bar'
})
})
it('should ignore spaces', () => {
expect(cookie.parse('foo = bar;')).toStrictEqual({
foo: 'bar'
})
})
it('should properly do escaping', () => {
expect(cookie.parse('foo="bar=123456789&name=Magic+Mouse"')).toStrictEqual({
foo: 'bar=123456789&name=Magic+Mouse'
})
})
it('should ignore escaping error and return original value', () => {
expect(cookie.parse('foo=%1;bar=bar')).toStrictEqual({ foo: '%1', bar: 'bar' })
})
it('should ignore non values', () => {
expect(cookie.parse('foo=%1;bar=bar;HttpOnly;Secure')).toStrictEqual({ foo: '%1', bar: 'bar' })
})
it('should assign only once', () => {
expect(cookie.parse('foo=%1;bar=bar;foo=boo')).toStrictEqual({ foo: '%1', bar: 'bar' })
})
it('should decode with a custom decoder', () => {
expect(
cookie.parse('foo=%1;bar=bar;foo=boo', {
decode: () => 'foobar'
})
).toStrictEqual({ foo: 'foobar', bar: 'foobar' })
})
})
describe('Cookie serializing', () => {
it('should serialize basic cookie', () => {
expect(cookie.serialize('foo', 'bar')).toBe('foo=bar')
})
it('should "Path" in cookie', () => {
expect(
cookie.serialize('foo', 'bar', {
path: '/'
})
).toBe('foo=bar; Path=/')
})
it('should put "Secure" in cookie', () => {
expect(
cookie.serialize('foo', 'bar', {
secure: true
})
).toBe('foo=bar; Secure')
expect(
cookie.serialize('foo', 'bar', {
secure: false
})
).toBe('foo=bar')
})
it('should put "httpOnly" in cookie', () => {
expect(
cookie.serialize('foo', 'bar', {
httpOnly: true
})
).toBe('foo=bar; HttpOnly')
})
it('should put valid "maxAge" in cookie', () => {
expect(
cookie.serialize('foo', 'bar', {
maxAge: 1000
})
).toBe('foo=bar; Max-Age=1000')
})
it('should throw on infinite "maxAge" parameter', () => {
try {
cookie.serialize('foo', 'bar', {
maxAge: Number.POSITIVE_INFINITY
})
} catch (e) {
expect((e as TypeError).message).toBe('option maxAge is invalid')
return
}
throw new Error('Did not throw an error')
})
it('should properly set "sameSite" parameter', () => {
expect(
cookie.serialize('foo', 'bar', {
sameSite: true
})
).toBe('foo=bar; SameSite=Strict')
expect(
cookie.serialize('foo', 'bar', {
sameSite: 'Lax'
})
).toBe('foo=bar; SameSite=Lax')
expect(
cookie.serialize('foo', 'bar', {
sameSite: 'None'
})
).toBe('foo=bar; SameSite=None')
})
it('should throw on invalid "sameSite" option', () => {
try {
cookie.serialize('foo', 'bar', {
sameSite: 'blah blah'
})
} catch (e) {
expect((e as TypeError).message).toBe('option sameSite is invalid')
return
}
throw new Error('Did not throw an error')
})
it('should do escaping', () => {
expect(cookie.serialize('cat', '+ ')).toBe('cat=%2B%20')
})
it('should parse serialized cookies', () => {
expect(cookie.parse(cookie.serialize('cat', 'foo=123&name=baz five'))).toStrictEqual({
cat: 'foo=123&name=baz five'
})
})
it('should throw an error if argument name is invalid', () => {
try {
cookie.serialize('➡️', 'bar')
} catch (e) {
expect((e as TypeError).message).toBe('argument name is invalid')
return
}
throw new Error('Did not throw an error')
})
it('should throw an error if argument val is invalid', () => {
try {
const customEncode = {
encode: (val: string) => {
return val
}
}
cookie.serialize('foo', '➡️', customEncode)
} catch (e) {
expect((e as TypeError).message).toBe('argument val is invalid')
return
}
throw new Error('Did not throw an error')
})
it('should throw an error if opt.domain is invalid', () => {
try {
const customOpt = { domain: '➡️' }
cookie.serialize('foo', 'bar', customOpt)
} catch (e) {
expect((e as TypeError).message).toBe('option domain is invalid')
return
}
throw new Error('Did not throw an error')
})
it('should throw an error if opt.path is invalid', () => {
try {
const customOpt = { path: '➡️' }
cookie.serialize('foo', 'bar', customOpt)
} catch (e) {
expect((e as TypeError).message).toBe('option path is invalid')
return
}
throw new Error('Did not throw an error')
})
it('should serialize a string correctly', () => {
try {
const customOpt = { path: 'lorem', domain: 'ipsum', expires: new Date(), sameSite: 'strict' }
cookie.serialize('foo', 'bar', customOpt)
} catch (e) {
expect(e).toBe(undefined)
return
}
})
})