Skip to content

Commit 1522c6f

Browse files
committed
fix(destroy): handle previously set cookies
before this commit, when using destroy(), previously set cookies would be ignored fixes #229
1 parent cc16bb8 commit 1522c6f

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

lib/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ export async function applySession(
7777
...cookieOptions,
7878
maxAge: 0,
7979
});
80-
res.setHeader("set-cookie", [cookieValue]);
80+
const existingSetCookie = [res.getHeader("set-cookie") || []].flat();
81+
res.setHeader("set-cookie", [...existingSetCookie, cookieValue]);
8182
},
8283
};
8384

lib/index.test.js

+34-1
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ test("req.session.destroy", () => {
276276
},
277277
{
278278
setHeader: jest.fn(),
279+
getHeader: jest.fn(),
279280
},
280281
);
281282
});
@@ -647,7 +648,7 @@ test("it handles previously set cookies (single value)", () => {
647648
});
648649
});
649650

650-
test("it handles previously set cookies (multiple values)", () => {
651+
test("it handles previously set cookies (multiple values) on save()", () => {
651652
return new Promise((done) => {
652653
const handler = async (req, res) => {
653654
await req.session.save();
@@ -672,3 +673,35 @@ test("it handles previously set cookies (multiple values)", () => {
672673
);
673674
});
674675
});
676+
677+
test("it handles previously set cookies (multiple values) on destroy()", () => {
678+
return new Promise((done) => {
679+
const handler = async (req, res) => {
680+
await req.session.destroy();
681+
682+
expect(res.setHeader.mock.calls[0]).toMatchInlineSnapshot(`
683+
Array [
684+
"set-cookie",
685+
Array [
686+
"existingCookie=value",
687+
"anotherCookie=value2",
688+
"test=; Max-Age=0; Path=/; HttpOnly; Secure; SameSite=Lax",
689+
],
690+
]
691+
`);
692+
done();
693+
};
694+
const wrappedHandler = withIronSession(handler, { password, cookieName });
695+
wrappedHandler(
696+
{
697+
headers: { cookie: "" },
698+
},
699+
{
700+
setHeader: jest.fn(),
701+
getHeader: function () {
702+
return ["existingCookie=value", "anotherCookie=value2"];
703+
},
704+
},
705+
);
706+
});
707+
});

0 commit comments

Comments
 (0)