After extensive research, I found conflicting information on this topic.
I need to intercept an XMLHttpRequest and simulate a 200 status response from the backend using plain Javascript. This is specifically for testing purposes.
So far, I have made progress:
const originalXHROpen = window.XMLHttpRequest.prototype.open;
const originalXHRSend = window.XMLHttpRequest.prototype.send;
window.XMLHttpRequest.prototype.open = function (method, url, async, user, password) {
this.interceptRequest = url.includes('foo/bar');
originalXHROpen.apply(this, arguments);
};
window.XMLHttpRequest.prototype.send = function (data) {
if (this.interceptRequest) {
console.log('## INTERCEPTED REQUEST');
// attempting to fake a 200 response
return;
}
originalXHRSend.apply(this, arguments);
};
While I can successfully intercept the call, I have struggled to imitate the 200
status response. Is it actually feasible? If so, how can I accomplish it?
Thank you!