重定向
模拟重定向响应。
你可以通过构造有效的重定向 Response
实例并从响应解析器返回它来模拟重定向响应。这意味着设置重定向状态码 (3xx
) 和指向目标 URL 的 location
标头。
¥You can mock a redirect response by constructing a valid redirect Response
instance and returning it from the response resolver. This implies setting a redirect status code (3xx
) and the location
header that points to the destination URL.
http.get('/resource-a', () => {
return new HttpResponse(null, {
status: 301,
headers: {
location: '/resource-b',
},
})
})
在此示例中,
GET /resource-a
请求将被重定向到/resource-b
,在这种情况下/resource-b
不会被处理。你也可以为其定义一个请求处理程序,它将照常进行匹配!¥In this example, the
GET /resource-a
request will be redirected to/resource-b
, which in this scenario is unhandled. You can define a request handler for it, too, and it will be matched as usual!