2010-09-30 05:56:17 +08:00
|
|
|
/*
|
|
|
|
|
2013-01-09 15:47:56 +08:00
|
|
|
Jasmine-Ajax : a set of helpers for testing AJAX requests under the Jasmine
|
|
|
|
BDD framework for JavaScript.
|
2010-09-30 05:56:17 +08:00
|
|
|
|
2013-01-09 15:47:56 +08:00
|
|
|
http://github.com/pivotal/jasmine-ajax
|
2010-09-30 05:56:17 +08:00
|
|
|
|
2013-01-09 15:47:56 +08:00
|
|
|
Jasmine Home page: http://pivotal.github.com/jasmine
|
2010-09-30 05:56:17 +08:00
|
|
|
|
2013-10-19 03:44:34 +08:00
|
|
|
Copyright (c) 2008-2013 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
|
|
|
|
2013-01-09 15:47:56 +08:00
|
|
|
(function() {
|
|
|
|
function extend(destination, source) {
|
2013-02-20 02:33:40 +08:00
|
|
|
for (var property in source) {
|
|
|
|
destination[property] = source[property];
|
|
|
|
}
|
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
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
function MockAjax(global) {
|
|
|
|
var requestTracker = new RequestTracker(),
|
|
|
|
stubTracker = new StubTracker(),
|
|
|
|
realAjaxFunction = global.XMLHttpRequest,
|
|
|
|
mockAjaxFunction = fakeRequest(requestTracker, stubTracker);
|
2013-01-09 14:40:59 +08:00
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
this.install = function() {
|
|
|
|
global.XMLHttpRequest = mockAjaxFunction;
|
|
|
|
};
|
2010-09-17 08:07:01 +08:00
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
this.uninstall = function() {
|
|
|
|
global.XMLHttpRequest = realAjaxFunction;
|
2013-11-02 05:41:35 +08:00
|
|
|
|
|
|
|
this.stubs.reset();
|
|
|
|
this.requests.reset();
|
2013-10-12 13:16:22 +08:00
|
|
|
};
|
2010-09-17 07:51:52 +08:00
|
|
|
|
2014-01-18 03:12:01 +08:00
|
|
|
this.stubRequest = function(url, data) {
|
|
|
|
var stub = new RequestStub(url, data);
|
2013-10-12 13:16:22 +08:00
|
|
|
stubTracker.addStub(stub);
|
|
|
|
return stub;
|
|
|
|
};
|
2010-09-17 06:46:55 +08:00
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
this.withMock = function(closure) {
|
|
|
|
this.install();
|
|
|
|
try {
|
|
|
|
closure();
|
|
|
|
} finally {
|
|
|
|
this.uninstall();
|
2012-05-17 02:40:24 +08:00
|
|
|
}
|
2013-10-12 13:16:22 +08:00
|
|
|
};
|
2010-09-17 06:46:55 +08:00
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
this.requests = requestTracker;
|
|
|
|
this.stubs = stubTracker;
|
|
|
|
}
|
2013-02-05 01:20:42 +08:00
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
function StubTracker() {
|
|
|
|
var stubs = [];
|
2010-09-29 02:41:50 +08:00
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
this.addStub = function(stub) {
|
|
|
|
stubs.push(stub);
|
|
|
|
};
|
2010-09-17 06:46:55 +08:00
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
this.reset = function() {
|
|
|
|
stubs = [];
|
|
|
|
};
|
2013-02-05 11:32:59 +08:00
|
|
|
|
2014-01-18 03:12:01 +08:00
|
|
|
this.findStub = function(url, data) {
|
2013-10-12 13:16:22 +08:00
|
|
|
for (var i = stubs.length - 1; i >= 0; i--) {
|
|
|
|
var stub = stubs[i];
|
2014-01-18 03:38:27 +08:00
|
|
|
if (stub.matches(url, data)) {
|
2013-10-12 13:16:22 +08:00
|
|
|
return stub;
|
2011-02-22 14:27:11 +08:00
|
|
|
}
|
|
|
|
}
|
2013-10-12 13:16:22 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function fakeRequest(requestTracker, stubTracker) {
|
|
|
|
function FakeXMLHttpRequest() {
|
|
|
|
requestTracker.track(this);
|
2013-12-03 09:51:21 +08:00
|
|
|
this.requestHeaders = {};
|
2010-09-17 06:46:55 +08:00
|
|
|
}
|
|
|
|
|
2014-04-04 22:29:49 +08:00
|
|
|
extend(FakeXMLHttpRequest.prototype, new window.XMLHttpRequest());
|
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;
|
2014-01-18 02:41:23 +08:00
|
|
|
this.onreadystatechange();
|
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) {
|
|
|
|
this.requestHeaders[header] = value;
|
|
|
|
},
|
2011-08-08 04:01:38 +08:00
|
|
|
|
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";
|
2014-01-18 02:41:23 +08:00
|
|
|
this.onreadystatechange();
|
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
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
onload: function() {
|
|
|
|
},
|
2011-08-08 04:01:38 +08:00
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
onreadystatechange: function(isTimeout) {
|
|
|
|
},
|
2011-08-08 04:01:38 +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;
|
|
|
|
this.readyState = 2;
|
2014-01-18 02:41:23 +08:00
|
|
|
this.onreadystatechange();
|
2013-02-05 11:32:59 +08:00
|
|
|
|
2014-01-18 03:12:01 +08:00
|
|
|
var stub = stubTracker.findStub(this.url, data);
|
2013-10-12 13:16:22 +08:00
|
|
|
if (stub) {
|
|
|
|
this.response(stub);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
data: function() {
|
|
|
|
var data = {};
|
|
|
|
if (typeof this.params !== 'string') { return data; }
|
|
|
|
var params = this.params.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]));
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
},
|
|
|
|
|
|
|
|
getResponseHeader: function(name) {
|
|
|
|
return this.responseHeaders[name];
|
|
|
|
},
|
|
|
|
|
|
|
|
getAllResponseHeaders: function() {
|
|
|
|
var responseHeaders = [];
|
|
|
|
for (var i in this.responseHeaders) {
|
|
|
|
if (this.responseHeaders.hasOwnProperty(i)) {
|
|
|
|
responseHeaders.push(i + ': ' + this.responseHeaders[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return responseHeaders.join('\r\n');
|
|
|
|
},
|
|
|
|
|
|
|
|
responseText: null,
|
|
|
|
|
|
|
|
response: function(response) {
|
|
|
|
this.status = response.status;
|
2013-11-02 06:51:21 +08:00
|
|
|
this.statusText = response.statusText || "";
|
2013-10-12 13:16:22 +08:00
|
|
|
this.responseText = response.responseText || "";
|
|
|
|
this.readyState = 4;
|
|
|
|
this.responseHeaders = response.responseHeaders ||
|
|
|
|
{"Content-type": response.contentType || "application/json" };
|
|
|
|
|
|
|
|
this.onload();
|
|
|
|
this.onreadystatechange();
|
|
|
|
},
|
|
|
|
|
|
|
|
responseTimeout: function() {
|
|
|
|
this.readyState = 4;
|
2013-11-01 00:54:47 +08:00
|
|
|
jasmine.clock().tick(30000);
|
2013-10-12 13:16:22 +08:00
|
|
|
this.onreadystatechange('timeout');
|
|
|
|
}
|
|
|
|
});
|
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
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
function RequestTracker() {
|
|
|
|
var requests = [];
|
2013-02-05 11:32:59 +08:00
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
this.track = function(request) {
|
|
|
|
requests.push(request);
|
|
|
|
};
|
2013-02-05 11:32:59 +08:00
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
this.first = function() {
|
|
|
|
return requests[0];
|
|
|
|
};
|
|
|
|
|
|
|
|
this.count = function() {
|
|
|
|
return requests.length;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.reset = function() {
|
|
|
|
requests = [];
|
|
|
|
};
|
|
|
|
|
|
|
|
this.mostRecent = function() {
|
|
|
|
return requests[requests.length - 1];
|
|
|
|
};
|
2013-10-19 04:05:47 +08:00
|
|
|
|
|
|
|
this.at = function(index) {
|
|
|
|
return requests[index];
|
|
|
|
};
|
2013-11-02 06:05:30 +08:00
|
|
|
|
|
|
|
this.filter = function(url_to_match) {
|
|
|
|
if (requests.length == 0) return [];
|
|
|
|
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;
|
|
|
|
};
|
2013-10-12 13:16:22 +08:00
|
|
|
}
|
2013-02-05 11:32:59 +08:00
|
|
|
|
2014-01-18 03:38:27 +08:00
|
|
|
function RequestStub(url, stubData) {
|
2014-01-22 11:06:23 +08:00
|
|
|
var split = url.split('?');
|
|
|
|
this.url = split[0];
|
|
|
|
|
|
|
|
var normalizeQuery = function(query) {
|
|
|
|
return query ? query.split('&').sort().join('&') : undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.query = normalizeQuery(split[1]);
|
|
|
|
this.data = normalizeQuery(stubData);
|
2013-01-09 15:47:56 +08:00
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
this.andReturn = function(options) {
|
|
|
|
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;
|
|
|
|
this.responseText = options.responseText;
|
|
|
|
};
|
2014-01-18 03:38:27 +08:00
|
|
|
|
2014-01-22 11:06:23 +08:00
|
|
|
this.matches = function(fullUrl, data) {
|
|
|
|
var urlSplit = fullUrl.split('?'),
|
|
|
|
url = urlSplit[0],
|
|
|
|
query = urlSplit[1];
|
|
|
|
return this.url === url && this.query === normalizeQuery(query) && (!this.data || this.data === normalizeQuery(data));
|
2014-01-18 03:38:27 +08:00
|
|
|
};
|
2013-10-12 13:16:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof window === "undefined" && typeof exports === "object") {
|
|
|
|
exports.MockAjax = MockAjax;
|
2013-10-19 04:05:47 +08:00
|
|
|
jasmine.Ajax = new MockAjax(exports);
|
2013-10-12 13:16:22 +08:00
|
|
|
} else {
|
|
|
|
window.MockAjax = MockAjax;
|
2013-10-19 04:05:47 +08:00
|
|
|
jasmine.Ajax = new MockAjax(window);
|
2013-10-12 13:16:22 +08:00
|
|
|
}
|
2013-01-09 15:47:56 +08:00
|
|
|
}());
|
2011-08-08 04:01:38 +08:00
|
|
|
|