Redirect to different URL #743
Answered
by
kettanaito
kamalaRamaswamy
asked this question in
Q&A
-
Hi, I have a requirement to redirect to a different URL in one of the response handlers. |
Beta Was this translation helpful? Give feedback.
Answered by
kettanaito
May 20, 2021
Replies: 1 comment 1 reply
-
Hey, @Post-box. You can redirect from a mocked response by constructing a valid redirect response: rest.get('/user', (req, res, ctx) => {
return res(
// Respond with a redirect (3xx) status code.
ctx.status(301),
// Provide the redirect target in the "Location" header.
ctx.set('Location', '/redirect-target')
)
}) Take a look at the redirect tests from our repo: msw/test/rest-api/redirect.mocks.ts Lines 1 to 17 in 86c23ec |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
kettanaito
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey, @Post-box.
You can redirect from a mocked response by constructing a valid redirect response:
Take a look at the redirect tests from our repo:
msw/test/rest-api/redirect.mocks.ts
Lines 1 to 17 in 86c23ec