根据请求正文更改 TestCafe RequestMock API 响应
Change TestCafe RequestMock API response based on request body
我正在使用 TestCafe 请求模拟功能来模拟 API 响应,如下所示。
export const deleteDocMock = (status = 200) =>
RequestMock()
.onRequestTo({
url: /\/api\/my-api\/.*/,
method: 'DELETE',
})
.respond({some response}, status, {
'access-control-allow-credentials': 'true',
'access-control-allow-methods': 'GET, POST, PUT, OPTIONS',
});
我可以根据 URL 和方法类型而不是请求正文来过滤请求。
现在我有一个用例,如果请求正文不同,则 API 响应需要对同一个 API 请求不同。
例如,如果 api 的请求正文如下所示,则响应正文将更改
{
id: 1
name: 'foo'
}
如果请求正文如下所示,响应将再次不同
{
id: 2
name: 'bar'
}
有什么方法可以根据请求主体过滤请求,以便可以使用 TestCafe 相应地更改响应?
感谢任何帮助。
此页面上的其他过滤器具有 requestMock 和 requestLogger 的示例,但谓词仅具有 requestLogger 的示例。无论如何,检查它是否有效。
您可以在 onRequestTo
中使用谓词函数。我创建了一个简单的例子来演示它:
import { RequestMock } from 'testcafe';
fixture `Fixture`;
const mock = RequestMock()
.onRequestTo(request => {
console.log(request); // The full request object
return request.url.indexOf('google') > -1; // Here you can set your own rule
})
.respond((req, res) => {
res.setBody('<html><body><h1>OK</h1></body></html>');
});
test
.requestHooks(mock)
('test', async t => {
await t
.navigateTo('https://google.com')
.debug(); // Here we can see our mocked response
});
感谢你们的帮助。这真的帮助了我。我现在可以根据请求正文过滤我的请求。我正在将 API 请求正文与我预期的 json 正文进行比较,并在条件通过时模拟响应。我的代码现在是这样的。我正在比较缓冲区,因为 request.body
属于 buffer
.
类型
const mock = RequestMock()
.onRequestTo(
request =>
request.url.match(/api\/assets\/.*\/staged-document/) &&
request.method === 'post' &&
!Buffer.compare(request.body, Buffer.from({"assetId":1,"name":"file1.name"}, 'utf-8')),
)
.respond({some response}, status, {
'access-control-allow-credentials': 'true',
'access-control-allow-methods': 'GET, POST, PUT, OPTIONS',
});
我正在使用 TestCafe 请求模拟功能来模拟 API 响应,如下所示。
export const deleteDocMock = (status = 200) =>
RequestMock()
.onRequestTo({
url: /\/api\/my-api\/.*/,
method: 'DELETE',
})
.respond({some response}, status, {
'access-control-allow-credentials': 'true',
'access-control-allow-methods': 'GET, POST, PUT, OPTIONS',
});
我可以根据 URL 和方法类型而不是请求正文来过滤请求。 现在我有一个用例,如果请求正文不同,则 API 响应需要对同一个 API 请求不同。 例如,如果 api 的请求正文如下所示,则响应正文将更改
{
id: 1
name: 'foo'
}
如果请求正文如下所示,响应将再次不同
{
id: 2
name: 'bar'
}
有什么方法可以根据请求主体过滤请求,以便可以使用 TestCafe 相应地更改响应? 感谢任何帮助。
此页面上的其他过滤器具有 requestMock 和 requestLogger 的示例,但谓词仅具有 requestLogger 的示例。无论如何,检查它是否有效。
您可以在 onRequestTo
中使用谓词函数。我创建了一个简单的例子来演示它:
import { RequestMock } from 'testcafe';
fixture `Fixture`;
const mock = RequestMock()
.onRequestTo(request => {
console.log(request); // The full request object
return request.url.indexOf('google') > -1; // Here you can set your own rule
})
.respond((req, res) => {
res.setBody('<html><body><h1>OK</h1></body></html>');
});
test
.requestHooks(mock)
('test', async t => {
await t
.navigateTo('https://google.com')
.debug(); // Here we can see our mocked response
});
感谢你们的帮助。这真的帮助了我。我现在可以根据请求正文过滤我的请求。我正在将 API 请求正文与我预期的 json 正文进行比较,并在条件通过时模拟响应。我的代码现在是这样的。我正在比较缓冲区,因为 request.body
属于 buffer
.
const mock = RequestMock()
.onRequestTo(
request =>
request.url.match(/api\/assets\/.*\/staged-document/) &&
request.method === 'post' &&
!Buffer.compare(request.body, Buffer.from({"assetId":1,"name":"file1.name"}, 'utf-8')),
)
.respond({some response}, status, {
'access-control-allow-credentials': 'true',
'access-control-allow-methods': 'GET, POST, PUT, OPTIONS',
});