git-svn-id: http://svn.openlayers.org/trunk/openlayers@8816 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
255 lines
8.5 KiB
HTML
255 lines
8.5 KiB
HTML
<html>
|
|
<head>
|
|
<script src="../lib/OpenLayers.js"></script>
|
|
<script type="text/javascript">
|
|
function setup() {
|
|
window._xhr = OpenLayers.Request.XMLHttpRequest;
|
|
var anon = new Function();
|
|
OpenLayers.Request.XMLHttpRequest = function() {};
|
|
OpenLayers.Request.XMLHttpRequest.prototype = {
|
|
open: anon,
|
|
setRequestHeader: anon,
|
|
send: anon
|
|
};
|
|
OpenLayers.Request.XMLHttpRequest.DONE = 4;
|
|
}
|
|
function teardown() {
|
|
OpenLayers.Request.XMLHttpRequest = window._xhr;
|
|
}
|
|
|
|
function test_issue(t) {
|
|
setup();
|
|
|
|
t.plan(21);
|
|
var request, config;
|
|
var proto = OpenLayers.Request.XMLHttpRequest.prototype;
|
|
var issue = OpenLayers.Function.bind(OpenLayers.Request.issue,
|
|
OpenLayers.Request);
|
|
|
|
// test that issue returns a new XMLHttpRequest - 1 test
|
|
request = issue();
|
|
t.ok(request instanceof OpenLayers.Request.XMLHttpRequest,
|
|
"returns an XMLHttpRequest instance");
|
|
|
|
// test that issue calls xhr.open with correct args from config - 5 tests
|
|
var _open = proto.open;
|
|
config = {
|
|
method: "foo",
|
|
url: "http://nowhere",
|
|
async: "bar",
|
|
user: "uncle",
|
|
password: "sam"
|
|
};
|
|
proto.open = function(method, url, async, user, password) {
|
|
t.eq(method, config.method, "open called with correct method");
|
|
t.eq(url, config.url, "open called with correct url");
|
|
t.eq(async, config.async, "open called with correct async");
|
|
t.eq(user, config.user, "open called with correct user");
|
|
t.eq(password, config.password, "open called with correct password");
|
|
};
|
|
request = issue(config);
|
|
|
|
// test that params are serialized as query string - 1 test
|
|
config = {
|
|
method: "GET",
|
|
url: "http://example.com/",
|
|
params: {"foo": "bar"}
|
|
};
|
|
proto.open = function(method, url, async, user, password) {
|
|
t.eq(url, config.url + "?foo=bar", "params serialized as query string");
|
|
};
|
|
request = issue(config);
|
|
|
|
// test that empty params object doesn't produce query string - 1 test
|
|
config = {
|
|
method: "GET",
|
|
url: "http://example.com/",
|
|
params: {}
|
|
};
|
|
proto.open = function(method, url, async, user, password) {
|
|
t.eq(url, config.url, "empty params doesn't produce query string");
|
|
}
|
|
request = issue(config);
|
|
|
|
// test that query string doesn't get two ? separators
|
|
config = {
|
|
method: "GET",
|
|
url: "http://example.com/?existing=query",
|
|
params: {"foo": "bar"}
|
|
};
|
|
proto.open = function(method, url, async, user, password) {
|
|
t.eq(url, config.url + "&foo=bar", "existing query string gets extended with &");
|
|
}
|
|
request = issue(config);
|
|
|
|
// reset open method
|
|
proto.open = _open;
|
|
|
|
// test that headers are correctly set - 4 tests
|
|
var _setRequestHeader = proto.setRequestHeader;
|
|
config = {
|
|
headers: {
|
|
foo: "bar",
|
|
chicken: "soup"
|
|
}
|
|
};
|
|
proto.setRequestHeader = function(key, value) {
|
|
t.ok(key in config.headers, "setRequestHeader called with key: " + key);
|
|
t.eq(value, config.headers[key], "setRequestHeader called with correct value: " + value);
|
|
}
|
|
request = issue(config);
|
|
proto.setRequestHeader = _setRequestHeader;
|
|
|
|
// test that callback is called (no scope) - 1 test
|
|
var unbound = function(request) {
|
|
t.ok(request instanceof OpenLayers.Request.XMLHttpRequest,
|
|
"unbound callback called with xhr instance");
|
|
}
|
|
config = {
|
|
callback: unbound
|
|
};
|
|
request = issue(config);
|
|
request.readyState = OpenLayers.Request.XMLHttpRequest.DONE;
|
|
request.onreadystatechange();
|
|
|
|
// test that callback is called (with scope) - 2 tests
|
|
var obj = {};
|
|
var bound = function(request) {
|
|
t.ok(this === obj, "bound callback has correct scope");
|
|
t.ok(request instanceof OpenLayers.Request.XMLHttpRequest,
|
|
"bound callback called with xhr instance");
|
|
}
|
|
config = {
|
|
callback: bound,
|
|
scope: obj
|
|
};
|
|
request = issue(config);
|
|
request.readyState = 4;
|
|
request.onreadystatechange();
|
|
|
|
// test that optional success callback is only called with 200s and
|
|
// failure is only called with non-200s
|
|
var _send = proto.send;
|
|
proto.send = function() {};
|
|
|
|
config = {
|
|
success: function(req) {
|
|
t.ok(!req.status || (req.status >= 200 && req.status < 300),
|
|
"success callback called with " + req.status + " status");
|
|
},
|
|
failure: function(req) {
|
|
t.ok(req.status && (req.status < 200 || req.status >= 300),
|
|
"failure callback called with " + req.status + " status");
|
|
}
|
|
};
|
|
request = issue(config);
|
|
request.readyState = 4;
|
|
|
|
// mock up status 200 (1 test)
|
|
request.status = 200;
|
|
request.onreadystatechange();
|
|
|
|
// mock up status 299 (1 test)
|
|
request.status = 299;
|
|
request.onreadystatechange();
|
|
|
|
// mock up status 100 (1 test)
|
|
request.status = 100;
|
|
request.onreadystatechange();
|
|
|
|
// mock up status 300 (1 test)
|
|
request.status = 300;
|
|
request.onreadystatechange();
|
|
|
|
// mock up a status null (1 test)
|
|
request.status = null;
|
|
request.onreadystatechange();
|
|
|
|
proto.send = _send;
|
|
|
|
teardown();
|
|
}
|
|
|
|
function test_delayed_send(t) {
|
|
t.plan(1);
|
|
var proto = OpenLayers.Request.XMLHttpRequest.prototype;
|
|
var _send = proto.send;
|
|
|
|
// test that send is called with data - 1 test
|
|
var config = {
|
|
method: "PUT",
|
|
data: "bling"
|
|
};
|
|
var got = {};
|
|
proto.send = function(data) {
|
|
got.data = data;
|
|
}
|
|
OpenLayers.Request.issue(config);
|
|
|
|
t.delay_call(1, function() {
|
|
t.eq(got.data, config.data, "proper data sent");
|
|
proto.send = _send;
|
|
});
|
|
|
|
}
|
|
|
|
function test_GET(t) {
|
|
t.plan(1);
|
|
var _issue = OpenLayers.Request.issue;
|
|
OpenLayers.Request.issue = function(config) {
|
|
t.eq(config.method, "GET", "calls issue with correct method");
|
|
}
|
|
OpenLayers.Request.GET();
|
|
OpenLayers.Request.issue = _issue;
|
|
}
|
|
function test_POST(t) {
|
|
t.plan(1);
|
|
var _issue = OpenLayers.Request.issue;
|
|
OpenLayers.Request.issue = function(config) {
|
|
t.eq(config.method, "POST", "calls issue with correct method");
|
|
}
|
|
OpenLayers.Request.POST();
|
|
OpenLayers.Request.issue = _issue;
|
|
}
|
|
function test_PUT(t) {
|
|
t.plan(1);
|
|
var _issue = OpenLayers.Request.issue;
|
|
OpenLayers.Request.issue = function(config) {
|
|
t.eq(config.method, "PUT", "calls issue with correct method");
|
|
}
|
|
OpenLayers.Request.PUT();
|
|
OpenLayers.Request.issue = _issue;
|
|
}
|
|
function test_DELETE(t) {
|
|
t.plan(1);
|
|
var _issue = OpenLayers.Request.issue;
|
|
OpenLayers.Request.issue = function(config) {
|
|
t.eq(config.method, "DELETE", "calls issue with correct method");
|
|
}
|
|
OpenLayers.Request.DELETE();
|
|
OpenLayers.Request.issue = _issue;
|
|
}
|
|
function test_HEAD(t) {
|
|
t.plan(1);
|
|
var _issue = OpenLayers.Request.issue;
|
|
OpenLayers.Request.issue = function(config) {
|
|
t.eq(config.method, "HEAD", "calls issue with correct method");
|
|
}
|
|
OpenLayers.Request.HEAD();
|
|
OpenLayers.Request.issue = _issue;
|
|
}
|
|
function test_OPTIONS(t) {
|
|
t.plan(1);
|
|
var _issue = OpenLayers.Request.issue;
|
|
OpenLayers.Request.issue = function(config) {
|
|
t.eq(config.method, "OPTIONS", "calls issue with correct method");
|
|
}
|
|
OpenLayers.Request.OPTIONS();
|
|
OpenLayers.Request.issue = _issue;
|
|
}
|
|
</script>
|
|
</head>
|
|
<body>
|
|
</body>
|
|
</html>
|