2010-09-30 05:56:17 +08:00
|
|
|
/*
|
|
|
|
|
2015-07-03 07:12:44 +08:00
|
|
|
Jasmine-Ajax - v3.2.0: a set of helpers for testing AJAX requests under the Jasmine
|
2013-01-09 15:47:56 +08:00
|
|
|
BDD framework for JavaScript.
|
2010-09-30 05:56:17 +08:00
|
|
|
|
2015-04-01 12:10:54 +08:00
|
|
|
http://github.com/jasmine/jasmine-ajax
|
2010-09-30 05:56:17 +08:00
|
|
|
|
2015-04-01 12:10:54 +08:00
|
|
|
Jasmine Home page: http://jasmine.github.io/
|
2010-09-30 05:56:17 +08:00
|
|
|
|
2015-04-01 10:28:26 +08:00
|
|
|
Copyright (c) 2008-2015 Pivotal Labs
|
2010-09-30 05:56:17 +08:00
|
|
|
|
2013-01-09 15:47:56 +08:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
a copy of this software and associated documentation files (the
|
|
|
|
"Software"), to deal in the Software without restriction, including
|
|
|
|
without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
the following conditions:
|
2010-09-30 05:56:17 +08:00
|
|
|
|
2013-01-09 15:47:56 +08:00
|
|
|
The above copyright notice and this permission notice shall be
|
|
|
|
included in all copies or substantial portions of the Software.
|
2010-09-30 05:56:17 +08:00
|
|
|
|
2013-01-09 15:47:56 +08:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2010-09-30 05:56:17 +08:00
|
|
|
|
2013-01-09 15:47:56 +08:00
|
|
|
*/
|
2010-09-30 05:56:17 +08:00
|
|
|
|
2016-02-24 07:28:14 +08:00
|
|
|
//Module wrapper to support both browser and CommonJS environment
|
|
|
|
(function (root, factory) {
|
|
|
|
if (typeof exports === 'object' && typeof exports.nodeName !== 'string') {
|
|
|
|
// CommonJS
|
|
|
|
var jasmineRequire = require('jasmine-core');
|
|
|
|
module.exports = factory(root, function() {
|
|
|
|
return jasmineRequire;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Browser globals
|
|
|
|
window.MockAjax = factory(root, getJasmineRequireObj);
|
|
|
|
}
|
|
|
|
}(typeof window !== 'undefined' ? window : global, function (global, getJasmineRequireObj) {
|
|
|
|
|
|
|
|
//
|
2014-08-04 22:46:32 +08:00
|
|
|
getJasmineRequireObj().ajax = function(jRequire) {
|
|
|
|
var $ajax = {};
|
|
|
|
|
|
|
|
$ajax.RequestStub = jRequire.AjaxRequestStub();
|
|
|
|
$ajax.RequestTracker = jRequire.AjaxRequestTracker();
|
|
|
|
$ajax.StubTracker = jRequire.AjaxStubTracker();
|
|
|
|
$ajax.ParamParser = jRequire.AjaxParamParser();
|
2015-06-07 18:23:51 +08:00
|
|
|
$ajax.event = jRequire.AjaxEvent();
|
|
|
|
$ajax.eventBus = jRequire.AjaxEventBus($ajax.event);
|
2015-03-05 03:34:55 +08:00
|
|
|
$ajax.fakeRequest = jRequire.AjaxFakeRequest($ajax.eventBus);
|
2014-08-04 22:46:32 +08:00
|
|
|
$ajax.MockAjax = jRequire.MockAjax($ajax);
|
|
|
|
|
|
|
|
return $ajax.MockAjax;
|
|
|
|
};
|
|
|
|
|
2015-06-07 18:23:51 +08:00
|
|
|
getJasmineRequireObj().AjaxEvent = function() {
|
|
|
|
function now() {
|
|
|
|
return new Date().getTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
function noop() {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Event object
|
|
|
|
// https://dom.spec.whatwg.org/#concept-event
|
|
|
|
function XMLHttpRequestEvent(xhr, type) {
|
|
|
|
this.type = type;
|
|
|
|
this.bubbles = false;
|
|
|
|
this.cancelable = false;
|
|
|
|
this.timeStamp = now();
|
|
|
|
|
|
|
|
this.isTrusted = false;
|
|
|
|
this.defaultPrevented = false;
|
|
|
|
|
|
|
|
// Event phase should be "AT_TARGET"
|
|
|
|
// https://dom.spec.whatwg.org/#dom-event-at_target
|
|
|
|
this.eventPhase = 2;
|
|
|
|
|
|
|
|
this.target = xhr;
|
|
|
|
this.currentTarget = xhr;
|
|
|
|
}
|
|
|
|
|
|
|
|
XMLHttpRequestEvent.prototype.preventDefault = noop;
|
|
|
|
XMLHttpRequestEvent.prototype.stopPropagation = noop;
|
|
|
|
XMLHttpRequestEvent.prototype.stopImmediatePropagation = noop;
|
|
|
|
|
|
|
|
function XMLHttpRequestProgressEvent() {
|
|
|
|
XMLHttpRequestEvent.apply(this, arguments);
|
|
|
|
|
|
|
|
this.lengthComputable = false;
|
|
|
|
this.loaded = 0;
|
|
|
|
this.total = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extend prototype
|
|
|
|
XMLHttpRequestProgressEvent.prototype = XMLHttpRequestEvent.prototype;
|
|
|
|
|
|
|
|
return {
|
|
|
|
event: function(xhr, type) {
|
|
|
|
return new XMLHttpRequestEvent(xhr, type);
|
|
|
|
},
|
|
|
|
|
|
|
|
progressEvent: function(xhr, type) {
|
|
|
|
return new XMLHttpRequestProgressEvent(xhr, type);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
getJasmineRequireObj().AjaxEventBus = function(eventFactory) {
|
|
|
|
function EventBus(source) {
|
2015-03-05 03:34:55 +08:00
|
|
|
this.eventList = {};
|
2015-06-07 18:23:51 +08:00
|
|
|
this.source = source;
|
2015-03-05 03:34:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function ensureEvent(eventList, name) {
|
|
|
|
eventList[name] = eventList[name] || [];
|
|
|
|
return eventList[name];
|
|
|
|
}
|
|
|
|
|
|
|
|
function findIndex(list, thing) {
|
|
|
|
if (list.indexOf) {
|
|
|
|
return list.indexOf(thing);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(var i = 0; i < list.length; i++) {
|
|
|
|
if (thing === list[i]) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
EventBus.prototype.addEventListener = function(event, callback) {
|
|
|
|
ensureEvent(this.eventList, event).push(callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
EventBus.prototype.removeEventListener = function(event, callback) {
|
|
|
|
var index = findIndex(this.eventList[event], callback);
|
|
|
|
|
|
|
|
if (index >= 0) {
|
|
|
|
this.eventList[event].splice(index, 1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
EventBus.prototype.trigger = function(event) {
|
2015-06-07 18:23:51 +08:00
|
|
|
var evt;
|
|
|
|
|
|
|
|
// Event 'readystatechange' is should be a simple event.
|
|
|
|
// Others are progress event.
|
|
|
|
// https://xhr.spec.whatwg.org/#events
|
|
|
|
if (event === 'readystatechange') {
|
|
|
|
evt = eventFactory.event(this.source, event);
|
|
|
|
} else {
|
|
|
|
evt = eventFactory.progressEvent(this.source, event);
|
|
|
|
}
|
|
|
|
|
2015-03-05 03:34:55 +08:00
|
|
|
var eventListeners = this.eventList[event];
|
|
|
|
|
2015-06-07 18:23:51 +08:00
|
|
|
if (eventListeners) {
|
|
|
|
for (var i = 0; i < eventListeners.length; i++) {
|
|
|
|
eventListeners[i].call(this.source, evt);
|
2015-03-05 03:34:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-06-07 18:23:51 +08:00
|
|
|
return function(source) {
|
|
|
|
return new EventBus(source);
|
2015-03-05 03:34:55 +08:00
|
|
|
};
|
|
|
|
};
|
2015-06-07 18:23:51 +08:00
|
|
|
|
2015-03-05 03:34:55 +08:00
|
|
|
getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
|
2014-06-02 06:37:19 +08:00
|
|
|
function extend(destination, source, propertiesToSkip) {
|
|
|
|
propertiesToSkip = propertiesToSkip || [];
|
2013-02-20 02:33:40 +08:00
|
|
|
for (var property in source) {
|
2014-06-02 06:37:19 +08:00
|
|
|
if (!arrayContains(propertiesToSkip, property)) {
|
|
|
|
destination[property] = source[property];
|
|
|
|
}
|
2013-02-20 02:33:40 +08:00
|
|
|
}
|
2013-01-09 15:47:56 +08:00
|
|
|
return destination;
|
2010-09-18 01:10:34 +08:00
|
|
|
}
|
2013-01-09 15:47:56 +08:00
|
|
|
|
2014-06-02 06:37:19 +08:00
|
|
|
function arrayContains(arr, item) {
|
|
|
|
for (var i = 0; i < arr.length; i++) {
|
|
|
|
if (arr[i] === item) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-04 23:39:23 +08:00
|
|
|
function wrapProgressEvent(xhr, eventName) {
|
|
|
|
return function() {
|
2014-11-08 00:07:40 +08:00
|
|
|
if (xhr[eventName]) {
|
2015-06-02 01:12:13 +08:00
|
|
|
xhr[eventName].apply(xhr, arguments);
|
2014-11-08 00:07:40 +08:00
|
|
|
}
|
2014-11-04 23:39:23 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function initializeEvents(xhr) {
|
2015-06-02 01:12:13 +08:00
|
|
|
xhr.eventBus.addEventListener('readystatechange', wrapProgressEvent(xhr, 'onreadystatechange'));
|
2015-03-05 03:34:55 +08:00
|
|
|
xhr.eventBus.addEventListener('loadstart', wrapProgressEvent(xhr, 'onloadstart'));
|
|
|
|
xhr.eventBus.addEventListener('load', wrapProgressEvent(xhr, 'onload'));
|
|
|
|
xhr.eventBus.addEventListener('loadend', wrapProgressEvent(xhr, 'onloadend'));
|
|
|
|
xhr.eventBus.addEventListener('progress', wrapProgressEvent(xhr, 'onprogress'));
|
|
|
|
xhr.eventBus.addEventListener('error', wrapProgressEvent(xhr, 'onerror'));
|
|
|
|
xhr.eventBus.addEventListener('abort', wrapProgressEvent(xhr, 'onabort'));
|
|
|
|
xhr.eventBus.addEventListener('timeout', wrapProgressEvent(xhr, 'ontimeout'));
|
2014-11-04 23:39:23 +08:00
|
|
|
}
|
|
|
|
|
2014-11-27 13:38:45 +08:00
|
|
|
function unconvertibleResponseTypeMessage(type) {
|
|
|
|
var msg = [
|
|
|
|
"Can't build XHR.response for XHR.responseType of '",
|
|
|
|
type,
|
|
|
|
"'.",
|
|
|
|
"XHR.response must be explicitly stubbed"
|
|
|
|
];
|
|
|
|
return msg.join(' ');
|
|
|
|
}
|
|
|
|
|
2014-08-08 23:06:42 +08:00
|
|
|
function fakeRequest(global, requestTracker, stubTracker, paramParser) {
|
2013-10-12 13:16:22 +08:00
|
|
|
function FakeXMLHttpRequest() {
|
|
|
|
requestTracker.track(this);
|
2015-06-07 18:23:51 +08:00
|
|
|
this.eventBus = eventBusFactory(this);
|
2015-03-05 03:34:55 +08:00
|
|
|
initializeEvents(this);
|
2013-12-03 09:51:21 +08:00
|
|
|
this.requestHeaders = {};
|
2014-07-30 09:28:13 +08:00
|
|
|
this.overriddenMimeType = null;
|
2010-09-17 06:46:55 +08:00
|
|
|
}
|
|
|
|
|
2014-07-29 23:08:03 +08:00
|
|
|
function findHeader(name, headers) {
|
|
|
|
name = name.toLowerCase();
|
|
|
|
for (var header in headers) {
|
|
|
|
if (header.toLowerCase() === name) {
|
|
|
|
return headers[header];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-31 09:46:02 +08:00
|
|
|
function normalizeHeaders(rawHeaders, contentType) {
|
|
|
|
var headers = [];
|
|
|
|
|
|
|
|
if (rawHeaders) {
|
|
|
|
if (rawHeaders instanceof Array) {
|
|
|
|
headers = rawHeaders;
|
|
|
|
} else {
|
|
|
|
for (var headerName in rawHeaders) {
|
|
|
|
if (rawHeaders.hasOwnProperty(headerName)) {
|
|
|
|
headers.push({ name: headerName, value: rawHeaders[headerName] });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
headers.push({ name: "Content-Type", value: contentType || "application/json" });
|
|
|
|
}
|
|
|
|
|
|
|
|
return headers;
|
|
|
|
}
|
|
|
|
|
2014-11-02 06:13:55 +08:00
|
|
|
function parseXml(xmlText, contentType) {
|
|
|
|
if (global.DOMParser) {
|
2014-11-02 07:26:12 +08:00
|
|
|
return (new global.DOMParser()).parseFromString(xmlText, 'text/xml');
|
2014-11-02 06:13:55 +08:00
|
|
|
} else {
|
|
|
|
var xml = new global.ActiveXObject("Microsoft.XMLDOM");
|
|
|
|
xml.async = "false";
|
|
|
|
xml.loadXML(xmlText);
|
|
|
|
return xml;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-02 07:26:12 +08:00
|
|
|
var xmlParsables = ['text/xml', 'application/xml'];
|
2014-11-02 06:13:55 +08:00
|
|
|
|
|
|
|
function getResponseXml(responseText, contentType) {
|
2014-11-02 07:26:12 +08:00
|
|
|
if (arrayContains(xmlParsables, contentType.toLowerCase())) {
|
2014-11-02 06:13:55 +08:00
|
|
|
return parseXml(responseText, contentType);
|
|
|
|
} else if (contentType.match(/\+xml$/)) {
|
|
|
|
return parseXml(responseText, 'text/xml');
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-01-13 04:24:19 +08:00
|
|
|
var iePropertiesThatCannotBeCopied = ['responseBody', 'responseText', 'responseXML', 'status', 'statusText', 'responseTimeout', 'responseURL'];
|
2014-08-08 23:06:42 +08:00
|
|
|
extend(FakeXMLHttpRequest.prototype, new global.XMLHttpRequest(), iePropertiesThatCannotBeCopied);
|
2013-10-12 13:16:22 +08:00
|
|
|
extend(FakeXMLHttpRequest.prototype, {
|
|
|
|
open: function() {
|
|
|
|
this.method = arguments[0];
|
|
|
|
this.url = arguments[1];
|
|
|
|
this.username = arguments[3];
|
|
|
|
this.password = arguments[4];
|
|
|
|
this.readyState = 1;
|
2016-02-17 08:59:04 +08:00
|
|
|
this.requestHeaders = {};
|
2015-06-02 01:12:13 +08:00
|
|
|
this.eventBus.trigger('readystatechange');
|
2013-10-12 13:16:22 +08:00
|
|
|
},
|
2011-08-08 04:01:38 +08:00
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
setRequestHeader: function(header, value) {
|
2014-07-29 22:45:12 +08:00
|
|
|
if(this.requestHeaders.hasOwnProperty(header)) {
|
|
|
|
this.requestHeaders[header] = [this.requestHeaders[header], value].join(', ');
|
|
|
|
} else {
|
|
|
|
this.requestHeaders[header] = value;
|
|
|
|
}
|
2013-10-12 13:16:22 +08:00
|
|
|
},
|
2011-08-08 04:01:38 +08:00
|
|
|
|
2014-07-30 09:28:13 +08:00
|
|
|
overrideMimeType: function(mime) {
|
|
|
|
this.overriddenMimeType = mime;
|
|
|
|
},
|
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
abort: function() {
|
|
|
|
this.readyState = 0;
|
2013-10-30 05:06:43 +08:00
|
|
|
this.status = 0;
|
|
|
|
this.statusText = "abort";
|
2015-06-02 01:12:13 +08:00
|
|
|
this.eventBus.trigger('readystatechange');
|
2015-03-05 03:34:55 +08:00
|
|
|
this.eventBus.trigger('progress');
|
|
|
|
this.eventBus.trigger('abort');
|
|
|
|
this.eventBus.trigger('loadend');
|
2013-10-12 13:16:22 +08:00
|
|
|
},
|
2011-08-08 04:01:38 +08:00
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
readyState: 0,
|
2011-08-08 04:01:38 +08:00
|
|
|
|
2014-11-07 23:50:33 +08:00
|
|
|
onloadstart: null,
|
|
|
|
onprogress: null,
|
|
|
|
onabort: null,
|
|
|
|
onerror: null,
|
|
|
|
onload: null,
|
|
|
|
ontimeout: null,
|
|
|
|
onloadend: null,
|
2015-06-02 01:12:13 +08:00
|
|
|
onreadystatechange: null,
|
2011-08-08 04:01:38 +08:00
|
|
|
|
2015-03-05 03:34:55 +08:00
|
|
|
addEventListener: function() {
|
|
|
|
this.eventBus.addEventListener.apply(this.eventBus, arguments);
|
2014-11-04 23:39:23 +08:00
|
|
|
},
|
2015-06-07 18:23:51 +08:00
|
|
|
|
2014-12-14 22:12:56 +08:00
|
|
|
removeEventListener: function(event, callback) {
|
2015-03-05 03:34:55 +08:00
|
|
|
this.eventBus.removeEventListener.apply(this.eventBus, arguments);
|
2014-12-14 22:12:56 +08:00
|
|
|
},
|
2014-11-04 23:39:23 +08:00
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
status: null,
|
2013-02-05 11:32:59 +08:00
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
send: function(data) {
|
|
|
|
this.params = data;
|
2015-03-05 03:34:55 +08:00
|
|
|
this.eventBus.trigger('loadstart');
|
2013-02-05 11:32:59 +08:00
|
|
|
|
2014-07-11 03:44:56 +08:00
|
|
|
var stub = stubTracker.findStub(this.url, data, this.method);
|
2016-03-14 11:15:00 +08:00
|
|
|
|
|
|
|
this.dispatchStub(stub);
|
|
|
|
},
|
|
|
|
|
|
|
|
dispatchStub: function(stub) {
|
2013-10-12 13:16:22 +08:00
|
|
|
if (stub) {
|
2015-06-02 01:12:13 +08:00
|
|
|
if (stub.isReturn()) {
|
|
|
|
this.respondWith(stub);
|
|
|
|
} else if (stub.isError()) {
|
|
|
|
this.responseError();
|
|
|
|
} else if (stub.isTimeout()) {
|
|
|
|
this.responseTimeout();
|
2016-03-14 11:15:00 +08:00
|
|
|
} else if (stub.isCallFunction()) {
|
|
|
|
this.responseCallFunction(stub);
|
2015-06-02 01:12:13 +08:00
|
|
|
}
|
2013-10-12 13:16:22 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-06-11 23:03:34 +08:00
|
|
|
contentType: function() {
|
2014-07-29 23:08:03 +08:00
|
|
|
return findHeader('content-type', this.requestHeaders);
|
2014-06-11 23:03:34 +08:00
|
|
|
},
|
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
data: function() {
|
2014-06-11 23:03:34 +08:00
|
|
|
if (!this.params) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2014-06-13 03:54:56 +08:00
|
|
|
return paramParser.findParser(this).parse(this.params);
|
2013-10-12 13:16:22 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
getResponseHeader: function(name) {
|
2014-07-31 09:46:02 +08:00
|
|
|
name = name.toLowerCase();
|
|
|
|
var resultHeader;
|
|
|
|
for(var i = 0; i < this.responseHeaders.length; i++) {
|
|
|
|
var header = this.responseHeaders[i];
|
|
|
|
if (name === header.name.toLowerCase()) {
|
|
|
|
if (resultHeader) {
|
|
|
|
resultHeader = [resultHeader, header.value].join(', ');
|
|
|
|
} else {
|
|
|
|
resultHeader = header.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return resultHeader;
|
2013-10-12 13:16:22 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
getAllResponseHeaders: function() {
|
|
|
|
var responseHeaders = [];
|
2014-07-13 09:06:24 +08:00
|
|
|
for (var i = 0; i < this.responseHeaders.length; i++) {
|
|
|
|
responseHeaders.push(this.responseHeaders[i].name + ': ' +
|
|
|
|
this.responseHeaders[i].value);
|
2013-10-12 13:16:22 +08:00
|
|
|
}
|
2015-01-24 05:11:42 +08:00
|
|
|
return responseHeaders.join('\r\n') + '\r\n';
|
2013-10-12 13:16:22 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
responseText: null,
|
2014-11-27 13:38:45 +08:00
|
|
|
response: null,
|
|
|
|
responseType: null,
|
2016-01-13 04:24:19 +08:00
|
|
|
responseURL: null,
|
2014-11-27 13:38:45 +08:00
|
|
|
|
|
|
|
responseValue: function() {
|
|
|
|
switch(this.responseType) {
|
|
|
|
case null:
|
|
|
|
case "":
|
|
|
|
case "text":
|
|
|
|
return this.readyState >= 3 ? this.responseText : "";
|
|
|
|
case "json":
|
|
|
|
return JSON.parse(this.responseText);
|
|
|
|
case "arraybuffer":
|
|
|
|
throw unconvertibleResponseTypeMessage('arraybuffer');
|
|
|
|
case "blob":
|
|
|
|
throw unconvertibleResponseTypeMessage('blob');
|
|
|
|
case "document":
|
|
|
|
return this.responseXML;
|
2014-11-18 05:33:19 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-11-27 13:38:45 +08:00
|
|
|
|
2014-11-18 05:33:19 +08:00
|
|
|
respondWith: function(response) {
|
2014-07-11 15:23:13 +08:00
|
|
|
if (this.readyState === 4) {
|
|
|
|
throw new Error("FakeXMLHttpRequest already completed");
|
|
|
|
}
|
2015-06-02 01:12:13 +08:00
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
this.status = response.status;
|
2013-11-02 06:51:21 +08:00
|
|
|
this.statusText = response.statusText || "";
|
2015-06-02 01:12:13 +08:00
|
|
|
this.responseHeaders = normalizeHeaders(response.responseHeaders, response.contentType);
|
|
|
|
this.readyState = 2;
|
|
|
|
this.eventBus.trigger('readystatechange');
|
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
this.responseText = response.responseText || "";
|
2014-11-27 13:38:45 +08:00
|
|
|
this.responseType = response.responseType || "";
|
2016-01-13 04:24:19 +08:00
|
|
|
this.responseURL = response.responseURL || null;
|
2013-10-12 13:16:22 +08:00
|
|
|
this.readyState = 4;
|
2014-11-02 06:13:55 +08:00
|
|
|
this.responseXML = getResponseXml(response.responseText, this.getResponseHeader('content-type') || '');
|
2014-11-27 13:38:45 +08:00
|
|
|
if (this.responseXML) {
|
|
|
|
this.responseType = 'document';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('response' in response) {
|
|
|
|
this.response = response.response;
|
|
|
|
} else {
|
|
|
|
this.response = this.responseValue();
|
|
|
|
}
|
2013-10-12 13:16:22 +08:00
|
|
|
|
2015-06-02 01:12:13 +08:00
|
|
|
this.eventBus.trigger('readystatechange');
|
2015-03-05 03:34:55 +08:00
|
|
|
this.eventBus.trigger('progress');
|
|
|
|
this.eventBus.trigger('load');
|
|
|
|
this.eventBus.trigger('loadend');
|
2013-10-12 13:16:22 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
responseTimeout: function() {
|
2014-07-11 15:23:13 +08:00
|
|
|
if (this.readyState === 4) {
|
|
|
|
throw new Error("FakeXMLHttpRequest already completed");
|
|
|
|
}
|
2013-10-12 13:16:22 +08:00
|
|
|
this.readyState = 4;
|
2013-11-01 00:54:47 +08:00
|
|
|
jasmine.clock().tick(30000);
|
2015-06-07 18:23:51 +08:00
|
|
|
this.eventBus.trigger('readystatechange');
|
2015-03-05 03:34:55 +08:00
|
|
|
this.eventBus.trigger('progress');
|
|
|
|
this.eventBus.trigger('timeout');
|
|
|
|
this.eventBus.trigger('loadend');
|
2014-11-02 11:43:21 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
responseError: function() {
|
|
|
|
if (this.readyState === 4) {
|
|
|
|
throw new Error("FakeXMLHttpRequest already completed");
|
|
|
|
}
|
|
|
|
this.readyState = 4;
|
2015-06-02 01:12:13 +08:00
|
|
|
this.eventBus.trigger('readystatechange');
|
2015-03-05 03:34:55 +08:00
|
|
|
this.eventBus.trigger('progress');
|
|
|
|
this.eventBus.trigger('error');
|
|
|
|
this.eventBus.trigger('loadend');
|
2016-03-14 11:15:00 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
responseCallFunction: function(stub) {
|
|
|
|
stub.action = undefined;
|
|
|
|
stub.functionToCall(stub, this);
|
|
|
|
this.dispatchStub(stub);
|
2013-10-12 13:16:22 +08:00
|
|
|
}
|
|
|
|
});
|
2011-08-08 04:01:38 +08:00
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
return FakeXMLHttpRequest;
|
|
|
|
}
|
2011-08-08 04:01:38 +08:00
|
|
|
|
2014-08-04 22:46:32 +08:00
|
|
|
return fakeRequest;
|
|
|
|
};
|
2013-02-05 11:32:59 +08:00
|
|
|
|
2014-08-04 22:46:32 +08:00
|
|
|
getJasmineRequireObj().MockAjax = function($ajax) {
|
|
|
|
function MockAjax(global) {
|
|
|
|
var requestTracker = new $ajax.RequestTracker(),
|
|
|
|
stubTracker = new $ajax.StubTracker(),
|
|
|
|
paramParser = new $ajax.ParamParser(),
|
|
|
|
realAjaxFunction = global.XMLHttpRequest,
|
2014-08-08 23:06:42 +08:00
|
|
|
mockAjaxFunction = $ajax.fakeRequest(global, requestTracker, stubTracker, paramParser);
|
2013-02-05 11:32:59 +08:00
|
|
|
|
2014-08-04 22:46:32 +08:00
|
|
|
this.install = function() {
|
2015-03-31 12:15:34 +08:00
|
|
|
if (global.XMLHttpRequest === mockAjaxFunction) {
|
|
|
|
throw "MockAjax is already installed.";
|
|
|
|
}
|
|
|
|
|
2014-08-04 22:46:32 +08:00
|
|
|
global.XMLHttpRequest = mockAjaxFunction;
|
2013-10-12 13:16:22 +08:00
|
|
|
};
|
|
|
|
|
2014-08-04 22:46:32 +08:00
|
|
|
this.uninstall = function() {
|
2015-10-29 02:56:36 +08:00
|
|
|
if (global.XMLHttpRequest !== mockAjaxFunction) {
|
|
|
|
throw "MockAjax not installed.";
|
|
|
|
}
|
2014-08-04 22:46:32 +08:00
|
|
|
global.XMLHttpRequest = realAjaxFunction;
|
|
|
|
|
|
|
|
this.stubs.reset();
|
|
|
|
this.requests.reset();
|
|
|
|
paramParser.reset();
|
2013-10-12 13:16:22 +08:00
|
|
|
};
|
|
|
|
|
2014-08-04 22:46:32 +08:00
|
|
|
this.stubRequest = function(url, data, method) {
|
|
|
|
var stub = new $ajax.RequestStub(url, data, method);
|
|
|
|
stubTracker.addStub(stub);
|
|
|
|
return stub;
|
2013-10-12 13:16:22 +08:00
|
|
|
};
|
|
|
|
|
2014-08-04 22:46:32 +08:00
|
|
|
this.withMock = function(closure) {
|
|
|
|
this.install();
|
|
|
|
try {
|
|
|
|
closure();
|
|
|
|
} finally {
|
|
|
|
this.uninstall();
|
|
|
|
}
|
2013-10-12 13:16:22 +08:00
|
|
|
};
|
2013-10-19 04:05:47 +08:00
|
|
|
|
2014-08-04 22:46:32 +08:00
|
|
|
this.addCustomParamParser = function(parser) {
|
|
|
|
paramParser.add(parser);
|
2013-10-19 04:05:47 +08:00
|
|
|
};
|
2013-11-02 06:05:30 +08:00
|
|
|
|
2014-08-04 22:46:32 +08:00
|
|
|
this.requests = requestTracker;
|
|
|
|
this.stubs = stubTracker;
|
|
|
|
}
|
2013-11-02 06:05:30 +08:00
|
|
|
|
2014-08-04 22:46:32 +08:00
|
|
|
return MockAjax;
|
|
|
|
};
|
|
|
|
|
|
|
|
getJasmineRequireObj().AjaxParamParser = function() {
|
|
|
|
function ParamParser() {
|
|
|
|
var defaults = [
|
|
|
|
{
|
|
|
|
test: function(xhr) {
|
|
|
|
return (/^application\/json/).test(xhr.contentType());
|
|
|
|
},
|
|
|
|
parse: function jsonParser(paramString) {
|
|
|
|
return JSON.parse(paramString);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
test: function(xhr) {
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
parse: function naiveParser(paramString) {
|
|
|
|
var data = {};
|
|
|
|
var params = paramString.split('&');
|
|
|
|
|
|
|
|
for (var i = 0; i < params.length; ++i) {
|
|
|
|
var kv = params[i].replace(/\+/g, ' ').split('=');
|
|
|
|
var key = decodeURIComponent(kv[0]);
|
|
|
|
data[key] = data[key] || [];
|
|
|
|
data[key].push(decodeURIComponent(kv[1]));
|
2013-11-02 06:05:30 +08:00
|
|
|
}
|
2014-08-04 22:46:32 +08:00
|
|
|
return data;
|
2013-11-02 06:05:30 +08:00
|
|
|
}
|
|
|
|
}
|
2014-08-04 22:46:32 +08:00
|
|
|
];
|
|
|
|
var paramParsers = [];
|
2013-11-02 06:05:30 +08:00
|
|
|
|
2014-08-04 22:46:32 +08:00
|
|
|
this.add = function(parser) {
|
|
|
|
paramParsers.unshift(parser);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.findParser = function(xhr) {
|
|
|
|
for(var i in paramParsers) {
|
|
|
|
var parser = paramParsers[i];
|
|
|
|
if (parser.test(xhr)) {
|
|
|
|
return parser;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.reset = function() {
|
|
|
|
paramParsers = [];
|
|
|
|
for(var i in defaults) {
|
|
|
|
paramParsers.push(defaults[i]);
|
|
|
|
}
|
2013-11-02 06:05:30 +08:00
|
|
|
};
|
2014-08-04 22:46:32 +08:00
|
|
|
|
|
|
|
this.reset();
|
2013-10-12 13:16:22 +08:00
|
|
|
}
|
2013-02-05 11:32:59 +08:00
|
|
|
|
2014-08-04 22:46:32 +08:00
|
|
|
return ParamParser;
|
|
|
|
};
|
|
|
|
|
|
|
|
getJasmineRequireObj().AjaxRequestStub = function() {
|
2015-06-02 01:12:13 +08:00
|
|
|
var RETURN = 0,
|
|
|
|
ERROR = 1,
|
2016-03-14 11:15:00 +08:00
|
|
|
TIMEOUT = 2,
|
|
|
|
CALL = 3;
|
2015-06-02 01:12:13 +08:00
|
|
|
|
2014-07-11 03:44:56 +08:00
|
|
|
function RequestStub(url, stubData, method) {
|
2014-01-22 11:06:23 +08:00
|
|
|
var normalizeQuery = function(query) {
|
|
|
|
return query ? query.split('&').sort().join('&') : undefined;
|
|
|
|
};
|
|
|
|
|
2014-06-10 23:24:21 +08:00
|
|
|
if (url instanceof RegExp) {
|
|
|
|
this.url = url;
|
|
|
|
this.query = undefined;
|
|
|
|
} else {
|
|
|
|
var split = url.split('?');
|
|
|
|
this.url = split[0];
|
|
|
|
this.query = split.length > 1 ? normalizeQuery(split[1]) : undefined;
|
|
|
|
}
|
|
|
|
|
2015-06-08 22:38:26 +08:00
|
|
|
this.data = (stubData instanceof RegExp) ? stubData : normalizeQuery(stubData);
|
2014-07-11 03:44:56 +08:00
|
|
|
this.method = method;
|
2013-01-09 15:47:56 +08:00
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
this.andReturn = function(options) {
|
2015-06-02 01:12:13 +08:00
|
|
|
this.action = RETURN;
|
2013-10-12 13:16:22 +08:00
|
|
|
this.status = options.status || 200;
|
2013-01-09 15:47:56 +08:00
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
this.contentType = options.contentType;
|
2014-11-27 13:38:45 +08:00
|
|
|
this.response = options.response;
|
2013-10-12 13:16:22 +08:00
|
|
|
this.responseText = options.responseText;
|
2015-02-17 00:26:22 +08:00
|
|
|
this.responseHeaders = options.responseHeaders;
|
2016-01-13 04:24:19 +08:00
|
|
|
this.responseURL = options.responseURL;
|
2013-10-12 13:16:22 +08:00
|
|
|
};
|
2014-01-18 03:38:27 +08:00
|
|
|
|
2015-06-02 01:12:13 +08:00
|
|
|
this.isReturn = function() {
|
|
|
|
return this.action === RETURN;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.andError = function() {
|
|
|
|
this.action = ERROR;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.isError = function() {
|
|
|
|
return this.action === ERROR;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.andTimeout = function() {
|
|
|
|
this.action = TIMEOUT;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.isTimeout = function() {
|
|
|
|
return this.action === TIMEOUT;
|
|
|
|
};
|
|
|
|
|
2016-03-14 11:15:00 +08:00
|
|
|
this.andCallFunction = function(functionToCall) {
|
|
|
|
this.action = CALL;
|
|
|
|
this.functionToCall = functionToCall;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.isCallFunction = function() {
|
|
|
|
return this.action === CALL;
|
|
|
|
};
|
|
|
|
|
2014-07-11 03:44:56 +08:00
|
|
|
this.matches = function(fullUrl, data, method) {
|
2015-06-08 22:38:26 +08:00
|
|
|
var urlMatches = false;
|
2014-06-10 23:24:21 +08:00
|
|
|
fullUrl = fullUrl.toString();
|
|
|
|
if (this.url instanceof RegExp) {
|
2015-06-08 22:38:26 +08:00
|
|
|
urlMatches = this.url.test(fullUrl);
|
2014-06-10 23:24:21 +08:00
|
|
|
} else {
|
|
|
|
var urlSplit = fullUrl.split('?'),
|
|
|
|
url = urlSplit[0],
|
|
|
|
query = urlSplit[1];
|
2015-06-08 22:38:26 +08:00
|
|
|
urlMatches = this.url === url && this.query === normalizeQuery(query);
|
2014-06-10 23:24:21 +08:00
|
|
|
}
|
2015-06-08 22:38:26 +08:00
|
|
|
var dataMatches = false;
|
|
|
|
if (this.data instanceof RegExp) {
|
|
|
|
dataMatches = this.data.test(data);
|
|
|
|
} else {
|
|
|
|
dataMatches = !this.data || this.data === normalizeQuery(data);
|
|
|
|
}
|
|
|
|
return urlMatches && dataMatches && (!this.method || this.method === method);
|
2014-01-18 03:38:27 +08:00
|
|
|
};
|
2013-10-12 13:16:22 +08:00
|
|
|
}
|
|
|
|
|
2014-08-04 22:46:32 +08:00
|
|
|
return RequestStub;
|
|
|
|
};
|
|
|
|
|
|
|
|
getJasmineRequireObj().AjaxRequestTracker = function() {
|
|
|
|
function RequestTracker() {
|
|
|
|
var requests = [];
|
|
|
|
|
|
|
|
this.track = function(request) {
|
|
|
|
requests.push(request);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.first = function() {
|
|
|
|
return requests[0];
|
|
|
|
};
|
|
|
|
|
|
|
|
this.count = function() {
|
|
|
|
return requests.length;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.reset = function() {
|
|
|
|
requests = [];
|
|
|
|
};
|
|
|
|
|
|
|
|
this.mostRecent = function() {
|
|
|
|
return requests[requests.length - 1];
|
|
|
|
};
|
|
|
|
|
|
|
|
this.at = function(index) {
|
|
|
|
return requests[index];
|
|
|
|
};
|
|
|
|
|
|
|
|
this.filter = function(url_to_match) {
|
|
|
|
var matching_requests = [];
|
|
|
|
|
|
|
|
for (var i = 0; i < requests.length; i++) {
|
|
|
|
if (url_to_match instanceof RegExp &&
|
|
|
|
url_to_match.test(requests[i].url)) {
|
|
|
|
matching_requests.push(requests[i]);
|
|
|
|
} else if (url_to_match instanceof Function &&
|
|
|
|
url_to_match(requests[i])) {
|
|
|
|
matching_requests.push(requests[i]);
|
|
|
|
} else {
|
|
|
|
if (requests[i].url === url_to_match) {
|
|
|
|
matching_requests.push(requests[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return matching_requests;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return RequestTracker;
|
|
|
|
};
|
|
|
|
|
|
|
|
getJasmineRequireObj().AjaxStubTracker = function() {
|
|
|
|
function StubTracker() {
|
|
|
|
var stubs = [];
|
|
|
|
|
|
|
|
this.addStub = function(stub) {
|
|
|
|
stubs.push(stub);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.reset = function() {
|
|
|
|
stubs = [];
|
|
|
|
};
|
|
|
|
|
|
|
|
this.findStub = function(url, data, method) {
|
|
|
|
for (var i = stubs.length - 1; i >= 0; i--) {
|
|
|
|
var stub = stubs[i];
|
|
|
|
if (stub.matches(url, data, method)) {
|
|
|
|
return stub;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return StubTracker;
|
|
|
|
};
|
|
|
|
|
2016-02-24 07:28:14 +08:00
|
|
|
|
|
|
|
var jRequire = getJasmineRequireObj();
|
|
|
|
var MockAjax = jRequire.ajax(jRequire);
|
|
|
|
jasmine.Ajax = new MockAjax(global);
|
|
|
|
|
|
|
|
return MockAjax;
|
|
|
|
}));
|