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 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
|
// Copyright 2007 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
goog.provide('goog.net.XhrIoTest');
goog.setTestOnly('goog.net.XhrIoTest');
goog.require('goog.Uri');
goog.require('goog.debug.EntryPointMonitor');
goog.require('goog.debug.ErrorHandler');
goog.require('goog.debug.entryPointRegistry');
goog.require('goog.events');
goog.require('goog.functions');
goog.require('goog.net.EventType');
goog.require('goog.net.WrapperXmlHttpFactory');
goog.require('goog.net.XhrIo');
goog.require('goog.net.XmlHttp');
goog.require('goog.object');
goog.require('goog.string');
goog.require('goog.testing.MockClock');
goog.require('goog.testing.PropertyReplacer');
goog.require('goog.testing.jsunit');
goog.require('goog.testing.net.XhrIo');
goog.require('goog.testing.recordFunction');
goog.require('goog.userAgent.product');
function MockXmlHttp() {
/**
* The request headers for this XmlHttpRequest.
* @type {!Object<string>}
*/
this.requestHeaders = {};
/**
* The response headers for this XmlHttpRequest.
* @type {!Object<string>}
*/
this.responseHeaders = {};
/**
* The upload object associated with this XmlHttpRequest.
* @type {!Object}
*/
this.upload = {};
}
MockXmlHttp.prototype.readyState = goog.net.XmlHttp.ReadyState.UNINITIALIZED;
MockXmlHttp.prototype.status = 200;
MockXmlHttp.syncSend = false;
MockXmlHttp.prototype.send = function(opt_data) {
this.readyState = goog.net.XmlHttp.ReadyState.UNINITIALIZED;
if (MockXmlHttp.syncSend) {
this.complete();
}
};
MockXmlHttp.prototype.complete = function() {
this.readyState = goog.net.XmlHttp.ReadyState.LOADING;
this.onreadystatechange();
this.readyState = goog.net.XmlHttp.ReadyState.LOADED;
this.onreadystatechange();
this.readyState = goog.net.XmlHttp.ReadyState.INTERACTIVE;
this.onreadystatechange();
this.readyState = goog.net.XmlHttp.ReadyState.COMPLETE;
this.onreadystatechange();
};
MockXmlHttp.prototype.open = function(verb, uri, async) {};
MockXmlHttp.prototype.abort = function() {};
MockXmlHttp.prototype.setRequestHeader = function(key, value) {
this.requestHeaders[key] = value;
};
/**
* @param {string} key
* @return {?string}
*/
MockXmlHttp.prototype.getResponseHeader = function(key) {
return key in this.responseHeaders ? this.responseHeaders[key] : null;
};
var lastMockXmlHttp;
goog.net.XmlHttp.setGlobalFactory(
new goog.net.WrapperXmlHttpFactory(
function() {
lastMockXmlHttp = new MockXmlHttp();
return lastMockXmlHttp;
},
function() { return {}; }));
var propertyReplacer = new goog.testing.PropertyReplacer();
var clock;
var originalEntryPoint = goog.net.XhrIo.prototype.onReadyStateChangeEntryPoint_;
function setUp() {
lastMockXmlHttp = null;
clock = new goog.testing.MockClock(true);
}
function tearDown() {
propertyReplacer.reset();
clock.dispose();
goog.net.XhrIo.prototype.onReadyStateChangeEntryPoint_ = originalEntryPoint;
}
function testSyncSend() {
if (goog.userAgent.product.SAFARI) {
// TODO(b/20733468): Disabled so we can get the rest of the Closure test
// suite running in a continuous build. Will investigate later.
return;
}
MockXmlHttp.syncSend = true;
var count = 0;
var x = new goog.net.XhrIo;
goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
assertFalse('Should not fire complete from inside send', inSend);
assertTrue('Should be successful', e.target.isSuccess());
count++;
});
var inSend = true;
x.send('url');
inSend = false;
clock.tick(1); // callOnce(f, 0, ...)
assertEquals('Complete should have been called once', 1, count);
}
function testSyncSendFailure() {
if (goog.userAgent.product.SAFARI) {
// TODO(b/20733468): Disabled so we can get the rest of the Closure test
// suite running in a continuous build. Will investigate later.
return;
}
MockXmlHttp.syncSend = true;
var count = 0;
var x = new goog.net.XhrIo;
goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
assertFalse('Should not fire complete from inside send', inSend);
assertFalse('Should not be successful', e.target.isSuccess());
count++;
});
var inSend = true;
x.send('url');
lastMockXmlHttp.status = 404;
inSend = false;
clock.tick(1); // callOnce(f, 0, ...)
assertEquals('Complete should have been called once', 1, count);
}
function testSendRelativeZeroStatus() {
if (goog.userAgent.product.SAFARI) {
// TODO(b/20733468): Disabled so we can get the rest of the Closure test
// suite running in a continuous build. Will investigate later.
return;
}
MockXmlHttp.syncSend = true;
var count = 0;
var x = new goog.net.XhrIo;
goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
assertFalse('Should not fire complete from inside send', inSend);
assertEquals(
'Should be the same as ', e.target.isSuccess(),
window.location.href.toLowerCase().indexOf('file:') == 0);
count++;
});
var inSend = true;
x.send('relative');
lastMockXmlHttp.status = 0;
inSend = false;
clock.tick(1); // callOnce(f, 0, ...)
assertEquals('Complete should have been called once', 1, count);
}
function testSendRelativeUriZeroStatus() {
if (goog.userAgent.product.SAFARI) {
// TODO(b/20733468): Disabled so we can get the rest of the Closure test
// suite running in a continuous build. Will investigate later.
return;
}
MockXmlHttp.syncSend = true;
var count = 0;
var x = new goog.net.XhrIo;
goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
assertFalse('Should not fire complete from inside send', inSend);
assertEquals(
'Should be the same as ', e.target.isSuccess(),
window.location.href.toLowerCase().indexOf('file:') == 0);
count++;
});
var inSend = true;
x.send(goog.Uri.parse('relative'));
lastMockXmlHttp.status = 0;
inSend = false;
clock.tick(1); // callOnce(f, 0, ...)
assertEquals('Complete should have been called once', 1, count);
}
function testSendHttpZeroStatusFailure() {
if (goog.userAgent.product.SAFARI) {
// TODO(b/20733468): Disabled so we can get the rest of the Closure test
// suite running in a continuous build. Will investigate later.
return;
}
MockXmlHttp.syncSend = true;
var count = 0;
var x = new goog.net.XhrIo;
goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
assertFalse('Should not fire complete from inside send', inSend);
assertFalse('Should not be successful', e.target.isSuccess());
count++;
});
var inSend = true;
x.send('http://foo');
lastMockXmlHttp.status = 0;
inSend = false;
clock.tick(1); // callOnce(f, 0, ...)
assertEquals('Complete should have been called once', 1, count);
}
function testSendHttpUpperZeroStatusFailure() {
MockXmlHttp.syncSend = true;
var count = 0;
var x = new goog.net.XhrIo;
goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
assertFalse('Should not fire complete from inside send', inSend);
assertFalse('Should not be successful', e.target.isSuccess());
count++;
});
var inSend = true;
x.send('HTTP://foo');
lastMockXmlHttp.status = 0;
inSend = false;
clock.tick(1); // callOnce(f, 0, ...)
assertEquals('Complete should have been called once', 1, count);
}
function testSendHttpUpperUriZeroStatusFailure() {
MockXmlHttp.syncSend = true;
var count = 0;
var x = new goog.net.XhrIo;
goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
assertFalse('Should not fire complete from inside send', inSend);
assertFalse('Should not be successful', e.target.isSuccess());
count++;
});
var inSend = true;
x.send(goog.Uri.parse('HTTP://foo'));
lastMockXmlHttp.status = 0;
inSend = false;
clock.tick(1); // callOnce(f, 0, ...)
assertEquals('Complete should have been called once', 1, count);
}
function testSendHttpUriZeroStatusFailure() {
MockXmlHttp.syncSend = true;
var count = 0;
var x = new goog.net.XhrIo;
goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
assertFalse('Should not fire complete from inside send', inSend);
assertFalse('Should not be successful', e.target.isSuccess());
count++;
});
var inSend = true;
x.send(goog.Uri.parse('http://foo'));
lastMockXmlHttp.status = 0;
inSend = false;
clock.tick(1); // callOnce(f, 0, ...)
assertEquals('Complete should have been called once', 1, count);
}
function testSendHttpUriZeroStatusFailure() {
MockXmlHttp.syncSend = true;
var count = 0;
var x = new goog.net.XhrIo;
goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
assertFalse('Should not fire complete from inside send', inSend);
assertFalse('Should not be successful', e.target.isSuccess());
count++;
});
var inSend = true;
x.send(goog.Uri.parse('HTTP://foo'));
lastMockXmlHttp.status = 0;
inSend = false;
clock.tick(1); // callOnce(f, 0, ...)
assertEquals('Complete should have been called once', 1, count);
}
function testSendHttpsZeroStatusFailure() {
if (goog.userAgent.product.SAFARI) {
// TODO(b/20733468): Disabled so we can get the rest of the Closure test
// suite running in a continuous build. Will investigate later.
return;
}
MockXmlHttp.syncSend = true;
var count = 0;
var x = new goog.net.XhrIo;
goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
assertFalse('Should not fire complete from inside send', inSend);
assertFalse('Should not be successful', e.target.isSuccess());
count++;
});
var inSend = true;
x.send('https://foo');
lastMockXmlHttp.status = 0;
inSend = false;
clock.tick(1); // callOnce(f, 0, ...)
assertEquals('Complete should have been called once', 1, count);
}
function testSendFileUpperZeroStatusSuccess() {
MockXmlHttp.syncSend = true;
var count = 0;
var x = new goog.net.XhrIo;
goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
assertFalse('Should not fire complete from inside send', inSend);
assertTrue('Should not be successful', e.target.isSuccess());
count++;
});
var inSend = true;
x.send('FILE:///foo');
lastMockXmlHttp.status = 0;
inSend = false;
clock.tick(1); // callOnce(f, 0, ...)
assertEquals('Complete should have been called once', 1, count);
}
function testSendFileUriZeroStatusSuccess() {
MockXmlHttp.syncSend = true;
var count = 0;
var x = new goog.net.XhrIo;
goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
assertFalse('Should not fire complete from inside send', inSend);
assertTrue('Should not be successful', e.target.isSuccess());
count++;
});
var inSend = true;
x.send(goog.Uri.parse('file:///foo'));
lastMockXmlHttp.status = 0;
inSend = false;
clock.tick(1); // callOnce(f, 0, ...)
assertEquals('Complete should have been called once', 1, count);
}
function testSendDummyUriZeroStatusSuccess() {
MockXmlHttp.syncSend = true;
var count = 0;
var x = new goog.net.XhrIo;
goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
assertFalse('Should not fire complete from inside send', inSend);
assertTrue('Should not be successful', e.target.isSuccess());
count++;
});
var inSend = true;
x.send(goog.Uri.parse('dummy:///foo'));
lastMockXmlHttp.status = 0;
inSend = false;
clock.tick(1); // callOnce(f, 0, ...)
assertEquals('Complete should have been called once', 1, count);
}
function testSendFileUpperUriZeroStatusSuccess() {
MockXmlHttp.syncSend = true;
var count = 0;
var x = new goog.net.XhrIo;
goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
assertFalse('Should not fire complete from inside send', inSend);
assertTrue('Should not be successful', e.target.isSuccess());
count++;
});
var inSend = true;
x.send(goog.Uri.parse('FILE:///foo'));
lastMockXmlHttp.status = 0;
inSend = false;
clock.tick(1); // callOnce(f, 0, ...)
assertEquals('Complete should have been called once', 1, count);
}
function testSendFromListener() {
MockXmlHttp.syncSend = true;
var count = 0;
var x = new goog.net.XhrIo;
goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
count++;
var e = assertThrows(function() { x.send('url2'); });
assertEquals(
'[goog.net.XhrIo] Object is active with another request=url' +
'; newUri=url2',
e.message);
});
x.send('url');
clock.tick(1); // callOnce(f, 0, ...)
assertEquals('Complete should have been called once', 1, count);
}
function testStatesDuringEvents() {
if (goog.userAgent.product.SAFARI) {
// TODO(b/20733468): Disabled so we can get the rest of the Closure test
// suite running in a continuous build. Will investigate later.
return;
}
MockXmlHttp.syncSend = true;
var x = new goog.net.XhrIo;
var readyState = goog.net.XmlHttp.ReadyState.UNINITIALIZED;
goog.events.listen(x, goog.net.EventType.READY_STATE_CHANGE, function(e) {
readyState++;
assertObjectEquals(e.target, x);
assertEquals(x.getReadyState(), readyState);
assertTrue(x.isActive());
});
goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
assertObjectEquals(e.target, x);
assertTrue(x.isActive());
});
goog.events.listen(x, goog.net.EventType.SUCCESS, function(e) {
assertObjectEquals(e.target, x);
assertTrue(x.isActive());
});
goog.events.listen(x, goog.net.EventType.READY, function(e) {
assertObjectEquals(e.target, x);
assertFalse(x.isActive());
});
x.send('url');
clock.tick(1); // callOnce(f, 0, ...)
}
function testProtectEntryPointCalledOnAsyncSend() {
MockXmlHttp.syncSend = false;
var errorHandlerCallbackCalled = false;
var errorHandler = new goog.debug.ErrorHandler(function() {
errorHandlerCallbackCalled = true;
});
goog.net.XhrIo.protectEntryPoints(errorHandler);
var x = new goog.net.XhrIo;
goog.events.listen(x, goog.net.EventType.READY_STATE_CHANGE, function(e) {
throw new Error();
});
x.send('url');
assertThrows(function() { lastMockXmlHttp.complete(); });
assertTrue(
'Error handler callback should be called on async send.',
errorHandlerCallbackCalled);
}
function testXHRIsDiposedEvenIfAListenerThrowsAnExceptionOnComplete() {
MockXmlHttp.syncSend = false;
var x = new goog.net.XhrIo;
goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
throw new Error();
}, false, x);
x.send('url');
assertThrows(function() { lastMockXmlHttp.complete(); });
// The XHR should have been disposed, even though the listener threw an
// exception.
assertNull(x.xhr_);
}
function testDisposeInternalDoesNotAbortXhrRequestObjectWhenActiveIsFalse() {
MockXmlHttp.syncSend = false;
var xmlHttp = goog.net.XmlHttp;
var abortCalled = false;
var x = new goog.net.XhrIo;
goog.net.XmlHttp.prototype.abort = function() { abortCalled = true; };
goog.events.listen(x, goog.net.EventType.COMPLETE, function(e) {
this.active_ = false;
this.dispose();
}, false, x);
x.send('url');
lastMockXmlHttp.complete();
goog.net.XmlHttp = xmlHttp;
assertFalse(abortCalled);
}
function testCallingAbortFromWithinAbortCallbackDoesntLoop() {
var x = new goog.net.XhrIo;
goog.events.listen(x, goog.net.EventType.ABORT, function(e) {
x.abort(); // Shouldn't get a stack overflow
});
x.send('url');
x.abort();
}
function testPostSetsContentTypeHeader() {
var x = new goog.net.XhrIo;
x.send('url', 'POST', 'content');
var headers = lastMockXmlHttp.requestHeaders;
assertEquals(1, goog.object.getCount(headers));
assertEquals(
headers[goog.net.XhrIo.CONTENT_TYPE_HEADER],
goog.net.XhrIo.FORM_CONTENT_TYPE);
}
function testNonPostSetsContentTypeHeader() {
var x = new goog.net.XhrIo;
x.send('url', 'PUT', 'content');
headers = lastMockXmlHttp.requestHeaders;
assertEquals(1, goog.object.getCount(headers));
assertEquals(
headers[goog.net.XhrIo.CONTENT_TYPE_HEADER],
goog.net.XhrIo.FORM_CONTENT_TYPE);
}
function testContentTypeIsTreatedCaseInsensitively() {
var x = new goog.net.XhrIo;
x.send('url', 'POST', 'content', {'content-type': 'testing'});
assertObjectEquals(
'Headers should not be modified since they already contain a ' +
'content type definition',
{'content-type': 'testing'}, lastMockXmlHttp.requestHeaders);
}
function testIsContentTypeHeader_() {
assertTrue(goog.net.XhrIo.isContentTypeHeader_('content-type'));
assertTrue(goog.net.XhrIo.isContentTypeHeader_('Content-type'));
assertTrue(goog.net.XhrIo.isContentTypeHeader_('CONTENT-TYPE'));
assertTrue(goog.net.XhrIo.isContentTypeHeader_('Content-Type'));
assertFalse(goog.net.XhrIo.isContentTypeHeader_('Content Type'));
}
function testPostFormDataDoesNotSetContentTypeHeader() {
function FakeFormData() {}
propertyReplacer.set(goog.global, 'FormData', FakeFormData);
var x = new goog.net.XhrIo;
x.send('url', 'POST', new FakeFormData());
var headers = lastMockXmlHttp.requestHeaders;
assertTrue(goog.object.isEmpty(headers));
}
function testNonPostFormDataDoesNotSetContentTypeHeader() {
function FakeFormData() {}
propertyReplacer.set(goog.global, 'FormData', FakeFormData);
var x = new goog.net.XhrIo;
x.send('url', 'PUT', new FakeFormData());
headers = lastMockXmlHttp.requestHeaders;
assertTrue(goog.object.isEmpty(headers));
}
function testFactoryInjection() {
var xhr = new MockXmlHttp();
var optionsFactoryCalled = 0;
var xhrFactoryCalled = 0;
var wrapperFactory = new goog.net.WrapperXmlHttpFactory(
function() {
xhrFactoryCalled++;
return xhr;
},
function() {
optionsFactoryCalled++;
return {};
});
var xhrIo = new goog.net.XhrIo(wrapperFactory);
xhrIo.send('url');
assertEquals('XHR factory should have been called', 1, xhrFactoryCalled);
assertEquals(
'Options factory should have been called', 1, optionsFactoryCalled);
}
function testGoogTestingNetXhrIoIsInSync() {
var xhrIo = new goog.net.XhrIo();
var testingXhrIo = new goog.testing.net.XhrIo();
var propertyComparator = function(value, key, obj) {
if (goog.string.endsWith(key, '_')) {
// Ignore private properties/methods
return true;
} else if (typeof value == 'function' && typeof this[key] != 'function') {
// Only type check is sufficient for functions
fail(
'Mismatched property:' + key + ': goog.net.XhrIo has:<' + value +
'>; while goog.testing.net.XhrIo has:<' + this[key] + '>');
return true;
} else {
// Ignore all other type of properties.
return true;
}
};
goog.object.every(xhrIo, propertyComparator, testingXhrIo);
}
function testEntryPointRegistry() {
var monitor = new goog.debug.EntryPointMonitor();
var replacement = function() {};
monitor.wrap =
goog.testing.recordFunction(goog.functions.constant(replacement));
goog.debug.entryPointRegistry.monitorAll(monitor);
assertTrue(monitor.wrap.getCallCount() >= 1);
assertEquals(
replacement, goog.net.XhrIo.prototype.onReadyStateChangeEntryPoint_);
}
function testSetWithCredentials() {
// Test on XHR objects that don't have the withCredentials property (older
// browsers).
var x = new goog.net.XhrIo;
x.setWithCredentials(true);
x.send('url');
assertFalse(
'withCredentials should not be set on an XHR object if the property ' +
'does not exist.',
goog.object.containsKey(lastMockXmlHttp, 'withCredentials'));
// Test on XHR objects that have the withCredentials property.
MockXmlHttp.prototype.withCredentials = false;
x = new goog.net.XhrIo;
x.setWithCredentials(true);
x.send('url');
assertTrue(
'withCredentials should be set on an XHR object if the property exists',
goog.object.containsKey(lastMockXmlHttp, 'withCredentials'));
assertTrue(
'withCredentials value not set on XHR object',
lastMockXmlHttp.withCredentials);
// Reset the prototype so it does not effect other tests.
delete MockXmlHttp.prototype.withCredentials;
}
function testSetProgressEventsEnabled() {
// The default MockXhr object contained by the XhrIo object has no
// reference to the necessary onprogress field. This is equivalent
// to a browser which does not support progress events.
var progressNotSupported = new goog.net.XhrIo;
progressNotSupported.setProgressEventsEnabled(true);
assertTrue(progressNotSupported.getProgressEventsEnabled());
progressNotSupported.send('url');
assertUndefined(
'Progress is not supported for downloads on this request.',
progressNotSupported.xhr_.onprogress);
assertUndefined(
'Progress is not supported for uploads on this request.',
progressNotSupported.xhr_.upload.onprogress);
// The following tests will include the necessary onprogress fields
// indicating progress events are supported.
MockXmlHttp.prototype.onprogress = null;
var progressDisabled = new goog.net.XhrIo;
progressDisabled.setProgressEventsEnabled(false);
assertFalse(progressDisabled.getProgressEventsEnabled());
progressDisabled.send('url');
assertNull(
'No progress handler should be set for downloads.',
progressDisabled.xhr_.onprogress);
assertUndefined(
'No progress handler should be set for uploads.',
progressDisabled.xhr_.upload.onprogress);
var progressEnabled = new goog.net.XhrIo;
progressEnabled.setProgressEventsEnabled(true);
assertTrue(progressEnabled.getProgressEventsEnabled());
progressEnabled.send('url');
assertTrue(
'Progress handler should be set for downloads.',
goog.isFunction(progressEnabled.xhr_.onprogress));
assertTrue(
'Progress handler should be set for uploads.',
goog.isFunction(progressEnabled.xhr_.upload.onprogress));
// Clean-up.
delete MockXmlHttp.prototype.onprogress;
}
function testGetResponse() {
var x = new goog.net.XhrIo;
// No XHR yet
assertEquals(null, x.getResponse());
// XHR with no .response and no response type, gets text.
x.xhr_ = {};
x.xhr_.responseText = 'text';
assertEquals('text', x.getResponse());
// Response type of text gets text as well.
x.setResponseType(goog.net.XhrIo.ResponseType.TEXT);
x.xhr_.responseText = '';
assertEquals('', x.getResponse());
// Response type of array buffer gets the array buffer.
x.xhr_.mozResponseArrayBuffer = 'ab';
x.setResponseType(goog.net.XhrIo.ResponseType.ARRAY_BUFFER);
assertEquals('ab', x.getResponse());
// With a response field, it is returned no matter what value it has.
x.xhr_.response = undefined;
assertEquals(undefined, x.getResponse());
x.xhr_.response = null;
assertEquals(null, x.getResponse());
x.xhr_.response = '';
assertEquals('', x.getResponse());
x.xhr_.response = 'resp';
assertEquals('resp', x.getResponse());
}
function testGetResponseHeader() {
var x = new goog.net.XhrIo();
x.send('http://foo');
x.xhr_.responseHeaders['foo'] = null;
x.xhr_.responseHeaders['bar'] = 'xyz';
x.xhr_.responseHeaders['baz'] = '';
// All headers should be undefined prior to the request completing.
assertUndefined(x.getResponseHeader('foo'));
assertUndefined(x.getResponseHeader('bar'));
assertUndefined(x.getResponseHeader('baz'));
x.xhr_.readyState = goog.net.XmlHttp.ReadyState.COMPLETE;
assertUndefined(x.getResponseHeader('foo'));
assertEquals('xyz', x.getResponseHeader('bar'));
assertEquals('', x.getResponseHeader('baz'));
}
function testGetResponseHeaders() {
var x = new goog.net.XhrIo();
// No XHR yet
assertEquals(0, goog.object.getCount(x.getResponseHeaders()));
// Simulate an XHR with 2 headers.
var headersRaw = 'test1: foo\r\ntest2: bar';
propertyReplacer.set(
x, 'getAllResponseHeaders', goog.functions.constant(headersRaw));
var headers = x.getResponseHeaders();
assertEquals(2, goog.object.getCount(headers));
assertEquals('foo', headers['test1']);
assertEquals('bar', headers['test2']);
}
function testGetResponseHeadersWithColonInValue() {
var x = new goog.net.XhrIo();
// Simulate an XHR with a colon in the http header value.
var headersRaw = 'test1: f:o:o';
propertyReplacer.set(
x, 'getAllResponseHeaders', goog.functions.constant(headersRaw));
var headers = x.getResponseHeaders();
assertEquals(1, goog.object.getCount(headers));
assertEquals('f:o:o', headers['test1']);
}
function testGetResponseHeadersMultipleValuesForOneKey() {
var x = new goog.net.XhrIo();
// No XHR yet
assertEquals(0, goog.object.getCount(x.getResponseHeaders()));
// Simulate an XHR with 2 headers.
var headersRaw = 'test1: foo\r\ntest1: bar';
propertyReplacer.set(
x, 'getAllResponseHeaders', goog.functions.constant(headersRaw));
var headers = x.getResponseHeaders();
assertEquals(1, goog.object.getCount(headers));
assertEquals('foo, bar', headers['test1']);
}
function testGetResponseHeadersEmptyHeader() {
var x = new goog.net.XhrIo();
// No XHR yet
assertEquals(0, goog.object.getCount(x.getResponseHeaders()));
// Simulate an XHR with 2 headers, the last of which is empty.
var headersRaw = 'test2: bar\r\n';
propertyReplacer.set(
x, 'getAllResponseHeaders', goog.functions.constant(headersRaw));
var headers = x.getResponseHeaders();
assertEquals(1, goog.object.getCount(headers));
assertEquals('bar', headers['test2']);
}
|