默认行为
模拟 Service Worker 的重要默认行为。
默认直通
¥Default passthrough
MSW 采用网络优先的方法,这意味着除非你在处理程序中明确说明,否则它不会干扰网络。
¥MSW embraces a network-first approach, which means that it will not interfere with the network unless you explicitly say so in your handlers.
然而,由于 WebSocket 规范的限制,拦截 WebSocket 是模拟优先的。
¥Intercepting WebSockets, however, is mock-first due to the limitations of their specification.
处理程序失效
¥Handler fallthrough
每个被拦截的请求都会 “失败” 到你的处理程序列表中,寻找第一个匹配的处理程序并返回一个 instruction 来指示如何处理请求(模拟响应、响应补丁、直通、不执行任何操作)。单个请求可能同时匹配多个处理程序,但只有一个处理程序可以负责处理它。
¥Every intercepted request “falls through” the list of your handlers, looking for the first matching handler to return an instruction on how to handle the request (mock response, response-patch, passthrough, do nothing). A single request may match multiple handlers at the same time but only one handler can be responsible for handling it.
import { http, HttpResponse } from 'msw'
export const handlers = [
http.get('/user', () => console.log('One')),
http.get('/user', () => HttpResponse.json({ name: 'John' })),
http.get('/user', () => console.log('Three')),
]
在上面的例子中,给定一个传出的
GET /user
请求,你将看到"One"
字符串打印到控制台,然后收到第二个处理程序中定义的模拟 JSON 响应。你将看不到"Three"
字符串,因为第三个处理程序从未到达(即请求已被处理)。¥In the example above, given an outgoing
GET /user
request, you will see the"One"
string printed to the console and then receive the mocked JSON response as defined in the second handler. You will not see the"Three"
string because the third handler is never reached (i.e. request already handled).
你可以使用 fallthrough 来有效地分层你的网络行为。请参阅 网络行为覆盖。
¥You can use fallthrough to a great effect to layer your network behaviors. See Network behavior overrides.
处理程序顺序敏感度
¥Handler order sensitivity
处理程序源自 fallthrough 行为,对其定义的顺序很敏感。MSW 从左到右执行,从处理程序覆盖(如果有)开始,因为这些覆盖会被添加到处理程序列表的前面。
¥Derived from the fallthrough behavior, handlers are sensitive to the order in which they are defined. MSW executes them left-to-right, starting from handler overrides, if any, since those are prepended to the list of handlers.
import { http, HttpResponse } from 'msw'
import { setupServer } from 'msw/node
export const handlers = [
http.get('/user', () => console.log('One')),
http.get('/user', () => console.log('Two')),
http.get('/user', () => console.log('Three')),
]
const server = setupServer(...handlers)
server.listen()
server.use(
http.get('/user', () => console.log('Override one')),
)
给定一个传出的 GET /user
请求,你将在控制台中看到以下内容:
¥Given an outgoing GET /user
request, this is what you will see in the console:
Override one
One
Two
Three
由于上述任何处理程序均未处理过该请求,因此请求本身将根据默认直通方式按原样执行。
¥The request itself will be performed as-is according to the default passthrough since none of the handlers above has handled it.
使用处理程序顺序敏感性来区分模糊的请求匹配:
¥Use the handler order sensitivity to differentiate between ambiguous request matches:
export const handlers = [
// By placing a more specific request predicate first,
// the "GET /user/messages" request will be handled
// properly, despite it also matching a more permissive
// "/user/*" handler below.
http.get('/user/messages', messagesResolver)
http.get('/user/*', resolverOne),
]