2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-09-20 08:28:06 +00:00
|
|
|
import { stub } from "@std/testing/mock";
|
2022-08-13 14:49:25 +00:00
|
|
|
import {
|
|
|
|
deleteCookie,
|
|
|
|
getCookies,
|
|
|
|
getSetCookies,
|
|
|
|
setCookie,
|
|
|
|
} from "./cookie.ts";
|
2024-04-29 02:57:30 +00:00
|
|
|
import { assert, assertEquals, assertThrows } from "@std/assert";
|
2019-04-24 11:38:52 +00:00
|
|
|
|
2020-06-12 19:23:38 +00:00
|
|
|
Deno.test({
|
2024-02-27 20:03:28 +00:00
|
|
|
name: "getCookies() parses cookie",
|
2022-08-24 01:21:57 +00:00
|
|
|
fn() {
|
2021-09-13 06:16:16 +00:00
|
|
|
let headers = new Headers();
|
|
|
|
assertEquals(getCookies(headers), {});
|
|
|
|
headers = new Headers();
|
|
|
|
headers.set("Cookie", "foo=bar");
|
|
|
|
assertEquals(getCookies(headers), { foo: "bar" });
|
2019-04-24 11:38:52 +00:00
|
|
|
|
2021-09-13 06:16:16 +00:00
|
|
|
headers = new Headers();
|
|
|
|
headers.set("Cookie", "full=of ; tasty=chocolate");
|
|
|
|
assertEquals(getCookies(headers), { full: "of ", tasty: "chocolate" });
|
2019-04-24 11:38:52 +00:00
|
|
|
|
2021-09-13 06:16:16 +00:00
|
|
|
headers = new Headers();
|
|
|
|
headers.set("Cookie", "igot=99; problems=but...");
|
|
|
|
assertEquals(getCookies(headers), { igot: "99", problems: "but..." });
|
2019-04-29 14:49:50 +00:00
|
|
|
|
2021-09-13 06:16:16 +00:00
|
|
|
headers = new Headers();
|
|
|
|
headers.set("Cookie", "PREF=al=en-GB&f1=123; wide=1; SID=123");
|
|
|
|
assertEquals(getCookies(headers), {
|
2019-04-29 14:49:50 +00:00
|
|
|
PREF: "al=en-GB&f1=123",
|
|
|
|
wide: "1",
|
2020-03-28 17:03:49 +00:00
|
|
|
SID: "123",
|
2019-04-29 14:49:50 +00:00
|
|
|
});
|
2020-03-28 17:03:49 +00:00
|
|
|
},
|
2019-04-27 23:07:11 +00:00
|
|
|
});
|
|
|
|
|
2020-11-17 20:06:06 +00:00
|
|
|
Deno.test({
|
2024-02-27 20:03:28 +00:00
|
|
|
name: "setCookie() validates names",
|
2022-08-24 01:21:57 +00:00
|
|
|
fn() {
|
2020-11-17 20:06:06 +00:00
|
|
|
const tokens = [
|
|
|
|
'"id"',
|
|
|
|
"id\t",
|
|
|
|
"i\td",
|
|
|
|
"i d",
|
|
|
|
"i;d",
|
|
|
|
"{id}",
|
|
|
|
"[id]",
|
|
|
|
'"',
|
|
|
|
"id\u0091",
|
|
|
|
];
|
2021-09-13 06:16:16 +00:00
|
|
|
const headers = new Headers();
|
2020-11-17 20:06:06 +00:00
|
|
|
tokens.forEach((name) => {
|
|
|
|
assertThrows(
|
2022-08-24 01:21:57 +00:00
|
|
|
() => {
|
2021-09-13 06:16:16 +00:00
|
|
|
setCookie(headers, {
|
2020-11-17 20:06:06 +00:00
|
|
|
name,
|
|
|
|
value: "Cat",
|
|
|
|
httpOnly: true,
|
|
|
|
secure: true,
|
|
|
|
maxAge: 3,
|
|
|
|
});
|
|
|
|
},
|
2024-06-25 05:20:58 +00:00
|
|
|
SyntaxError,
|
2024-08-26 05:05:00 +00:00
|
|
|
'Invalid cookie name: "' + name + '"',
|
2020-11-17 20:06:06 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-12-01 13:23:03 +00:00
|
|
|
Deno.test({
|
2024-02-27 20:03:28 +00:00
|
|
|
name: "setCookie() validates value",
|
2022-08-24 01:21:57 +00:00
|
|
|
fn() {
|
2020-12-01 13:23:03 +00:00
|
|
|
const tokens = [
|
|
|
|
"1f\tWa",
|
|
|
|
"\t",
|
|
|
|
"1f Wa",
|
|
|
|
"1f;Wa",
|
|
|
|
'"1fWa',
|
|
|
|
"1f\\Wa",
|
|
|
|
'1f"Wa',
|
|
|
|
'"',
|
|
|
|
"1fWa\u0005",
|
|
|
|
"1f\u0091Wa",
|
|
|
|
];
|
2021-09-13 06:16:16 +00:00
|
|
|
const headers = new Headers();
|
2020-12-01 13:23:03 +00:00
|
|
|
tokens.forEach((value) => {
|
|
|
|
assertThrows(
|
2022-08-24 01:21:57 +00:00
|
|
|
() => {
|
2020-12-01 13:23:03 +00:00
|
|
|
setCookie(
|
2021-09-13 06:16:16 +00:00
|
|
|
headers,
|
2020-12-01 13:23:03 +00:00
|
|
|
{
|
|
|
|
name: "Space",
|
|
|
|
value,
|
|
|
|
httpOnly: true,
|
|
|
|
secure: true,
|
|
|
|
maxAge: 3,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
2024-06-25 05:20:58 +00:00
|
|
|
SyntaxError,
|
2020-12-01 13:23:03 +00:00
|
|
|
"RFC2616 cookie 'Space'",
|
|
|
|
);
|
|
|
|
});
|
2022-11-23 13:45:33 +00:00
|
|
|
|
|
|
|
assertThrows(
|
|
|
|
() => {
|
|
|
|
setCookie(headers, {
|
|
|
|
name: "location",
|
|
|
|
value: "United Kingdom",
|
|
|
|
});
|
|
|
|
},
|
2024-06-25 05:20:58 +00:00
|
|
|
SyntaxError,
|
2022-11-23 13:45:33 +00:00
|
|
|
"RFC2616 cookie 'location' cannot contain character ' '",
|
|
|
|
);
|
2020-12-01 13:23:03 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-11-22 14:34:31 +00:00
|
|
|
Deno.test({
|
2024-02-27 20:03:28 +00:00
|
|
|
name: "setCookie() validates path",
|
2022-08-24 01:21:57 +00:00
|
|
|
fn() {
|
2020-11-22 14:34:31 +00:00
|
|
|
const path = "/;domain=sub.domain.com";
|
2021-09-13 06:16:16 +00:00
|
|
|
const headers = new Headers();
|
2020-11-22 14:34:31 +00:00
|
|
|
assertThrows(
|
2022-08-24 01:21:57 +00:00
|
|
|
() => {
|
2021-09-13 06:16:16 +00:00
|
|
|
setCookie(headers, {
|
2020-11-22 14:34:31 +00:00
|
|
|
name: "Space",
|
|
|
|
value: "Cat",
|
|
|
|
httpOnly: true,
|
|
|
|
secure: true,
|
|
|
|
path,
|
|
|
|
maxAge: 3,
|
|
|
|
});
|
|
|
|
},
|
2024-06-25 05:20:58 +00:00
|
|
|
SyntaxError,
|
2024-08-26 05:05:00 +00:00
|
|
|
'Cookie path "/;domain=sub.domain.com" contains invalid character: ";"',
|
2020-11-22 14:34:31 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-07-08 17:19:10 +00:00
|
|
|
Deno.test({
|
2024-02-27 20:03:28 +00:00
|
|
|
name: "setCookie() validates domain",
|
2022-08-24 01:21:57 +00:00
|
|
|
fn() {
|
2021-07-08 17:19:10 +00:00
|
|
|
const tokens = ["-domain.com", "domain.org.", "domain.org-"];
|
2021-09-13 06:16:16 +00:00
|
|
|
const headers = new Headers();
|
2021-07-08 17:19:10 +00:00
|
|
|
tokens.forEach((domain) => {
|
|
|
|
assertThrows(
|
2022-08-24 01:21:57 +00:00
|
|
|
() => {
|
2021-09-13 06:16:16 +00:00
|
|
|
setCookie(headers, {
|
2021-07-08 17:19:10 +00:00
|
|
|
name: "Space",
|
|
|
|
value: "Cat",
|
|
|
|
httpOnly: true,
|
|
|
|
secure: true,
|
|
|
|
domain,
|
|
|
|
maxAge: 3,
|
|
|
|
});
|
|
|
|
},
|
2024-06-25 05:20:58 +00:00
|
|
|
SyntaxError,
|
2021-07-08 17:19:10 +00:00
|
|
|
"Invalid first/last char in cookie domain: " + domain,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-06-12 19:23:38 +00:00
|
|
|
Deno.test({
|
2024-02-27 20:03:28 +00:00
|
|
|
name: "deleteCookie()",
|
2022-08-24 01:21:57 +00:00
|
|
|
fn() {
|
2021-09-13 06:16:16 +00:00
|
|
|
let headers = new Headers();
|
|
|
|
deleteCookie(headers, "deno");
|
2019-04-27 23:07:11 +00:00
|
|
|
assertEquals(
|
2021-09-13 06:16:16 +00:00
|
|
|
headers.get("Set-Cookie"),
|
2020-07-14 19:24:17 +00:00
|
|
|
"deno=; Expires=Thu, 01 Jan 1970 00:00:00 GMT",
|
2019-04-27 23:07:11 +00:00
|
|
|
);
|
2021-09-13 06:16:16 +00:00
|
|
|
headers = new Headers();
|
|
|
|
setCookie(headers, {
|
2021-07-06 18:57:35 +00:00
|
|
|
name: "Space",
|
|
|
|
value: "Cat",
|
|
|
|
domain: "deno.land",
|
|
|
|
path: "/",
|
|
|
|
});
|
2021-09-13 06:16:16 +00:00
|
|
|
deleteCookie(headers, "Space", { domain: "", path: "" });
|
2021-07-06 18:57:35 +00:00
|
|
|
assertEquals(
|
2021-09-13 06:16:16 +00:00
|
|
|
headers.get("Set-Cookie"),
|
2021-07-06 18:57:35 +00:00
|
|
|
"Space=Cat; Domain=deno.land; Path=/, Space=; Expires=Thu, 01 Jan 1970 00:00:00 GMT",
|
|
|
|
);
|
2024-07-09 04:39:25 +00:00
|
|
|
headers = new Headers();
|
|
|
|
deleteCookie(headers, "Space", {
|
|
|
|
domain: "deno.land",
|
|
|
|
path: "/",
|
|
|
|
secure: true,
|
|
|
|
httpOnly: true,
|
|
|
|
partitioned: true,
|
|
|
|
});
|
|
|
|
assertEquals(
|
|
|
|
headers.get("Set-Cookie"),
|
|
|
|
"Space=; Secure; HttpOnly; Partitioned; Domain=deno.land; Path=/; Expires=Thu, 01 Jan 1970 00:00:00 GMT",
|
|
|
|
);
|
2020-03-28 17:03:49 +00:00
|
|
|
},
|
2019-04-27 23:07:11 +00:00
|
|
|
});
|
|
|
|
|
2020-06-12 19:23:38 +00:00
|
|
|
Deno.test({
|
2024-02-27 20:03:28 +00:00
|
|
|
name: "setCookie() handles Set-Cookie",
|
2022-08-24 01:21:57 +00:00
|
|
|
fn() {
|
2021-09-13 06:16:16 +00:00
|
|
|
let headers = new Headers();
|
|
|
|
setCookie(headers, { name: "Space", value: "Cat" });
|
|
|
|
assertEquals(headers.get("Set-Cookie"), "Space=Cat");
|
2019-04-27 23:07:11 +00:00
|
|
|
|
2021-09-13 06:16:16 +00:00
|
|
|
headers = new Headers();
|
|
|
|
setCookie(headers, { name: "Space", value: "Cat", secure: true });
|
|
|
|
assertEquals(headers.get("Set-Cookie"), "Space=Cat; Secure");
|
2019-04-27 23:07:11 +00:00
|
|
|
|
2021-09-13 06:16:16 +00:00
|
|
|
headers = new Headers();
|
|
|
|
setCookie(headers, { name: "Space", value: "Cat", httpOnly: true });
|
|
|
|
assertEquals(headers.get("Set-Cookie"), "Space=Cat; HttpOnly");
|
2019-04-27 23:07:11 +00:00
|
|
|
|
2024-06-15 07:00:46 +00:00
|
|
|
headers = new Headers();
|
|
|
|
setCookie(headers, { name: "Space", value: "Cat", partitioned: true });
|
|
|
|
assertEquals(headers.get("Set-Cookie"), "Space=Cat; Partitioned");
|
|
|
|
|
2021-09-13 06:16:16 +00:00
|
|
|
headers = new Headers();
|
|
|
|
setCookie(headers, {
|
2019-04-27 23:07:11 +00:00
|
|
|
name: "Space",
|
|
|
|
value: "Cat",
|
|
|
|
httpOnly: true,
|
2020-03-28 17:03:49 +00:00
|
|
|
secure: true,
|
2019-04-27 23:07:11 +00:00
|
|
|
});
|
2021-09-13 06:16:16 +00:00
|
|
|
assertEquals(headers.get("Set-Cookie"), "Space=Cat; Secure; HttpOnly");
|
2019-04-27 23:07:11 +00:00
|
|
|
|
2021-09-13 06:16:16 +00:00
|
|
|
headers = new Headers();
|
|
|
|
setCookie(headers, {
|
2019-04-27 23:07:11 +00:00
|
|
|
name: "Space",
|
|
|
|
value: "Cat",
|
|
|
|
httpOnly: true,
|
|
|
|
secure: true,
|
2020-03-28 17:03:49 +00:00
|
|
|
maxAge: 2,
|
2019-04-27 23:07:11 +00:00
|
|
|
});
|
|
|
|
assertEquals(
|
2021-09-13 06:16:16 +00:00
|
|
|
headers.get("Set-Cookie"),
|
2020-07-14 19:24:17 +00:00
|
|
|
"Space=Cat; Secure; HttpOnly; Max-Age=2",
|
2019-04-27 23:07:11 +00:00
|
|
|
);
|
|
|
|
|
2021-09-13 06:16:16 +00:00
|
|
|
headers = new Headers();
|
|
|
|
setCookie(headers, {
|
2021-06-30 18:02:22 +00:00
|
|
|
name: "Space",
|
|
|
|
value: "Cat",
|
|
|
|
httpOnly: true,
|
|
|
|
secure: true,
|
|
|
|
maxAge: 0,
|
|
|
|
});
|
|
|
|
assertEquals(
|
2021-09-13 06:16:16 +00:00
|
|
|
headers.get("Set-Cookie"),
|
2021-06-30 18:02:22 +00:00
|
|
|
"Space=Cat; Secure; HttpOnly; Max-Age=0",
|
|
|
|
);
|
|
|
|
|
2019-04-27 23:07:11 +00:00
|
|
|
let error = false;
|
2021-09-13 06:16:16 +00:00
|
|
|
headers = new Headers();
|
2019-04-27 23:07:11 +00:00
|
|
|
try {
|
2021-09-13 06:16:16 +00:00
|
|
|
setCookie(headers, {
|
2019-04-27 23:07:11 +00:00
|
|
|
name: "Space",
|
|
|
|
value: "Cat",
|
|
|
|
httpOnly: true,
|
|
|
|
secure: true,
|
2021-06-30 18:02:22 +00:00
|
|
|
maxAge: -1,
|
2019-04-27 23:07:11 +00:00
|
|
|
});
|
2021-03-02 06:31:37 +00:00
|
|
|
} catch {
|
2019-04-27 23:07:11 +00:00
|
|
|
error = true;
|
|
|
|
}
|
|
|
|
assert(error);
|
|
|
|
|
2021-09-13 06:16:16 +00:00
|
|
|
headers = new Headers();
|
|
|
|
setCookie(headers, {
|
2019-04-27 23:07:11 +00:00
|
|
|
name: "Space",
|
|
|
|
value: "Cat",
|
|
|
|
httpOnly: true,
|
|
|
|
secure: true,
|
|
|
|
maxAge: 2,
|
2020-03-28 17:03:49 +00:00
|
|
|
domain: "deno.land",
|
2019-04-27 23:07:11 +00:00
|
|
|
});
|
|
|
|
assertEquals(
|
2021-09-13 06:16:16 +00:00
|
|
|
headers.get("Set-Cookie"),
|
2020-07-14 19:24:17 +00:00
|
|
|
"Space=Cat; Secure; HttpOnly; Max-Age=2; Domain=deno.land",
|
2019-04-27 23:07:11 +00:00
|
|
|
);
|
|
|
|
|
2021-09-13 06:16:16 +00:00
|
|
|
headers = new Headers();
|
|
|
|
setCookie(headers, {
|
2019-04-27 23:07:11 +00:00
|
|
|
name: "Space",
|
|
|
|
value: "Cat",
|
|
|
|
httpOnly: true,
|
|
|
|
secure: true,
|
|
|
|
maxAge: 2,
|
|
|
|
domain: "deno.land",
|
2020-03-28 17:03:49 +00:00
|
|
|
sameSite: "Strict",
|
2019-04-27 23:07:11 +00:00
|
|
|
});
|
|
|
|
assertEquals(
|
2021-09-13 06:16:16 +00:00
|
|
|
headers.get("Set-Cookie"),
|
2019-06-19 04:22:01 +00:00
|
|
|
"Space=Cat; Secure; HttpOnly; Max-Age=2; Domain=deno.land; " +
|
2020-07-14 19:24:17 +00:00
|
|
|
"SameSite=Strict",
|
2019-04-27 23:07:11 +00:00
|
|
|
);
|
|
|
|
|
2021-09-13 06:16:16 +00:00
|
|
|
headers = new Headers();
|
|
|
|
setCookie(headers, {
|
2019-04-27 23:07:11 +00:00
|
|
|
name: "Space",
|
|
|
|
value: "Cat",
|
|
|
|
httpOnly: true,
|
|
|
|
secure: true,
|
|
|
|
maxAge: 2,
|
|
|
|
domain: "deno.land",
|
2020-03-28 17:03:49 +00:00
|
|
|
sameSite: "Lax",
|
2019-04-27 23:07:11 +00:00
|
|
|
});
|
|
|
|
assertEquals(
|
2021-09-13 06:16:16 +00:00
|
|
|
headers.get("Set-Cookie"),
|
2020-07-14 19:24:17 +00:00
|
|
|
"Space=Cat; Secure; HttpOnly; Max-Age=2; Domain=deno.land; SameSite=Lax",
|
2019-04-27 23:07:11 +00:00
|
|
|
);
|
|
|
|
|
2021-09-13 06:16:16 +00:00
|
|
|
headers = new Headers();
|
|
|
|
setCookie(headers, {
|
2019-04-27 23:07:11 +00:00
|
|
|
name: "Space",
|
|
|
|
value: "Cat",
|
|
|
|
httpOnly: true,
|
|
|
|
secure: true,
|
|
|
|
maxAge: 2,
|
|
|
|
domain: "deno.land",
|
2020-03-28 17:03:49 +00:00
|
|
|
path: "/",
|
2019-04-27 23:07:11 +00:00
|
|
|
});
|
|
|
|
assertEquals(
|
2021-09-13 06:16:16 +00:00
|
|
|
headers.get("Set-Cookie"),
|
2020-07-14 19:24:17 +00:00
|
|
|
"Space=Cat; Secure; HttpOnly; Max-Age=2; Domain=deno.land; Path=/",
|
2019-04-27 23:07:11 +00:00
|
|
|
);
|
|
|
|
|
2021-09-13 06:16:16 +00:00
|
|
|
headers = new Headers();
|
|
|
|
setCookie(headers, {
|
2019-04-27 23:07:11 +00:00
|
|
|
name: "Space",
|
|
|
|
value: "Cat",
|
|
|
|
httpOnly: true,
|
|
|
|
secure: true,
|
|
|
|
maxAge: 2,
|
|
|
|
domain: "deno.land",
|
|
|
|
path: "/",
|
2020-03-28 17:03:49 +00:00
|
|
|
unparsed: ["unparsed=keyvalue", "batman=Bruce"],
|
2019-04-27 23:07:11 +00:00
|
|
|
});
|
|
|
|
assertEquals(
|
2021-09-13 06:16:16 +00:00
|
|
|
headers.get("Set-Cookie"),
|
2019-06-19 04:22:01 +00:00
|
|
|
"Space=Cat; Secure; HttpOnly; Max-Age=2; Domain=deno.land; Path=/; " +
|
2020-07-14 19:24:17 +00:00
|
|
|
"unparsed=keyvalue; batman=Bruce",
|
2019-04-27 23:07:11 +00:00
|
|
|
);
|
|
|
|
|
2021-09-13 06:16:16 +00:00
|
|
|
headers = new Headers();
|
|
|
|
setCookie(headers, {
|
2019-04-27 23:07:11 +00:00
|
|
|
name: "Space",
|
|
|
|
value: "Cat",
|
|
|
|
httpOnly: true,
|
|
|
|
secure: true,
|
|
|
|
maxAge: 2,
|
|
|
|
domain: "deno.land",
|
|
|
|
path: "/",
|
2020-03-28 17:03:49 +00:00
|
|
|
expires: new Date(Date.UTC(1983, 0, 7, 15, 32)),
|
2019-04-27 23:07:11 +00:00
|
|
|
});
|
|
|
|
assertEquals(
|
2021-09-13 06:16:16 +00:00
|
|
|
headers.get("Set-Cookie"),
|
2019-06-19 04:22:01 +00:00
|
|
|
"Space=Cat; Secure; HttpOnly; Max-Age=2; Domain=deno.land; Path=/; " +
|
2020-07-14 19:24:17 +00:00
|
|
|
"Expires=Fri, 07 Jan 1983 15:32:00 GMT",
|
2019-04-27 23:07:11 +00:00
|
|
|
);
|
|
|
|
|
2022-11-23 16:43:35 +00:00
|
|
|
headers = new Headers();
|
|
|
|
setCookie(headers, {
|
|
|
|
name: "Space",
|
|
|
|
value: "Cat",
|
|
|
|
expires: Date.UTC(1983, 0, 7, 15, 32),
|
|
|
|
});
|
|
|
|
assertEquals(
|
|
|
|
headers.get("Set-Cookie"),
|
|
|
|
"Space=Cat; Expires=Fri, 07 Jan 1983 15:32:00 GMT",
|
|
|
|
);
|
|
|
|
|
2021-09-13 06:16:16 +00:00
|
|
|
headers = new Headers();
|
|
|
|
setCookie(headers, { name: "__Secure-Kitty", value: "Meow" });
|
|
|
|
assertEquals(headers.get("Set-Cookie"), "__Secure-Kitty=Meow; Secure");
|
2019-04-27 23:07:11 +00:00
|
|
|
|
2021-09-13 06:16:16 +00:00
|
|
|
headers = new Headers();
|
|
|
|
setCookie(headers, {
|
2019-04-27 23:07:11 +00:00
|
|
|
name: "__Host-Kitty",
|
|
|
|
value: "Meow",
|
2020-03-28 17:03:49 +00:00
|
|
|
domain: "deno.land",
|
2019-04-27 23:07:11 +00:00
|
|
|
});
|
|
|
|
assertEquals(
|
2021-09-13 06:16:16 +00:00
|
|
|
headers.get("Set-Cookie"),
|
2020-07-14 19:24:17 +00:00
|
|
|
"__Host-Kitty=Meow; Secure; Path=/",
|
2019-04-27 23:07:11 +00:00
|
|
|
);
|
2020-04-01 13:37:11 +00:00
|
|
|
|
2021-09-13 06:16:16 +00:00
|
|
|
headers = new Headers();
|
|
|
|
setCookie(headers, { name: "cookie-1", value: "value-1", secure: true });
|
|
|
|
setCookie(headers, { name: "cookie-2", value: "value-2", maxAge: 3600 });
|
2020-04-01 13:37:11 +00:00
|
|
|
assertEquals(
|
2021-09-13 06:16:16 +00:00
|
|
|
headers.get("Set-Cookie"),
|
2020-07-14 19:24:17 +00:00
|
|
|
"cookie-1=value-1; Secure, cookie-2=value-2; Max-Age=3600",
|
2020-04-01 13:37:11 +00:00
|
|
|
);
|
2020-04-10 14:12:42 +00:00
|
|
|
|
2021-09-13 06:16:16 +00:00
|
|
|
headers = new Headers();
|
|
|
|
setCookie(headers, { name: "", value: "" });
|
|
|
|
assertEquals(headers.get("Set-Cookie"), null);
|
2020-03-28 17:03:49 +00:00
|
|
|
},
|
2019-04-24 11:38:52 +00:00
|
|
|
});
|
2022-08-13 14:49:25 +00:00
|
|
|
|
|
|
|
Deno.test({
|
2024-02-27 20:03:28 +00:00
|
|
|
name: "setCookie() parses Set-Cookie",
|
2022-08-13 14:49:25 +00:00
|
|
|
fn() {
|
|
|
|
let headers = new Headers({ "set-cookie": "Space=Cat" });
|
|
|
|
assertEquals(getSetCookies(headers), [{
|
|
|
|
name: "Space",
|
|
|
|
value: "Cat",
|
|
|
|
}]);
|
|
|
|
|
2023-03-06 07:55:42 +00:00
|
|
|
headers = new Headers({ "set-cookie": "Space=Cat=Happiness" });
|
|
|
|
assertEquals(getSetCookies(headers), [{
|
|
|
|
name: "Space",
|
|
|
|
value: "Cat=Happiness",
|
|
|
|
}]);
|
|
|
|
|
|
|
|
headers = new Headers({ "set-cookie": "Space=Cat= Happiness" });
|
|
|
|
assertEquals(getSetCookies(headers), [{
|
|
|
|
name: "Space",
|
|
|
|
value: "Cat= Happiness",
|
|
|
|
}]);
|
|
|
|
|
|
|
|
headers = new Headers({ "set-cookie": "Space=Cat = Happiness; Secure" });
|
|
|
|
assertEquals(getSetCookies(headers), [{
|
|
|
|
name: "Space",
|
|
|
|
value: "Cat = Happiness",
|
|
|
|
secure: true,
|
|
|
|
}]);
|
|
|
|
|
|
|
|
headers = new Headers({ "set-cookie": " Space=Cat = Happiness ; Secure" });
|
|
|
|
assertEquals(getSetCookies(headers), [{
|
|
|
|
name: "Space",
|
|
|
|
value: "Cat = Happiness",
|
|
|
|
secure: true,
|
|
|
|
}]);
|
|
|
|
|
2022-08-13 14:49:25 +00:00
|
|
|
headers = new Headers({ "set-cookie": "Space=Cat; Secure" });
|
|
|
|
assertEquals(getSetCookies(headers), [{
|
|
|
|
name: "Space",
|
|
|
|
value: "Cat",
|
|
|
|
secure: true,
|
|
|
|
}]);
|
|
|
|
|
|
|
|
headers = new Headers({ "set-cookie": "Space=Cat; HttpOnly" });
|
|
|
|
assertEquals(getSetCookies(headers), [{
|
|
|
|
name: "Space",
|
|
|
|
value: "Cat",
|
|
|
|
httpOnly: true,
|
|
|
|
}]);
|
|
|
|
|
|
|
|
headers = new Headers({ "set-cookie": "Space=Cat; Secure; HttpOnly" });
|
|
|
|
assertEquals(getSetCookies(headers), [{
|
|
|
|
name: "Space",
|
|
|
|
value: "Cat",
|
|
|
|
secure: true,
|
|
|
|
httpOnly: true,
|
|
|
|
}]);
|
|
|
|
|
|
|
|
headers = new Headers({
|
|
|
|
"set-cookie": "Space=Cat; Secure; HttpOnly; Max-Age=2",
|
|
|
|
});
|
|
|
|
assertEquals(getSetCookies(headers), [{
|
|
|
|
name: "Space",
|
|
|
|
value: "Cat",
|
|
|
|
secure: true,
|
|
|
|
httpOnly: true,
|
|
|
|
maxAge: 2,
|
|
|
|
}]);
|
|
|
|
|
|
|
|
headers = new Headers({
|
|
|
|
"set-cookie": "Space=Cat; Secure; HttpOnly; Max-Age=0",
|
|
|
|
});
|
|
|
|
assertEquals(getSetCookies(headers), [{
|
|
|
|
name: "Space",
|
|
|
|
value: "Cat",
|
|
|
|
secure: true,
|
|
|
|
httpOnly: true,
|
|
|
|
maxAge: 0,
|
|
|
|
}]);
|
|
|
|
|
|
|
|
headers = new Headers({
|
|
|
|
"set-cookie": "Space=Cat; Secure; HttpOnly; Max-Age=-1",
|
|
|
|
});
|
|
|
|
assertEquals(getSetCookies(headers), []);
|
|
|
|
|
|
|
|
headers = new Headers({
|
|
|
|
"set-cookie": "Space=Cat; Secure; HttpOnly; Max-Age=2; Domain=deno.land",
|
|
|
|
});
|
|
|
|
assertEquals(getSetCookies(headers), [{
|
|
|
|
name: "Space",
|
|
|
|
value: "Cat",
|
|
|
|
secure: true,
|
|
|
|
httpOnly: true,
|
|
|
|
maxAge: 2,
|
|
|
|
domain: "deno.land",
|
|
|
|
}]);
|
|
|
|
|
|
|
|
headers = new Headers({
|
|
|
|
"set-cookie":
|
|
|
|
"Space=Cat; Secure; HttpOnly; Max-Age=2; Domain=deno.land; SameSite=Strict",
|
|
|
|
});
|
|
|
|
assertEquals(getSetCookies(headers), [{
|
|
|
|
name: "Space",
|
|
|
|
value: "Cat",
|
|
|
|
secure: true,
|
|
|
|
httpOnly: true,
|
|
|
|
maxAge: 2,
|
|
|
|
domain: "deno.land",
|
|
|
|
sameSite: "Strict",
|
|
|
|
}]);
|
|
|
|
|
|
|
|
headers = new Headers({
|
|
|
|
"set-cookie":
|
|
|
|
"Space=Cat; Secure; HttpOnly; Max-Age=2; Domain=deno.land; SameSite=Lax",
|
|
|
|
});
|
|
|
|
assertEquals(getSetCookies(headers), [{
|
|
|
|
name: "Space",
|
|
|
|
value: "Cat",
|
|
|
|
secure: true,
|
|
|
|
httpOnly: true,
|
|
|
|
maxAge: 2,
|
|
|
|
domain: "deno.land",
|
|
|
|
sameSite: "Lax",
|
|
|
|
}]);
|
|
|
|
|
|
|
|
headers = new Headers({
|
|
|
|
"set-cookie":
|
|
|
|
"Space=Cat; Secure; HttpOnly; Max-Age=2; Domain=deno.land; Path=/",
|
|
|
|
});
|
|
|
|
assertEquals(getSetCookies(headers), [{
|
|
|
|
name: "Space",
|
|
|
|
value: "Cat",
|
|
|
|
secure: true,
|
|
|
|
httpOnly: true,
|
|
|
|
maxAge: 2,
|
|
|
|
domain: "deno.land",
|
|
|
|
path: "/",
|
|
|
|
}]);
|
|
|
|
|
|
|
|
headers = new Headers({
|
|
|
|
"set-cookie":
|
|
|
|
"Space=Cat; Secure; HttpOnly; Max-Age=2; Domain=deno.land; Path=/; unparsed=keyvalue; batman=Bruce",
|
|
|
|
});
|
|
|
|
assertEquals(getSetCookies(headers), [{
|
|
|
|
name: "Space",
|
|
|
|
value: "Cat",
|
|
|
|
secure: true,
|
|
|
|
httpOnly: true,
|
|
|
|
maxAge: 2,
|
|
|
|
domain: "deno.land",
|
|
|
|
path: "/",
|
|
|
|
unparsed: ["unparsed=keyvalue", "batman=Bruce"],
|
|
|
|
}]);
|
|
|
|
|
|
|
|
headers = new Headers({
|
|
|
|
"set-cookie":
|
|
|
|
"Space=Cat; Secure; HttpOnly; Max-Age=2; Domain=deno.land; Path=/; " +
|
|
|
|
"Expires=Fri, 07 Jan 1983 15:32:00 GMT",
|
|
|
|
});
|
|
|
|
assertEquals(getSetCookies(headers), [{
|
|
|
|
name: "Space",
|
|
|
|
value: "Cat",
|
|
|
|
secure: true,
|
|
|
|
httpOnly: true,
|
|
|
|
maxAge: 2,
|
|
|
|
domain: "deno.land",
|
|
|
|
path: "/",
|
|
|
|
expires: new Date(Date.UTC(1983, 0, 7, 15, 32)),
|
|
|
|
}]);
|
|
|
|
|
|
|
|
headers = new Headers({ "set-cookie": "__Secure-Kitty=Meow; Secure" });
|
|
|
|
assertEquals(getSetCookies(headers), [{
|
|
|
|
name: "__Secure-Kitty",
|
|
|
|
value: "Meow",
|
|
|
|
secure: true,
|
|
|
|
}]);
|
|
|
|
|
|
|
|
headers = new Headers({ "set-cookie": "__Secure-Kitty=Meow" });
|
|
|
|
assertEquals(getSetCookies(headers), []);
|
|
|
|
|
|
|
|
headers = new Headers({
|
|
|
|
"set-cookie": "__Host-Kitty=Meow; Secure; Path=/",
|
|
|
|
});
|
|
|
|
assertEquals(getSetCookies(headers), [{
|
|
|
|
name: "__Host-Kitty",
|
|
|
|
value: "Meow",
|
|
|
|
secure: true,
|
|
|
|
path: "/",
|
|
|
|
}]);
|
|
|
|
|
|
|
|
headers = new Headers({ "set-cookie": "__Host-Kitty=Meow; Path=/" });
|
|
|
|
assertEquals(getSetCookies(headers), []);
|
|
|
|
|
|
|
|
headers = new Headers({
|
|
|
|
"set-cookie": "__Host-Kitty=Meow; Secure; Domain=deno.land; Path=/",
|
|
|
|
});
|
|
|
|
assertEquals(getSetCookies(headers), []);
|
|
|
|
|
|
|
|
headers = new Headers({
|
|
|
|
"set-cookie": "__Host-Kitty=Meow; Secure; Path=/not-root",
|
|
|
|
});
|
|
|
|
assertEquals(getSetCookies(headers), []);
|
|
|
|
|
|
|
|
headers = new Headers([
|
|
|
|
["set-cookie", "cookie-1=value-1; Secure"],
|
|
|
|
["set-cookie", "cookie-2=value-2; Max-Age=3600"],
|
|
|
|
]);
|
|
|
|
assertEquals(getSetCookies(headers), [
|
|
|
|
{ name: "cookie-1", value: "value-1", secure: true },
|
|
|
|
{ name: "cookie-2", value: "value-2", maxAge: 3600 },
|
|
|
|
]);
|
|
|
|
|
|
|
|
headers = new Headers();
|
|
|
|
assertEquals(getSetCookies(headers), []);
|
|
|
|
},
|
|
|
|
});
|
2024-09-20 08:28:06 +00:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "getSetCookies() is locale independent",
|
|
|
|
fn() {
|
|
|
|
const setCookie = "a=b; EXPIRES=Thu, 19 Sep 2024 07:47:28 GMT";
|
|
|
|
const headers = new Headers({ "set-cookie": setCookie });
|
|
|
|
const expected = [{
|
|
|
|
"name": "a",
|
|
|
|
"value": "b",
|
|
|
|
"expires": new Date("2024-09-19T07:47:28.000Z"),
|
|
|
|
}];
|
|
|
|
|
|
|
|
assertEquals(getSetCookies(headers), expected);
|
|
|
|
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Use of locale-sensitive methods with undefined locale may cause
|
|
|
|
* environment-sensitive bugs -
|
|
|
|
* [issue](https://github.com/denoland/std/issues/6016)
|
|
|
|
*/
|
|
|
|
const toLocaleLowerCase = String.prototype.toLocaleLowerCase;
|
|
|
|
using _ = stub(
|
|
|
|
String.prototype,
|
|
|
|
"toLocaleLowerCase",
|
|
|
|
function (locale) {
|
|
|
|
return toLocaleLowerCase.call(this, locale ?? "tr-TR");
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
assertEquals(getSetCookies(headers), expected);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|