Debian Codegraph @

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

raw
1 // Copyright 2008 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.ChannelRequestTest');
16 goog.setTestOnly('goog.net.ChannelRequestTest');
17
18 goog.require('goog.Uri');
19 goog.require('goog.functions');
20 goog.require('goog.net.BrowserChannel');
21 goog.require('goog.net.ChannelDebug');
22 goog.require('goog.net.ChannelRequest');
23 goog.require('goog.testing.MockClock');
24 goog.require('goog.testing.PropertyReplacer');
25 goog.require('goog.testing.jsunit');
26 goog.require('goog.testing.net.XhrIo');
27 goog.require('goog.testing.recordFunction');
28
29 var channelRequest;
30 var mockBrowserChannel;
31 var mockClock;
32 var stubs;
33 var xhrIo;
34
35
36 /**
37 * Time to wait for a network request to time out, before aborting.
38 */
39 var WATCHDOG_TIME = 2000;
40
41
42 /**
43 * Time to throttle readystatechange events.
44 */
45 var THROTTLE_TIME = 500;
46
47
48 /**
49 * A really long time - used to make sure no more timeouts will fire.
50 */
51 var ALL_DAY_MS = 1000 * 60 * 60 * 24;
52
53
54 function setUp() {
55 mockClock = new goog.testing.MockClock();
56 mockClock.install();
57 stubs = new goog.testing.PropertyReplacer();
58 }
59
60
61 function tearDown() {
62 stubs.reset();
63 mockClock.uninstall();
64 }
65
66
67
68 /**
69 * Constructs a duck-type BrowserChannel that tracks the completed requests.
70 * @constructor
71 */
72 function MockBrowserChannel() {
73 this.reachabilityEvents = {};
74 this.isClosed = function() { return false; };
75 this.isActive = function() { return true; };
76 this.shouldUseSecondaryDomains = function() { return false; };
77 this.completedRequests = [];
78 this.notifyServerReachabilityEvent = function(reachabilityType) {
79 if (!this.reachabilityEvents[reachabilityType]) {
80 this.reachabilityEvents[reachabilityType] = 0;
81 }
82 this.reachabilityEvents[reachabilityType]++;
83 };
84 this.onRequestComplete = function(request) {
85 this.completedRequests.push(request);
86 };
87 this.onRequestData = function(request, data) {};
88 }
89
90
91 /**
92 * Creates a real ChannelRequest object, with some modifications for
93 * testability:
94 * <ul>
95 * <li>The BrowserChannel is a MockBrowserChannel.
96 * <li>The new watchdogTimeoutCallCount property tracks onWatchDogTimeout_()
97 * calls.
98 * <li>The timeout is set to WATCHDOG_TIME.
99 * </ul>
100 */
101 function createChannelRequest() {
102 xhrIo = new goog.testing.net.XhrIo();
103 xhrIo.abort = xhrIo.abort || function() { this.active_ = false; };
104
105 // Install mock browser channel and no-op debug logger.
106 mockBrowserChannel = new MockBrowserChannel();
107 channelRequest = new goog.net.ChannelRequest(
108 mockBrowserChannel, new goog.net.ChannelDebug());
109
110 // Install test XhrIo.
111 mockBrowserChannel.createXhrIo = function() { return xhrIo; };
112
113 // Install watchdogTimeoutCallCount.
114 channelRequest.watchdogTimeoutCallCount = 0;
115 channelRequest.originalOnWatchDogTimeout = channelRequest.onWatchDogTimeout_;
116 channelRequest.onWatchDogTimeout_ = function() {
117 this.watchdogTimeoutCallCount++;
118 return this.originalOnWatchDogTimeout();
119 };
120
121 channelRequest.setTimeout(WATCHDOG_TIME);
122 }
123
124
125 /**
126 * Run through the lifecycle of a long lived request, checking that the right
127 * network events are reported.
128 */
129 function testNetworkEvents() {
130 createChannelRequest();
131
132 channelRequest.xmlHttpPost(new goog.Uri('some_uri'), 'some_postdata', true);
133 checkReachabilityEvents(1, 0, 0, 0);
134 if (goog.net.ChannelRequest.supportsXhrStreaming()) {
135 xhrIo.simulatePartialResponse('17\nI am a BC Message');
136 checkReachabilityEvents(1, 0, 0, 1);
137 xhrIo.simulatePartialResponse('23\nI am another BC Message');
138 checkReachabilityEvents(1, 0, 0, 2);
139 xhrIo.simulateResponse(200, '16\Final BC Message');
140 checkReachabilityEvents(1, 1, 0, 2);
141 } else {
142 xhrIo.simulateResponse(200, '16\Final BC Message');
143 checkReachabilityEvents(1, 1, 0, 0);
144 }
145 }
146
147
148 /**
149 * Test throttling of readystatechange events.
150 */
151 function testNetworkEvents_throttleReadyStateChange() {
152 createChannelRequest();
153 channelRequest.setReadyStateChangeThrottle(THROTTLE_TIME);
154
155 var recordedHandler =
156 goog.testing.recordFunction(channelRequest.xmlHttpHandler_);
157 stubs.set(channelRequest, 'xmlHttpHandler_', recordedHandler);
158
159 channelRequest.xmlHttpPost(new goog.Uri('some_uri'), 'some_postdata', true);
160 assertEquals(1, recordedHandler.getCallCount());
161
162 checkReachabilityEvents(1, 0, 0, 0);
163 if (goog.net.ChannelRequest.supportsXhrStreaming()) {
164 xhrIo.simulatePartialResponse('17\nI am a BC Message');
165 checkReachabilityEvents(1, 0, 0, 1);
166 assertEquals(3, recordedHandler.getCallCount());
167
168 // Second event should be throttled
169 xhrIo.simulatePartialResponse('23\nI am another BC Message');
170 assertEquals(3, recordedHandler.getCallCount());
171
172 xhrIo.simulatePartialResponse('27\nI am yet another BC Message');
173 assertEquals(3, recordedHandler.getCallCount());
174 mockClock.tick(THROTTLE_TIME);
175
176 checkReachabilityEvents(1, 0, 0, 3);
177 // Only one more call because of throttling.
178 assertEquals(4, recordedHandler.getCallCount());
179
180 xhrIo.simulateResponse(200, '16\Final BC Message');
181 checkReachabilityEvents(1, 1, 0, 3);
182 assertEquals(5, recordedHandler.getCallCount());
183 } else {
184 xhrIo.simulateResponse(200, '16\Final BC Message');
185 checkReachabilityEvents(1, 1, 0, 0);
186 }
187 }
188
189
190 /**
191 * Make sure that the request "completes" with an error when the timeout
192 * expires.
193 */
194 function testRequestTimeout() {
195 createChannelRequest();
196
197 channelRequest.xmlHttpPost(new goog.Uri('some_uri'), 'some_postdata', true);
198 assertEquals(0, channelRequest.watchdogTimeoutCallCount);
199 assertEquals(0, channelRequest.channel_.completedRequests.length);
200
201 // Watchdog timeout.
202 mockClock.tick(WATCHDOG_TIME);
203 assertEquals(1, channelRequest.watchdogTimeoutCallCount);
204 assertEquals(1, channelRequest.channel_.completedRequests.length);
205 assertFalse(channelRequest.getSuccess());
206
207 // Make sure no more timers are firing.
208 mockClock.tick(ALL_DAY_MS);
209 assertEquals(1, channelRequest.watchdogTimeoutCallCount);
210 assertEquals(1, channelRequest.channel_.completedRequests.length);
211
212 checkReachabilityEvents(1, 0, 1, 0);
213 }
214
215
216 function testRequestTimeoutWithUnexpectedException() {
217 createChannelRequest();
218 channelRequest.channel_.createXhrIo = goog.functions.error('Weird error');
219
220 try {
221 channelRequest.xmlHttpGet(new goog.Uri('some_uri'), true, null);
222 fail('Expected error');
223 } catch (e) {
224 assertEquals('Weird error', e.message);
225 }
226
227
228 assertEquals(0, channelRequest.watchdogTimeoutCallCount);
229 assertEquals(0, channelRequest.channel_.completedRequests.length);
230
231 // Watchdog timeout.
232 mockClock.tick(WATCHDOG_TIME);
233 assertEquals(1, channelRequest.watchdogTimeoutCallCount);
234 assertEquals(1, channelRequest.channel_.completedRequests.length);
235 assertFalse(channelRequest.getSuccess());
236
237 // Make sure no more timers are firing.
238 mockClock.tick(ALL_DAY_MS);
239 assertEquals(1, channelRequest.watchdogTimeoutCallCount);
240 assertEquals(1, channelRequest.channel_.completedRequests.length);
241
242 checkReachabilityEvents(0, 0, 1, 0);
243 }
244
245 function testActiveXBlocked() {
246 createChannelRequest();
247 stubs.set(
248 goog.global, 'ActiveXObject', goog.functions.error('Active X blocked'));
249
250 channelRequest.tridentGet(new goog.Uri('some_uri'), false);
251 assertFalse(channelRequest.getSuccess());
252 assertEquals(
253 goog.net.ChannelRequest.Error.ACTIVE_X_BLOCKED,
254 channelRequest.getLastError());
255
256 checkReachabilityEvents(0, 0, 0, 0);
257 }
258
259
260 /**
261 * This is a private method but we rely on it to avoid XSS, so it's important
262 * to verify it works properly.
263 */
264 function testEscapeForStringInScript() {
265 var actual = goog.net.ChannelRequest.escapeForStringInScript_('"\'<>');
266 assertEquals('\\"\\\'\\x3c\\x3e', actual);
267 }
268
269 function checkReachabilityEvents(reqMade, reqSucceeded, reqFail, backChannel) {
270 var Reachability = goog.net.BrowserChannel.ServerReachability;
271 assertEquals(
272 reqMade,
273 mockBrowserChannel.reachabilityEvents[Reachability.REQUEST_MADE] || 0);
274 assertEquals(
275 reqSucceeded,
276 mockBrowserChannel.reachabilityEvents[Reachability.REQUEST_SUCCEEDED] ||
277 0);
278 assertEquals(
279 reqFail,
280 mockBrowserChannel.reachabilityEvents[Reachability.REQUEST_FAILED] || 0);
281 assertEquals(
282 backChannel,
283 mockBrowserChannel
284 .reachabilityEvents[Reachability.BACK_CHANNEL_ACTIVITY] ||
285 0);
286 }