Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
/* eslint-disable */
import FakeXMLHttpRequest from 'fake-xml-http-request';
/**
* Monkey patch FakeXMLHttpRequest and remove `response` based on the work in:
* https://github.com/pretenderjs/FakeXMLHttpRequest/pull/53/files
*
* which caused the issue described under:
*
* https://github.com/pretenderjs/FakeXMLHttpRequest/issues/54
*
*/
FakeXMLHttpRequest.prototype.open = function(method, url, async, username, password) {
this.method = method;
this.url = url;
this.async = typeof async == "boolean" ? async : true;
this.username = username;
this.password = password;
this.responseText = null;
this.responseXML = null;
this.responseURL = url;
this.requestHeaders = {};
this.sendFlag = false;
delete this.response;
this._readyStateChange(FakeXMLHttpRequest.OPENED);
}
FakeXMLHttpRequest.prototype.abort = function() {
this.aborted = true;
let mock = tractor.getMatchingMock(method, url);
if (!mock) {
let message = `Unexpected "${method}" request to "${url}".`;
/* eslint-disable no-console */
console.error(message);
/* eslint-enable no-console */
}
if (!mock || mock && mock.passThrough) {
return originalOpen.apply(this, arguments);
}
let real = this;
let fake = new FakeXMLHttpRequest();
Object.setPrototypeOf(real, FakeXMLHttpRequest.prototype);
real.send = function () {
copyEvents(real, fake);
FakeXMLHttpRequest.prototype.send.apply(this, arguments);
fake.send.apply(fake, arguments);
FakeXMLHttpRequest.prototype.respond.call(this, mock.status, mock.headers, mock.body);
fake.respond.apply(fake, arguments);
};
copyEvents(real, fake);
FakeXMLHttpRequest.prototype.open.apply(this, arguments);
fake.open.apply(fake, arguments);
};
function interceptor(ctx) {
function FakeRequest() {
// super()
FakeXMLHttpRequest.call(this);
}
FakeRequest.prototype = Object.create(FakeXMLHttpRequest.prototype);
FakeRequest.prototype.constructor = FakeRequest;
// extend
FakeRequest.prototype.send = function send() {
this.sendArguments = arguments;
if (!ctx.pretender.running) {
throw new Error('You shut down a Pretender instance while there was a pending request. ' +
'That request just tried to complete. Check to see if you accidentally shut down ' +
'a pretender earlier than you intended to');
}
FakeXMLHttpRequest.prototype.send.apply(this, arguments);
if (ctx.pretender.checkPassthrough(this)) {
this.passthrough();
} else {
function interceptor(ctx) {
function FakeRequest() {
// super()
FakeXMLHttpRequest.call(this);
}
FakeRequest.prototype = Object.create(FakeXMLHttpRequest.prototype);
FakeRequest.prototype.constructor = FakeRequest;
// extend
FakeRequest.prototype.send = function send() {
if (!ctx.pretender.running) {
throw new Error('You shut down a Pretender instance while there was a pending request. ' +
'That request just tried to complete. Check to see if you accidentally shut down ' +
'a pretender earlier than you intended to');
}
FakeXMLHttpRequest.prototype.send.apply(this, arguments);
if (ctx.pretender.checkPassthrough(this)) {
var xhr = createPassthrough(this);
xhr.send.apply(xhr, arguments);
}
else {
ctx.pretender.handleRequest(this);
}
FakeXMLHttpRequest.prototype.open = function(method, url, async, username, password) {
this.method = method;
this.url = url;
this.async = typeof async == "boolean" ? async : true;
this.username = username;
this.password = password;
this.responseText = null;
this.responseXML = null;
this.responseURL = url;
this.requestHeaders = {};
this.sendFlag = false;
delete this.response;
this._readyStateChange(FakeXMLHttpRequest.OPENED);
}
FakeXMLHttpRequest.prototype.abort = function() {
this.aborted = true;
this.responseText = null;
delete this.response;
this.errorFlag = true;
this.requestHeaders = {};
this.dispatchEvent(new _Event("abort", false, false, this));
if (this.readyState > FakeXMLHttpRequest.UNSENT && this.sendFlag) {
this._readyStateChange(FakeXMLHttpRequest.UNSENT);
this.sendFlag = false;
}
if (typeof this.onerror === "function") {
this.onerror();
}
this.errorFlag = true;
this.requestHeaders = {};
this.dispatchEvent(new _Event("abort", false, false, this));
if (this.readyState > FakeXMLHttpRequest.UNSENT && this.sendFlag) {
this._readyStateChange(FakeXMLHttpRequest.UNSENT);
this.sendFlag = false;
}
if (typeof this.onerror === "function") {
this.onerror();
}
}
FakeXMLHttpRequest.prototype._setResponseBody = function (body) {
verifyRequestSent(this);
verifyHeadersReceived(this);
verifyResponseBodyType(body);
var chunkSize = this.chunkSize || 10;
var index = 0;
this.responseText = "";
delete this.response;
do {
if (this.async) {
this._readyStateChange(FakeXMLHttpRequest.LOADING);
}
this.responseText += body.substring(index, index + chunkSize);
index += chunkSize;
FakeRequest.prototype._passthroughCheck = function(method, args) {
if (this._passthroughRequest) {
return this._passthroughRequest[method].apply(this._passthroughRequest, args);
}
return FakeXMLHttpRequest.prototype[method].apply(this, args);
};
real.send = function () {
copyEvents(real, fake);
FakeXMLHttpRequest.prototype.send.apply(this, arguments);
fake.send.apply(fake, arguments);
FakeXMLHttpRequest.prototype.respond.call(this, mock.status, mock.headers, mock.body);
fake.respond.apply(fake, arguments);
};
FakeRequest.prototype.send = function send() {
if (!ctx.pretender.running) {
throw new Error('You shut down a Pretender instance while there was a pending request. ' +
'That request just tried to complete. Check to see if you accidentally shut down ' +
'a pretender earlier than you intended to');
}
FakeXMLHttpRequest.prototype.send.apply(this, arguments);
if (ctx.pretender.checkPassthrough(this)) {
var xhr = createPassthrough(this);
xhr.send.apply(xhr, arguments);
}
else {
ctx.pretender.handleRequest(this);
}
};
function createPassthrough(fakeXHR) {