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;
|
|
|
|
};
|
2010-09-17 07:51:52 +08:00
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
this.stubRequest = function(url) {
|
|
|
|
var stub = new RequestStub(url);
|
|
|
|
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
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
this.findStub = function(url) {
|
|
|
|
for (var i = stubs.length - 1; i >= 0; i--) {
|
|
|
|
var stub = stubs[i];
|
|
|
|
if (stub.url === url) {
|
|
|
|
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);
|
2010-09-17 06:46:55 +08:00
|
|
|
}
|
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
extend(FakeXMLHttpRequest.prototype, window.XMLHttpRequest);
|
|
|
|
extend(FakeXMLHttpRequest.prototype, {
|
|
|
|
requestHeaders: {},
|
2010-09-17 06:46:55 +08:00
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
open: function() {
|
|
|
|
this.method = arguments[0];
|
|
|
|
this.url = arguments[1];
|
|
|
|
this.username = arguments[3];
|
|
|
|
this.password = arguments[4];
|
|
|
|
this.readyState = 1;
|
|
|
|
},
|
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";
|
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;
|
2013-02-05 11:32:59 +08:00
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
var stub = stubTracker.findStub(this.url);
|
|
|
|
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]));
|
|
|
|
data[key].sort();
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
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-10-12 13:16:22 +08:00
|
|
|
}
|
2013-02-05 11:32:59 +08:00
|
|
|
|
2013-10-12 13:16:22 +08:00
|
|
|
function RequestStub(url) {
|
|
|
|
this.url = url;
|
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;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|