-
Notifications
You must be signed in to change notification settings - Fork 541
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve conversation of azure cookies
- Loading branch information
1 parent
1ccf0d1
commit 1d5b9e0
Showing
2 changed files
with
112 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}, | ||
]); | ||
}); | ||
}); |