Skip to content

Commit

Permalink
fix(destroy): handle previously set cookies
Browse files Browse the repository at this point in the history
before this commit, when using destroy(), previously set cookies would be
ignored

fixes #229
  • Loading branch information
vvo committed Oct 1, 2020
1 parent cc16bb8 commit 1522c6f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
},
};

Expand Down
35 changes: 34 additions & 1 deletion lib/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ test("req.session.destroy", () => {
},
{
setHeader: jest.fn(),
getHeader: jest.fn(),
},
);
});
Expand Down Expand Up @@ -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();
Expand All @@ -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"];
},
},
);
});
});

1 comment on commit 1522c6f

@vercel
Copy link

@vercel vercel bot commented on 1522c6f Oct 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.