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 optional success callback is only called with 200s and
131 // failure is only called with non-200s
132 var _send = proto.send;
133 proto.send = function() {};
136 success: function(req) {
137 t.ok(!req.status || (req.status >= 200 && req.status < 300),
138 "success callback called with " + req.status + " status");
140 failure: function(req) {
141 t.ok(req.status && (req.status < 200 || req.status >= 300),
142 "failure callback called with " + req.status + " status");
145 request = issue(config);
146 request.readyState = 4;
148 // mock up status 200 (1 test)
149 request.status = 200;
150 request.onreadystatechange();
152 // mock up status 299 (1 test)
153 request.status = 299;
154 request.onreadystatechange();
156 // mock up status 100 (1 test)
157 request.status = 100;
158 request.onreadystatechange();
160 // mock up status 300 (1 test)
161 request.status = 300;
162 request.onreadystatechange();
164 // mock up a status null (1 test)
165 request.status = null;
166 request.onreadystatechange();
173 function test_delayed_send(t) {
175 var proto = OpenLayers.Request.XMLHttpRequest.prototype;
176 var _send = proto.send;
178 // test that send is called with data - 1 test
184 proto.send = function(data) {
187 OpenLayers.Request.issue(config);
189 t.delay_call(1, function() {
190 t.eq(got.data, config.data, "proper data sent");
196 function test_GET(t) {
198 var _issue = OpenLayers.Request.issue;
199 OpenLayers.Request.issue = function(config) {
200 t.eq(config.method, "GET", "calls issue with correct method");
202 OpenLayers.Request.GET();
203 OpenLayers.Request.issue = _issue;
205 function test_POST(t) {
207 var _issue = OpenLayers.Request.issue;
208 OpenLayers.Request.issue = function(config) {
209 t.eq(config.method, "POST", "calls issue with correct method");
211 OpenLayers.Request.POST();
212 OpenLayers.Request.issue = _issue;
214 function test_PUT(t) {
216 var _issue = OpenLayers.Request.issue;
217 OpenLayers.Request.issue = function(config) {
218 t.eq(config.method, "PUT", "calls issue with correct method");
220 OpenLayers.Request.PUT();
221 OpenLayers.Request.issue = _issue;
223 function test_DELETE(t) {
225 var _issue = OpenLayers.Request.issue;
226 OpenLayers.Request.issue = function(config) {
227 t.eq(config.method, "DELETE", "calls issue with correct method");
229 OpenLayers.Request.DELETE();
230 OpenLayers.Request.issue = _issue;
232 function test_HEAD(t) {
234 var _issue = OpenLayers.Request.issue;
235 OpenLayers.Request.issue = function(config) {
236 t.eq(config.method, "HEAD", "calls issue with correct method");
238 OpenLayers.Request.HEAD();
239 OpenLayers.Request.issue = _issue;
241 function test_OPTIONS(t) {
243 var _issue = OpenLayers.Request.issue;
244 OpenLayers.Request.issue = function(config) {
245 t.eq(config.method, "OPTIONS", "calls issue with correct method");
247 OpenLayers.Request.OPTIONS();
248 OpenLayers.Request.issue = _issue;
251 function test_events_success(t) {
256 function listener(event) {
260 // set up event listeners
261 OpenLayers.Request.events.on({
267 // issue a request that succeeds
268 OpenLayers.Request.GET({
269 url: ".", params: {bar: "baz"}, async: false
271 t.eq(events.length, 2, "two events logged");
272 t.eq(events[0].type, "complete", "first event is complete");
273 t.eq(events[1].type, "success", "second event is success");
274 t.ok(events[1].config, "success listener sent config");
275 t.eq(events[1].requestUrl, ".?bar=baz", "success listener sent config.url");
277 // remove event listeners
278 OpenLayers.Request.events.un({
286 function test_events_failure(t) {
291 function listener(event) {
295 // set up event listeners
296 OpenLayers.Request.events.on({
302 // issue a request that succeeds
303 OpenLayers.Request.GET({
304 url: "foo", params: {bar: "baz"}, async: false
306 t.eq(events.length, 2, "two events logged");
307 t.eq(events[0].type, "complete", "first event is complete");
308 t.eq(events[1].type, "failure", "second event is failure");
309 t.ok(events[1].config, "failure listener sent config");
310 t.eq(events[1].requestUrl, "foo?bar=baz", "failure listener sent requestUrl");
312 // remove event listeners
313 OpenLayers.Request.events.un({