|
|
|
@ -1,6 +1,6 @@
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
Jasmine-Ajax - v3.1.0: a set of helpers for testing AJAX requests under the Jasmine
|
|
|
|
|
Jasmine-Ajax - v3.1.1: a set of helpers for testing AJAX requests under the Jasmine
|
|
|
|
|
BDD framework for JavaScript.
|
|
|
|
|
|
|
|
|
|
http://github.com/jasmine/jasmine-ajax
|
|
|
|
@ -68,6 +68,10 @@ getJasmineRequireObj().AjaxEventBus = function() {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function slice(array, start) {
|
|
|
|
|
return Array.prototype.slice.call(array, start);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EventBus.prototype.addEventListener = function(event, callback) {
|
|
|
|
|
ensureEvent(this.eventList, event).push(callback);
|
|
|
|
|
};
|
|
|
|
@ -81,11 +85,13 @@ getJasmineRequireObj().AjaxEventBus = function() {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
EventBus.prototype.trigger = function(event) {
|
|
|
|
|
// Arguments specified after event name need to be propagated
|
|
|
|
|
var args = slice(arguments, 1);
|
|
|
|
|
var eventListeners = this.eventList[event];
|
|
|
|
|
|
|
|
|
|
if(eventListeners){
|
|
|
|
|
for(var i = 0; i < eventListeners.length; i++){
|
|
|
|
|
eventListeners[i]();
|
|
|
|
|
eventListeners[i].apply(this, args);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
@ -117,12 +123,13 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
|
|
|
|
|
function wrapProgressEvent(xhr, eventName) {
|
|
|
|
|
return function() {
|
|
|
|
|
if (xhr[eventName]) {
|
|
|
|
|
xhr[eventName]();
|
|
|
|
|
xhr[eventName].apply(xhr, arguments);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function initializeEvents(xhr) {
|
|
|
|
|
xhr.eventBus.addEventListener('readystatechange', wrapProgressEvent(xhr, 'onreadystatechange'));
|
|
|
|
|
xhr.eventBus.addEventListener('loadstart', wrapProgressEvent(xhr, 'onloadstart'));
|
|
|
|
|
xhr.eventBus.addEventListener('load', wrapProgressEvent(xhr, 'onload'));
|
|
|
|
|
xhr.eventBus.addEventListener('loadend', wrapProgressEvent(xhr, 'onloadend'));
|
|
|
|
@ -211,7 +218,7 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
|
|
|
|
|
this.username = arguments[3];
|
|
|
|
|
this.password = arguments[4];
|
|
|
|
|
this.readyState = 1;
|
|
|
|
|
this.onreadystatechange();
|
|
|
|
|
this.eventBus.trigger('readystatechange');
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
setRequestHeader: function(header, value) {
|
|
|
|
@ -230,7 +237,7 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
|
|
|
|
|
this.readyState = 0;
|
|
|
|
|
this.status = 0;
|
|
|
|
|
this.statusText = "abort";
|
|
|
|
|
this.onreadystatechange();
|
|
|
|
|
this.eventBus.trigger('readystatechange');
|
|
|
|
|
this.eventBus.trigger('progress');
|
|
|
|
|
this.eventBus.trigger('abort');
|
|
|
|
|
this.eventBus.trigger('loadend');
|
|
|
|
@ -245,9 +252,7 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
|
|
|
|
|
onload: null,
|
|
|
|
|
ontimeout: null,
|
|
|
|
|
onloadend: null,
|
|
|
|
|
|
|
|
|
|
onreadystatechange: function(isTimeout) {
|
|
|
|
|
},
|
|
|
|
|
onreadystatechange: null,
|
|
|
|
|
|
|
|
|
|
addEventListener: function() {
|
|
|
|
|
this.eventBus.addEventListener.apply(this.eventBus, arguments);
|
|
|
|
@ -261,13 +266,17 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
|
|
|
|
|
|
|
|
|
|
send: function(data) {
|
|
|
|
|
this.params = data;
|
|
|
|
|
this.readyState = 2;
|
|
|
|
|
this.eventBus.trigger('loadstart');
|
|
|
|
|
this.onreadystatechange();
|
|
|
|
|
|
|
|
|
|
var stub = stubTracker.findStub(this.url, data, this.method);
|
|
|
|
|
if (stub) {
|
|
|
|
|
this.respondWith(stub);
|
|
|
|
|
if (stub.isReturn()) {
|
|
|
|
|
this.respondWith(stub);
|
|
|
|
|
} else if (stub.isError()) {
|
|
|
|
|
this.responseError();
|
|
|
|
|
} else if (stub.isTimeout()) {
|
|
|
|
|
this.responseTimeout();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
@ -334,12 +343,16 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
|
|
|
|
|
if (this.readyState === 4) {
|
|
|
|
|
throw new Error("FakeXMLHttpRequest already completed");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.status = response.status;
|
|
|
|
|
this.statusText = response.statusText || "";
|
|
|
|
|
this.responseHeaders = normalizeHeaders(response.responseHeaders, response.contentType);
|
|
|
|
|
this.readyState = 2;
|
|
|
|
|
this.eventBus.trigger('readystatechange');
|
|
|
|
|
|
|
|
|
|
this.responseText = response.responseText || "";
|
|
|
|
|
this.responseType = response.responseType || "";
|
|
|
|
|
this.readyState = 4;
|
|
|
|
|
this.responseHeaders = normalizeHeaders(response.responseHeaders, response.contentType);
|
|
|
|
|
this.responseXML = getResponseXml(response.responseText, this.getResponseHeader('content-type') || '');
|
|
|
|
|
if (this.responseXML) {
|
|
|
|
|
this.responseType = 'document';
|
|
|
|
@ -351,7 +364,7 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
|
|
|
|
|
this.response = this.responseValue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.onreadystatechange();
|
|
|
|
|
this.eventBus.trigger('readystatechange');
|
|
|
|
|
this.eventBus.trigger('progress');
|
|
|
|
|
this.eventBus.trigger('load');
|
|
|
|
|
this.eventBus.trigger('loadend');
|
|
|
|
@ -363,7 +376,7 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
|
|
|
|
|
}
|
|
|
|
|
this.readyState = 4;
|
|
|
|
|
jasmine.clock().tick(30000);
|
|
|
|
|
this.onreadystatechange('timeout');
|
|
|
|
|
this.eventBus.trigger('readystatechange', 'timeout');
|
|
|
|
|
this.eventBus.trigger('progress');
|
|
|
|
|
this.eventBus.trigger('timeout');
|
|
|
|
|
this.eventBus.trigger('loadend');
|
|
|
|
@ -374,7 +387,7 @@ getJasmineRequireObj().AjaxFakeRequest = function(eventBusFactory) {
|
|
|
|
|
throw new Error("FakeXMLHttpRequest already completed");
|
|
|
|
|
}
|
|
|
|
|
this.readyState = 4;
|
|
|
|
|
this.onreadystatechange();
|
|
|
|
|
this.eventBus.trigger('readystatechange');
|
|
|
|
|
this.eventBus.trigger('progress');
|
|
|
|
|
this.eventBus.trigger('error');
|
|
|
|
|
this.eventBus.trigger('loadend');
|
|
|
|
@ -495,6 +508,10 @@ getJasmineRequireObj().AjaxParamParser = function() {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
getJasmineRequireObj().AjaxRequestStub = function() {
|
|
|
|
|
var RETURN = 0,
|
|
|
|
|
ERROR = 1,
|
|
|
|
|
TIMEOUT = 2;
|
|
|
|
|
|
|
|
|
|
function RequestStub(url, stubData, method) {
|
|
|
|
|
var normalizeQuery = function(query) {
|
|
|
|
|
return query ? query.split('&').sort().join('&') : undefined;
|
|
|
|
@ -513,6 +530,7 @@ getJasmineRequireObj().AjaxRequestStub = function() {
|
|
|
|
|
this.method = method;
|
|
|
|
|
|
|
|
|
|
this.andReturn = function(options) {
|
|
|
|
|
this.action = RETURN;
|
|
|
|
|
this.status = options.status || 200;
|
|
|
|
|
|
|
|
|
|
this.contentType = options.contentType;
|
|
|
|
@ -521,6 +539,26 @@ getJasmineRequireObj().AjaxRequestStub = function() {
|
|
|
|
|
this.responseHeaders = options.responseHeaders;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.matches = function(fullUrl, data, method) {
|
|
|
|
|
var matches = false;
|
|
|
|
|
fullUrl = fullUrl.toString();
|
|
|
|
|