fix: setCookie with maxAge of 0 (#992)

This commit is contained in:
Simon Rask 2021-06-30 20:02:22 +02:00 committed by GitHub
parent 3f3a1a74ba
commit f3828fa852
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 3 deletions

View File

@ -11,7 +11,7 @@ export interface Cookie {
value: string;
/** Expiration date of the cookie. */
expires?: Date;
/** Max-Age of the Cookie. Must be integer superior to 0. */
/** Max-Age of the Cookie. Max-Age must be an integer superior or equal to 0. */
maxAge?: number;
/** Specifies those hosts to which the cookie will be sent. */
domain?: string;
@ -57,7 +57,10 @@ function toString(cookie: Cookie): string {
out.push("HttpOnly");
}
if (typeof cookie.maxAge === "number" && Number.isInteger(cookie.maxAge)) {
assert(cookie.maxAge > 0, "Max-Age must be an integer superior to 0");
assert(
cookie.maxAge >= 0,
"Max-Age must be an integer superior or equal to 0",
);
out.push(`Max-Age=${cookie.maxAge}`);
}
if (cookie.domain) {

View File

@ -177,6 +177,19 @@ Deno.test({
"Space=Cat; Secure; HttpOnly; Max-Age=2",
);
res.headers = new Headers();
setCookie(res, {
name: "Space",
value: "Cat",
httpOnly: true,
secure: true,
maxAge: 0,
});
assertEquals(
res.headers.get("Set-Cookie"),
"Space=Cat; Secure; HttpOnly; Max-Age=0",
);
let error = false;
res.headers = new Headers();
try {
@ -185,7 +198,7 @@ Deno.test({
value: "Cat",
httpOnly: true,
secure: true,
maxAge: 0,
maxAge: -1,
});
} catch {
error = true;