Skip to content

Redirect to different URL #743

Answered by kettanaito
kamalaRamaswamy asked this question in Q&A
Discussion options

You must be logged in to vote

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:

import { setupWorker, rest } from 'msw'
const worker = setupWorker(
rest.get('/login', (req, res, ctx) => {
return res(ctx.status(307), ctx.set('Location', '/user'))
}),
rest.get('/user', (req, res, ctx) => {

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@kamalaRamaswamy
Comment options

Answer selected by kettanaito
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants