deno.land / x / servest@v1.3.4 / cookie_test.ts

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
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
// Copyright 2019-2020 Yusuke Sakurai. All rights reserved. MIT license.import { assert, assertEquals, assertThrows,} from "./vendor/https/deno.land/std/testing/asserts.ts";import { group } from "./_test_util.ts";import { cookieToString, parseCookie, parseSetCookie } from "./cookie.ts";import { toIMF } from "./vendor/https/deno.land/std/datetime/mod.ts";import { createApp } from "./app.ts";
group("parseCookie", ({ test }) => { test("basic", () => { const cookie = parseCookie( `deno=land; foo=var; ${encodeURIComponent("👉=🦕")}`, ); assertEquals(cookie.get("deno"), "land"); assertEquals(cookie.get("foo"), "var"); assertEquals(cookie.get("👉"), "🦕"); });});group("parseSetCookie", ({ test }) => { const expires = new Date(); const maxAge = 1000; const domain = "servestjs.org"; const path = "/path"; const sameSite = "Lax"; test("basic", () => { const e = `deno=land; Expires=${ toIMF( expires, ) }; Max-Age=${maxAge}; Domain=${domain}; Path=${path}; Secure; HttpOnly; SameSite=${sameSite}`; const { name, value, ...opts } = parseSetCookie(e); assertEquals(name, "deno"); assertEquals(value, "land"); assertEquals(opts.expires!.toDateString(), expires.toDateString()); assertEquals(opts.maxAge, maxAge); assertEquals(opts.domain, domain); assertEquals(opts.path, path); assertEquals(opts.secure, true); assertEquals(opts.httpOnly, true); assertEquals(opts.sameSite, sameSite); });});group("cookieToString", ({ test }) => { test("basic", () => { const expires = new Date(); const maxAge = 1000; const domain = "servestjs.org"; const path = "/path"; const sameSite = "Lax"; const cookie = cookieToString("deno", "land", { maxAge, expires, domain, path, sameSite, secure: true, httpOnly: true, }); assertEquals( cookie, `deno=land; Expires=${ toIMF( expires, ) }; Max-Age=${maxAge}; Domain=${domain}; Path=${path}; Secure; HttpOnly; SameSite=${sameSite}`, ); }); test("should throw if maxAge is not integer", () => { assertThrows(() => cookieToString("deno", "land", { maxAge: 1.11, }) ); }); test("should throw if maxAge is lesser than or equals 0", () => { assertThrows(() => { cookieToString("deno", "land", { maxAge: -1, }); }); });});
group({ name: "cookie integration",}, ({ setupAll, test }) => { const now = new Date(); now.setMilliseconds(0); setupAll(() => { const router = createApp(); router.get("/", (req) => { req.setCookie("deno", "land", { path: "/", maxAge: 1000, expires: now, }); return req.respond({ status: 200, body: "ok", headers: new Headers({ "Connection": "close", }), }); }); router.get("/deno", (req) => { const deno = req.cookies.get("deno"); return req.respond({ status: 200, body: deno || "", headers: new Headers({ "Connection": "close", }), }); }); const lis = router.listen({ port: 9983 }); return () => lis.close(); }); test("basic", async () => { const resp = await fetch("http://127.0.0.1:9983/"); const sc = resp.headers.get("Set-Cookie"); assert(sc != null, "should set cookie"); const cookie = parseSetCookie(sc); await resp.text(); assertEquals(cookie, { name: "deno", value: "land", path: "/", maxAge: 1000, expires: now, sameSite: undefined, domain: undefined, secure: undefined, httpOnly: undefined, }); const resp2 = await fetch("http://127.0.0.1:9983/deno", { headers: { Cookie: "deno=land", }, }); const body = await resp2.text(); assertEquals(body, "land"); });});
servest

Version Info

Tagged at
2 years ago