twreporter-react
Version:
React-Redux site for The Reporter Foundation in Taiwan
1,702 lines (1,409 loc) • 719 kB
JavaScript
/**
* Sinon.JS 1.17.0, 2015/11/25
*
* @author Christian Johansen (christian@cjohansen.no)
* @author Contributors: https://github.com/cjohansen/Sinon.JS/blob/master/AUTHORS
*
* (The BSD License)
*
* Copyright (c) 2010-2014, Christian Johansen, christian@cjohansen.no
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of Christian Johansen nor the names of his contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.sinon = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
"use strict";
// Adapted from https://developer.mozilla.org/en/docs/ECMAScript_DontEnum_attribute#JScript_DontEnum_Bug
var hasDontEnumBug = (function () {
var obj = {
constructor: function () {
return "0";
},
toString: function () {
return "1";
},
valueOf: function () {
return "2";
},
toLocaleString: function () {
return "3";
},
prototype: function () {
return "4";
},
isPrototypeOf: function () {
return "5";
},
propertyIsEnumerable: function () {
return "6";
},
hasOwnProperty: function () {
return "7";
},
length: function () {
return "8";
},
unique: function () {
return "9";
}
};
var result = [];
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
result.push(obj[prop]());
}
}
return result.join("") !== "0123456789";
})();
/* Public: Extend target in place with all (own) properties from sources in-order. Thus, last source will
* override properties in previous sources.
*
* target - The Object to extend
* sources - Objects to copy properties from.
*
* Returns the extended target
*/
module.exports = function extend(target /*, sources */) {
var sources = Array.prototype.slice.call(arguments, 1);
var source, i, prop;
for (i = 0; i < sources.length; i++) {
source = sources[i];
for (prop in source) {
if (source.hasOwnProperty(prop)) {
target[prop] = source[prop];
}
}
// Make sure we copy (own) toString method even when in JScript with DontEnum bug
// See https://developer.mozilla.org/en/docs/ECMAScript_DontEnum_attribute#JScript_DontEnum_Bug
if (hasDontEnumBug && source.hasOwnProperty("toString") && source.toString !== target.toString) {
target.toString = source.toString;
}
}
return target;
};
},{}],2:[function(require,module,exports){
/**
* Logs errors
*
* @author Christian Johansen (christian@cjohansen.no)
* @license BSD
*
* Copyright (c) 2010-2014 Christian Johansen
*/
"use strict";
var sinon = require("./util/core");
// cache a reference to setTimeout, so that our reference won't be stubbed out
// when using fake timers and errors will still get logged
// https://github.com/cjohansen/Sinon.JS/issues/381
var realSetTimeout = setTimeout;
function log() {}
function logError(label, err) {
var msg = label + " threw exception: ";
function throwLoggedError() {
err.message = msg + err.message;
throw err;
}
sinon.log(msg + "[" + err.name + "] " + err.message);
if (err.stack) {
sinon.log(err.stack);
}
if (logError.useImmediateExceptions) {
throwLoggedError();
} else {
logError.setTimeout(throwLoggedError, 0);
}
}
// When set to true, any errors logged will be thrown immediately;
// If set to false, the errors will be thrown in separate execution frame.
logError.useImmediateExceptions = true;
// wrap realSetTimeout with something we can stub in tests
logError.setTimeout = function (func, timeout) {
realSetTimeout(func, timeout);
};
var exports = {};
exports.log = sinon.log = log;
exports.logError = sinon.logError = logError;
},{"./util/core":12}],3:[function(require,module,exports){
"use strict";
module.exports = function calledInOrder(spies) {
for (var i = 1, l = spies.length; i < l; i++) {
if (!spies[i - 1].calledBefore(spies[i]) || !spies[i].called) {
return false;
}
}
return true;
};
},{}],4:[function(require,module,exports){
"use strict";
var Klass = function () {};
module.exports = function create(proto) {
Klass.prototype = proto;
return new Klass();
};
},{}],5:[function(require,module,exports){
"use strict";
var div = typeof document !== "undefined" && document.createElement("div");
function isReallyNaN(val) {
return val !== val;
}
function isDOMNode(obj) {
var success = false;
try {
obj.appendChild(div);
success = div.parentNode === obj;
} catch (e) {
return false;
} finally {
try {
obj.removeChild(div);
} catch (e) {
// Remove failed, not much we can do about that
}
}
return success;
}
function isElement(obj) {
return div && obj && obj.nodeType === 1 && isDOMNode(obj);
}
var deepEqual = module.exports = function deepEqual(a, b) {
if (typeof a !== "object" || typeof b !== "object") {
return isReallyNaN(a) && isReallyNaN(b) || a === b;
}
if (isElement(a) || isElement(b)) {
return a === b;
}
if (a === b) {
return true;
}
if ((a === null && b !== null) || (a !== null && b === null)) {
return false;
}
if (a instanceof RegExp && b instanceof RegExp) {
return (a.source === b.source) && (a.global === b.global) &&
(a.ignoreCase === b.ignoreCase) && (a.multiline === b.multiline);
}
var aString = Object.prototype.toString.call(a);
if (aString !== Object.prototype.toString.call(b)) {
return false;
}
if (aString === "[object Date]") {
return a.valueOf() === b.valueOf();
}
var prop;
var aLength = 0;
var bLength = 0;
if (aString === "[object Array]" && a.length !== b.length) {
return false;
}
for (prop in a) {
if (Object.prototype.hasOwnProperty.call(a, prop)) {
aLength += 1;
if (!(prop in b)) {
return false;
}
// allow alternative function for recursion
if (!(arguments[2] || deepEqual)(a[prop], b[prop])) {
return false;
}
}
}
for (prop in b) {
if (Object.prototype.hasOwnProperty.call(b, prop)) {
bLength += 1;
}
}
return aLength === bLength;
};
deepEqual.use = function (match) {
return function deepEqual$matcher(a, b) {
if (match.isMatcher(a)) {
return a.test(b);
}
return deepEqual(a, b, deepEqual$matcher);
};
};
},{}],6:[function(require,module,exports){
"use strict";
module.exports = {
injectIntoThis: true,
injectInto: null,
properties: ["spy", "stub", "mock", "clock", "server", "requests"],
useFakeTimers: true,
useFakeServer: true
};
},{}],7:[function(require,module,exports){
/**
* Format functions
*
* @author Christian Johansen (christian@cjohansen.no)
* @license BSD
*
* Copyright (c) 2010-2014 Christian Johansen
*/
"use strict";
var formatio = require("formatio");
var formatter = formatio.configure({
quoteStrings: false,
limitChildrenCount: 250
});
module.exports = function format() {
return formatter.ascii.apply(formatter, arguments);
};
},{"formatio":24}],8:[function(require,module,exports){
"use strict";
module.exports = function functionName(func) {
var name = func.displayName || func.name;
var matches;
// Use function decomposition as a last resort to get function
// name. Does not rely on function decomposition to work - if it
// doesn't debugging will be slightly less informative
// (i.e. toString will say 'spy' rather than 'myFunc').
if (!name && (matches = func.toString().match(/function ([^\s\(]+)/))) {
name = matches[1];
}
return name;
};
},{}],9:[function(require,module,exports){
"use strict";
module.exports = function toString() {
var i, prop, thisValue;
if (this.getCall && this.callCount) {
i = this.callCount;
while (i--) {
thisValue = this.getCall(i).thisValue;
for (prop in thisValue) {
if (thisValue[prop] === this) {
return prop;
}
}
}
}
return this.displayName || "sinon fake";
};
},{}],10:[function(require,module,exports){
"use strict";
var defaultConfig = require("./default-config");
module.exports = function getConfig(custom) {
var config = {};
var prop;
custom = custom || {};
for (prop in defaultConfig) {
if (defaultConfig.hasOwnProperty(prop)) {
config[prop] = custom.hasOwnProperty(prop) ? custom[prop] : defaultConfig[prop];
}
}
return config;
};
},{"./default-config":6}],11:[function(require,module,exports){
"use strict";
module.exports = function getPropertyDescriptor(object, property) {
var proto = object;
var descriptor;
while (proto && !(descriptor = Object.getOwnPropertyDescriptor(proto, property))) {
proto = Object.getPrototypeOf(proto);
}
return descriptor;
};
},{}],12:[function(require,module,exports){
/**
* Sinon core utilities. For internal use only.
*
* @author Christian Johansen (christian@cjohansen.no)
* @license BSD
*
* Copyright (c) 2010-2013 Christian Johansen
*/
"use strict";
exports.wrapMethod = require("./wrap-method");
exports.create = require("./create");
exports.deepEqual = require("./deep-equal");
exports.format = require("./format");
exports.functionName = require("./function-name");
exports.functionToString = require("./function-to-string");
exports.objectKeys = require("./object-keys");
exports.getPropertyDescriptor = require("./get-property-descriptor");
exports.getConfig = require("./get-config");
exports.defaultConfig = require("./default-config");
exports.timesInWords = require("./times-in-words");
exports.calledInOrder = require("./called-in-order");
exports.orderByFirstCall = require("./order-by-first-call");
exports.restore = require("./restore");
},{"./called-in-order":3,"./create":4,"./deep-equal":5,"./default-config":6,"./format":7,"./function-name":8,"./function-to-string":9,"./get-config":10,"./get-property-descriptor":11,"./object-keys":13,"./order-by-first-call":14,"./restore":15,"./times-in-words":16,"./wrap-method":17}],13:[function(require,module,exports){
"use strict";
var hasOwn = Object.prototype.hasOwnProperty;
module.exports = function objectKeys(obj) {
if (obj !== Object(obj)) {
throw new TypeError("sinon.objectKeys called on a non-object");
}
var keys = [];
var key;
for (key in obj) {
if (hasOwn.call(obj, key)) {
keys.push(key);
}
}
return keys;
};
},{}],14:[function(require,module,exports){
"use strict";
module.exports = function orderByFirstCall(spies) {
return spies.sort(function (a, b) {
// uuid, won't ever be equal
var aCall = a.getCall(0);
var bCall = b.getCall(0);
var aId = aCall && aCall.callId || -1;
var bId = bCall && bCall.callId || -1;
return aId < bId ? -1 : 1;
});
};
},{}],15:[function(require,module,exports){
"use strict";
function isRestorable(obj) {
return typeof obj === "function" && typeof obj.restore === "function" && obj.restore.sinon;
}
module.exports = function restore(object) {
if (object !== null && typeof object === "object") {
for (var prop in object) {
if (isRestorable(object[prop])) {
object[prop].restore();
}
}
} else if (isRestorable(object)) {
object.restore();
}
};
},{}],16:[function(require,module,exports){
"use strict";
var array = [null, "once", "twice", "thrice"];
module.exports = function timesInWords(count) {
return array[count] || (count || 0) + " times";
};
},{}],17:[function(require,module,exports){
"use strict";
var getPropertyDescriptor = require("./get-property-descriptor");
var objectKeys = require("./object-keys");
var hasOwn = Object.prototype.hasOwnProperty;
function isFunction(obj) {
return typeof obj === "function" || !!(obj && obj.constructor && obj.call && obj.apply);
}
function mirrorProperties(target, source) {
for (var prop in source) {
if (!hasOwn.call(target, prop)) {
target[prop] = source[prop];
}
}
}
// Cheap way to detect if we have ES5 support.
var hasES5Support = "keys" in Object;
module.exports = function wrapMethod(object, property, method) {
if (!object) {
throw new TypeError("Should wrap property of object");
}
if (typeof method !== "function" && typeof method !== "object") {
throw new TypeError("Method wrapper should be a function or a property descriptor");
}
function checkWrappedMethod(wrappedMethod) {
var error;
if (!isFunction(wrappedMethod)) {
error = new TypeError("Attempted to wrap " + (typeof wrappedMethod) + " property " +
property + " as function");
} else if (wrappedMethod.restore && wrappedMethod.restore.sinon) {
error = new TypeError("Attempted to wrap " + property + " which is already wrapped");
} else if (wrappedMethod.calledBefore) {
var verb = wrappedMethod.returns ? "stubbed" : "spied on";
error = new TypeError("Attempted to wrap " + property + " which is already " + verb);
}
if (error) {
if (wrappedMethod && wrappedMethod.stackTrace) {
error.stack += "\n--------------\n" + wrappedMethod.stackTrace;
}
throw error;
}
}
var error, wrappedMethod, i;
// IE 8 does not support hasOwnProperty on the window object and Firefox has a problem
// when using hasOwn.call on objects from other frames.
var owned = object.hasOwnProperty ? object.hasOwnProperty(property) : hasOwn.call(object, property);
if (hasES5Support) {
var methodDesc = (typeof method === "function") ? {value: method} : method;
var wrappedMethodDesc = getPropertyDescriptor(object, property);
if (!wrappedMethodDesc) {
error = new TypeError("Attempted to wrap " + (typeof wrappedMethod) + " property " +
property + " as function");
} else if (wrappedMethodDesc.restore && wrappedMethodDesc.restore.sinon) {
error = new TypeError("Attempted to wrap " + property + " which is already wrapped");
}
if (error) {
if (wrappedMethodDesc && wrappedMethodDesc.stackTrace) {
error.stack += "\n--------------\n" + wrappedMethodDesc.stackTrace;
}
throw error;
}
var types = objectKeys(methodDesc);
for (i = 0; i < types.length; i++) {
wrappedMethod = wrappedMethodDesc[types[i]];
checkWrappedMethod(wrappedMethod);
}
mirrorProperties(methodDesc, wrappedMethodDesc);
for (i = 0; i < types.length; i++) {
mirrorProperties(methodDesc[types[i]], wrappedMethodDesc[types[i]]);
}
Object.defineProperty(object, property, methodDesc);
} else {
wrappedMethod = object[property];
checkWrappedMethod(wrappedMethod);
object[property] = method;
method.displayName = property;
}
method.displayName = property;
// Set up a stack trace which can be used later to find what line of
// code the original method was created on.
method.stackTrace = (new Error("Stack Trace for original")).stack;
method.restore = function () {
// For prototype properties try to reset by delete first.
// If this fails (ex: localStorage on mobile safari) then force a reset
// via direct assignment.
if (!owned) {
// In some cases `delete` may throw an error
try {
delete object[property];
} catch (e) {} // eslint-disable-line no-empty
// For native code functions `delete` fails without throwing an error
// on Chrome < 43, PhantomJS, etc.
} else if (hasES5Support) {
Object.defineProperty(object, property, wrappedMethodDesc);
}
if (hasES5Support) {
var descriptor = getPropertyDescriptor(object, property);
if (descriptor && descriptor.value === method) {
object[property] = wrappedMethod;
}
}
else {
// Use strict equality comparison to check failures then force a reset
// via direct assignment.
if (object[property] === method) {
object[property] = wrappedMethod;
}
}
};
method.restore.sinon = true;
if (!hasES5Support) {
mirrorProperties(method, wrappedMethod);
}
return method;
};
},{"./get-property-descriptor":11,"./object-keys":13}],18:[function(require,module,exports){
/**
* 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
*/
"use strict";
var push = [].push;
var sinon = require("./core");
sinon.Event = function Event(type, bubbles, cancelable, target) {
this.initEvent(type, bubbles, cancelable, target);
};
sinon.Event.prototype = {
initEvent: function (type, bubbles, cancelable, target) {
this.type = type;
this.bubbles = bubbles;
this.cancelable = cancelable;
this.target = target;
},
stopPropagation: function () {},
preventDefault: function () {
this.defaultPrevented = true;
}
};
sinon.ProgressEvent = function ProgressEvent(type, progressEventRaw, target) {
this.initEvent(type, false, false, target);
this.loaded = typeof progressEventRaw.loaded === "number" ? progressEventRaw.loaded : null;
this.total = typeof progressEventRaw.total === "number" ? progressEventRaw.total : null;
this.lengthComputable = !!progressEventRaw.total;
};
sinon.ProgressEvent.prototype = new sinon.Event();
sinon.ProgressEvent.prototype.constructor = sinon.ProgressEvent;
sinon.CustomEvent = function CustomEvent(type, customData, target) {
this.initEvent(type, false, false, target);
this.detail = customData.detail || null;
};
sinon.CustomEvent.prototype = new sinon.Event();
sinon.CustomEvent.prototype.constructor = sinon.CustomEvent;
sinon.EventTarget = {
addEventListener: function addEventListener(event, listener) {
this.eventListeners = this.eventListeners || {};
this.eventListeners[event] = this.eventListeners[event] || [];
push.call(this.eventListeners[event], listener);
},
removeEventListener: function removeEventListener(event, listener) {
var listeners = this.eventListeners && this.eventListeners[event] || [];
for (var i = 0, l = listeners.length; i < l; ++i) {
if (listeners[i] === listener) {
return listeners.splice(i, 1);
}
}
},
dispatchEvent: function dispatchEvent(event) {
var type = event.type;
var listeners = this.eventListeners && 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;
}
};
},{"./core":12}],19:[function(require,module,exports){
/**
* The Sinon "server" mimics a web server that receives requests from
* sinon.FakeXMLHttpRequest and provides an API to respond to those requests,
* both synchronously and asynchronously. To respond synchronuously, canned
* answers have to be provided upfront.
*
* @author Christian Johansen (christian@cjohansen.no)
* @license BSD
*
* Copyright (c) 2010-2013 Christian Johansen
*/
"use strict";
require("./fake_xdomain_request");
require("./fake_xml_http_request");
require("../log_error");
var push = [].push;
var sinon = require("./core");
function responseArray(handler) {
var response = handler;
if (Object.prototype.toString.call(handler) !== "[object Array]") {
response = [200, {}, handler];
}
if (typeof response[2] !== "string") {
throw new TypeError("Fake server response body should be string, but was " +
typeof response[2]);
}
return response;
}
var wloc = typeof window !== "undefined" ? window.location : {};
var rCurrLoc = new RegExp("^" + wloc.protocol + "//" + wloc.host);
function matchOne(response, reqMethod, reqUrl) {
var rmeth = response.method;
var matchMethod = !rmeth || rmeth.toLowerCase() === reqMethod.toLowerCase();
var url = response.url;
var matchUrl = !url || url === reqUrl || (typeof url.test === "function" && url.test(reqUrl));
return matchMethod && matchUrl;
}
function match(response, request) {
var requestUrl = request.url;
if (!/^https?:\/\//.test(requestUrl) || rCurrLoc.test(requestUrl)) {
requestUrl = requestUrl.replace(rCurrLoc, "");
}
if (matchOne(response, this.getHTTPMethod(request), requestUrl)) {
if (typeof response.response === "function") {
var ru = response.url;
var args = [request].concat(ru && typeof ru.exec === "function" ? ru.exec(requestUrl).slice(1) : []);
return response.response.apply(response, args);
}
return true;
}
return false;
}
sinon.fakeServer = {
create: function (config) {
var server = sinon.create(this);
server.configure(config);
if (!sinon.xhr.supportsCORS) {
this.xhr = sinon.useFakeXDomainRequest();
} else {
this.xhr = sinon.useFakeXMLHttpRequest();
}
server.requests = [];
this.xhr.onCreate = function (xhrObj) {
server.addRequest(xhrObj);
};
return server;
},
configure: function (config) {
var whitelist = {
"autoRespond": true,
"autoRespondAfter": true,
"respondImmediately": true,
"fakeHTTPMethods": true
};
var setting;
config = config || {};
for (setting in config) {
if (whitelist.hasOwnProperty(setting) && config.hasOwnProperty(setting)) {
this[setting] = config[setting];
}
}
},
addRequest: function addRequest(xhrObj) {
var server = this;
push.call(this.requests, xhrObj);
xhrObj.onSend = function () {
server.handleRequest(this);
if (server.respondImmediately) {
server.respond();
} else if (server.autoRespond && !server.responding) {
setTimeout(function () {
server.responding = false;
server.respond();
}, server.autoRespondAfter || 10);
server.responding = true;
}
};
},
getHTTPMethod: function getHTTPMethod(request) {
if (this.fakeHTTPMethods && /post/i.test(request.method)) {
var matches = (request.requestBody || "").match(/_method=([^\b;]+)/);
return matches ? matches[1] : request.method;
}
return request.method;
},
handleRequest: function handleRequest(xhr) {
if (xhr.async) {
if (!this.queue) {
this.queue = [];
}
push.call(this.queue, xhr);
} else {
this.processRequest(xhr);
}
},
log: function log(response, request) {
var str;
str = "Request:\n" + sinon.format(request) + "\n\n";
str += "Response:\n" + sinon.format(response) + "\n\n";
sinon.log(str);
},
respondWith: function respondWith(method, url, body) {
if (arguments.length === 1 && typeof method !== "function") {
this.response = responseArray(method);
return;
}
if (!this.responses) {
this.responses = [];
}
if (arguments.length === 1) {
body = method;
url = method = null;
}
if (arguments.length === 2) {
body = url;
url = method;
method = null;
}
push.call(this.responses, {
method: method,
url: url,
response: typeof body === "function" ? body : responseArray(body)
});
},
respond: function respond() {
if (arguments.length > 0) {
this.respondWith.apply(this, arguments);
}
var queue = this.queue || [];
var requests = queue.splice(0, queue.length);
for (var i = 0; i < requests.length; i++) {
this.processRequest(requests[i]);
}
},
processRequest: function processRequest(request) {
try {
if (request.aborted) {
return;
}
var response = this.response || [404, {}, ""];
if (this.responses) {
for (var l = this.responses.length, i = l - 1; i >= 0; i--) {
if (match.call(this, this.responses[i], request)) {
response = this.responses[i].response;
break;
}
}
}
if (request.readyState !== 4) {
this.log(response, request);
request.respond(response[0], response[1], response[2]);
}
} catch (e) {
sinon.logError("Fake server request processing", e);
}
},
restore: function restore() {
return this.xhr.restore && this.xhr.restore.apply(this.xhr, arguments);
}
};
},{"../log_error":2,"./core":12,"./fake_xdomain_request":22,"./fake_xml_http_request":23}],20:[function(require,module,exports){
/**
* Add-on for sinon.fakeServer that automatically handles a fake timer along with
* the FakeXMLHttpRequest. The direct inspiration for this add-on is jQuery
* 1.3.x, which does not use xhr object's onreadystatehandler at all - instead,
* it polls the object for completion with setInterval. Dispite the direct
* motivation, there is nothing jQuery-specific in this file, so it can be used
* in any environment where the ajax implementation depends on setInterval or
* setTimeout.
*
* @author Christian Johansen (christian@cjohansen.no)
* @license BSD
*
* Copyright (c) 2010-2013 Christian Johansen
*/
"use strict";
require("./fake_server");
require("./fake_timers");
var sinon = require("./core");
function Server() {}
Server.prototype = sinon.fakeServer;
sinon.fakeServerWithClock = new Server();
sinon.fakeServerWithClock.addRequest = function addRequest(xhr) {
if (xhr.async) {
if (typeof setTimeout.clock === "object") {
this.clock = setTimeout.clock;
} else {
this.clock = sinon.useFakeTimers();
this.resetClock = true;
}
if (!this.longestTimeout) {
var clockSetTimeout = this.clock.setTimeout;
var clockSetInterval = this.clock.setInterval;
var server = this;
this.clock.setTimeout = function (fn, timeout) {
server.longestTimeout = Math.max(timeout, server.longestTimeout || 0);
return clockSetTimeout.apply(this, arguments);
};
this.clock.setInterval = function (fn, timeout) {
server.longestTimeout = Math.max(timeout, server.longestTimeout || 0);
return clockSetInterval.apply(this, arguments);
};
}
}
return sinon.fakeServer.addRequest.call(this, xhr);
};
sinon.fakeServerWithClock.respond = function respond() {
var returnVal = sinon.fakeServer.respond.apply(this, arguments);
if (this.clock) {
this.clock.tick(this.longestTimeout || 0);
this.longestTimeout = 0;
if (this.resetClock) {
this.clock.restore();
this.resetClock = false;
}
}
return returnVal;
};
sinon.fakeServerWithClock.restore = function restore() {
if (this.clock) {
this.clock.restore();
}
return sinon.fakeServer.restore.apply(this, arguments);
};
},{"./core":12,"./fake_server":19,"./fake_timers":21}],21:[function(require,module,exports){
/**
* Fake timer API
* setTimeout
* setInterval
* clearTimeout
* clearInterval
* tick
* reset
* Date
*
* Inspired by jsUnitMockTimeOut from JsUnit
*
* @author Christian Johansen (christian@cjohansen.no)
* @license BSD
*
* Copyright (c) 2010-2013 Christian Johansen
*/
"use strict";
var s = require("./core");
var llx = require("lolex");
s.useFakeTimers = function () {
var now;
var methods = Array.prototype.slice.call(arguments);
if (typeof methods[0] === "string") {
now = 0;
} else {
now = methods.shift();
}
var clock = llx.install(now || 0, methods);
clock.restore = clock.uninstall;
return clock;
};
s.clock = {
create: function (now) {
return llx.createClock(now);
}
};
s.timers = {
setTimeout: setTimeout,
clearTimeout: clearTimeout,
setImmediate: (typeof setImmediate !== "undefined" ? setImmediate : undefined),
clearImmediate: (typeof clearImmediate !== "undefined" ? clearImmediate : undefined),
setInterval: setInterval,
clearInterval: clearInterval,
Date: Date
};
},{"./core":12,"lolex":25}],22:[function(require,module,exports){
(function (global){
/**
* Fake XDomainRequest object
*/
"use strict";
require("../extend");
require("./event");
require("../log_error");
var xdr = { XDomainRequest: global.XDomainRequest };
xdr.GlobalXDomainRequest = global.XDomainRequest;
xdr.supportsXDR = typeof xdr.GlobalXDomainRequest !== "undefined";
xdr.workingXDR = xdr.supportsXDR ? xdr.GlobalXDomainRequest : false;
var sinon = require("./core");
sinon.xdr = xdr;
function FakeXDomainRequest() {
this.readyState = FakeXDomainRequest.UNSENT;
this.requestBody = null;
this.requestHeaders = {};
this.status = 0;
this.timeout = null;
if (typeof FakeXDomainRequest.onCreate === "function") {
FakeXDomainRequest.onCreate(this);
}
}
function verifyState(x) {
if (x.readyState !== FakeXDomainRequest.OPENED) {
throw new Error("INVALID_STATE_ERR");
}
if (x.sendFlag) {
throw new Error("INVALID_STATE_ERR");
}
}
function verifyRequestSent(x) {
if (x.readyState === FakeXDomainRequest.UNSENT) {
throw new Error("Request not sent");
}
if (x.readyState === FakeXDomainRequest.DONE) {
throw new Error("Request done");
}
}
function verifyResponseBodyType(body) {
if (typeof body !== "string") {
var error = new Error("Attempted to respond to fake XDomainRequest with " +
body + ", which is not a string.");
error.name = "InvalidBodyException";
throw error;
}
}
sinon.extend(FakeXDomainRequest.prototype, sinon.EventTarget, {
open: function open(method, url) {
this.method = method;
this.url = url;
this.responseText = null;
this.sendFlag = false;
this.readyStateChange(FakeXDomainRequest.OPENED);
},
readyStateChange: function readyStateChange(state) {
this.readyState = state;
var eventName = "";
switch (this.readyState) {
case FakeXDomainRequest.UNSENT:
break;
case FakeXDomainRequest.OPENED:
break;
case FakeXDomainRequest.LOADING:
if (this.sendFlag) {
//raise the progress event
eventName = "onprogress";
}
break;
case FakeXDomainRequest.DONE:
if (this.isTimeout) {
eventName = "ontimeout";
} else if (this.errorFlag || (this.status < 200 || this.status > 299)) {
eventName = "onerror";
} else {
eventName = "onload";
}
break;
}
// raising event (if defined)
if (eventName) {
if (typeof this[eventName] === "function") {
try {
this[eventName]();
} catch (e) {
sinon.logError("Fake XHR " + eventName + " handler", e);
}
}
}
},
send: function send(data) {
verifyState(this);
if (!/^(head)$/i.test(this.method)) {
this.requestBody = data;
}
this.requestHeaders["Content-Type"] = "text/plain;charset=utf-8";
this.errorFlag = false;
this.sendFlag = true;
this.readyStateChange(FakeXDomainRequest.OPENED);
if (typeof this.onSend === "function") {
this.onSend(this);
}
},
abort: function abort() {
this.aborted = true;
this.responseText = null;
this.errorFlag = true;
if (this.readyState > sinon.FakeXDomainRequest.UNSENT && this.sendFlag) {
this.readyStateChange(sinon.FakeXDomainRequest.DONE);
this.sendFlag = false;
}
},
setResponseBody: function setResponseBody(body) {
verifyRequestSent(this);
verifyResponseBodyType(body);
var chunkSize = this.chunkSize || 10;
var index = 0;
this.responseText = "";
do {
this.readyStateChange(FakeXDomainRequest.LOADING);
this.responseText += body.substring(index, index + chunkSize);
index += chunkSize;
} while (index < body.length);
this.readyStateChange(FakeXDomainRequest.DONE);
},
respond: function respond(status, contentType, body) {
// content-type ignored, since XDomainRequest does not carry this
// we keep the same syntax for respond(...) as for FakeXMLHttpRequest to ease
// test integration across browsers
this.status = typeof status === "number" ? status : 200;
this.setResponseBody(body || "");
},
simulatetimeout: function simulatetimeout() {
this.status = 0;
this.isTimeout = true;
// Access to this should actually throw an error
this.responseText = undefined;
this.readyStateChange(FakeXDomainRequest.DONE);
}
});
sinon.extend(FakeXDomainRequest, {
UNSENT: 0,
OPENED: 1,
LOADING: 3,
DONE: 4
});
sinon.useFakeXDomainRequest = function useFakeXDomainRequest() {
sinon.FakeXDomainRequest.restore = function restore(keepOnCreate) {
if (xdr.supportsXDR) {
global.XDomainRequest = xdr.GlobalXDomainRequest;
}
delete sinon.FakeXDomainRequest.restore;
if (keepOnCreate !== true) {
delete sinon.FakeXDomainRequest.onCreate;
}
};
if (xdr.supportsXDR) {
global.XDomainRequest = sinon.FakeXDomainRequest;
}
return sinon.FakeXDomainRequest;
};
sinon.FakeXDomainRequest = FakeXDomainRequest;
}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{"../extend":1,"../log_error":2,"./core":12,"./event":18}],23:[function(require,module,exports){
(function (global){
/**
* Fake XMLHttpRequest object
*
* @author Christian Johansen (christian@cjohansen.no)
* @license BSD
*
* Copyright (c) 2010-2013 Christian Johansen
*/
"use strict";
require("../extend");
require("./event");
require("../log_error");
var TextEncoder = require("text-encoding").TextEncoder;
var sinon = require("./core");
function getWorkingXHR(globalScope) {
var supportsXHR = typeof globalScope.XMLHttpRequest !== "undefined";
if (supportsXHR) {
return globalScope.XMLHttpRequest;
}
var supportsActiveX = typeof globalScope.ActiveXObject !== "undefined";
if (supportsActiveX) {
return function () {
return new globalScope.ActiveXObject("MSXML2.XMLHTTP.3.0");
};
}
return false;
}
var supportsProgress = typeof ProgressEvent !== "undefined";
var supportsCustomEvent = typeof CustomEvent !== "undefined";
var supportsFormData = typeof FormData !== "undefined";
var supportsArrayBuffer = typeof ArrayBuffer !== "undefined";
var supportsBlob = typeof Blob === "function";
var sinonXhr = { XMLHttpRequest: global.XMLHttpRequest };
sinonXhr.GlobalXMLHttpRequest = global.XMLHttpRequest;
sinonXhr.GlobalActiveXObject = global.ActiveXObject;
sinonXhr.supportsActiveX = typeof sinonXhr.GlobalActiveXObject !== "undefined";
sinonXhr.supportsXHR = typeof sinonXhr.GlobalXMLHttpRequest !== "undefined";
sinonXhr.workingXHR = getWorkingXHR(global);
sinonXhr.supportsCORS = sinonXhr.supportsXHR && "withCredentials" in (new sinonXhr.GlobalXMLHttpRequest());
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
};
// An upload object is created for each
// FakeXMLHttpRequest and allows upload
// events to be simulated using uploadProgress
// and uploadError.
function UploadProgress() {
this.eventListeners = {
abort: [],
error: [],
load: [],
loadend: [],
progress: []
};
}
UploadProgress.prototype.addEventListener = function addEventListener(event, listener) {
this.eventListeners[event].push(listener);
};
UploadProgress.prototype.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);
}
}
};
UploadProgress.prototype.dispatchEvent = function dispatchEvent(event) {
var listeners = this.eventListeners[event.type] || [];
for (var i = 0, listener; (listener = listeners[i]) != null; i++) {
listener(event);
}
};
// Note that for FakeXMLHttpRequest to work pre ES5
// we lose some of the alignment with the spec.
// To ensure as close a match as possible,
// set responseType before calling open, send or respond;
function FakeXMLHttpRequest() {
this.readyState = FakeXMLHttpRequest.UNSENT;
this.requestHeaders = {};
this.requestBody = null;
this.status = 0;
this.statusText = "";
this.upload = new UploadProgress();
this.responseType = "";
this.response = "";
if (sinonXhr.supportsCORS) {
this.withCredentials = false;
}
var xhr = this;
var events = ["loadstart", "load", "abort", "loadend"];
function addEventListener(eventName) {
xhr.addEventListener(eventName, function (event) {
var listener = xhr["on" + eventName];
if (listener && typeof listener === "function") {
listener.call(this, event);
}
});
}
for (var i = events.length - 1; i >= 0; i--) {
addEventListener(events[i]);
}
if (typeof FakeXMLHttpRequest.onCreate === "function") {
FakeXMLHttpRequest.onCreate(this);
}
}
function verifyState(xhr) {
if (xhr.readyState !== FakeXMLHttpRequest.OPENED) {
throw new Error("INVALID_STATE_ERR");
}
if (xhr.sendFlag) {
throw new Error("INVALID_STATE_ERR");
}
}
function getHeader(headers, header) {
header = header.toLowerCase();
for (var h in headers) {
if (h.toLowerCase() === header) {
return h;
}
}
return null;
}
// filtering to enable a white-list version of Sinon FakeXhr,
// where whitelisted requests are passed through to real XHR
function each(collection, callback) {
if (!collection) {
return;
}
for (var i = 0, l = collection.length; i < l; i += 1) {
callback(collection[i]);
}
}
function some(collection, callback) {
for (var index = 0; index < collection.length; index++) {
if (callback(collection[index]) === true) {
return true;
}
}
return false;
}
// largest arity in XHR is 5 - XHR#open
var apply = function (obj, method, args) {
switch (args.length) {
case 0: return obj[method]();
case 1: return obj[method](args[0]);
case 2: return obj[method](args[0], args[1]);
case 3: return obj[method](args[0], args[1], args[2]);
case 4: return obj[method](args[0], args[1], args[2], args[3]);
case 5: return obj[method](args[0], args[1], args[2], args[3], args[4]);
}
};
FakeXMLHttpRequest.filters = [];
FakeXMLHttpRequest.addFilter = function addFilter(fn) {
this.filters.push(fn);
};
var IE6Re = /MSIE 6/;
FakeXMLHttpRequest.defake = function defake(fakeXhr, xhrArgs) {
var xhr = new sinonXhr.workingXHR(); // eslint-disable-line new-cap
each([
"open",
"setRequestHeader",
"send",
"abort",
"getResponseHeader",
"getAllResponseHeaders",
"addEventListener",
"overrideMimeType",
"removeEventListener"
], function (method) {
fakeXhr[method] = function () {
return apply(xhr, method, arguments);
};
});
var copyAttrs = function (args) {
each(args, function (attr) {
try {
fakeXhr[attr] = xhr[attr];
} catch (e) {
if (!IE6Re.test(navigator.userAgent)) {
throw e;
}
}
});
};
var stateChange = function stateChange() {
fakeXhr.readyState = xhr.readyState;
if (xhr.readyState >= FakeXMLHttpRequest.HEADERS_RECEIVED) {
copyAttrs(["status", "statusText"]);
}
if (xhr.readyState >= FakeXMLHttpRequest.LOADING) {
copyAttrs(["responseText", "response"]);
}
if (xhr.readyState === FakeXMLHttpRequest.DONE) {
copyAttrs(["responseXML"]);
}
if (fakeXhr.onreadystatechange) {
fakeXhr.onreadystatechange.call(fakeXhr, { target: fakeXhr });
}
};
if (xhr.addEventListener) {
for (var event in fakeXhr.eventListeners) {
if (fakeXhr.eventListeners.hasOwnProperty(event)) {
/*eslint-disable no-loop-func*/
each(fakeXhr.eventListeners[event], function (handler) {
xhr.addEventListener(event, handler);
});
/*eslint-enable no-loop-func*/
}
}
xhr.addEventListener("readystatechange", stateChange);
} else {
xhr.onreadystatechange = stateChange;
}
apply(xhr, "open", xhrArgs);
};
FakeXMLHttpRequest.useFilters = false;
function verifyRequestOpened(xhr) {
if (xhr.readyState !== FakeXMLHttpRequest.OPENED) {
throw new Error("INVALID_STATE_ERR - " + xhr.readyState);
}
}
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 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 convertToArrayBuffer(body, encoding) {
return new TextEncoder(encoding || "utf-8").encode(body).buffer;
}
function isXmlContentType(contentType) {
return !contentType || /(text\/xml)|(application\/xml)|(\+xml)/.test(contentType);
}
function convertResponseBody(responseType, contentType, body) {
if (responseType === "" || responseType === "text") {
return body;
} else if (supportsArrayBuffer && responseType === "arraybuffer") {
return convertToArrayBuffer(body);
} else if (responseType === "json") {
try {
return JSON.parse(body);
} catch (e) {
// Return parsing failure as null
return null;
}
} else if (supportsBlob && responseType === "blob") {
var blobOptions = {};
if (contentType) {
blobOptions.type = contentType;
}
return new Blob([convertToArrayBuffer(body)], blobOptions);
} else if (responseType === "document") {
if (isXmlContentType(contentType)) {
return FakeXMLHttpRequest.parseXML(body);
}
return null;
}
throw new Error("Invalid responseType " + responseType);
}
function clearResponse(xhr) {
if (xhr.responseType === "" || xhr.responseType === "text") {
xhr.response = xhr.responseText = "";
} else {
xhr.response = xhr.responseText = null;
}
xhr.responseXML = null;
}
FakeXMLHttpRequest.parseXML = function parseXML(text) {
// Treat empty string as parsing failure
if (text !== "") {
try {
if (typeof DOMParser !== "undefined") {
var parser = new DOMParser();
return parser.parseFromString(text, "text/xml");
}
var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(text);
return xmlDoc;
} catch (e) {
// Unable to parse XML - no biggie
}
}
return null;
};
FakeXMLHttpRequest.statusCodes = {
100: "Continue",
101: "Switching Protocols",
200: "OK",
201: "Created",
202: "Accepted",
203: "Non-Authoritative Information",
204: "No Content",
205: "Reset Content",
206: "Partial Content",
207: "Multi-Status",
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"
};
sinon.xhr = sinonXhr;
sinon.extend(FakeXMLHttpRequest.prototype, sinon.EventTarget, {
async: true,
open: fu