fetchxmlhttpfactory_test.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // Copyright 2015 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.FetchXmlHttpFactoryTest');
  15. goog.setTestOnly('goog.net.FetchXmlHttpFactoryTest');
  16. goog.require('goog.net.FetchXmlHttp');
  17. goog.require('goog.net.FetchXmlHttpFactory');
  18. goog.require('goog.testing.MockControl');
  19. goog.require('goog.testing.jsunit');
  20. goog.require('goog.testing.recordFunction');
  21. goog.require('goog.userAgent.product');
  22. goog.require('goog.userAgent.product.isVersion');
  23. /** @type {!goog.testing.MockControl} */
  24. var mockControl;
  25. /** @type {!goog.testing.FunctionMock} */
  26. var fetchMock;
  27. /** @type {!goog.net.FetchXmlHttpFactory} */
  28. var factory;
  29. /** @type {!WorkerGlobalScope} */
  30. var worker;
  31. /**
  32. * Whether the browser supports running this test.
  33. * @return {boolean}
  34. */
  35. function shouldRunTests() {
  36. return goog.userAgent.product.CHROME && goog.userAgent.product.isVersion(43);
  37. }
  38. function setUp() {
  39. mockControl = new goog.testing.MockControl();
  40. worker = {};
  41. fetchMock = mockControl.createFunctionMock('fetch');
  42. worker.fetch = fetchMock;
  43. factory = new goog.net.FetchXmlHttpFactory(worker);
  44. }
  45. function tearDown() {
  46. mockControl.$tearDown();
  47. }
  48. /**
  49. * Verifies the open method.
  50. */
  51. function testOpen() {
  52. mockControl.$replayAll();
  53. var xhr = factory.createInstance();
  54. assertEquals(0, xhr.status);
  55. assertEquals('', xhr.responseText);
  56. assertEquals(xhr.readyState, goog.net.FetchXmlHttp.RequestState.UNSENT);
  57. var onReadyStateChangeHandler = new goog.testing.recordFunction();
  58. xhr.onreadystatechange = onReadyStateChangeHandler;
  59. xhr.open('GET', 'https://www.google.com', true /* opt_async */);
  60. assertEquals(xhr.readyState, goog.net.FetchXmlHttp.RequestState.OPENED);
  61. onReadyStateChangeHandler.assertCallCount(1);
  62. mockControl.$verifyAll();
  63. }
  64. /**
  65. * Verifies the open method when the ready state is not unsent.
  66. */
  67. function testOpen_notUnsent() {
  68. mockControl.$replayAll();
  69. var xhr = factory.createInstance();
  70. xhr.open('GET', 'https://www.google.com', true /* opt_async */);
  71. assertThrows(function() {
  72. xhr.open('GET', 'https://www.google.com', true /* opt_async */);
  73. });
  74. mockControl.$verifyAll();
  75. }
  76. /**
  77. * Verifies that synchronous fetches are not supported.
  78. */
  79. function testOpen_notAsync() {
  80. mockControl.$replayAll();
  81. var xhr = factory.createInstance();
  82. assertThrows(function() {
  83. xhr.open('GET', 'https://www.google.com', false /* opt_async */);
  84. });
  85. mockControl.$verifyAll();
  86. }
  87. /**
  88. * Verifies the send method.
  89. */
  90. function testSend() {
  91. fetchMock(new Request('https://www.google.com', {
  92. headers: new Headers(),
  93. method: 'GET'
  94. })).$returns(Promise.resolve(createSuccessResponse()));
  95. mockControl.$replayAll();
  96. verifySendSuccess('GET');
  97. }
  98. /**
  99. * Verifies the send method with POST mode.
  100. */
  101. function testSendPost() {
  102. fetchMock(new Request('https://www.google.com', {
  103. headers: new Headers(),
  104. method: 'POST'
  105. })).$returns(Promise.resolve(createSuccessResponse()));
  106. mockControl.$replayAll();
  107. verifySendSuccess('POST');
  108. }
  109. /**
  110. * Verifies the send method including credentials.
  111. */
  112. function testSend_includeCredentials() {
  113. factory = new goog.net.FetchXmlHttpFactory(worker);
  114. factory.setCredentialsMode(/** @type {RequestCredentials} */ ('include'));
  115. fetchMock(new Request('https://www.google.com', {
  116. headers: new Headers(),
  117. method: 'POST',
  118. credentials: 'include'
  119. })).$returns(Promise.resolve(createSuccessResponse()));
  120. mockControl.$replayAll();
  121. verifySendSuccess('POST');
  122. }
  123. /**
  124. * Verifies the send method setting cache mode.
  125. */
  126. function testSend_setCacheMode() {
  127. factory = new goog.net.FetchXmlHttpFactory(worker);
  128. factory.setCacheMode(/** @type {RequestCache} */ ('no-cache'));
  129. fetchMock(new Request('https://www.google.com', {
  130. headers: new Headers(),
  131. method: 'POST',
  132. cache: 'no-cache'
  133. })).$returns(Promise.resolve(createSuccessResponse()));
  134. mockControl.$replayAll();
  135. verifySendSuccess('POST');
  136. }
  137. /**
  138. * Util function to verify send method is successful.
  139. */
  140. function verifySendSuccess(sendMethod) {
  141. var xhr = factory.createInstance();
  142. xhr.open(sendMethod, 'https://www.google.com', true /* opt_async */);
  143. xhr.onreadystatechange = function() {
  144. assertEquals(
  145. xhr.readyState, goog.net.FetchXmlHttp.RequestState.HEADER_RECEIVED);
  146. assertEquals(0, xhr.status);
  147. assertEquals('', xhr.responseText);
  148. assertEquals(xhr.getResponseHeader('dummyHeader'), 'dummyHeaderValue');
  149. xhr.onreadystatechange = function() {
  150. assertEquals(xhr.readyState, goog.net.FetchXmlHttp.RequestState.LOADING);
  151. assertEquals(0, xhr.status);
  152. assertEquals('', xhr.responseText);
  153. xhr.onreadystatechange = function() {
  154. assertEquals(200, xhr.status);
  155. assertEquals('responseBody', xhr.responseText);
  156. assertEquals(xhr.readyState, goog.net.FetchXmlHttp.RequestState.DONE);
  157. };
  158. };
  159. };
  160. xhr.send();
  161. assertEquals(xhr.readyState, goog.net.FetchXmlHttp.RequestState.OPENED);
  162. mockControl.$verifyAll();
  163. }
  164. /**
  165. * Verifies the send method in case of error response.
  166. */
  167. function testSend_error() {
  168. fetchMock(new Request('https://www.google.com', {
  169. headers: new Headers(),
  170. method: 'GET'
  171. })).$returns(Promise.resolve(createFailedResponse()));
  172. mockControl.$replayAll();
  173. var xhr = factory.createInstance();
  174. xhr.open('GET', 'https://www.google.com', true /* opt_async */);
  175. xhr.onreadystatechange = function() {
  176. assertEquals(
  177. xhr.readyState, goog.net.FetchXmlHttp.RequestState.HEADER_RECEIVED);
  178. assertEquals(0, xhr.status);
  179. assertEquals('', xhr.responseText);
  180. assertEquals(xhr.getResponseHeader('dummyHeader'), 'dummyHeaderValue');
  181. xhr.onreadystatechange = function() {
  182. assertEquals(xhr.readyState, goog.net.FetchXmlHttp.RequestState.LOADING);
  183. assertEquals(0, xhr.status);
  184. assertEquals('', xhr.responseText);
  185. xhr.onreadystatechange = function() {
  186. assertEquals(500, xhr.status);
  187. assertEquals('responseBody', xhr.responseText);
  188. assertEquals(xhr.readyState, goog.net.FetchXmlHttp.RequestState.DONE);
  189. };
  190. };
  191. };
  192. xhr.send();
  193. assertEquals(xhr.readyState, goog.net.FetchXmlHttp.RequestState.OPENED);
  194. mockControl.$verifyAll();
  195. }
  196. /**
  197. * Verifies the send method in case of failure to fetch the url.
  198. */
  199. function testSend_failToFetch() {
  200. var failedPromise =
  201. new Promise(function() { throw Error('failed to fetch'); });
  202. fetchMock(new Request('https://www.google.com', {
  203. headers: new Headers(),
  204. method: 'GET'
  205. })).$returns(failedPromise);
  206. mockControl.$replayAll();
  207. var xhr = factory.createInstance();
  208. xhr.open('GET', 'https://www.google.com', true /* opt_async */);
  209. xhr.onreadystatechange = function() {
  210. assertEquals(xhr.readyState, goog.net.FetchXmlHttp.RequestState.DONE);
  211. assertEquals(0, xhr.status);
  212. assertEquals('', xhr.responseText);
  213. };
  214. xhr.send();
  215. assertEquals(xhr.readyState, goog.net.FetchXmlHttp.RequestState.OPENED);
  216. mockControl.$verifyAll();
  217. }
  218. /**
  219. * Creates a successful response.
  220. * @return {!Response}
  221. */
  222. function createSuccessResponse() {
  223. var headers = new Headers();
  224. headers.set('dummyHeader', 'dummyHeaderValue');
  225. return new Response(
  226. 'responseBody' /* opt_body */, {status: 200, headers: headers});
  227. }
  228. /**
  229. * Creates a successful response.
  230. * @return {!Response}
  231. */
  232. function createFailedResponse() {
  233. var headers = new Headers();
  234. headers.set('dummyHeader', 'dummyHeaderValue');
  235. return new Response(
  236. 'responseBody' /* opt_body */, {status: 500, headers: headers});
  237. }