diff --git a/lib/set-cookie.d.ts b/lib/set-cookie.d.ts index 1342043..b06a875 100644 --- a/lib/set-cookie.d.ts +++ b/lib/set-cookie.d.ts @@ -61,6 +61,7 @@ export interface Options { type ResponseLike = { headers: | { getSetCookie(): string[] } + | { get(name: string): string | null } | { "set-cookie"?: string | readonly string[] } | Record; }; diff --git a/lib/set-cookie.js b/lib/set-cookie.js index 39480a2..6fdf166 100644 --- a/lib/set-cookie.js +++ b/lib/set-cookie.js @@ -155,6 +155,8 @@ function parseSetCookie(input, options) { // for fetch responses - they combine headers of the same type in the headers array, // but getSetCookie returns an uncombined array input = input.headers.getSetCookie(); + } else if (typeof input.headers.get === "function") { + input = input.headers.get("set-cookie"); } else if (input.headers["set-cookie"]) { // fast-path for node.js (which automatically normalizes header names to lower-case) input = input.headers["set-cookie"]; diff --git a/test-d/set-cookie.test-d.ts b/test-d/set-cookie.test-d.ts index d0a0db9..6321297 100644 --- a/test-d/set-cookie.test-d.ts +++ b/test-d/set-cookie.test-d.ts @@ -95,3 +95,8 @@ setCookieParser.parseSetCookie("foo=bar"); setCookieParser.parse("foo=bar"); setCookieParser.parseString("foo=bar"); setCookieParser.splitCookiesString("foo=bar"); + +// Headers implementations that predate getSetCookie expose only get(). +parseSetCookie({ + headers: { get: (_name: string): string | null => "foo=bar" }, +}); diff --git a/test/fetch.js b/test/fetch.js index 43961cf..199ed9a 100644 --- a/test/fetch.js +++ b/test/fetch.js @@ -24,4 +24,45 @@ describe("fetch", () => { var expected = [{ name: "foo", value: "bar" }]; assert.deepEqual(actual, expected); }); + + describe("Headers without getSetCookie", () => { + function legacyResponse(value) { + const response = new Response(null, { + headers: value === null ? {} : { "set-cookie": value }, + }); + // Older fetch implementations expose get(), but not getSetCookie(). + Object.defineProperty(response.headers, "getSetCookie", { + value: undefined, + }); + return response; + } + + it("should parse a combined header without splitting the Expires date", () => { + const response = legacyResponse( + "foo=bar; Expires=Tue, 01 Jul 2025 10:01:11 GMT, baz=qux; Secure" + ); + assert.deepEqual(setCookie.parse(response), [ + { + name: "foo", + value: "bar", + expires: new Date("2025-07-01T10:01:11Z"), + }, + { name: "baz", value: "qux", secure: true }, + ]); + }); + + it("should honor map and decoding options", () => { + const response = legacyResponse("foo=hello%20world"); + assert.deepEqual( + setCookie.parse(response, { map: true, decodeValues: false }), + { + foo: { name: "foo", value: "hello%20world" }, + } + ); + }); + + it("should return an empty array when the header is absent", () => { + assert.deepEqual(setCookie.parse(legacyResponse(null)), []); + }); + }); });