Socket
Socket
Sign inDemoInstall

fake-xml-http-request

Package Overview
Dependencies
0
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.1.3 to 1.2.0

test/upload_test.js

2

bower.json
{
"name": "fake-xml-http-request",
"version": "1.1.3",
"version": "1.2.0",
"main": [

@@ -5,0 +5,0 @@ "./fake_xml_http_request.js"

@@ -1,472 +0,497 @@

(function() {
"use strict";
/**
* 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.FakeXMLHttpRequest = factory()
}(this, function () { 'use strict';
var $$$src$fake$xml$http$request$$_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
*/
$$$src$fake$xml$http$request$$_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 $$$src$fake$xml$http$request$$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"
};
_Event.prototype = {
stopPropagation: function () {},
preventDefault: function () {
this.defaultPrevented = true;
}
};
/*
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 $$$src$fake$xml$http$request$$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);
/*
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);
}
});
}
return xmlDoc;
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 = {
/*
Without mocking, the native XMLHttpRequest object will throw
an error when attempting to set these headers. We match this behavior.
Duplicates the behavior of native XMLHttpRequest's addEventListener function
*/
var $$$src$fake$xml$http$request$$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
};
addEventListener: function addEventListener(event, listener) {
this._eventListeners[event] = this._eventListeners[event] || [];
this._eventListeners[event].push(listener);
},
/*
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.
Duplicates the behavior of native XMLHttpRequest's removeEventListener function
*/
function $$$src$fake$xml$http$request$$_addEventListener(eventName, xhr){
xhr.addEventListener(eventName, function (event) {
var listener = xhr["on" + eventName];
removeEventListener: function removeEventListener(event, listener) {
var listeners = this._eventListeners[event] || [];
if (listener && typeof listener == "function") {
listener(event);
for (var i = 0, l = listeners.length; i < l; ++i) {
if (listeners[i] == listener) {
return listeners.splice(i, 1);
}
});
}
}
},
/*
Constructor for a fake window.XMLHttpRequest
Duplicates the behavior of native XMLHttpRequest's dispatchEvent function
*/
function $$$src$fake$xml$http$request$$FakeXMLHttpRequest() {
this.readyState = $$$src$fake$xml$http$request$$FakeXMLHttpRequest.UNSENT;
this.requestHeaders = {};
this.requestBody = null;
this.status = 0;
this.statusText = "";
dispatchEvent: function dispatchEvent(event) {
var type = event.type;
var listeners = this._eventListeners[type] || [];
this._eventListeners = {};
var events = ["loadstart", "load", "abort", "loadend"];
for (var i = events.length - 1; i >= 0; i--) {
$$$src$fake$xml$http$request$$_addEventListener(events[i], this);
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;
},
// These status codes are available on the native XMLHttpRequest
// object, so we match that here in case a library is relying on them.
$$$src$fake$xml$http$request$$FakeXMLHttpRequest.UNSENT = 0;
$$$src$fake$xml$http$request$$FakeXMLHttpRequest.OPENED = 1;
$$$src$fake$xml$http$request$$FakeXMLHttpRequest.HEADERS_RECEIVED = 2;
$$$src$fake$xml$http$request$$FakeXMLHttpRequest.LOADING = 3;
$$$src$fake$xml$http$request$$FakeXMLHttpRequest.DONE = 4;
/*
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);
}
}
$$$src$fake$xml$http$request$$FakeXMLHttpRequest.prototype = {
UNSENT: 0,
OPENED: 1,
HEADERS_RECEIVED: 2,
LOADING: 3,
DONE: 4,
async: true,
/*
Constructor for a fake window.XMLHttpRequest
*/
function FakeXMLHttpRequest() {
EventedObject.call(this);
this.readyState = FakeXMLHttpRequest.UNSENT;
this.requestHeaders = {};
this.requestBody = null;
this.status = 0;
this.statusText = "";
this.upload = new EventedObject();
}
/*
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.responseXML = null;
this.requestHeaders = {};
this.sendFlag = false;
this._readyStateChange($$$src$fake$xml$http$request$$FakeXMLHttpRequest.OPENED);
},
FakeXMLHttpRequest.prototype = new EventedObject();
/*
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);
},
// 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;
/*
Duplicates the behavior of native XMLHttpRequest's removeEventListener function
*/
removeEventListener: function removeEventListener(event, listener) {
var listeners = this._eventListeners[event] || [];
var FakeXMLHttpRequestProto = {
UNSENT: 0,
OPENED: 1,
HEADERS_RECEIVED: 2,
LOADING: 3,
DONE: 4,
async: true,
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 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.responseXML = null;
this.requestHeaders = {};
this.sendFlag = false;
this._readyStateChange(FakeXMLHttpRequest.OPENED);
},
/*
Duplicates the behavior of native XMLHttpRequest's dispatchEvent function
*/
dispatchEvent: function dispatchEvent(event) {
var type = event.type;
var listeners = this._eventListeners[type] || [];
/*
Duplicates the behavior of native XMLHttpRequest's setRequestHeader function
*/
setRequestHeader: function setRequestHeader(header, value) {
verifyState(this);
for (var i = 0; i < listeners.length; i++) {
if (typeof listeners[i] == "function") {
listeners[i].call(this, event);
} else {
listeners[i].handleEvent(event);
}
}
if (unsafeHeaders[header] || /^(Sec-|Proxy-)/.test(header)) {
throw new Error("Refused to set unsafe header \"" + header + "\"");
}
return !!event.defaultPrevented;
},
if (this.requestHeaders[header]) {
this.requestHeaders[header] += "," + value;
} else {
this.requestHeaders[header] = value;
}
},
/*
Duplicates the behavior of native XMLHttpRequest's setRequestHeader function
*/
setRequestHeader: function setRequestHeader(header, value) {
$$$src$fake$xml$http$request$$verifyState(this);
/*
Duplicates the behavior of native XMLHttpRequest's send function
*/
send: function send(data) {
verifyState(this);
if ($$$src$fake$xml$http$request$$unsafeHeaders[header] || /^(Sec-|Proxy-)/.test(header)) {
throw new Error("Refused to set unsafe header \"" + header + "\"");
}
if (this.requestHeaders[header]) {
this.requestHeaders[header] += "," + value;
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[header] = value;
this.requestHeaders["Content-Type"] = "text/plain;charset=utf-8";
}
},
/*
Duplicates the behavior of native XMLHttpRequest's send function
*/
send: function send(data) {
$$$src$fake$xml$http$request$$verifyState(this);
this.requestBody = data;
}
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";
}
this.errorFlag = false;
this.sendFlag = this.async;
this._readyStateChange(FakeXMLHttpRequest.OPENED);
this.requestBody = data;
}
if (typeof this.onSend == "function") {
this.onSend(this);
}
this.errorFlag = false;
this.sendFlag = this.async;
this._readyStateChange($$$src$fake$xml$http$request$$FakeXMLHttpRequest.OPENED);
this.dispatchEvent(new _Event("loadstart", false, false, this));
},
if (typeof this.onSend == "function") {
this.onSend(this);
}
/*
Duplicates the behavior of native XMLHttpRequest's abort function
*/
abort: function abort() {
this.aborted = true;
this.responseText = null;
this.errorFlag = true;
this.requestHeaders = {};
this.dispatchEvent(new $$$src$fake$xml$http$request$$_Event("loadstart", false, false, this));
},
if (this.readyState > FakeXMLHttpRequest.UNSENT && this.sendFlag) {
this._readyStateChange(FakeXMLHttpRequest.DONE);
this.sendFlag = false;
}
/*
Duplicates the behavior of native XMLHttpRequest's abort function
*/
abort: function abort() {
this.aborted = true;
this.responseText = null;
this.errorFlag = true;
this.requestHeaders = {};
this.readyState = FakeXMLHttpRequest.UNSENT;
if (this.readyState > $$$src$fake$xml$http$request$$FakeXMLHttpRequest.UNSENT && this.sendFlag) {
this._readyStateChange($$$src$fake$xml$http$request$$FakeXMLHttpRequest.DONE);
this.sendFlag = false;
}
this.dispatchEvent(new _Event("abort", false, false, this));
if (typeof this.onerror === "function") {
this.onerror();
}
},
this.readyState = $$$src$fake$xml$http$request$$FakeXMLHttpRequest.UNSENT;
/*
Duplicates the behavior of native XMLHttpRequest's getResponseHeader function
*/
getResponseHeader: function getResponseHeader(header) {
if (this.readyState < FakeXMLHttpRequest.HEADERS_RECEIVED) {
return null;
}
this.dispatchEvent(new $$$src$fake$xml$http$request$$_Event("abort", false, false, this));
if (typeof this.onerror === "function") {
this.onerror();
}
},
if (/^Set-Cookie2?$/i.test(header)) {
return null;
}
/*
Duplicates the behavior of native XMLHttpRequest's getResponseHeader function
*/
getResponseHeader: function getResponseHeader(header) {
if (this.readyState < $$$src$fake$xml$http$request$$FakeXMLHttpRequest.HEADERS_RECEIVED) {
return null;
}
header = header.toLowerCase();
if (/^Set-Cookie2?$/i.test(header)) {
return null;
for (var h in this.responseHeaders) {
if (h.toLowerCase() == header) {
return this.responseHeaders[h];
}
}
header = header.toLowerCase();
return null;
},
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 < $$$src$fake$xml$http$request$$FakeXMLHttpRequest.HEADERS_RECEIVED) {
return "";
for (var header in this.responseHeaders) {
if (this.responseHeaders.hasOwnProperty(header) && !/^Set-Cookie2?$/i.test(header)) {
headers += header + ": " + this.responseHeaders[header] + "\r\n";
}
}
var headers = "";
return headers;
},
for (var header in this.responseHeaders) {
if (this.responseHeaders.hasOwnProperty(header) && !/^Set-Cookie2?$/i.test(header)) {
headers += header + ": " + this.responseHeaders[header] + "\r\n";
}
}
/*
Places a FakeXMLHttpRequest object into the passed
state.
*/
_readyStateChange: function _readyStateChange(state) {
this.readyState = state;
return headers;
},
if (typeof this.onreadystatechange == "function") {
this.onreadystatechange();
}
/*
Places a FakeXMLHttpRequest object into the passed
state.
*/
_readyStateChange: function _readyStateChange(state) {
this.readyState = state;
this.dispatchEvent(new _Event("readystatechange"));
if (typeof this.onreadystatechange == "function") {
this.onreadystatechange();
}
if (this.readyState == FakeXMLHttpRequest.DONE) {
this.dispatchEvent(new _Event("load", false, false, this));
this.dispatchEvent(new _Event("loadend", false, false, this));
}
},
this.dispatchEvent(new $$$src$fake$xml$http$request$$_Event("readystatechange"));
if (this.readyState == $$$src$fake$xml$http$request$$FakeXMLHttpRequest.DONE) {
this.dispatchEvent(new $$$src$fake$xml$http$request$$_Event("load", false, false, this));
this.dispatchEvent(new $$$src$fake$xml$http$request$$_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.async) {
this._readyStateChange(FakeXMLHttpRequest.HEADERS_RECEIVED);
} else {
this.readyState = FakeXMLHttpRequest.HEADERS_RECEIVED;
}
},
/*
Sets the FakeXMLHttpRequest object's response headers and
places the object into readyState 2
*/
_setResponseHeaders: function _setResponseHeaders(headers) {
this.responseHeaders = {};
/*
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);
for (var header in headers) {
if (headers.hasOwnProperty(header)) {
this.responseHeaders[header] = headers[header];
}
}
var chunkSize = this.chunkSize || 10;
var index = 0;
this.responseText = "";
do {
if (this.async) {
this._readyStateChange($$$src$fake$xml$http$request$$FakeXMLHttpRequest.HEADERS_RECEIVED);
} else {
this.readyState = $$$src$fake$xml$http$request$$FakeXMLHttpRequest.HEADERS_RECEIVED;
this._readyStateChange(FakeXMLHttpRequest.LOADING);
}
},
this.responseText += body.substring(index, index + chunkSize);
index += chunkSize;
} while (index < body.length);
var type = this.getResponseHeader("Content-Type");
/*
Sets the FakeXMLHttpRequest object's response body and
if body text is XML, sets responseXML to parsed document
object
*/
_setResponseBody: function _setResponseBody(body) {
$$$src$fake$xml$http$request$$verifyRequestSent(this);
$$$src$fake$xml$http$request$$verifyHeadersReceived(this);
$$$src$fake$xml$http$request$$verifyResponseBodyType(body);
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
}
}
var chunkSize = this.chunkSize || 10;
var index = 0;
this.responseText = "";
if (this.async) {
this._readyStateChange(FakeXMLHttpRequest.DONE);
} else {
this.readyState = FakeXMLHttpRequest.DONE;
}
},
do {
if (this.async) {
this._readyStateChange($$$src$fake$xml$http$request$$FakeXMLHttpRequest.LOADING);
}
/*
Forces a response on to the FakeXMLHttpRequest object.
this.responseText += body.substring(index, index + chunkSize);
index += chunkSize;
} while (index < body.length);
This is the public API for faking responses. This function
takes a number status, headers object, and string body:
var type = this.getResponseHeader("Content-Type");
```
xhr.respond(404, {Content-Type: 'text/plain'}, "Sorry. This object was not found.")
if (this.responseText && (!type || /(text\/xml)|(application\/xml)|(\+xml)/.test(type))) {
try {
this.responseXML = $$$src$fake$xml$http$request$$parseXML(this.responseText);
} catch (e) {
// Unable to parse XML - no biggie
}
}
```
*/
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($$$src$fake$xml$http$request$$FakeXMLHttpRequest.DONE);
} else {
this.readyState = $$$src$fake$xml$http$request$$FakeXMLHttpRequest.DONE;
}
},
for (var property in FakeXMLHttpRequestProto) {
FakeXMLHttpRequest.prototype[property] = FakeXMLHttpRequestProto[property];
}
/*
Forces a response on to the FakeXMLHttpRequest object.
function verifyState(xhr) {
if (xhr.readyState !== FakeXMLHttpRequest.OPENED) {
throw new Error("INVALID_STATE_ERR");
}
This is the public API for faking responses. This function
takes a number status, headers object, and string body:
if (xhr.sendFlag) {
throw new Error("INVALID_STATE_ERR");
}
}
```
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 = $$$src$fake$xml$http$request$$httpStatusCodes[this.status];
this._setResponseBody(body || "");
function verifyRequestSent(xhr) {
if (xhr.readyState == FakeXMLHttpRequest.DONE) {
throw new Error("Request done");
}
};
}
function $$$src$fake$xml$http$request$$verifyState(xhr) {
if (xhr.readyState !== $$$src$fake$xml$http$request$$FakeXMLHttpRequest.OPENED) {
throw new Error("INVALID_STATE_ERR");
function verifyHeadersReceived(xhr) {
if (xhr.async && xhr.readyState != FakeXMLHttpRequest.HEADERS_RECEIVED) {
throw new Error("No headers received");
}
}
if (xhr.sendFlag) {
throw new Error("INVALID_STATE_ERR");
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;
}
}
}
var fake_xml_http_request = FakeXMLHttpRequest;
return fake_xml_http_request;
function $$$src$fake$xml$http$request$$verifyRequestSent(xhr) {
if (xhr.readyState == $$$src$fake$xml$http$request$$FakeXMLHttpRequest.DONE) {
throw new Error("Request done");
}
}
function $$$src$fake$xml$http$request$$verifyHeadersReceived(xhr) {
if (xhr.async && xhr.readyState != $$$src$fake$xml$http$request$$FakeXMLHttpRequest.HEADERS_RECEIVED) {
throw new Error("No headers received");
}
}
function $$$src$fake$xml$http$request$$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;
}
}
var $$$src$fake$xml$http$request$$default = $$$src$fake$xml$http$request$$FakeXMLHttpRequest;
window.FakeXMLHttpRequest = $$$src$fake$xml$http$request$$default;
}).call(this);
//# sourceMappingURL=fake_xml_http_request.js.map
}));
{
"name": "fake-xml-http-request",
"version": "1.1.3",
"version": "1.2.0",
"description": "test infrastructure for a fake XMLHttpRequest object",

@@ -8,3 +8,3 @@ "main": "fake_xml_http_request.js",

"scripts": {
"test": "compile-modules convert builds/globals.js -o fake_xml_http_request.js && karma start --single-run --browsers PhantomJS"
"test": "esperanto -i src/fake-xml-http-request.js -t umd -b -n FakeXMLHttpRequest -o fake_xml_http_request.js && ./node_modules/karma/bin/karma start --single-run --browsers PhantomJS"
},

@@ -17,15 +17,9 @@ "author": {

"devDependencies": {
"qunitjs": "~1.14.0",
"karma-qunit": "~0.1.1",
"karma-script-launcher": "~0.1.0",
"karma-firefox-launcher": "~0.1.0",
"karma-chrome-launcher": "~0.1.0",
"karma-html2js-preprocessor": "~0.1.0",
"karma-jasmine": "~0.1.3",
"karma-requirejs": "~0.2.0",
"karma-coffee-preprocessor": "~0.1.0",
"karma-phantomjs-launcher": "~0.1.0",
"karma": "~0.10.2",
"es6-module-transpiler": "^0.9.3"
"esperanto": "^0.6.15",
"karma": "~0.12.31",
"karma-chrome-launcher": "~0.1.7",
"karma-firefox-launcher": "~0.1.4",
"karma-phantomjs-launcher": "~0.1.4",
"karma-qunit": "~0.1.4"
}
}

@@ -136,53 +136,12 @@ /**

/*
Constructor for a fake window.XMLHttpRequest
*/
function FakeXMLHttpRequest() {
this.readyState = FakeXMLHttpRequest.UNSENT;
this.requestHeaders = {};
this.requestBody = null;
this.status = 0;
this.statusText = "";
function EventedObject() {
this._eventListeners = {};
var events = ["loadstart", "load", "abort", "loadend"];
var events = ["loadstart", "progress", "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,
EventedObject.prototype = {
/*
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.responseXML = null;
this.requestHeaders = {};
this.sendFlag = false;
this._readyStateChange(FakeXMLHttpRequest.OPENED);
},
/*
Duplicates the behavior of native XMLHttpRequest's addEventListener function

@@ -227,2 +186,62 @@ */

/*
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);
}
}
/*
Constructor for a fake window.XMLHttpRequest
*/
function FakeXMLHttpRequest() {
EventedObject.call(this);
this.readyState = FakeXMLHttpRequest.UNSENT;
this.requestHeaders = {};
this.requestBody = null;
this.status = 0;
this.statusText = "";
this.upload = new EventedObject();
}
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,
/*
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.responseXML = null;
this.requestHeaders = {};
this.sendFlag = false;
this._readyStateChange(FakeXMLHttpRequest.OPENED);
},
/*
Duplicates the behavior of native XMLHttpRequest's setRequestHeader function

@@ -376,4 +395,2 @@ */

/*

@@ -438,2 +455,6 @@ Sets the FakeXMLHttpRequest object's response body and

for (var property in FakeXMLHttpRequestProto) {
FakeXMLHttpRequest.prototype[property] = FakeXMLHttpRequestProto[property];
}
function verifyState(xhr) {

@@ -440,0 +461,0 @@ if (xhr.readyState !== FakeXMLHttpRequest.OPENED) {

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc