Status always 200 on redirect. #764
Unanswered
adrian-kriegel
asked this question in
Q&A
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.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Problem
I think the title is self-explanatory. Status appears to be 200 in any case.
Reproduction steps
Jest example:Beta Was this translation helpful? Give feedback.
All reactions