请求 cookies

读取请求 cookie。

你可以通过响应解析器参数中提供给你的 cookies 对象读取请求的 cookie。虽然你始终可以手动读取 request.headers.get('cookie'),但为了方便起见,cookie 对象会为你解析请求 cookie。

¥You can read the request’s cookies via the cookies object provided to you in the response resolver argument. While you can always read the request.headers.get('cookie') manually, the cookie object parses request cookies for you for convenience.

http.get('/api/user', ({ cookies }) => {
  if (!cookies.authToken) {
    return new HttpResponse(null, { status: 403 })
  }
 
  return HttpResponse.json({ name: 'John' })
})

MSW 使用第三方 Cookie 解析包,该包根据 HTTP Cookie 规范解析 Cookie。

¥MSW uses a third-party cookie parsing package that parses cookies per the HTTP Cookie specification.