Debian Codegraph @

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

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