Skip to content

Commit

Permalink
fix: improve conversation of azure cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdiez committed Sep 23, 2023
1 parent 1ccf0d1 commit 1d5b9e0
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/runtime/utils.azure.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
import type { Cookie } from "@azure/functions";
import { parse } from "cookie-es";
import { splitCookiesString } from "h3";
import { joinHeaders } from "./utils";
import { satisfies } from "semver";

export function getAzureParsedCookiesFromHeaders(
headers: Record<string, number | string | string[] | undefined>
) {
const c = String(headers["set-cookie"]);
if (!c || c.length === 0) {
): Cookie[] {
const raw = headers["set-cookie"];
if (!raw || typeof raw === "number" || raw.length === 0) {
return [];
}
const cookies = splitCookiesString(joinHeaders(c)).map((cookie) =>
parse(cookie)
);
return cookies as unknown as Cookie[];
const rawCookies = Array.isArray(raw) ? raw : splitCookiesString(String(raw));
const cookies = rawCookies.flatMap((cookie) => {
const entries = Object.entries(parse(cookie));
console.log(parse(cookie), cookie);
if (entries.length > 0) {
const [entry, ...rest] = entries;
return [
{
name: entry[0],
value: entry[1],
...Object.fromEntries(
rest.map(([key, value]) => [key.toLowerCase(), value])
),
},
];
}
return [];
});
return cookies;
}
89 changes: 89 additions & 0 deletions test/unit/azure.utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { describe, it, expect } from "vitest";
import { getAzureParsedCookiesFromHeaders } from "../../src/runtime/utils.azure";

describe("getAzureParsedCookiesFromHeaders", () => {
it("returns empty array if no cookies", () => {
expect(getAzureParsedCookiesFromHeaders({})).toEqual([]);
});
it("returns empty array if no set-cookie header", () => {
expect(
getAzureParsedCookiesFromHeaders({ "set-cookie": undefined })
).toEqual([]);
});
it("returns empty array if empty set-cookie header", () => {
expect(getAzureParsedCookiesFromHeaders({ "set-cookie": " " })).toEqual([]);
});
it("returns single cookie", () => {
expect(
getAzureParsedCookiesFromHeaders({ "set-cookie": "foo=bar" })
).toEqual([
{
name: "foo",
value: "bar",
},
]);
});
it('returns cookie with "expires" attribute', () => {
expect(
getAzureParsedCookiesFromHeaders({
"set-cookie": "foo=bar; expires=Thu, 01 Jan 1970 00:00:00 GMT",
})
).toEqual([
{
name: "foo",
value: "bar",
expires: "Thu, 01 Jan 1970 00:00:00 GMT",
},
]);
});
it("returns a complex cookie", () => {
expect(
getAzureParsedCookiesFromHeaders({
"set-cookie": [
"session=xyz; Path=/; Expires=Sun, 24 Mar 2024 09:13:27 GMT; HttpOnly; SameSite=Strict",
],
})
).toEqual([
{
name: "session",
value: "xyz",
expires: "Sun, 24 Mar 2024 09:13:27 GMT",
path: "/",
// TODO: httponly: true,
samesite: "Strict",
},
]);
});
it("returns multiple cookies", () => {
expect(
getAzureParsedCookiesFromHeaders({
"set-cookie": ["foo=bar", "baz=qux"],
})
).toEqual([
{
name: "foo",
value: "bar",
},
{
name: "baz",
value: "qux",
},
]);
});
it("returns multiple cookies given as string", () => {
expect(
getAzureParsedCookiesFromHeaders({
"set-cookie": "foo=bar, baz=qux",
})
).toEqual([
{
name: "foo",
value: "bar",
},
{
name: "baz",
value: "qux",
},
]);
});
});

0 comments on commit 1d5b9e0

Please sign in to comment.