我正在测试一个带有mock和stub的网站api,需要响应http GET请求。我使用的是“webdriverio”:“8.32.2”和browser.mock。
响应需要将statusCode:200的statusText设置为自定义文本,比如“我的文本”。但是,我没有找到任何使用webdriverio的方法。
而不是这种回应:
{
"headers": {
"normalizedNames": {},
"lazyUpdate": null
},
"status": 200,
"statusText": "OK",
"url": "https://localhost:8090/api/my-api,
"ok": true,
"type": 4,
"body": {}
}
我需要提供这个:
{
"headers": {
"normalizedNames": {},
"lazyUpdate": null
},
"status": 200,
"statusText": "my-text",
"url": "https://localhost:8090/api/my-api",
"ok": true,
"type": 4,
"body": {}
}
其中statusText设置为“my text”而不是“OK”。
webdriverio mock和types库允许我使用MockResponseParams结构进行响应:
export type MockResponseParams = {
statusCode?: number | ((request: Matches) => number);
headers?: Record<string, string> | ((request: Matches) => Record<string, string>);
/**
* fetch real response before responding with mocked data. Default: true
*/
fetchResponse?: boolean;
};
但正如你所看到的,没有空间
statusText
.
我试图强制添加
状态文本
这样地:
export async function myStub(): Promise<void> {
const mock = await browser.mock(myUrlRegExp, { method: HttpMethod.Get });
mock.respond(getResponseFromRequest,
{
fetchResponse: false,
headers: CORS_HEADERS,
statusCode: 200,
...{ statusText: 'my-text' },
}
);
}
但这并没有改变人们的反应。
我还试图将statusText添加到响应的正文中,但如果statusText在正文中,我的网站不会将其识别为statusCode 200的文本,所以我的测试失败了。
有什么方法可以更改或自定义webdriverio中的状态代码的文本吗?