-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookie.test.ts
66 lines (57 loc) · 1.57 KB
/
cookie.test.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
import { equals, strict } from './deps.test.ts';
import {
getDomCookies,
getRequestCookies,
parse,
setDomCookie,
setResponseCookie,
stringify,
} from './cookie.ts';
Deno.test('parse', () => {
equals(parse('a=b;b=%3D'), { a: 'b', b: '=' });
});
Deno.test('stringify', () => {
strict(stringify('key', 'value'), 'key=value;samesite=none;secure');
strict(
stringify('key', 'value', {
expires: new Date('2040-01-01'),
samesite: 'lax',
secure: false,
}),
'key=value;expires=Sun, 01 Jan 2040 00:00:00 GMT;samesite=lax',
);
strict(
stringify('key', ''),
'key=;expires=Thu, 01 Jan 1970 00:00:00 GMT;samesite=none;secure',
);
});
declare global {
interface document {
cookie: string;
}
}
// @ts-ignore TODO reference global document in Deno
globalThis.document = { cookie: '' };
Deno.test('setDomCookie', () => {
setDomCookie('key', 'value');
// @ts-ignore TODO reference global document in Deno
strict(document.cookie, 'key=value;samesite=none;secure');
});
Deno.test('getDomCookies', () => {
equals(getDomCookies('a=b;b=%3D'), { a: 'b', b: '=' });
});
Deno.test('getRequestCookies', () => {
const request = new Request('file:///', {
headers: new Headers({ cookie: 'a=b;b=%3D' }),
});
equals(getRequestCookies(request), { a: 'b', b: '=' });
});
Deno.test('setResponseCookie', () => {
const response = new Response('/');
setResponseCookie(response, 'a', 'b');
setResponseCookie(response, 'b', '=');
equals(
response.headers.get('set-cookie'),
'a=b;samesite=none;secure, b=%3D;samesite=none;secure',
);
});