xhrmanager_test.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2012 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. goog.provide('goog.net.XhrManagerTest');
  15. goog.setTestOnly('goog.net.XhrManagerTest');
  16. goog.require('goog.events');
  17. goog.require('goog.net.EventType');
  18. goog.require('goog.net.XhrIo');
  19. goog.require('goog.net.XhrManager');
  20. goog.require('goog.testing.jsunit');
  21. goog.require('goog.testing.net.XhrIoPool');
  22. goog.require('goog.testing.recordFunction');
  23. /** @type {goog.net.XhrManager} */
  24. var xhrManager;
  25. /** @type {goog.testing.net.XhrIo} */
  26. var xhrIo;
  27. function setUp() {
  28. xhrManager = new goog.net.XhrManager();
  29. xhrManager.xhrPool_ = new goog.testing.net.XhrIoPool();
  30. xhrIo = xhrManager.xhrPool_.getXhr();
  31. }
  32. function tearDown() {
  33. goog.dispose(xhrManager);
  34. }
  35. function testGetOutstandingRequestIds() {
  36. assertArrayEquals(
  37. 'No outstanding requests', [], xhrManager.getOutstandingRequestIds());
  38. xhrManager.send('test1', '/test1');
  39. assertArrayEquals(
  40. 'Single outstanding request', ['test1'],
  41. xhrManager.getOutstandingRequestIds());
  42. xhrManager.send('test2', '/test2');
  43. assertArrayEquals(
  44. 'Two outstanding requests', ['test1', 'test2'],
  45. xhrManager.getOutstandingRequestIds());
  46. xhrIo.simulateResponse(200, 'data');
  47. assertArrayEquals(
  48. 'Single outstanding request', ['test2'],
  49. xhrManager.getOutstandingRequestIds());
  50. xhrIo.simulateResponse(200, 'data');
  51. assertArrayEquals(
  52. 'No outstanding requests', [], xhrManager.getOutstandingRequestIds());
  53. }
  54. function testForceAbortQueuedRequest() {
  55. xhrManager.send('test', '/test');
  56. xhrManager.send('queued', '/queued');
  57. assertNotThrows(
  58. 'Forced abort of queued request should not throw an error',
  59. goog.bind(xhrManager.abort, xhrManager, 'queued', true));
  60. assertNotThrows(
  61. 'Forced abort of normal request should not throw an error',
  62. goog.bind(xhrManager.abort, xhrManager, 'test', true));
  63. }
  64. function testDefaultResponseType() {
  65. var callback = goog.testing.recordFunction(function(e) {
  66. assertEquals('test1', e.id);
  67. assertEquals(
  68. goog.net.XhrIo.ResponseType.DEFAULT, e.xhrIo.getResponseType());
  69. eventCalled = true;
  70. });
  71. goog.events.listenOnce(xhrManager, goog.net.EventType.READY, callback);
  72. xhrManager.send('test1', '/test2');
  73. assertEquals(1, callback.getCallCount());
  74. xhrIo.simulateResponse(200, 'data'); // Do this to make tearDown() happy.
  75. }
  76. function testNonDefaultResponseType() {
  77. var callback = goog.testing.recordFunction(function(e) {
  78. assertEquals('test2', e.id);
  79. assertEquals(
  80. goog.net.XhrIo.ResponseType.ARRAY_BUFFER, e.xhrIo.getResponseType());
  81. eventCalled = true;
  82. });
  83. goog.events.listenOnce(xhrManager, goog.net.EventType.READY, callback);
  84. xhrManager.send(
  85. 'test2', '/test2', undefined /* opt_method */,
  86. undefined /* opt_content */, undefined /* opt_headers */,
  87. undefined /* opt_priority */, undefined /* opt_callback */,
  88. undefined /* opt_maxRetries */, goog.net.XhrIo.ResponseType.ARRAY_BUFFER);
  89. assertEquals(1, callback.getCallCount());
  90. xhrIo.simulateResponse(200, 'data'); // Do this to make tearDown() happy.
  91. }