Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback
Module

std/http/cookie_test.ts

Deno standard library
Go to Latest
File
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.import { deleteCookie, getCookies, getSetCookies, setCookie,} from "./cookie.ts";import { assert, assertEquals, assertThrows } from "../testing/asserts.ts";
Deno.test({ name: "Cookie parser", fn() { let headers = new Headers(); assertEquals(getCookies(headers), {}); headers = new Headers(); headers.set("Cookie", "foo=bar"); assertEquals(getCookies(headers), { foo: "bar" });
headers = new Headers(); headers.set("Cookie", "full=of ; tasty=chocolate"); assertEquals(getCookies(headers), { full: "of ", tasty: "chocolate" });
headers = new Headers(); headers.set("Cookie", "igot=99; problems=but..."); assertEquals(getCookies(headers), { igot: "99", problems: "but..." });
headers = new Headers(); headers.set("Cookie", "PREF=al=en-GB&f1=123; wide=1; SID=123"); assertEquals(getCookies(headers), { PREF: "al=en-GB&f1=123", wide: "1", SID: "123", }); },});
Deno.test({ name: "Cookie Name Validation", fn() { const tokens = [ '"id"', "id\t", "i\td", "i d", "i;d", "{id}", "[id]", '"', "id\u0091", ]; const headers = new Headers(); tokens.forEach((name) => { assertThrows( () => { setCookie(headers, { name, value: "Cat", httpOnly: true, secure: true, maxAge: 3, }); }, Error, 'Invalid cookie name: "' + name + '".', ); }); },});
Deno.test({ name: "Cookie Value Validation", fn() { const tokens = [ "1f\tWa", "\t", "1f Wa", "1f;Wa", '"1fWa', "1f\\Wa", '1f"Wa', '"', "1fWa\u0005", "1f\u0091Wa", ]; const headers = new Headers(); tokens.forEach((value) => { assertThrows( () => { setCookie( headers, { name: "Space", value, httpOnly: true, secure: true, maxAge: 3, }, ); }, Error, "RFC2616 cookie 'Space'", ); });
assertThrows( () => { setCookie(headers, { name: "location", value: "United Kingdom", }); }, Error, "RFC2616 cookie 'location' cannot contain character ' '", ); },});
Deno.test({ name: "Cookie Path Validation", fn() { const path = "/;domain=sub.domain.com"; const headers = new Headers(); assertThrows( () => { setCookie(headers, { name: "Space", value: "Cat", httpOnly: true, secure: true, path, maxAge: 3, }); }, Error, path + ": Invalid cookie path char ';'", ); },});
Deno.test({ name: "Cookie Domain Validation", fn() { const tokens = ["-domain.com", "domain.org.", "domain.org-"]; const headers = new Headers(); tokens.forEach((domain) => { assertThrows( () => { setCookie(headers, { name: "Space", value: "Cat", httpOnly: true, secure: true, domain, maxAge: 3, }); }, Error, "Invalid first/last char in cookie domain: " + domain, ); }); },});
Deno.test({ name: "Cookie Delete", fn() { let headers = new Headers(); deleteCookie(headers, "deno"); assertEquals( headers.get("Set-Cookie"), "deno=; Expires=Thu, 01 Jan 1970 00:00:00 GMT", ); headers = new Headers(); setCookie(headers, { name: "Space", value: "Cat", domain: "deno.land", path: "/", }); deleteCookie(headers, "Space", { domain: "", path: "" }); assertEquals( headers.get("Set-Cookie"), "Space=Cat; Domain=deno.land; Path=/, Space=; Expires=Thu, 01 Jan 1970 00:00:00 GMT", ); },});
Deno.test({ name: "Cookie Set", fn() { let headers = new Headers(); setCookie(headers, { name: "Space", value: "Cat" }); assertEquals(headers.get("Set-Cookie"), "Space=Cat");
headers = new Headers(); setCookie(headers, { name: "Space", value: "Cat", secure: true }); assertEquals(headers.get("Set-Cookie"), "Space=Cat; Secure");
headers = new Headers(); setCookie(headers, { name: "Space", value: "Cat", httpOnly: true }); assertEquals(headers.get("Set-Cookie"), "Space=Cat; HttpOnly");
headers = new Headers(); setCookie(headers, { name: "Space", value: "Cat", httpOnly: true, secure: true, }); assertEquals(headers.get("Set-Cookie"), "Space=Cat; Secure; HttpOnly");
headers = new Headers(); setCookie(headers, { name: "Space", value: "Cat", httpOnly: true, secure: true, maxAge: 2, }); assertEquals( headers.get("Set-Cookie"), "Space=Cat; Secure; HttpOnly; Max-Age=2", );
headers = new Headers(); setCookie(headers, { name: "Space", value: "Cat", httpOnly: true, secure: true, maxAge: 0, }); assertEquals( headers.get("Set-Cookie"), "Space=Cat; Secure; HttpOnly; Max-Age=0", );
let error = false; headers = new Headers(); try { setCookie(headers, { name: "Space", value: "Cat", httpOnly: true, secure: true, maxAge: -1, }); } catch { error = true; } assert(error);
headers = new Headers(); setCookie(headers, { name: "Space", value: "Cat", httpOnly: true, secure: true, maxAge: 2, domain: "deno.land", }); assertEquals( headers.get("Set-Cookie"), "Space=Cat; Secure; HttpOnly; Max-Age=2; Domain=deno.land", );
headers = new Headers(); setCookie(headers, { name: "Space", value: "Cat", httpOnly: true, secure: true, maxAge: 2, domain: "deno.land", sameSite: "Strict", }); assertEquals( headers.get("Set-Cookie"), "Space=Cat; Secure; HttpOnly; Max-Age=2; Domain=deno.land; " + "SameSite=Strict", );
headers = new Headers(); setCookie(headers, { name: "Space", value: "Cat", httpOnly: true, secure: true, maxAge: 2, domain: "deno.land", sameSite: "Lax", }); assertEquals( headers.get("Set-Cookie"), "Space=Cat; Secure; HttpOnly; Max-Age=2; Domain=deno.land; SameSite=Lax", );
headers = new Headers(); setCookie(headers, { name: "Space", value: "Cat", httpOnly: true, secure: true, maxAge: 2, domain: "deno.land", path: "/", }); assertEquals( headers.get("Set-Cookie"), "Space=Cat; Secure; HttpOnly; Max-Age=2; Domain=deno.land; Path=/", );
headers = new Headers(); setCookie(headers, { name: "Space", value: "Cat", httpOnly: true, secure: true, maxAge: 2, domain: "deno.land", path: "/", unparsed: ["unparsed=keyvalue", "batman=Bruce"], }); assertEquals( headers.get("Set-Cookie"), "Space=Cat; Secure; HttpOnly; Max-Age=2; Domain=deno.land; Path=/; " + "unparsed=keyvalue; batman=Bruce", );
headers = new Headers(); setCookie(headers, { name: "Space", value: "Cat", httpOnly: true, secure: true, maxAge: 2, domain: "deno.land", path: "/", expires: new Date(Date.UTC(1983, 0, 7, 15, 32)), }); assertEquals( headers.get("Set-Cookie"), "Space=Cat; Secure; HttpOnly; Max-Age=2; Domain=deno.land; Path=/; " + "Expires=Fri, 07 Jan 1983 15:32:00 GMT", );
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", );
headers = new Headers(); setCookie(headers, { name: "__Secure-Kitty", value: "Meow" }); assertEquals(headers.get("Set-Cookie"), "__Secure-Kitty=Meow; Secure");
headers = new Headers(); setCookie(headers, { name: "__Host-Kitty", value: "Meow", domain: "deno.land", }); assertEquals( headers.get("Set-Cookie"), "__Host-Kitty=Meow; Secure; Path=/", );
headers = new Headers(); setCookie(headers, { name: "cookie-1", value: "value-1", secure: true }); setCookie(headers, { name: "cookie-2", value: "value-2", maxAge: 3600 }); assertEquals( headers.get("Set-Cookie"), "cookie-1=value-1; Secure, cookie-2=value-2; Max-Age=3600", );
headers = new Headers(); setCookie(headers, { name: "", value: "" }); assertEquals(headers.get("Set-Cookie"), null); },});
Deno.test({ name: "Set-Cookie parser", fn() { let headers = new Headers({ "set-cookie": "Space=Cat" }); assertEquals(getSetCookies(headers), [{ name: "Space", value: "Cat", }]);
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, }]);
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), []); },});