DEV: Update pretender and fake-xml-http-request (#13937)

We are still on a version of pretender since 2017
https://github.com/pretenderjs/pretender/releases/tag/v1.6.1

Since then many changes have been made, including adding support
for xhr.upload. Upgrading will let us write proper acceptance
tests for uppy, which uses XmlHTTPRequest internally including
xhr.upload.

Updates pretender to 3.4.7 and fake-xml-http-request to 2.1.2.

Note: There have been no breaking changes in the releases that would
affect us, mainly dropping support for old node versions.
This commit is contained in:
Martin Brennan 2021-08-05 08:23:01 +10:00 committed by GitHub
parent 17f28d4018
commit d8a0d2262c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 2341 additions and 925 deletions

View File

@ -54,7 +54,7 @@
"message-bus-client": "^3.3.0",
"mousetrap": "^1.6.5",
"mousetrap-global-bind": "^1.1.0",
"pretender": "^3.4.3",
"pretender": "^3.4.7",
"pretty-text": "^1.0.0",
"qunit": "^2.14.0",
"qunit-dom": "^1.6.0",

View File

@ -5935,7 +5935,7 @@ extsprintf@^1.2.0:
resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.4.0.tgz#e2689f8f356fad62cca65a3a91c5df5f9551692f"
integrity sha1-4mifjzVvrWLMplo6kcXfX5VRaS8=
fake-xml-http-request@^2.1.1:
fake-xml-http-request@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/fake-xml-http-request/-/fake-xml-http-request-2.1.2.tgz#f1786720cae50bbb46273035a0173414f3e85e74"
integrity sha512-HaFMBi7r+oEC9iJNpc3bvcW7Z7iLmM26hPDmlb0mFwyANSsOQAtJxbdWsXITKOzZUyMYK0zYCv3h5yDj9TsiXg==
@ -9347,12 +9347,12 @@ prepend-http@^2.0.0:
resolved "https://registry.yarnpkg.com/prepend-http/-/prepend-http-2.0.0.tgz#e92434bfa5ea8c19f41cdfd401d741a3c819d897"
integrity sha1-6SQ0v6XqjBn0HN/UAddBo8gZ2Jc=
pretender@^3.4.3:
version "3.4.3"
resolved "https://registry.yarnpkg.com/pretender/-/pretender-3.4.3.tgz#a3b4160516007075d29127262f3a0063d19896e9"
integrity sha512-AlbkBly9R8KR+R0sTCJ/ToOeEoUMtt52QVCetui5zoSmeLOU3S8oobFsyPLm1O2txR6t58qDNysqPnA1vVi8Hg==
pretender@^3.4.7:
version "3.4.7"
resolved "https://registry.yarnpkg.com/pretender/-/pretender-3.4.7.tgz#34a2ae2d1fc9db440a990d50e6c0f5481d8755fc"
integrity sha512-jkPAvt1BfRi0RKamweJdEcnjkeu7Es8yix3bJ+KgBC5VpG/Ln4JE3hYN6vJym4qprm8Xo5adhWpm3HCoft1dOw==
dependencies:
fake-xml-http-request "^2.1.1"
fake-xml-http-request "^2.1.2"
route-recognizer "^0.3.3"
printf@^0.6.1:

View File

@ -188,7 +188,10 @@ def dependencies
source: 'qunit/qunit/qunit.js'
},
{
source: 'pretender/pretender.js'
source: 'pretender/dist/pretender.js'
},
{
source: 'fake-xml-http-request/fake_xml_http_request.js'
},
{
source: 'sinon/pkg/sinon.js'

View File

@ -51,7 +51,7 @@
"chrome-launcher": "^0.12.0",
"chrome-remote-interface": "^0.25",
"lodash-cli": "https://github.com/lodash-archive/lodash-cli.git",
"pretender": "^1.6",
"pretender": "^3.4.7",
"puppeteer": "1.20",
"qunit": "2.8.0",
"route-recognizer": "^0.3.3",

View File

@ -1,480 +1,530 @@
(function(undefined){
/**
* Minimal Event interface implementation
*
* Original implementation by Sven Fuchs: https://gist.github.com/995028
* Modifications and tests by Christian Johansen.
*
* @author Sven Fuchs (svenfuchs@artweb-design.de)
* @author Christian Johansen (christian@cjohansen.no)
* @license BSD
*
* Copyright (c) 2011 Sven Fuchs, Christian Johansen
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.FakeXMLHttpRequest = factory());
}(this, (function () { 'use strict';
var _Event = function Event(type, bubbles, cancelable, target) {
this.type = type;
this.bubbles = bubbles;
this.cancelable = cancelable;
this.target = target;
};
/**
* Minimal Event interface implementation
*
* Original implementation by Sven Fuchs: https://gist.github.com/995028
* Modifications and tests by Christian Johansen.
*
* @author Sven Fuchs (svenfuchs@artweb-design.de)
* @author Christian Johansen (christian@cjohansen.no)
* @license BSD
*
* Copyright (c) 2011 Sven Fuchs, Christian Johansen
*/
_Event.prototype = {
stopPropagation: function () {},
preventDefault: function () {
this.defaultPrevented = true;
}
};
var _Event = function Event(type, bubbles, cancelable, target) {
this.type = type;
this.bubbles = bubbles;
this.cancelable = cancelable;
this.target = target;
};
/*
Used to set the statusText property of an xhr object
*/
var httpStatusCodes = {
100: "Continue",
101: "Switching Protocols",
200: "OK",
201: "Created",
202: "Accepted",
203: "Non-Authoritative Information",
204: "No Content",
205: "Reset Content",
206: "Partial Content",
300: "Multiple Choice",
301: "Moved Permanently",
302: "Found",
303: "See Other",
304: "Not Modified",
305: "Use Proxy",
307: "Temporary Redirect",
400: "Bad Request",
401: "Unauthorized",
402: "Payment Required",
403: "Forbidden",
404: "Not Found",
405: "Method Not Allowed",
406: "Not Acceptable",
407: "Proxy Authentication Required",
408: "Request Timeout",
409: "Conflict",
410: "Gone",
411: "Length Required",
412: "Precondition Failed",
413: "Request Entity Too Large",
414: "Request-URI Too Long",
415: "Unsupported Media Type",
416: "Requested Range Not Satisfiable",
417: "Expectation Failed",
422: "Unprocessable Entity",
500: "Internal Server Error",
501: "Not Implemented",
502: "Bad Gateway",
503: "Service Unavailable",
504: "Gateway Timeout",
505: "HTTP Version Not Supported"
};
/*
Cross-browser XML parsing. Used to turn
XML responses into Document objects
Borrowed from JSpec
*/
function parseXML(text) {
var xmlDoc;
if (typeof DOMParser != "undefined") {
var parser = new DOMParser();
xmlDoc = parser.parseFromString(text, "text/xml");
} else {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(text);
}
return xmlDoc;
}
/*
Without mocking, the native XMLHttpRequest object will throw
an error when attempting to set these headers. We match this behavior.
*/
var unsafeHeaders = {
"Accept-Charset": true,
"Accept-Encoding": true,
"Connection": true,
"Content-Length": true,
"Cookie": true,
"Cookie2": true,
"Content-Transfer-Encoding": true,
"Date": true,
"Expect": true,
"Host": true,
"Keep-Alive": true,
"Referer": true,
"TE": true,
"Trailer": true,
"Transfer-Encoding": true,
"Upgrade": true,
"User-Agent": true,
"Via": true
};
/*
Adds an "event" onto the fake xhr object
that just calls the same-named method. This is
in case a library adds callbacks for these events.
*/
function _addEventListener(eventName, xhr){
xhr.addEventListener(eventName, function (event) {
var listener = xhr["on" + eventName];
if (listener && typeof listener == "function") {
listener(event);
_Event.prototype = {
stopPropagation: function () {},
preventDefault: function () {
this.defaultPrevented = true;
}
});
}
/*
Constructor for a fake window.XMLHttpRequest
*/
function FakeXMLHttpRequest() {
this.readyState = FakeXMLHttpRequest.UNSENT;
this.requestHeaders = {};
this.requestBody = null;
this.status = 0;
this.statusText = "";
this._eventListeners = {};
var events = ["loadstart", "load", "abort", "loadend"];
for (var i = events.length - 1; i >= 0; i--) {
_addEventListener(events[i], this);
}
}
// These status codes are available on the native XMLHttpRequest
// object, so we match that here in case a library is relying on them.
FakeXMLHttpRequest.UNSENT = 0;
FakeXMLHttpRequest.OPENED = 1;
FakeXMLHttpRequest.HEADERS_RECEIVED = 2;
FakeXMLHttpRequest.LOADING = 3;
FakeXMLHttpRequest.DONE = 4;
FakeXMLHttpRequest.prototype = {
UNSENT: 0,
OPENED: 1,
HEADERS_RECEIVED: 2,
LOADING: 3,
DONE: 4,
async: true,
};
/*
Duplicates the behavior of native XMLHttpRequest's open function
Used to set the statusText property of an xhr object
*/
open: function open(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.requestHeaders = {};
this.sendFlag = false;
this._readyStateChange(FakeXMLHttpRequest.OPENED);
},
var httpStatusCodes = {
100: "Continue",
101: "Switching Protocols",
200: "OK",
201: "Created",
202: "Accepted",
203: "Non-Authoritative Information",
204: "No Content",
205: "Reset Content",
206: "Partial Content",
300: "Multiple Choice",
301: "Moved Permanently",
302: "Found",
303: "See Other",
304: "Not Modified",
305: "Use Proxy",
307: "Temporary Redirect",
400: "Bad Request",
401: "Unauthorized",
402: "Payment Required",
403: "Forbidden",
404: "Not Found",
405: "Method Not Allowed",
406: "Not Acceptable",
407: "Proxy Authentication Required",
408: "Request Timeout",
409: "Conflict",
410: "Gone",
411: "Length Required",
412: "Precondition Failed",
413: "Request Entity Too Large",
414: "Request-URI Too Long",
415: "Unsupported Media Type",
416: "Requested Range Not Satisfiable",
417: "Expectation Failed",
422: "Unprocessable Entity",
500: "Internal Server Error",
501: "Not Implemented",
502: "Bad Gateway",
503: "Service Unavailable",
504: "Gateway Timeout",
505: "HTTP Version Not Supported"
};
/*
Duplicates the behavior of native XMLHttpRequest's addEventListener function
Cross-browser XML parsing. Used to turn
XML responses into Document objects
Borrowed from JSpec
*/
addEventListener: function addEventListener(event, listener) {
this._eventListeners[event] = this._eventListeners[event] || [];
this._eventListeners[event].push(listener);
},
function parseXML(text) {
var xmlDoc;
/*
Duplicates the behavior of native XMLHttpRequest's removeEventListener function
*/
removeEventListener: function removeEventListener(event, listener) {
var listeners = this._eventListeners[event] || [];
for (var i = 0, l = listeners.length; i < l; ++i) {
if (listeners[i] == listener) {
return listeners.splice(i, 1);
}
}
},
/*
Duplicates the behavior of native XMLHttpRequest's dispatchEvent function
*/
dispatchEvent: function dispatchEvent(event) {
var type = event.type;
var listeners = this._eventListeners[type] || [];
for (var i = 0; i < listeners.length; i++) {
if (typeof listeners[i] == "function") {
listeners[i].call(this, event);
} else {
listeners[i].handleEvent(event);
}
}
return !!event.defaultPrevented;
},
/*
Duplicates the behavior of native XMLHttpRequest's setRequestHeader function
*/
setRequestHeader: function setRequestHeader(header, value) {
verifyState(this);
if (unsafeHeaders[header] || /^(Sec-|Proxy-)/.test(header)) {
throw new Error("Refused to set unsafe header \"" + header + "\"");
}
if (this.requestHeaders[header]) {
this.requestHeaders[header] += "," + value;
if (typeof DOMParser != "undefined") {
var parser = new DOMParser();
xmlDoc = parser.parseFromString(text, "text/xml");
} else {
this.requestHeaders[header] = value;
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(text);
}
},
return xmlDoc;
}
/*
Duplicates the behavior of native XMLHttpRequest's send function
Without mocking, the native XMLHttpRequest object will throw
an error when attempting to set these headers. We match this behavior.
*/
send: function send(data) {
verifyState(this);
var unsafeHeaders = {
"Accept-Charset": true,
"Accept-Encoding": true,
"Connection": true,
"Content-Length": true,
"Cookie": true,
"Cookie2": true,
"Content-Transfer-Encoding": true,
"Date": true,
"Expect": true,
"Host": true,
"Keep-Alive": true,
"Referer": true,
"TE": true,
"Trailer": true,
"Transfer-Encoding": true,
"Upgrade": true,
"User-Agent": true,
"Via": true
};
if (!/^(get|head)$/i.test(this.method)) {
if (this.requestHeaders["Content-Type"]) {
var value = this.requestHeaders["Content-Type"].split(";");
this.requestHeaders["Content-Type"] = value[0] + ";charset=utf-8";
} else {
this.requestHeaders["Content-Type"] = "text/plain;charset=utf-8";
/*
Adds an "event" onto the fake xhr object
that just calls the same-named method. This is
in case a library adds callbacks for these events.
*/
function _addEventListener(eventName, xhr){
xhr.addEventListener(eventName, function (event) {
var listener = xhr["on" + eventName];
if (listener && typeof listener == "function") {
listener.call(event.target, event);
}
});
}
function EventedObject() {
this._eventListeners = {};
var events = ["loadstart", "progress", "load", "abort", "loadend"];
for (var i = events.length - 1; i >= 0; i--) {
_addEventListener(events[i], this);
}
}
EventedObject.prototype = {
/*
Duplicates the behavior of native XMLHttpRequest's addEventListener function
*/
addEventListener: function addEventListener(event, listener) {
this._eventListeners[event] = this._eventListeners[event] || [];
this._eventListeners[event].push(listener);
},
/*
Duplicates the behavior of native XMLHttpRequest's removeEventListener function
*/
removeEventListener: function removeEventListener(event, listener) {
var listeners = this._eventListeners[event] || [];
for (var i = 0, l = listeners.length; i < l; ++i) {
if (listeners[i] == listener) {
return listeners.splice(i, 1);
}
}
},
/*
Duplicates the behavior of native XMLHttpRequest's dispatchEvent function
*/
dispatchEvent: function dispatchEvent(event) {
var type = event.type;
var listeners = this._eventListeners[type] || [];
for (var i = 0; i < listeners.length; i++) {
if (typeof listeners[i] == "function") {
listeners[i].call(this, event);
} else {
listeners[i].handleEvent(event);
}
}
this.requestBody = data;
return !!event.defaultPrevented;
},
/*
Triggers an `onprogress` event with the given parameters.
*/
_progress: function _progress(lengthComputable, loaded, total) {
var event = new _Event('progress');
event.target = this;
event.lengthComputable = lengthComputable;
event.loaded = loaded;
event.total = total;
this.dispatchEvent(event);
}
this.errorFlag = false;
this.sendFlag = this.async;
this._readyStateChange(FakeXMLHttpRequest.OPENED);
if (typeof this.onSend == "function") {
this.onSend(this);
}
this.dispatchEvent(new _Event("loadstart", false, false, this));
},
};
/*
Duplicates the behavior of native XMLHttpRequest's abort function
Constructor for a fake window.XMLHttpRequest
*/
abort: function abort() {
this.aborted = true;
this.responseText = null;
this.errorFlag = true;
this.requestHeaders = {};
if (this.readyState > FakeXMLHttpRequest.UNSENT && this.sendFlag) {
this._readyStateChange(FakeXMLHttpRequest.DONE);
this.sendFlag = false;
}
function FakeXMLHttpRequest() {
EventedObject.call(this);
this.readyState = FakeXMLHttpRequest.UNSENT;
this.requestHeaders = {};
this.requestBody = null;
this.status = 0;
this.statusText = "";
this.upload = new EventedObject();
this.onabort= null;
this.onerror= null;
this.onload= null;
this.onloadend= null;
this.onloadstart= null;
this.onprogress= null;
this.onreadystatechange= null;
this.ontimeout= null;
}
this.dispatchEvent(new _Event("abort", false, false, this));
if (typeof this.onerror === "function") {
FakeXMLHttpRequest.prototype = new EventedObject();
// These status codes are available on the native XMLHttpRequest
// object, so we match that here in case a library is relying on them.
FakeXMLHttpRequest.UNSENT = 0;
FakeXMLHttpRequest.OPENED = 1;
FakeXMLHttpRequest.HEADERS_RECEIVED = 2;
FakeXMLHttpRequest.LOADING = 3;
FakeXMLHttpRequest.DONE = 4;
var FakeXMLHttpRequestProto = {
UNSENT: 0,
OPENED: 1,
HEADERS_RECEIVED: 2,
LOADING: 3,
DONE: 4,
async: true,
withCredentials: false,
/*
Duplicates the behavior of native XMLHttpRequest's open function
*/
open: function open(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.response = this.responseText;
this.responseXML = null;
this.responseURL = url;
this.requestHeaders = {};
this.sendFlag = false;
this._readyStateChange(FakeXMLHttpRequest.OPENED);
},
/*
Duplicates the behavior of native XMLHttpRequest's setRequestHeader function
*/
setRequestHeader: function setRequestHeader(header, value) {
verifyState(this);
if (unsafeHeaders[header] || /^(Sec-|Proxy-)/.test(header)) {
throw new Error("Refused to set unsafe header \"" + header + "\"");
}
if (this.requestHeaders[header]) {
this.requestHeaders[header] += "," + value;
} else {
this.requestHeaders[header] = value;
}
},
/*
Duplicates the behavior of native XMLHttpRequest's send function
*/
send: function send(data) {
verifyState(this);
if (!/^(get|head)$/i.test(this.method)) {
var hasContentTypeHeader = false;
Object.keys(this.requestHeaders).forEach(function (key) {
if (key.toLowerCase() === 'content-type') {
hasContentTypeHeader = true;
}
});
if (!hasContentTypeHeader && !(data || '').toString().match('FormData')) {
this.requestHeaders["Content-Type"] = "text/plain;charset=UTF-8";
}
this.requestBody = data;
}
this.errorFlag = false;
this.sendFlag = this.async;
this._readyStateChange(FakeXMLHttpRequest.OPENED);
if (typeof this.onSend == "function") {
this.onSend(this);
}
this.dispatchEvent(new _Event("loadstart", false, false, this));
},
/*
Duplicates the behavior of native XMLHttpRequest's abort function
*/
abort: function abort() {
this.aborted = true;
this.responseText = null;
this.response = this.responseText;
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();
}
},
}
},
/*
Duplicates the behavior of native XMLHttpRequest's getResponseHeader function
*/
getResponseHeader: function getResponseHeader(header) {
if (this.readyState < FakeXMLHttpRequest.HEADERS_RECEIVED) {
return null;
}
if (/^Set-Cookie2?$/i.test(header)) {
return null;
}
header = header.toLowerCase();
for (var h in this.responseHeaders) {
if (h.toLowerCase() == header) {
return this.responseHeaders[h];
}
}
/*
Duplicates the behavior of native XMLHttpRequest's getResponseHeader function
*/
getResponseHeader: function getResponseHeader(header) {
if (this.readyState < FakeXMLHttpRequest.HEADERS_RECEIVED) {
return null;
}
},
if (/^Set-Cookie2?$/i.test(header)) {
return null;
}
header = header.toLowerCase();
for (var h in this.responseHeaders) {
if (h.toLowerCase() == header) {
return this.responseHeaders[h];
/*
Duplicates the behavior of native XMLHttpRequest's getAllResponseHeaders function
*/
getAllResponseHeaders: function getAllResponseHeaders() {
if (this.readyState < FakeXMLHttpRequest.HEADERS_RECEIVED) {
return "";
}
}
return null;
},
var headers = "";
/*
Duplicates the behavior of native XMLHttpRequest's getAllResponseHeaders function
*/
getAllResponseHeaders: function getAllResponseHeaders() {
if (this.readyState < FakeXMLHttpRequest.HEADERS_RECEIVED) {
return "";
}
var headers = "";
for (var header in this.responseHeaders) {
if (this.responseHeaders.hasOwnProperty(header) && !/^Set-Cookie2?$/i.test(header)) {
headers += header + ": " + this.responseHeaders[header] + "\r\n";
for (var header in this.responseHeaders) {
if (this.responseHeaders.hasOwnProperty(header) && !/^Set-Cookie2?$/i.test(header)) {
headers += header + ": " + this.responseHeaders[header] + "\r\n";
}
}
}
return headers;
},
return headers;
},
/*
Places a FakeXMLHttpRequest object into the passed
state.
*/
_readyStateChange: function _readyStateChange(state) {
this.readyState = state;
if (typeof this.onreadystatechange == "function") {
this.onreadystatechange();
}
this.dispatchEvent(new _Event("readystatechange"));
if (this.readyState == FakeXMLHttpRequest.DONE) {
this.dispatchEvent(new _Event("load", false, false, this));
this.dispatchEvent(new _Event("loadend", false, false, this));
}
},
/*
Sets the FakeXMLHttpRequest object's response headers and
places the object into readyState 2
*/
_setResponseHeaders: function _setResponseHeaders(headers) {
this.responseHeaders = {};
for (var header in headers) {
if (headers.hasOwnProperty(header)) {
this.responseHeaders[header] = headers[header];
/*
Duplicates the behavior of native XMLHttpRequest's overrideMimeType function
*/
overrideMimeType: function overrideMimeType(mimeType) {
if (typeof mimeType === "string") {
this.forceMimeType = mimeType.toLowerCase();
}
}
if (this.async) {
this._readyStateChange(FakeXMLHttpRequest.HEADERS_RECEIVED);
} else {
this.readyState = FakeXMLHttpRequest.HEADERS_RECEIVED;
}
},
},
/*
Places a FakeXMLHttpRequest object into the passed
state.
*/
_readyStateChange: function _readyStateChange(state) {
this.readyState = state;
/*
Sets the FakeXMLHttpRequest object's response body and
if body text is XML, sets responseXML to parsed document
object
*/
_setResponseBody: function _setResponseBody(body) {
verifyRequestSent(this);
verifyHeadersReceived(this);
verifyResponseBodyType(body);
if (typeof this.onreadystatechange == "function") {
this.onreadystatechange(new _Event("readystatechange"));
}
var chunkSize = this.chunkSize || 10;
var index = 0;
this.responseText = "";
this.dispatchEvent(new _Event("readystatechange"));
if (this.readyState == FakeXMLHttpRequest.DONE) {
this.dispatchEvent(new _Event("load", false, false, this));
}
if (this.readyState == FakeXMLHttpRequest.UNSENT || this.readyState == FakeXMLHttpRequest.DONE) {
this.dispatchEvent(new _Event("loadend", false, false, this));
}
},
/*
Sets the FakeXMLHttpRequest object's response headers and
places the object into readyState 2
*/
_setResponseHeaders: function _setResponseHeaders(headers) {
this.responseHeaders = {};
for (var header in headers) {
if (headers.hasOwnProperty(header)) {
this.responseHeaders[header] = headers[header];
}
}
if (this.forceMimeType) {
this.responseHeaders['Content-Type'] = this.forceMimeType;
}
do {
if (this.async) {
this._readyStateChange(FakeXMLHttpRequest.LOADING);
this._readyStateChange(FakeXMLHttpRequest.HEADERS_RECEIVED);
} else {
this.readyState = FakeXMLHttpRequest.HEADERS_RECEIVED;
}
},
/*
Sets the FakeXMLHttpRequest object's response body and
if body text is XML, sets responseXML to parsed document
object
*/
_setResponseBody: function _setResponseBody(body) {
verifyRequestSent(this);
verifyHeadersReceived(this);
verifyResponseBodyType(body);
var chunkSize = this.chunkSize || 10;
var index = 0;
this.responseText = "";
this.response = this.responseText;
do {
if (this.async) {
this._readyStateChange(FakeXMLHttpRequest.LOADING);
}
this.responseText += body.substring(index, index + chunkSize);
this.response = this.responseText;
index += chunkSize;
} while (index < body.length);
var type = this.getResponseHeader("Content-Type");
if (this.responseText && (!type || /(text\/xml)|(application\/xml)|(\+xml)/.test(type))) {
try {
this.responseXML = parseXML(this.responseText);
} catch (e) {
// Unable to parse XML - no biggie
}
}
this.responseText += body.substring(index, index + chunkSize);
index += chunkSize;
} while (index < body.length);
var type = this.getResponseHeader("Content-Type");
if (this.responseText && (!type || /(text\/xml)|(application\/xml)|(\+xml)/.test(type))) {
try {
this.responseXML = parseXML(this.responseText);
} catch (e) {
// Unable to parse XML - no biggie
if (this.async) {
this._readyStateChange(FakeXMLHttpRequest.DONE);
} else {
this.readyState = FakeXMLHttpRequest.DONE;
}
},
/*
Forces a response on to the FakeXMLHttpRequest object.
This is the public API for faking responses. This function
takes a number status, headers object, and string body:
```
xhr.respond(404, {Content-Type: 'text/plain'}, "Sorry. This object was not found.")
```
*/
respond: function respond(status, headers, body) {
this._setResponseHeaders(headers || {});
this.status = typeof status == "number" ? status : 200;
this.statusText = httpStatusCodes[this.status];
this._setResponseBody(body || "");
}
};
if (this.async) {
this._readyStateChange(FakeXMLHttpRequest.DONE);
} else {
this.readyState = FakeXMLHttpRequest.DONE;
}
},
/*
Forces a response on to the FakeXMLHttpRequest object.
This is the public API for faking responses. This function
takes a number status, headers object, and string body:
```
xhr.respond(404, {Content-Type: 'text/plain'}, "Sorry. This object was not found.")
```
*/
respond: function respond(status, headers, body) {
this._setResponseHeaders(headers || {});
this.status = typeof status == "number" ? status : 200;
this.statusText = httpStatusCodes[this.status];
this._setResponseBody(body || "");
if (typeof this.onload === "function"){
this.onload();
}
}
};
function verifyState(xhr) {
if (xhr.readyState !== FakeXMLHttpRequest.OPENED) {
throw new Error("INVALID_STATE_ERR");
for (var property in FakeXMLHttpRequestProto) {
FakeXMLHttpRequest.prototype[property] = FakeXMLHttpRequestProto[property];
}
if (xhr.sendFlag) {
throw new Error("INVALID_STATE_ERR");
function verifyState(xhr) {
if (xhr.readyState !== FakeXMLHttpRequest.OPENED) {
throw new Error("INVALID_STATE_ERR");
}
if (xhr.sendFlag) {
throw new Error("INVALID_STATE_ERR");
}
}
}
function verifyRequestSent(xhr) {
if (xhr.readyState == FakeXMLHttpRequest.DONE) {
throw new Error("Request done");
}
}
function verifyRequestSent(xhr) {
if (xhr.readyState == FakeXMLHttpRequest.DONE) {
throw new Error("Request done");
}
}
function verifyHeadersReceived(xhr) {
if (xhr.async && xhr.readyState != FakeXMLHttpRequest.HEADERS_RECEIVED) {
throw new Error("No headers received");
}
}
function verifyHeadersReceived(xhr) {
if (xhr.async && xhr.readyState != FakeXMLHttpRequest.HEADERS_RECEIVED) {
throw new Error("No headers received");
}
}
function verifyResponseBodyType(body) {
if (typeof body != "string") {
var error = new Error("Attempted to respond to fake XMLHttpRequest with " +
body + ", which is not a string.");
error.name = "InvalidBodyException";
throw error;
}
}
function verifyResponseBodyType(body) {
if (typeof body != "string") {
var error = new Error("Attempted to respond to fake XMLHttpRequest with " +
body + ", which is not a string.");
error.name = "InvalidBodyException";
throw error;
}
}
if (typeof module !== 'undefined' && module.exports) {
module.exports = FakeXMLHttpRequest;
} else if (typeof define === 'function' && define.amd) {
define(function() { return FakeXMLHttpRequest; });
} else if (typeof window !== 'undefined') {
window.FakeXMLHttpRequest = FakeXMLHttpRequest;
} else if (this) {
this.FakeXMLHttpRequest = FakeXMLHttpRequest;
}
})();
return FakeXMLHttpRequest;
})));
//# sourceMappingURL=fake_xml_http_request.js.map

File diff suppressed because it is too large Load Diff

View File

@ -1932,10 +1932,10 @@ extract-zip@^1.6.6:
mkdirp "^0.5.4"
yauzl "^2.10.0"
fake-xml-http-request@^1.6.0:
version "1.6.0"
resolved "https://registry.yarnpkg.com/fake-xml-http-request/-/fake-xml-http-request-1.6.0.tgz#bd0ac79ae3e2660098282048a12c730a6f64d550"
integrity sha512-99XPwwSg89BfzPuv4XCpZxn3EbauMCgAQCxq9MzrvS6DFD73OON6AnUTicL4A0HZtYMBwCZBWVnRqGjZDgQkTg==
fake-xml-http-request@^2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/fake-xml-http-request/-/fake-xml-http-request-2.1.2.tgz#f1786720cae50bbb46273035a0173414f3e85e74"
integrity sha512-HaFMBi7r+oEC9iJNpc3bvcW7Z7iLmM26hPDmlb0mFwyANSsOQAtJxbdWsXITKOzZUyMYK0zYCv3h5yDj9TsiXg==
fast-deep-equal@^3.1.1:
version "3.1.3"
@ -3527,12 +3527,12 @@ prelude-ls@~1.1.2:
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54"
integrity sha1-IZMqVJ9eUv/ZqCf1cOBL5iqX2lQ=
pretender@^1.6:
version "1.6.1"
resolved "https://registry.yarnpkg.com/pretender/-/pretender-1.6.1.tgz#77d1e42ac8c6b298f5cd43534a87645df035db8c"
integrity sha1-d9HkKsjGspj1zUNTSodkXfA124w=
pretender@^3.4.7:
version "3.4.7"
resolved "https://registry.yarnpkg.com/pretender/-/pretender-3.4.7.tgz#34a2ae2d1fc9db440a990d50e6c0f5481d8755fc"
integrity sha512-jkPAvt1BfRi0RKamweJdEcnjkeu7Es8yix3bJ+KgBC5VpG/Ln4JE3hYN6vJym4qprm8Xo5adhWpm3HCoft1dOw==
dependencies:
fake-xml-http-request "^1.6.0"
fake-xml-http-request "^2.1.2"
route-recognizer "^0.3.3"
prettier@2.2.1, prettier@^2.0.4: