deno.land / std@0.224.0 / http / unstable_signed_cookie_test.ts

unstable_signed_cookie_test.ts
View Documentation
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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.import { parseSignedCookie, signCookie, verifyCookie,} from "./unstable_signed_cookie.ts";import { assertEquals } from "../assert/assert_equals.ts";
Deno.test("signCookie() and verifyCookie() work circularly", async () => { const key = await crypto.subtle.generateKey( { name: "HMAC", hash: "SHA-256" }, true, ["sign", "verify"], ); const value = "boris";
const signedCookie = await signCookie(value, key); assertEquals(await verifyCookie(signedCookie, key), true);
const tamperedCookie = signedCookie.replace(value, "xenia"); assertEquals(await verifyCookie(tamperedCookie, key), false);});
Deno.test("signCookie() and verifyCookie() work circularly when the cookie value contains a period", async () => { const key = await crypto.subtle.generateKey( { name: "HMAC", hash: "SHA-256" }, true, ["sign", "verify"], ); const value = "boris.xenia";
const signedCookie = await signCookie(value, key); assertEquals(await verifyCookie(signedCookie, key), true);
const tamperedCookie = signedCookie.replace(value, "xenia"); assertEquals(await verifyCookie(tamperedCookie, key), false);});
Deno.test("verifyCookie() returns false on poorly formed value", async () => { const key = await crypto.subtle.generateKey( { name: "HMAC", hash: "SHA-256" }, true, ["sign", "verify"], );
assertEquals(await verifyCookie(".", key), false); assertEquals(await verifyCookie("hello.", key), false); assertEquals(await verifyCookie(".world", key), false);});
Deno.test("parseSignedCookie() returns parsed cookie value", () => { const value = "tokyo"; const signedCookie = `${value}.37f7481039762eef5cd46669f93c0a3214dfecba7d0cdc0b0dc40036063fb22e`; assertEquals(parseSignedCookie(signedCookie), value);});
Deno.test("parseSignedCookie() returns parsed cookie value with name containing period", () => { const value = "tokyo.osaka"; const signedCookie = `${value}.37f7481039762eef5cd46669f93c0a3214dfecba7d0cdc0b0dc40036063fb22e`; assertEquals(parseSignedCookie(signedCookie), value);});
std

Version Info

Tagged at
6 months ago