Status always 200 on redirect. #764
-
ProblemI think the title is self-explanatory. Status appears to be 200 in any case. Reproduction stepsJest example:import { testApiHandler } from 'next-test-api-route-handler';
test('redirect', async () =>
{
await testApiHandler(
{
handler: (req, res) => res.redirect(307, 'https://example.com'),
test: async ({ fetch }) =>
{
const res = await fetch();
// this fails, status is 200
expect(res.status).toBe(307);
},
},
);
}); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
@adrian-kriegel The fetch spec dictates that redirects should reuse the original method and body to query the new resource and return the response as-is. For example, the following (adapted from your reproduction steps) will return status 404: import { testApiHandler } from 'next-test-api-route-handler';
test('redirect', async () =>
{
await testApiHandler(
{
handler: (req, res) => res.redirect(307, 'https://google.com/404'),
test: async ({ fetch }) =>
{
const res = await fetch();
// this succeeds
expect(res.status).toBe(404);
},
},
);
}); If you visit https://google.com/404 in your browser, you'll also see that you'll get a 404. If you want to check that a request was redirected, use the test('redirect', async () =>
{
await testApiHandler(
{
handler: (req, res) => res.redirect(307, 'https://example.com'),
test: async ({ fetch }) =>
{
const res = await fetch();
// this succeeds
expect(res.redirected).toBeTruthy();
},
},
);
}); If you want node-fetch (or any relatively spec-compliant fetch library) not to follow redirects, set the redirect mode to import { testApiHandler } from 'next-test-api-route-handler';
test('redirect', async () =>
{
await testApiHandler(
{
handler: (req, res) => res.redirect(307, 'https://example.com'),
test: async ({ fetch }) =>
{
const res = await fetch({ redirect: 'manual' });
// this succeeds
expect(res.status).toBe(307);
},
},
);
}); |
Beta Was this translation helpful? Give feedback.
@adrian-kriegel The fetch spec dictates that redirects should reuse the original method and body to query the new resource and return the response as-is. For example, the following (adapted from your reproduction steps) will return status 404:
If you visit https://google.com/404 in your browser, you'll also see that you'll get a 404.
I…