jasmine-ajax/lib/mock-ajax.js
Davis W. Frank, dwfrank & Hunter Gillane e7ca84008c WIP: refactoring #response
2010-09-16 14:24:35 -07:00

65 lines
1.9 KiB
JavaScript

Ajax.Request.prototype.response = function(responseOptions) {
this.transport.readyState = 4;
if (typeof(responseOptions) == "string") {
responseOptions = {responseText: responseOptions};
}
this.transport.responseHeaders = responseOptions.responseHeaders ||
{"Content-type": responseOptions.contentType || Ajax.Response.defaultContentType};
this.transport.status = typeof(responseOptions.status) == "undefined" ? 200 : responseOptions.status;
this.transport.responseText = responseOptions.responseText;
this.transport.onreadystatechange();
};
Ajax.Response.defaultContentType = "application/json";
Ajax.Request.prototype.oldRequest = Ajax.Request.prototype.request;
Ajax.Request.prototype.request = function(url) {
this.oldRequest(url);
ajaxRequests.push(this);
};
Ajax.RealRequest = Class.create(Ajax.Request, {
request: function(url) {
this.transport = Try.these(
function() {
return new XMLHttpRequest()
},
function() {
return new ActiveXObject('Msxml2.XMLHTTP')
},
function() {
return new ActiveXObject('Microsoft.XMLHTTP')
}
) || false;
this.oldRequest(url);
}
});
var ajaxRequests = [];
FakeAjaxTransport = Class.create({
initialize: function() {
this.overrideMimeType = false;
this.readyState = 0;
this.setRequestHeader = jasmine.createSpy("setRequestHeader");
this.open = jasmine.createSpy("open");
this.send = jasmine.createSpy("send");
this.abort = jasmine.createSpy("abort");
},
getResponseHeader: function(name) {
return this.responseHeaders[name];
}
});
function mostRecentAjaxRequest() {
if (ajaxRequests.length > 0) {
return ajaxRequests[ajaxRequests.length - 1];
} else {
return null;
}
}
function clearAjaxRequests() {
ajaxRequests = [];
}