aboutsummaryrefslogtreecommitdiff
path: root/includes/js/dojo/tests/_base/xhr.html
blob: ddc1bb46a12fb9785964e31e3a90e023a9dc58da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
	"http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<title>testing form and xhr utils</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<style type="text/css">
			@import "../../resources/dojo.css";
		</style>
		<script type="text/javascript" 
			src="../../dojo.js" djConfig="isDebug: true"></script>
		<script type="text/javascript">
			dojo.require("doh.runner");
			dojo.addOnLoad(function(){
				var f1fo = { 'blah': "blah" };
				var f1foStr = "blah=blah";
				var f1foJson = '{"blah":"blah"}';

				var f2fo = { 
					blah: "blah",
					multi: [
						"thud",
						"thonk"
					],
					textarea: "textarea_value"
				};
				var f2foStr = "blah=blah&multi=thud&multi=thonk&textarea=textarea_value";
				var f2foJson = '{"blah":"blah","multi":["thud","thonk"],"textarea":"textarea_value"}';

				var f3fo = { 
					spaces: "string with spaces"
				};
				var f3foStr = "spaces=string%20with%20spaces&";
				var f3foJson = '{"spaces":"string with spaces"}';

				var f5fo = { 'blåh': "bláh" };
				var f5foStr = "bl%C3%A5h=bl%C3%A1h";
				var f5foJson = '{"blåh":"bláh"}';


				doh.register("t", 
					[
						function formNodeToObject(t){
							t.is(f1fo , dojo.formToObject(dojo.byId("f1")));
							t.is(f5fo , dojo.formToObject(dojo.byId("f5")));
						},
						function formIdToObject(t){
							t.is(f1fo , dojo.formToObject("f1"));
							t.is(f5fo , dojo.formToObject("f5"));
						},
						function formToObjectWithMultiSelect(t){
							t.is(f2fo , dojo.formToObject("f2"));
						},
						function objectToQuery(t){
							t.is(f1foStr , dojo.objectToQuery(f1fo));
							t.is(f5foStr , dojo.objectToQuery(f5fo));
						},
						function objectToQueryArr(t){
							t.is(f2foStr, dojo.objectToQuery(f2fo));
						},
						function formToQuery(t){
							t.is(f1foStr, dojo.formToQuery("f1"));
							t.is(f5foStr, dojo.formToQuery("f5"));
						},
						function formToQueryArr(t){
							t.is(f2foStr, dojo.formToQuery("f2"));
						},
						function formToJson(t){
							t.is(f1foJson, dojo.formToJson("f1"));
							t.is(f5foJson, dojo.formToJson("f5"));
						},
						function formToJsonArr(t){
							t.is(f2foJson, dojo.formToJson("f2"));
						},
						function queryToObject(t){
							t.is(f1fo , dojo.queryToObject(f1foStr));
							t.is(f2fo , dojo.queryToObject(f2foStr));
							t.is(f3fo , dojo.queryToObject(f3foStr));
							t.is(f5fo , dojo.queryToObject(f5foStr));
						},
						function textContentHandler(t){
							t.is("foo bar baz ", 
								dojo._contentHandlers.text({
									responseText: "foo bar baz "
								})
							);
						},
						function jsonContentHandler(t){
							var jsonObj = {
								foo: "bar",
								baz: [
									{ thonk: "blarg" },
									"xyzzy!"
								]
							};
							t.is(jsonObj, 
								dojo._contentHandlers.json({
									responseText: dojo.toJson(jsonObj)
								})
							);
						},
						function jsonCFContentHandler(t){
							var jsonObj = {
								foo: "bar",
								baz: [
									{ thonk: "blarg" },
									"xyzzy!"
								]
							};
							var e;
							try{
								dojo._contentHandlers["json-comment-filtered"]({
									responseText: dojo.toJson(jsonObj)
								})
							}catch(ex){
								e = ex;
							}finally{
								// did we fail closed?
								t.is((typeof e), "object");
							}
							t.is(jsonObj,
								dojo._contentHandlers["json-comment-filtered"]({
									responseText: "\tblag\n/*"+dojo.toJson(jsonObj)+"*/\n\r\t\r"
								})
							);
						},
						function jsContentHandler(t){
							var jsonObj = {
								foo: "bar",
								baz: [
									{ thonk: "blarg" },
									"xyzzy!"
								]
							};
							t.is(jsonObj,
								dojo._contentHandlers["javascript"]({
									responseText: "("+dojo.toJson(jsonObj)+")"
								})
							);
							t.t(dojo._contentHandlers["javascript"]({
									responseText: "true;"
								})
							);
							t.f(dojo._contentHandlers["javascript"]({
									responseText: "false;"
								})
							);
						},
						function xmlContentHandler(t){
							var fauxObj = {
								foo: "bar",
								baz: [
									{ thonk: "blarg" },
									"xyzzy!"
								]
							};
							t.is(fauxObj,
								dojo._contentHandlers["xml"]({ responseXML: fauxObj })
							);
						},
						function xhrGet(t){
							var d = new doh.Deferred();
							var td = dojo.xhrGet({
								url: "xhr.html", // self
								preventCache: true,
								load: function(text, ioArgs){
									t.is(4, ioArgs.xhr.readyState);
									return text; //must return a value here or the parent test deferred fails.
								}
							});
							t.t(td instanceof dojo.Deferred);
							td.addCallback(d, "callback");
							return d;
						},
						function xhrGet404(t){
							var d = new doh.Deferred();
							try{
								var td = dojo.xhrGet({
									url: "xhr_blarg.html", // doesn't exist
									error: function(err, ioArgs){
										t.is(404, ioArgs.xhr.status);
										return err; //must return a value here or the parent test deferred fails.
									}
								});
								// td.addErrback(d, "callback");
							}catch(e){
								d.callback(true);
							}
							// return d;
						},
						function xhrGetContent(t){
							var d = new doh.Deferred();
							var td = dojo.xhrGet({
								url: "xhr.html?color=blue",
								content: {
									foo: [ "bar", "baz" ],
									thud: "thonk",
									xyzzy: 3
								}
							});
							td.addCallback(function(text){
								// console.debug(td, td.xhr, td.args);
								t.is("xhr.html?color=blue&foo=bar&foo=baz&thud=thonk&xyzzy=3", 
										td.ioArgs.url);
								d.callback(true);
							});
							return d;
						},
						function xhrGetForm(t){
							var d = new doh.Deferred();
							var td = dojo.xhrGet({
								url: "xhr.html", // self
								form: "f3"
							});
							td.addCallback(function(xhr){
								// console.debug(td.args.url);
								t.is("xhr.html?spaces=string%20with%20spaces", td.ioArgs.url);
								d.callback(true);
							});
							return d;
						},
						function xhrGetFormWithContent(t){
							// ensure that stuff passed via content over-rides
							// what's specified in the form
							var d = new doh.Deferred();
							var td = dojo.xhrGet({
								url: "xhr.html", // self
								form: "f3",
								content: { spaces: "blah" }
							});
							td.addCallback(function(xhr){
								// console.debug(td.args.url);
								t.is("xhr.html?spaces=blah", td.ioArgs.url);
								d.callback(true);
							});
							return d;
						},
						function xhrPost(t){
							var d = new doh.Deferred();
							var td = dojo.xhrPost({
								url: "xhr.html?foo=bar", // self
								content: { color: "blue"},
								handle: function(res, ioArgs){
									if((dojo._isDocumentOk(ioArgs.xhr))||
										(ioArgs.xhr.status == 405)
									){
										d.callback(true);
									}else{
										d.errback(false);
									}								
								}
							});
							// t.t(td instanceof dojo.Deferred);
							return d;
						},
						function xhrPostWithContent(t){
							var d = new doh.Deferred();
							var td = dojo.xhrPost({
								url: "xhr.html",
								content: {
									foo: [ "bar", "baz" ],
									thud: "thonk",
									xyzzy: 3
								}
							});
							td.addBoth(function(text){
								t.is("foo=bar&foo=baz&thud=thonk&xyzzy=3", 
										td.ioArgs.query);
								if(	(dojo._isDocumentOk(td.ioArgs.xhr))||
									(td.ioArgs.xhr.status == 405)
								){
									d.callback(true);
								}else{
									d.errback(false);
								}
							});
							return d;
						},
						function xhrPostForm(t){
							var d = new doh.Deferred();
							var form = dojo.byId("f4");

							//Make sure we can send a form to its 
							//action URL.   See trac: #2844.
							var td = dojo.xhrPost({
								form: form
							});
							td.addCallback(function(){
								d.callback(true);
							});
							td.addErrback(function(error){
								d.callback(error);
							});
							// t.t(td instanceof dojo.Deferred);
							return d;
						},
						function rawXhrPost(t){
							var d = new doh.Deferred();
							var td = dojo.rawXhrPost({
								url: "xhr.html", // self
								postContent: "foo=bar&color=blue&height=average",
								handle: function(res, ioArgs){
									if((dojo._isDocumentOk(ioArgs.xhr))||
										(ioArgs.xhr.status == 405)
									){
										d.callback(true);
									}else{
										d.errback(false);
									}								
								}
							});
							// t.t(td instanceof dojo.Deferred);
							return d;
						},
						function xhrPut(t){
							var d = new doh.Deferred();
							var td = dojo.xhrPut({
								url: "xhrDummyMethod.php?foo=bar", // self
								content: { color: "blue"},
								handle: function(res, ioArgs){
									if((dojo._isDocumentOk(ioArgs.xhr))||
										(ioArgs.xhr.status == 403)
									){
										d.callback(true);
									}else{
										d.errback(false);
									}								
								}
							});
							// t.t(td instanceof dojo.Deferred);
							return d;
						},
						function xhrDelete(t){
							var d = new doh.Deferred();
							var td = dojo.xhrDelete({
								url: "xhrDummyMethod.php", // self
								preventCache: true,
								handle: function(res, ioArgs){
									if((dojo._isDocumentOk(ioArgs.xhr))||
										(ioArgs.xhr.status == 403)
									){
										d.callback(true);
									}else{
										d.errback(false);
									}								
								}
							});
							// t.t(td instanceof dojo.Deferred);
							return d;
						},
						function xhrCancel(t){
							var d = new doh.Deferred();
							var td = dojo.xhrPost({
								url: "xhrDummyMethod.php", // self
								handle: function(res, ioArgs){
									if(res instanceof Error && res.dojoType == "cancel"){
										d.callback(true);
									}else{
										d.errback(false);
									}								
								}
							});
							td.cancel();
							// t.t(td instanceof dojo.Deferred);
							return d;
						}
						// FIXME: need to add tests for wrapForm
					]
				);
				doh.run();
			});
		</script>
	</head>
	<body>
		<form id="f1" style="border: 1px solid black;">
			<input type="text" name="blah" value="blah">
			<input type="text" name="no_value" value="blah" disabled>
			<input type="button" name="no_value2" value="blah">
		</form>
		<form id="f2" style="border: 1px solid black;">
			<input type="text" name="blah" value="blah">
			<input type="text" name="no_value" value="blah" disabled>
			<input type="button" name="no_value2" value="blah">
			<select type="select" multiple name="multi" size="5">
				<option value="blah">blah</option>
				<option value="thud" selected>thud</option>
				<option value="thonk" selected>thonk</option>
			</select>
			<textarea name="textarea">textarea_value</textarea>
		</form>
		<form id="f3" style="border: 1px solid black;">
			<input type="hidden" name="spaces" value="string with spaces">
		</form>
		<form id="f4" style="border: 1px solid black;" action="xhrDummyMethod.php">
			<input type="hidden" name="action" value="Form with input named action">
		</form>
		<form id="f5" style="border: 1px solid black;">
			<input type="text" name="blåh" value="bláh">
			<input type="text" name="no_value" value="blah" disabled>
			<input type="button" name="no_value2" value="blah">
		</form>
	</body>
</html>