Debian Codegraph @

closure-library/closure/goog/net/xhrio_test.js

raw
1 // Copyright 2007 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.net.XhrIoTest');
16 goog.setTestOnly('goog.net.XhrIoTest');
17
18 goog.require('goog.Uri');
19 goog.require('goog.debug.EntryPointMonitor');
20 goog.require('goog.debug.ErrorHandler');
21 goog.require('goog.debug.entryPointRegistry');
22 goog.require('goog.events');
23 goog.require('goog.functions');
24 goog.require('goog.net.EventType');
25 goog.require('goog.net.WrapperXmlHttpFactory');
26 goog.require('goog.net.XhrIo');
27 goog.require('goog.net.XmlHttp');
28 goog.require('goog.object');
29 goog.require('goog.string');
30 goog.require('goog.testing.MockClock');
31 goog.require('goog.testing.PropertyReplacer');
32 goog.require('goog.testing.jsunit');
33 goog.require('goog.testing.net.XhrIo');
34 goog.require('goog.testing.recordFunction');
35 goog.require('goog.userAgent.product');
36
37 function MockXmlHttp() {
38 /**
39 * The request headers for this XmlHttpRequest.
40 * @type {!Object<string>}
41 */
42 this.requestHeaders = {};
43
44 /**
45 * The response headers for this XmlHttpRequest.
46 * @type {!Object<string>}
47 */
48 this.responseHeaders = {};
49
50 /**
51 * The upload object associated with this XmlHttpRequest.
52 * @type {!Object}
53 */
54 this.upload = {};
55 }
56
57 MockXmlHttp.prototype.readyState = goog.net.XmlHttp.ReadyState.UNINITIALIZED;
58
59 MockXmlHttp.prototype.status = 200;
60
61 MockXmlHttp.syncSend = false;
62
63 MockXmlHttp.prototype.send = function(opt_data) {
64 this.readyState = goog.net.XmlHttp.ReadyState.UNINITIALIZED;
65
66 if (MockXmlHttp.syncSend) {
67 this.complete();
68 }
69 };
70
71 MockXmlHttp.prototype.complete = function() {
72 this.readyState = goog.net.XmlHttp.ReadyState.LOADING;
73 this.onreadystatechange();
74
75 this.readyState = goog.net.XmlHttp.ReadyState.LOADED;
76 this.onreadystatechange();
77
78 this.readyState = goog.net.XmlHttp.ReadyState.INTERACTIVE;
79 this.onreadystatechange();
80
81 this.readyState = goog.net.XmlHttp.ReadyState.COMPLETE;
82 this.onreadystatechange();
83 };
84
85
86 MockXmlHttp.prototype.open = function(verb, uri, async) {};
87
88 MockXmlHttp.prototype.abort = function() {};
89
90 MockXmlHttp.prototype.setRequestHeader = function(key, value) {
91 this.requestHeaders[key] = value;
92 };
93
94 /**
95 * @param {string} key
96 * @return {?string}
97 */
98 MockXmlHttp.prototype.getResponseHeader = function(key) {
99 return key in this.responseHeaders ? this.responseHeaders[key] : null;
100 };
101
102 var lastMockXmlHttp;
103 goog.net.XmlHttp.setGlobalFactory(
104 new goog.net.WrapperXmlHttpFactory(
105 function() {
106 lastMockXmlHttp = new MockXmlHttp();
107 return lastMockXmlHttp;
108 },
109 function() { return {}; }));
110
111
112 var propertyReplacer = new goog.testing.PropertyReplacer();
113 var clock;
114 var originalEntryPoint = goog.net.XhrIo.prototype.onReadyStateChangeEntryPoint_;
115
116 function setUp() {
117 lastMockXmlHttp = null;
118 clock = new goog.testing.MockClock(true);
119 }
120
121 function tearDown() {
122 propertyReplacer.reset();
123 clock.dispose();
124 goog.net.XhrIo.prototype.onReadyStateChangeEntryPoint_ = originalEntryPoint;
125 }
126
127
128 function testSyncSend() {
129 if (goog.userAgent.product.SAFARI) {
130 // TODO(b/20733468): Disabled so we can get the rest of the Closure test
131 // suite running in a continuous build. Will investigate later.
132 return;
133 }
134
135 MockXmlHttp.syncSend = true;
136 var count = 0;
137
138 var x = new goog.net.XhrIo;
139 goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
140 assertFalse('Should not fire complete from inside send', inSend);
141 assertTrue('Should be successful', e.target.isSuccess());
142 count++;
143
144 });
145
146 var inSend = true;
147 x.send('url');
148 inSend = false;
149
150 clock.tick(1); // callOnce(f, 0, ...)
151
152 assertEquals('Complete should have been called once', 1, count);
153 }
154
155 function testSyncSendFailure() {
156 if (goog.userAgent.product.SAFARI) {
157 // TODO(b/20733468): Disabled so we can get the rest of the Closure test
158 // suite running in a continuous build. Will investigate later.
159 return;
160 }
161
162 MockXmlHttp.syncSend = true;
163 var count = 0;
164
165 var x = new goog.net.XhrIo;
166 goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
167 assertFalse('Should not fire complete from inside send', inSend);
168 assertFalse('Should not be successful', e.target.isSuccess());
169 count++;
170 });
171
172 var inSend = true;
173 x.send('url');
174 lastMockXmlHttp.status = 404;
175 inSend = false;
176
177 clock.tick(1); // callOnce(f, 0, ...)
178
179 assertEquals('Complete should have been called once', 1, count);
180 }
181
182
183 function testSendRelativeZeroStatus() {
184 if (goog.userAgent.product.SAFARI) {
185 // TODO(b/20733468): Disabled so we can get the rest of the Closure test
186 // suite running in a continuous build. Will investigate later.
187 return;
188 }
189
190 MockXmlHttp.syncSend = true;
191 var count = 0;
192
193 var x = new goog.net.XhrIo;
194 goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
195 assertFalse('Should not fire complete from inside send', inSend);
196 assertEquals(
197 'Should be the same as ', e.target.isSuccess(),
198 window.location.href.toLowerCase().indexOf('file:') == 0);
199 count++;
200 });
201
202 var inSend = true;
203 x.send('relative');
204 lastMockXmlHttp.status = 0;
205 inSend = false;
206
207 clock.tick(1); // callOnce(f, 0, ...)
208
209 assertEquals('Complete should have been called once', 1, count);
210 }
211
212
213 function testSendRelativeUriZeroStatus() {
214 if (goog.userAgent.product.SAFARI) {
215 // TODO(b/20733468): Disabled so we can get the rest of the Closure test
216 // suite running in a continuous build. Will investigate later.
217 return;
218 }
219
220 MockXmlHttp.syncSend = true;
221 var count = 0;
222
223 var x = new goog.net.XhrIo;
224 goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
225 assertFalse('Should not fire complete from inside send', inSend);
226 assertEquals(
227 'Should be the same as ', e.target.isSuccess(),
228 window.location.href.toLowerCase().indexOf('file:') == 0);
229 count++;
230 });
231
232 var inSend = true;
233 x.send(goog.Uri.parse('relative'));
234 lastMockXmlHttp.status = 0;
235 inSend = false;
236
237 clock.tick(1); // callOnce(f, 0, ...)
238
239 assertEquals('Complete should have been called once', 1, count);
240 }
241
242
243 function testSendHttpZeroStatusFailure() {
244 if (goog.userAgent.product.SAFARI) {
245 // TODO(b/20733468): Disabled so we can get the rest of the Closure test
246 // suite running in a continuous build. Will investigate later.
247 return;
248 }
249
250 MockXmlHttp.syncSend = true;
251 var count = 0;
252
253 var x = new goog.net.XhrIo;
254 goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
255 assertFalse('Should not fire complete from inside send', inSend);
256 assertFalse('Should not be successful', e.target.isSuccess());
257 count++;
258 });
259
260 var inSend = true;
261 x.send('http://foo');
262 lastMockXmlHttp.status = 0;
263 inSend = false;
264
265 clock.tick(1); // callOnce(f, 0, ...)
266
267 assertEquals('Complete should have been called once', 1, count);
268 }
269
270
271 function testSendHttpUpperZeroStatusFailure() {
272 MockXmlHttp.syncSend = true;
273 var count = 0;
274
275 var x = new goog.net.XhrIo;
276 goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
277 assertFalse('Should not fire complete from inside send', inSend);
278 assertFalse('Should not be successful', e.target.isSuccess());
279 count++;
280 });
281
282 var inSend = true;
283 x.send('HTTP://foo');
284 lastMockXmlHttp.status = 0;
285 inSend = false;
286
287 clock.tick(1); // callOnce(f, 0, ...)
288
289 assertEquals('Complete should have been called once', 1, count);
290 }
291
292
293 function testSendHttpUpperUriZeroStatusFailure() {
294 MockXmlHttp.syncSend = true;
295 var count = 0;
296
297 var x = new goog.net.XhrIo;
298 goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
299 assertFalse('Should not fire complete from inside send', inSend);
300 assertFalse('Should not be successful', e.target.isSuccess());
301 count++;
302 });
303
304 var inSend = true;
305 x.send(goog.Uri.parse('HTTP://foo'));
306 lastMockXmlHttp.status = 0;
307 inSend = false;
308
309 clock.tick(1); // callOnce(f, 0, ...)
310
311 assertEquals('Complete should have been called once', 1, count);
312 }
313
314
315 function testSendHttpUriZeroStatusFailure() {
316 MockXmlHttp.syncSend = true;
317 var count = 0;
318
319 var x = new goog.net.XhrIo;
320 goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
321 assertFalse('Should not fire complete from inside send', inSend);
322 assertFalse('Should not be successful', e.target.isSuccess());
323 count++;
324 });
325
326 var inSend = true;
327 x.send(goog.Uri.parse('http://foo'));
328 lastMockXmlHttp.status = 0;
329 inSend = false;
330
331 clock.tick(1); // callOnce(f, 0, ...)
332
333 assertEquals('Complete should have been called once', 1, count);
334 }
335
336
337 function testSendHttpUriZeroStatusFailure() {
338 MockXmlHttp.syncSend = true;
339 var count = 0;
340
341 var x = new goog.net.XhrIo;
342 goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
343 assertFalse('Should not fire complete from inside send', inSend);
344 assertFalse('Should not be successful', e.target.isSuccess());
345 count++;
346 });
347
348 var inSend = true;
349 x.send(goog.Uri.parse('HTTP://foo'));
350 lastMockXmlHttp.status = 0;
351 inSend = false;
352
353 clock.tick(1); // callOnce(f, 0, ...)
354
355 assertEquals('Complete should have been called once', 1, count);
356 }
357
358
359 function testSendHttpsZeroStatusFailure() {
360 if (goog.userAgent.product.SAFARI) {
361 // TODO(b/20733468): Disabled so we can get the rest of the Closure test
362 // suite running in a continuous build. Will investigate later.
363 return;
364 }
365
366 MockXmlHttp.syncSend = true;
367 var count = 0;
368
369 var x = new goog.net.XhrIo;
370 goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
371 assertFalse('Should not fire complete from inside send', inSend);
372 assertFalse('Should not be successful', e.target.isSuccess());
373 count++;
374 });
375
376 var inSend = true;
377 x.send('https://foo');
378 lastMockXmlHttp.status = 0;
379 inSend = false;
380
381 clock.tick(1); // callOnce(f, 0, ...)
382
383 assertEquals('Complete should have been called once', 1, count);
384 }
385
386
387 function testSendFileUpperZeroStatusSuccess() {
388 MockXmlHttp.syncSend = true;
389 var count = 0;
390
391 var x = new goog.net.XhrIo;
392 goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
393 assertFalse('Should not fire complete from inside send', inSend);
394 assertTrue('Should not be successful', e.target.isSuccess());
395 count++;
396 });
397
398 var inSend = true;
399 x.send('FILE:///foo');
400 lastMockXmlHttp.status = 0;
401 inSend = false;
402
403 clock.tick(1); // callOnce(f, 0, ...)
404
405 assertEquals('Complete should have been called once', 1, count);
406 }
407
408
409 function testSendFileUriZeroStatusSuccess() {
410 MockXmlHttp.syncSend = true;
411 var count = 0;
412
413 var x = new goog.net.XhrIo;
414 goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
415 assertFalse('Should not fire complete from inside send', inSend);
416 assertTrue('Should not be successful', e.target.isSuccess());
417 count++;
418 });
419
420 var inSend = true;
421 x.send(goog.Uri.parse('file:///foo'));
422 lastMockXmlHttp.status = 0;
423 inSend = false;
424
425 clock.tick(1); // callOnce(f, 0, ...)
426
427 assertEquals('Complete should have been called once', 1, count);
428 }
429
430
431 function testSendDummyUriZeroStatusSuccess() {
432 MockXmlHttp.syncSend = true;
433 var count = 0;
434
435 var x = new goog.net.XhrIo;
436 goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
437 assertFalse('Should not fire complete from inside send', inSend);
438 assertTrue('Should not be successful', e.target.isSuccess());
439 count++;
440 });
441
442 var inSend = true;
443 x.send(goog.Uri.parse('dummy:///foo'));
444 lastMockXmlHttp.status = 0;
445 inSend = false;
446
447 clock.tick(1); // callOnce(f, 0, ...)
448
449 assertEquals('Complete should have been called once', 1, count);
450 }
451
452
453 function testSendFileUpperUriZeroStatusSuccess() {
454 MockXmlHttp.syncSend = true;
455 var count = 0;
456
457 var x = new goog.net.XhrIo;
458 goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
459 assertFalse('Should not fire complete from inside send', inSend);
460 assertTrue('Should not be successful', e.target.isSuccess());
461 count++;
462 });
463
464 var inSend = true;
465 x.send(goog.Uri.parse('FILE:///foo'));
466 lastMockXmlHttp.status = 0;
467 inSend = false;
468
469 clock.tick(1); // callOnce(f, 0, ...)
470
471 assertEquals('Complete should have been called once', 1, count);
472 }
473
474
475 function testSendFromListener() {
476 MockXmlHttp.syncSend = true;
477 var count = 0;
478
479 var x = new goog.net.XhrIo;
480 goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
481 count++;
482
483 var e = assertThrows(function() { x.send('url2'); });
484 assertEquals(
485 '[goog.net.XhrIo] Object is active with another request=url' +
486 '; newUri=url2',
487 e.message);
488 });
489
490 x.send('url');
491
492 clock.tick(1); // callOnce(f, 0, ...)
493
494 assertEquals('Complete should have been called once', 1, count);
495 }
496
497
498 function testStatesDuringEvents() {
499 if (goog.userAgent.product.SAFARI) {
500 // TODO(b/20733468): Disabled so we can get the rest of the Closure test
501 // suite running in a continuous build. Will investigate later.
502 return;
503 }
504
505 MockXmlHttp.syncSend = true;
506
507 var x = new goog.net.XhrIo;
508 var readyState = goog.net.XmlHttp.ReadyState.UNINITIALIZED;
509 goog.events.listen(x, goog.net.EventType.READY_STATE_CHANGE, function(e) {
510 readyState++;
511 assertObjectEquals(e.target, x);
512 assertEquals(x.getReadyState(), readyState);
513 assertTrue(x.isActive());
514 });
515 goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
516 assertObjectEquals(e.target, x);
517 assertTrue(x.isActive());
518 });
519 goog.events.listen(x, goog.net.EventType.SUCCESS, function(e) {
520 assertObjectEquals(e.target, x);
521 assertTrue(x.isActive());
522 });
523 goog.events.listen(x, goog.net.EventType.READY, function(e) {
524 assertObjectEquals(e.target, x);
525 assertFalse(x.isActive());
526 });
527
528 x.send('url');
529
530 clock.tick(1); // callOnce(f, 0, ...)
531 }
532
533
534 function testProtectEntryPointCalledOnAsyncSend() {
535 MockXmlHttp.syncSend = false;
536
537 var errorHandlerCallbackCalled = false;
538 var errorHandler = new goog.debug.ErrorHandler(function() {
539 errorHandlerCallbackCalled = true;
540 });
541
542 goog.net.XhrIo.protectEntryPoints(errorHandler);
543
544 var x = new goog.net.XhrIo;
545 goog.events.listen(x, goog.net.EventType.READY_STATE_CHANGE, function(e) {
546 throw new Error();
547 });
548
549 x.send('url');
550 assertThrows(function() { lastMockXmlHttp.complete(); });
551
552 assertTrue(
553 'Error handler callback should be called on async send.',
554 errorHandlerCallbackCalled);
555 }
556
557 function testXHRIsDiposedEvenIfAListenerThrowsAnExceptionOnComplete() {
558 MockXmlHttp.syncSend = false;
559
560 var x = new goog.net.XhrIo;
561
562 goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
563 throw new Error();
564 }, false, x);
565
566 x.send('url');
567 assertThrows(function() { lastMockXmlHttp.complete(); });
568
569 // The XHR should have been disposed, even though the listener threw an
570 // exception.
571 assertNull(x.xhr_);
572 }
573
574 function testDisposeInternalDoesNotAbortXhrRequestObjectWhenActiveIsFalse() {
575 MockXmlHttp.syncSend = false;
576
577 var xmlHttp = goog.net.XmlHttp;
578 var abortCalled = false;
579 var x = new goog.net.XhrIo;
580
581 goog.net.XmlHttp.prototype.abort = function() { abortCalled = true; };
582
583 goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
584 this.active_ = false;
585 this.dispose();
586 }, false, x);
587
588 x.send('url');
589 lastMockXmlHttp.complete();
590
591 goog.net.XmlHttp = xmlHttp;
592 assertFalse(abortCalled);
593 }
594
595 function testCallingAbortFromWithinAbortCallbackDoesntLoop() {
596 var x = new goog.net.XhrIo;
597 goog.events.listen(x, goog.net.EventType.ABORT, function(e) {
598 x.abort(); // Shouldn't get a stack overflow
599 });
600 x.send('url');
601 x.abort();
602 }
603
604 function testPostSetsContentTypeHeader() {
605 var x = new goog.net.XhrIo;
606
607 x.send('url', 'POST', 'content');
608 var headers = lastMockXmlHttp.requestHeaders;
609 assertEquals(1, goog.object.getCount(headers));
610 assertEquals(
611 headers[goog.net.XhrIo.CONTENT_TYPE_HEADER],
612 goog.net.XhrIo.FORM_CONTENT_TYPE);
613 }
614
615 function testNonPostSetsContentTypeHeader() {
616 var x = new goog.net.XhrIo;
617
618 x.send('url', 'PUT', 'content');
619 headers = lastMockXmlHttp.requestHeaders;
620 assertEquals(1, goog.object.getCount(headers));
621 assertEquals(
622 headers[goog.net.XhrIo.CONTENT_TYPE_HEADER],
623 goog.net.XhrIo.FORM_CONTENT_TYPE);
624 }
625
626 function testContentTypeIsTreatedCaseInsensitively() {
627 var x = new goog.net.XhrIo;
628
629 x.send('url', 'POST', 'content', {'content-type': 'testing'});
630
631 assertObjectEquals(
632 'Headers should not be modified since they already contain a ' +
633 'content type definition',
634 {'content-type': 'testing'}, lastMockXmlHttp.requestHeaders);
635 }
636
637 function testIsContentTypeHeader_() {
638 assertTrue(goog.net.XhrIo.isContentTypeHeader_('content-type'));
639 assertTrue(goog.net.XhrIo.isContentTypeHeader_('Content-type'));
640 assertTrue(goog.net.XhrIo.isContentTypeHeader_('CONTENT-TYPE'));
641 assertTrue(goog.net.XhrIo.isContentTypeHeader_('Content-Type'));
642 assertFalse(goog.net.XhrIo.isContentTypeHeader_('Content Type'));
643 }
644
645 function testPostFormDataDoesNotSetContentTypeHeader() {
646 function FakeFormData() {}
647
648 propertyReplacer.set(goog.global, 'FormData', FakeFormData);
649
650 var x = new goog.net.XhrIo;
651 x.send('url', 'POST', new FakeFormData());
652 var headers = lastMockXmlHttp.requestHeaders;
653 assertTrue(goog.object.isEmpty(headers));
654 }
655
656 function testNonPostFormDataDoesNotSetContentTypeHeader() {
657 function FakeFormData() {}
658
659 propertyReplacer.set(goog.global, 'FormData', FakeFormData);
660
661 var x = new goog.net.XhrIo;
662 x.send('url', 'PUT', new FakeFormData());
663 headers = lastMockXmlHttp.requestHeaders;
664 assertTrue(goog.object.isEmpty(headers));
665 }
666
667 function testFactoryInjection() {
668 var xhr = new MockXmlHttp();
669 var optionsFactoryCalled = 0;
670 var xhrFactoryCalled = 0;
671 var wrapperFactory = new goog.net.WrapperXmlHttpFactory(
672 function() {
673 xhrFactoryCalled++;
674 return xhr;
675 },
676 function() {
677 optionsFactoryCalled++;
678 return {};
679 });
680 var xhrIo = new goog.net.XhrIo(wrapperFactory);
681
682 xhrIo.send('url');
683
684 assertEquals('XHR factory should have been called', 1, xhrFactoryCalled);
685 assertEquals(
686 'Options factory should have been called', 1, optionsFactoryCalled);
687 }
688
689 function testGoogTestingNetXhrIoIsInSync() {
690 var xhrIo = new goog.net.XhrIo();
691 var testingXhrIo = new goog.testing.net.XhrIo();
692
693 var propertyComparator = function(value, key, obj) {
694 if (goog.string.endsWith(key, '_')) {
695 // Ignore private properties/methods
696 return true;
697 } else if (typeof value == 'function' && typeof this[key] != 'function') {
698 // Only type check is sufficient for functions
699 fail(
700 'Mismatched property:' + key + ': goog.net.XhrIo has:<' + value +
701 '>; while goog.testing.net.XhrIo has:<' + this[key] + '>');
702 return true;
703 } else {
704 // Ignore all other type of properties.
705 return true;
706 }
707 };
708
709 goog.object.every(xhrIo, propertyComparator, testingXhrIo);
710 }
711
712 function testEntryPointRegistry() {
713 var monitor = new goog.debug.EntryPointMonitor();
714 var replacement = function() {};
715 monitor.wrap =
716 goog.testing.recordFunction(goog.functions.constant(replacement));
717
718 goog.debug.entryPointRegistry.monitorAll(monitor);
719 assertTrue(monitor.wrap.getCallCount() >= 1);
720 assertEquals(
721 replacement, goog.net.XhrIo.prototype.onReadyStateChangeEntryPoint_);
722 }
723
724 function testSetWithCredentials() {
725 // Test on XHR objects that don't have the withCredentials property (older
726 // browsers).
727 var x = new goog.net.XhrIo;
728 x.setWithCredentials(true);
729 x.send('url');
730 assertFalse(
731 'withCredentials should not be set on an XHR object if the property ' +
732 'does not exist.',
733 goog.object.containsKey(lastMockXmlHttp, 'withCredentials'));
734
735 // Test on XHR objects that have the withCredentials property.
736 MockXmlHttp.prototype.withCredentials = false;
737 x = new goog.net.XhrIo;
738 x.setWithCredentials(true);
739 x.send('url');
740 assertTrue(
741 'withCredentials should be set on an XHR object if the property exists',
742 goog.object.containsKey(lastMockXmlHttp, 'withCredentials'));
743
744 assertTrue(
745 'withCredentials value not set on XHR object',
746 lastMockXmlHttp.withCredentials);
747
748 // Reset the prototype so it does not effect other tests.
749 delete MockXmlHttp.prototype.withCredentials;
750 }
751
752 function testSetProgressEventsEnabled() {
753 // The default MockXhr object contained by the XhrIo object has no
754 // reference to the necessary onprogress field. This is equivalent
755 // to a browser which does not support progress events.
756 var progressNotSupported = new goog.net.XhrIo;
757 progressNotSupported.setProgressEventsEnabled(true);
758 assertTrue(progressNotSupported.getProgressEventsEnabled());
759 progressNotSupported.send('url');
760 assertUndefined(
761 'Progress is not supported for downloads on this request.',
762 progressNotSupported.xhr_.onprogress);
763 assertUndefined(
764 'Progress is not supported for uploads on this request.',
765 progressNotSupported.xhr_.upload.onprogress);
766
767 // The following tests will include the necessary onprogress fields
768 // indicating progress events are supported.
769 MockXmlHttp.prototype.onprogress = null;
770
771 var progressDisabled = new goog.net.XhrIo;
772 progressDisabled.setProgressEventsEnabled(false);
773 assertFalse(progressDisabled.getProgressEventsEnabled());
774 progressDisabled.send('url');
775 assertNull(
776 'No progress handler should be set for downloads.',
777 progressDisabled.xhr_.onprogress);
778 assertUndefined(
779 'No progress handler should be set for uploads.',
780 progressDisabled.xhr_.upload.onprogress);
781
782 var progressEnabled = new goog.net.XhrIo;
783 progressEnabled.setProgressEventsEnabled(true);
784 assertTrue(progressEnabled.getProgressEventsEnabled());
785 progressEnabled.send('url');
786 assertTrue(
787 'Progress handler should be set for downloads.',
788 goog.isFunction(progressEnabled.xhr_.onprogress));
789 assertTrue(
790 'Progress handler should be set for uploads.',
791 goog.isFunction(progressEnabled.xhr_.upload.onprogress));
792
793 // Clean-up.
794 delete MockXmlHttp.prototype.onprogress;
795 }
796
797
798 function testGetResponse() {
799 var x = new goog.net.XhrIo;
800
801 // No XHR yet
802 assertEquals(null, x.getResponse());
803
804 // XHR with no .response and no response type, gets text.
805 x.xhr_ = {};
806 x.xhr_.responseText = 'text';
807 assertEquals('text', x.getResponse());
808
809 // Response type of text gets text as well.
810 x.setResponseType(goog.net.XhrIo.ResponseType.TEXT);
811 x.xhr_.responseText = '';
812 assertEquals('', x.getResponse());
813
814 // Response type of array buffer gets the array buffer.
815 x.xhr_.mozResponseArrayBuffer = 'ab';
816 x.setResponseType(goog.net.XhrIo.ResponseType.ARRAY_BUFFER);
817 assertEquals('ab', x.getResponse());
818
819 // With a response field, it is returned no matter what value it has.
820 x.xhr_.response = undefined;
821 assertEquals(undefined, x.getResponse());
822
823 x.xhr_.response = null;
824 assertEquals(null, x.getResponse());
825
826 x.xhr_.response = '';
827 assertEquals('', x.getResponse());
828
829 x.xhr_.response = 'resp';
830 assertEquals('resp', x.getResponse());
831 }
832
833 function testGetResponseHeader() {
834 var x = new goog.net.XhrIo();
835 x.send('http://foo');
836
837 x.xhr_.responseHeaders['foo'] = null;
838 x.xhr_.responseHeaders['bar'] = 'xyz';
839 x.xhr_.responseHeaders['baz'] = '';
840
841 // All headers should be undefined prior to the request completing.
842 assertUndefined(x.getResponseHeader('foo'));
843 assertUndefined(x.getResponseHeader('bar'));
844 assertUndefined(x.getResponseHeader('baz'));
845
846 x.xhr_.readyState = goog.net.XmlHttp.ReadyState.COMPLETE;
847
848 assertUndefined(x.getResponseHeader('foo'));
849 assertEquals('xyz', x.getResponseHeader('bar'));
850 assertEquals('', x.getResponseHeader('baz'));
851 }
852
853 function testGetResponseHeaders() {
854 var x = new goog.net.XhrIo();
855
856 // No XHR yet
857 assertEquals(0, goog.object.getCount(x.getResponseHeaders()));
858
859 // Simulate an XHR with 2 headers.
860 var headersRaw = 'test1: foo\r\ntest2: bar';
861
862 propertyReplacer.set(
863 x, 'getAllResponseHeaders', goog.functions.constant(headersRaw));
864
865 var headers = x.getResponseHeaders();
866 assertEquals(2, goog.object.getCount(headers));
867 assertEquals('foo', headers['test1']);
868 assertEquals('bar', headers['test2']);
869 }
870
871 function testGetResponseHeadersWithColonInValue() {
872 var x = new goog.net.XhrIo();
873
874 // Simulate an XHR with a colon in the http header value.
875 var headersRaw = 'test1: f:o:o';
876
877 propertyReplacer.set(
878 x, 'getAllResponseHeaders', goog.functions.constant(headersRaw));
879
880 var headers = x.getResponseHeaders();
881 assertEquals(1, goog.object.getCount(headers));
882 assertEquals('f:o:o', headers['test1']);
883 }
884
885 function testGetResponseHeadersMultipleValuesForOneKey() {
886 var x = new goog.net.XhrIo();
887
888 // No XHR yet
889 assertEquals(0, goog.object.getCount(x.getResponseHeaders()));
890
891 // Simulate an XHR with 2 headers.
892 var headersRaw = 'test1: foo\r\ntest1: bar';
893
894 propertyReplacer.set(
895 x, 'getAllResponseHeaders', goog.functions.constant(headersRaw));
896
897 var headers = x.getResponseHeaders();
898 assertEquals(1, goog.object.getCount(headers));
899 assertEquals('foo, bar', headers['test1']);
900 }
901
902 function testGetResponseHeadersEmptyHeader() {
903 var x = new goog.net.XhrIo();
904
905 // No XHR yet
906 assertEquals(0, goog.object.getCount(x.getResponseHeaders()));
907
908 // Simulate an XHR with 2 headers, the last of which is empty.
909 var headersRaw = 'test2: bar\r\n';
910
911 propertyReplacer.set(
912 x, 'getAllResponseHeaders', goog.functions.constant(headersRaw));
913
914 var headers = x.getResponseHeaders();
915 assertEquals(1, goog.object.getCount(headers));
916 assertEquals('bar', headers['test2']);
917 }