diff --git a/lib/index.js b/lib/index.js index 7b6fb93b..ec731881 100644 --- a/lib/index.js +++ b/lib/index.js @@ -77,7 +77,8 @@ export async function applySession( ...cookieOptions, maxAge: 0, }); - res.setHeader("set-cookie", [cookieValue]); + const existingSetCookie = [res.getHeader("set-cookie") || []].flat(); + res.setHeader("set-cookie", [...existingSetCookie, cookieValue]); }, }; diff --git a/lib/index.test.js b/lib/index.test.js index 8a811472..cc35f5ab 100644 --- a/lib/index.test.js +++ b/lib/index.test.js @@ -276,6 +276,7 @@ test("req.session.destroy", () => { }, { setHeader: jest.fn(), + getHeader: jest.fn(), }, ); }); @@ -647,7 +648,7 @@ test("it handles previously set cookies (single value)", () => { }); }); -test("it handles previously set cookies (multiple values)", () => { +test("it handles previously set cookies (multiple values) on save()", () => { return new Promise((done) => { const handler = async (req, res) => { await req.session.save(); @@ -672,3 +673,35 @@ test("it handles previously set cookies (multiple values)", () => { ); }); }); + +test("it handles previously set cookies (multiple values) on destroy()", () => { + return new Promise((done) => { + const handler = async (req, res) => { + await req.session.destroy(); + + expect(res.setHeader.mock.calls[0]).toMatchInlineSnapshot(` + Array [ + "set-cookie", + Array [ + "existingCookie=value", + "anotherCookie=value2", + "test=; Max-Age=0; Path=/; HttpOnly; Secure; SameSite=Lax", + ], + ] + `); + done(); + }; + const wrappedHandler = withIronSession(handler, { password, cookieName }); + wrappedHandler( + { + headers: { cookie: "" }, + }, + { + setHeader: jest.fn(), + getHeader: function () { + return ["existingCookie=value", "anotherCookie=value2"]; + }, + }, + ); + }); +});