Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDK-4318] Enable responses from custom middleware #1265

Merged
merged 1 commit into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/helpers/with-middleware-auth-required.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ export default function withMiddlewareAuthRequiredFactory(
const res = await (middleware && middleware(...args));

if (res) {
const headers = new Headers(res.headers);
const cookies = headers.get('set-cookie')?.split(', ') || [];
const authCookies = authRes.headers.get('set-cookie')?.split(', ') || [];
if (cookies.length || authCookies.length) {
headers.set('set-cookie', [...authCookies, ...cookies].join(', '));
const nextRes = new NextResponse(res.body, res);
for (const cookie of authRes.cookies.getAll()) {
if (!nextRes.cookies.get(cookie.name)) {
nextRes.cookies.set(cookie);
}
}
return NextResponse.next({ ...res, status: res.status, headers });
return nextRes;
} else {
return authRes;
}
Expand Down
39 changes: 38 additions & 1 deletion tests/helpers/with-middleware-auth-required.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,44 @@ describe('with-middleware-auth-required', () => {
};
const res = await setup({ user: { name: 'dave' }, middleware });
expect(res.status).toEqual(200);
expect(res.headers.get('set-cookie')).toMatch(/^appSession=.+, foo=bar;/);
// @ts-expect-errors ts dom doesn't have getAll
expect(res.headers.getAll('set-cookie')).toHaveLength(2);
expect(res.headers.get('set-cookie')).toMatch(/appSession=/);
expect(res.headers.get('set-cookie')).toMatch(/foo=bar;/);
});

test('should allow responses from custom middleware', async () => {
const middleware = () => {
return NextResponse.json({ foo: 'bar' });
};
const res = await setup({ user: { name: 'dave' }, middleware });
expect(res.status).toEqual(200);
await expect(res.json()).resolves.toEqual({ foo: 'bar' });
});

test('should allow ReadableStream responses from custom middleware', async () => {
const middleware = () => {
// @ts-expect-errors ts dom doesn't have json
return new Response(Response.json({ foo: 'bar' }).body);
};
const res = await setup({ user: { name: 'dave' }, middleware });
expect(res.status).toEqual(200);
await expect(res.json()).resolves.toEqual({ foo: 'bar' });
});

test('should allow responses and cookies from custom middleware', async () => {
const middleware = () => {
const res = NextResponse.json({ foo: 'bar' });
res.cookies.set('foo', 'bar');
return res;
};
const res = await setup({ user: { name: 'dave' }, middleware });
expect(res.status).toEqual(200);
await expect(res.json()).resolves.toEqual({ foo: 'bar' });
// @ts-expect-errors ts dom doesn't have getAll
expect(res.headers.getAll('set-cookie')).toHaveLength(2);
expect(res.headers.get('set-cookie')).toMatch(/appSession=/);
expect(res.headers.get('set-cookie')).toMatch(/foo=bar;/);
});

test('should set just a custom cookie when session is not rolling', async () => {
Expand Down