Debian Codegraph @

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

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