I want to filter request based on the request body so as to provide the right mock response. It does not seem to work though. Any help with this is appreciated.
My code looks like this:
RequestMock()
.onRequestTo({
url: /\/api\/assets\/.*\/staged-document/,
method: 'POST',
body: '{id: something}'
})
.respond((req, res) => {
res.headers['access-control-allow-credentials'] = 'true';
res.headers['access-control-allow-origin'] = loadConfig.baseUrl;
res.headers['access-control-allow-methods'] = 'GET, POST, PUT, OPTIONS';
res.statusCode = status;
res.setBody({something})
TestCafe does not seem to filter based on the body defined in the onRequestTo section. It is filtering only based on the url and method parameters.
Am I doing this right? or is this a bug?
I expect the RequestMock to filter based on the request body specified in onRequestTo section
@amalsgit
Hello,
You can use a predicate function in your onRequestTo. I created a simple example to demonstrate it:
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
});
Result:
This thread has been automatically locked since it is closed and there has not been any recent activity. Please open a new issue for related bugs or feature requests. We recommend you ask TestCafe API, usage and configuration inquiries on StackOverflow.
Most helpful comment
@amalsgit
Hello,
You can use a predicate function in your
onRequestTo. I created a simple example to demonstrate it:Result:
