3
<script src="../lib/OpenLayers.js"></script>
4
<script type="text/javascript">
6
window._xhr = OpenLayers.Request.XMLHttpRequest;
7
var anon = new Function();
8
OpenLayers.Request.XMLHttpRequest = function() {};
9
OpenLayers.Request.XMLHttpRequest.prototype = {
11
setRequestHeader: anon,
14
OpenLayers.Request.XMLHttpRequest.DONE = 4;
17
OpenLayers.Request.XMLHttpRequest = window._xhr;
20
function test_issue(t) {
25
var proto = OpenLayers.Request.XMLHttpRequest.prototype;
26
var issue = OpenLayers.Function.bind(OpenLayers.Request.issue,
29
// test that issue returns a new XMLHttpRequest - 1 test
31
t.ok(request instanceof OpenLayers.Request.XMLHttpRequest,
32
"returns an XMLHttpRequest instance");
34
// test that issue calls xhr.open with correct args from config - 5 tests
35
var _open = proto.open;
38
url: "http://nowhere",
43
proto.open = function(method, url, async, user, password) {
44
t.eq(method, config.method, "open called with correct method");
45
t.eq(url, config.url, "open called with correct url");
46
t.eq(async, config.async, "open called with correct async");
47
t.eq(user, config.user, "open called with correct user");
48
t.eq(password, config.password, "open called with correct password");
50
request = issue(config);
52
// test that params are serialized as query string - 1 test
55
url: "http://example.com/",
56
params: {"foo": "bar"}
58
proto.open = function(method, url, async, user, password) {
59
t.eq(url, config.url + "?foo=bar", "params serialized as query string");
61
request = issue(config);
63
// test that empty params object doesn't produce query string - 1 test
66
url: "http://example.com/",
69
proto.open = function(method, url, async, user, password) {
70
t.eq(url, config.url, "empty params doesn't produce query string");
72
request = issue(config);
74
// test that query string doesn't get two ? separators
77
url: "http://example.com/?existing=query",
78
params: {"foo": "bar"}
80
proto.open = function(method, url, async, user, password) {
81
t.eq(url, config.url + "&foo=bar", "existing query string gets extended with &");
83
request = issue(config);
88
// test that headers are correctly set - 4 tests
89
var _setRequestHeader = proto.setRequestHeader;
96
proto.setRequestHeader = function(key, value) {
97
t.ok(key in config.headers, "setRequestHeader called with key: " + key);
98
t.eq(value, config.headers[key], "setRequestHeader called with correct value: " + value);
100
request = issue(config);
101
proto.setRequestHeader = _setRequestHeader;
103
// test that callback is called (no scope) - 1 test
104
var unbound = function(request) {
105
t.ok(request instanceof OpenLayers.Request.XMLHttpRequest,
106
"unbound callback called with xhr instance");
111
request = issue(config);
112
request.readyState = OpenLayers.Request.XMLHttpRequest.DONE;
113
request.onreadystatechange();
115
// test that callback is called (with scope) - 2 tests
117
var bound = function(request) {
118
t.ok(this === obj, "bound callback has correct scope");
119
t.ok(request instanceof OpenLayers.Request.XMLHttpRequest,
120
"bound callback called with xhr instance");
126
request = issue(config);
127
request.readyState = 4;
128
request.onreadystatechange();
130
// test that send is called with data - 1 test
131
var _send = proto.send;
136
proto.send = function(data) {
137
t.eq(data, config.data, "send called with correct data");
139
request = issue(config);
142
// test that optional success callback is only called with 200s and
143
// failure is only called with non-200s
144
var _send = proto.send;
145
proto.send = function() {};
148
success: function(req) {
149
t.ok(!req.status || (req.status >= 200 && req.status < 300),
150
"success callback called with " + req.status + " status");
152
failure: function(req) {
153
t.ok(req.status && (req.status < 200 || req.status >= 300),
154
"failure callback called with " + req.status + " status");
157
request = issue(config);
158
request.readyState = 4;
160
// mock up status 200 (1 test)
161
request.status = 200;
162
request.onreadystatechange();
164
// mock up status 299 (1 test)
165
request.status = 299;
166
request.onreadystatechange();
168
// mock up status 100 (1 test)
169
request.status = 100;
170
request.onreadystatechange();
172
// mock up status 300 (1 test)
173
request.status = 300;
174
request.onreadystatechange();
176
// mock up a status null (1 test)
177
request.status = null;
178
request.onreadystatechange();
185
function test_GET(t) {
187
var _issue = OpenLayers.Request.issue;
188
OpenLayers.Request.issue = function(config) {
189
t.eq(config.method, "GET", "calls issue with correct method");
191
OpenLayers.Request.GET();
192
OpenLayers.Request.issue = _issue;
194
function test_POST(t) {
196
var _issue = OpenLayers.Request.issue;
197
OpenLayers.Request.issue = function(config) {
198
t.eq(config.method, "POST", "calls issue with correct method");
200
OpenLayers.Request.POST();
201
OpenLayers.Request.issue = _issue;
203
function test_PUT(t) {
205
var _issue = OpenLayers.Request.issue;
206
OpenLayers.Request.issue = function(config) {
207
t.eq(config.method, "PUT", "calls issue with correct method");
209
OpenLayers.Request.PUT();
210
OpenLayers.Request.issue = _issue;
212
function test_DELETE(t) {
214
var _issue = OpenLayers.Request.issue;
215
OpenLayers.Request.issue = function(config) {
216
t.eq(config.method, "DELETE", "calls issue with correct method");
218
OpenLayers.Request.DELETE();
219
OpenLayers.Request.issue = _issue;
221
function test_HEAD(t) {
223
var _issue = OpenLayers.Request.issue;
224
OpenLayers.Request.issue = function(config) {
225
t.eq(config.method, "HEAD", "calls issue with correct method");
227
OpenLayers.Request.HEAD();
228
OpenLayers.Request.issue = _issue;
230
function test_OPTIONS(t) {
232
var _issue = OpenLayers.Request.issue;
233
OpenLayers.Request.issue = function(config) {
234
t.eq(config.method, "OPTIONS", "calls issue with correct method");
236
OpenLayers.Request.OPTIONS();
237
OpenLayers.Request.issue = _issue;