| 1 |
// Copyright 2013 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 |
/** |
| 16 |
* @fileoverview Unit tests for goog.labs.net.webChannel.WebChannelBase. |
| 17 |
* @suppress {accessControls} Private methods are accessed for test purposes. |
| 18 |
* |
| 19 |
*/ |
| 20 |
|
| 21 |
|
| 22 |
goog.provide('goog.labs.net.webChannel.webChannelBaseTransportTest'); |
| 23 |
|
| 24 |
goog.require('goog.events'); |
| 25 |
goog.require('goog.functions'); |
| 26 |
goog.require('goog.json'); |
| 27 |
goog.require('goog.labs.net.webChannel.ChannelRequest'); |
| 28 |
goog.require('goog.labs.net.webChannel.WebChannelBase'); |
| 29 |
goog.require('goog.labs.net.webChannel.WebChannelBaseTransport'); |
| 30 |
goog.require('goog.net.WebChannel'); |
| 31 |
goog.require('goog.testing.PropertyReplacer'); |
| 32 |
goog.require('goog.testing.jsunit'); |
| 33 |
|
| 34 |
|
| 35 |
goog.setTestOnly('goog.labs.net.webChannel.webChannelBaseTransportTest'); |
| 36 |
|
| 37 |
|
| 38 |
var webChannel; |
| 39 |
var channelUrl = 'http://127.0.0.1:8080/channel'; |
| 40 |
|
| 41 |
var stubs = new goog.testing.PropertyReplacer(); |
| 42 |
|
| 43 |
|
| 44 |
function shouldRunTests() { |
| 45 |
return goog.labs.net.webChannel.ChannelRequest.supportsXhrStreaming(); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
function setUp() {} |
| 50 |
|
| 51 |
|
| 52 |
function tearDown() { |
| 53 |
goog.dispose(webChannel); |
| 54 |
stubs.reset(); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
/** |
| 59 |
* Stubs goog.labs.net.webChannel.ChannelRequest. |
| 60 |
*/ |
| 61 |
function stubChannelRequest() { |
| 62 |
stubs.set( |
| 63 |
goog.labs.net.webChannel.ChannelRequest, 'supportsXhrStreaming', |
| 64 |
goog.functions.FALSE); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
function testUnsupportedTransports() { |
| 69 |
stubChannelRequest(); |
| 70 |
|
| 71 |
var err = assertThrows(function() { |
| 72 |
var webChannelTransport = |
| 73 |
new goog.labs.net.webChannel.WebChannelBaseTransport(); |
| 74 |
}); |
| 75 |
assertContains('error', err.message); |
| 76 |
} |
| 77 |
|
| 78 |
function testOpenWithUrl() { |
| 79 |
var webChannelTransport = |
| 80 |
new goog.labs.net.webChannel.WebChannelBaseTransport(); |
| 81 |
webChannel = webChannelTransport.createWebChannel(channelUrl); |
| 82 |
|
| 83 |
var eventFired = false; |
| 84 |
goog.events.listen( |
| 85 |
webChannel, goog.net.WebChannel.EventType.OPEN, |
| 86 |
function(e) { eventFired = true; }); |
| 87 |
|
| 88 |
webChannel.open(); |
| 89 |
assertFalse(eventFired); |
| 90 |
|
| 91 |
var channel = webChannel.channel_; |
| 92 |
assertNotNull(channel); |
| 93 |
|
| 94 |
simulateOpenEvent(channel); |
| 95 |
assertTrue(eventFired); |
| 96 |
} |
| 97 |
|
| 98 |
function testOpenWithTestUrl() { |
| 99 |
var webChannelTransport = |
| 100 |
new goog.labs.net.webChannel.WebChannelBaseTransport(); |
| 101 |
var options = {'testUrl': channelUrl + '/footest'}; |
| 102 |
webChannel = webChannelTransport.createWebChannel(channelUrl, options); |
| 103 |
webChannel.open(); |
| 104 |
|
| 105 |
var testPath = webChannel.channel_.connectionTest_.path_; |
| 106 |
assertNotNullNorUndefined(testPath); |
| 107 |
} |
| 108 |
|
| 109 |
function testOpenWithCustomHeaders() { |
| 110 |
var webChannelTransport = |
| 111 |
new goog.labs.net.webChannel.WebChannelBaseTransport(); |
| 112 |
var options = {'messageHeaders': {'foo-key': 'foo-value'}}; |
| 113 |
webChannel = webChannelTransport.createWebChannel(channelUrl, options); |
| 114 |
webChannel.open(); |
| 115 |
|
| 116 |
var extraHeaders_ = webChannel.channel_.extraHeaders_; |
| 117 |
assertNotNullNorUndefined(extraHeaders_); |
| 118 |
assertEquals('foo-value', extraHeaders_['foo-key']); |
| 119 |
assertEquals(undefined, extraHeaders_['X-Client-Protocol']); |
| 120 |
} |
| 121 |
|
| 122 |
function testOpenWithInitHeaders() { |
| 123 |
var webChannelTransport = |
| 124 |
new goog.labs.net.webChannel.WebChannelBaseTransport(); |
| 125 |
var options = {'initMessageHeaders': {'foo-key': 'foo-value'}}; |
| 126 |
webChannel = webChannelTransport.createWebChannel(channelUrl, options); |
| 127 |
webChannel.open(); |
| 128 |
|
| 129 |
var initHeaders_ = webChannel.channel_.initHeaders_; |
| 130 |
assertNotNullNorUndefined(initHeaders_); |
| 131 |
assertEquals('foo-value', initHeaders_['foo-key']); |
| 132 |
} |
| 133 |
|
| 134 |
function testOpenWithMessageContentType() { |
| 135 |
var webChannelTransport = |
| 136 |
new goog.labs.net.webChannel.WebChannelBaseTransport(); |
| 137 |
var options = {'messageContentType': 'application/protobuf+json'}; |
| 138 |
webChannel = webChannelTransport.createWebChannel(channelUrl, options); |
| 139 |
webChannel.open(); |
| 140 |
|
| 141 |
var initHeaders_ = webChannel.channel_.initHeaders_; |
| 142 |
assertNotNullNorUndefined(initHeaders_); |
| 143 |
assertEquals( |
| 144 |
'application/protobuf+json', initHeaders_['X-WebChannel-Content-Type']); |
| 145 |
} |
| 146 |
|
| 147 |
function testOpenWithMessageContentTypeAndInitHeaders() { |
| 148 |
var webChannelTransport = |
| 149 |
new goog.labs.net.webChannel.WebChannelBaseTransport(); |
| 150 |
var options = { |
| 151 |
'messageContentType': 'application/protobuf+json', |
| 152 |
'initMessageHeaders': {'foo-key': 'foo-value'} |
| 153 |
}; |
| 154 |
webChannel = webChannelTransport.createWebChannel(channelUrl, options); |
| 155 |
webChannel.open(); |
| 156 |
|
| 157 |
var initHeaders_ = webChannel.channel_.initHeaders_; |
| 158 |
assertNotNullNorUndefined(initHeaders_); |
| 159 |
assertEquals( |
| 160 |
'application/protobuf+json', initHeaders_['X-WebChannel-Content-Type']); |
| 161 |
assertEquals('foo-value', initHeaders_['foo-key']); |
| 162 |
} |
| 163 |
|
| 164 |
function testClientProtocolHeaderRequired() { |
| 165 |
var webChannelTransport = |
| 166 |
new goog.labs.net.webChannel.WebChannelBaseTransport(); |
| 167 |
var options = {'clientProtocolHeaderRequired': true}; |
| 168 |
webChannel = webChannelTransport.createWebChannel(channelUrl, options); |
| 169 |
webChannel.open(); |
| 170 |
|
| 171 |
var extraHeaders_ = webChannel.channel_.extraHeaders_; |
| 172 |
assertNotNullNorUndefined(extraHeaders_); |
| 173 |
assertEquals('webchannel', extraHeaders_['X-Client-Protocol']); |
| 174 |
} |
| 175 |
|
| 176 |
function testClientProtocolHeaderNotRequiredByDefault() { |
| 177 |
var webChannelTransport = |
| 178 |
new goog.labs.net.webChannel.WebChannelBaseTransport(); |
| 179 |
webChannel = webChannelTransport.createWebChannel(channelUrl); |
| 180 |
webChannel.open(); |
| 181 |
|
| 182 |
var extraHeaders_ = webChannel.channel_.extraHeaders_; |
| 183 |
assertNull(extraHeaders_); |
| 184 |
} |
| 185 |
|
| 186 |
function testClientProtocolHeaderRequiredWithCustomHeader() { |
| 187 |
var webChannelTransport = |
| 188 |
new goog.labs.net.webChannel.WebChannelBaseTransport(); |
| 189 |
var options = { |
| 190 |
'clientProtocolHeaderRequired': true, |
| 191 |
'messageHeaders': {'foo-key': 'foo-value'} |
| 192 |
}; |
| 193 |
webChannel = webChannelTransport.createWebChannel(channelUrl, options); |
| 194 |
webChannel.open(); |
| 195 |
|
| 196 |
var extraHeaders_ = webChannel.channel_.extraHeaders_; |
| 197 |
assertNotNullNorUndefined(extraHeaders_); |
| 198 |
assertEquals('foo-value', extraHeaders_['foo-key']); |
| 199 |
assertEquals('webchannel', extraHeaders_['X-Client-Protocol']); |
| 200 |
} |
| 201 |
|
| 202 |
function testOpenWithCustomParams() { |
| 203 |
var webChannelTransport = |
| 204 |
new goog.labs.net.webChannel.WebChannelBaseTransport(); |
| 205 |
var options = {'messageUrlParams': {'foo-key': 'foo-value'}}; |
| 206 |
webChannel = webChannelTransport.createWebChannel(channelUrl, options); |
| 207 |
webChannel.open(); |
| 208 |
|
| 209 |
var extraParams = webChannel.channel_.extraParams_; |
| 210 |
assertNotNullNorUndefined(extraParams); |
| 211 |
} |
| 212 |
|
| 213 |
function testOpenWithHttpSessionIdParam() { |
| 214 |
var webChannelTransport = |
| 215 |
new goog.labs.net.webChannel.WebChannelBaseTransport(); |
| 216 |
var options = {'httpSessionIdParam': 'xsessionid'}; |
| 217 |
webChannel = webChannelTransport.createWebChannel(channelUrl, options); |
| 218 |
webChannel.open(); |
| 219 |
|
| 220 |
var httpSessionIdParam = webChannel.channel_.getHttpSessionIdParam(); |
| 221 |
assertEquals('xsessionid', httpSessionIdParam); |
| 222 |
} |
| 223 |
|
| 224 |
function testOpenWithDuplicatedHttpSessionIdParam() { |
| 225 |
var webChannelTransport = |
| 226 |
new goog.labs.net.webChannel.WebChannelBaseTransport(); |
| 227 |
var options = { |
| 228 |
'messageUrlParams': {'xsessionid': 'abcd1234'}, |
| 229 |
'httpSessionIdParam': 'xsessionid' |
| 230 |
}; |
| 231 |
webChannel = webChannelTransport.createWebChannel(channelUrl, options); |
| 232 |
webChannel.open(); |
| 233 |
|
| 234 |
var httpSessionIdParam = webChannel.channel_.getHttpSessionIdParam(); |
| 235 |
assertEquals('xsessionid', httpSessionIdParam); |
| 236 |
|
| 237 |
var extraParams = webChannel.channel_.extraParams_; |
| 238 |
assertUndefined(extraParams['xsessionid']); |
| 239 |
} |
| 240 |
|
| 241 |
function testOpenWithCorsEnabled() { |
| 242 |
var webChannelTransport = |
| 243 |
new goog.labs.net.webChannel.WebChannelBaseTransport(); |
| 244 |
var options = {'supportsCrossDomainXhr': true}; |
| 245 |
webChannel = webChannelTransport.createWebChannel(channelUrl, options); |
| 246 |
webChannel.open(); |
| 247 |
|
| 248 |
assertTrue(webChannel.channel_.supportsCrossDomainXhrs_); |
| 249 |
} |
| 250 |
|
| 251 |
function testSendRawJson() { |
| 252 |
var channelMsg; |
| 253 |
stubs.set( |
| 254 |
goog.labs.net.webChannel.WebChannelBase.prototype, 'sendMap', |
| 255 |
function(message) { channelMsg = message; }); |
| 256 |
|
| 257 |
var webChannelTransport = |
| 258 |
new goog.labs.net.webChannel.WebChannelBaseTransport(); |
| 259 |
var options = {'sendRawJson': true}; |
| 260 |
webChannel = webChannelTransport.createWebChannel(channelUrl, options); |
| 261 |
webChannel.open(); |
| 262 |
|
| 263 |
webChannel.send({foo: 'bar'}); |
| 264 |
|
| 265 |
var receivedMsg = goog.json.parse(channelMsg['__data__']); |
| 266 |
assertEquals('bar', receivedMsg.foo); |
| 267 |
} |
| 268 |
|
| 269 |
function testOpenThenCloseChannel() { |
| 270 |
var webChannelTransport = |
| 271 |
new goog.labs.net.webChannel.WebChannelBaseTransport(); |
| 272 |
webChannel = webChannelTransport.createWebChannel(channelUrl); |
| 273 |
|
| 274 |
var eventFired = false; |
| 275 |
goog.events.listen( |
| 276 |
webChannel, goog.net.WebChannel.EventType.CLOSE, |
| 277 |
function(e) { eventFired = true; }); |
| 278 |
|
| 279 |
webChannel.open(); |
| 280 |
assertFalse(eventFired); |
| 281 |
|
| 282 |
var channel = webChannel.channel_; |
| 283 |
assertNotNull(channel); |
| 284 |
|
| 285 |
simulateCloseEvent(channel); |
| 286 |
assertTrue(eventFired); |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
function testChannelError() { |
| 291 |
var webChannelTransport = |
| 292 |
new goog.labs.net.webChannel.WebChannelBaseTransport(); |
| 293 |
webChannel = webChannelTransport.createWebChannel(channelUrl); |
| 294 |
|
| 295 |
var eventFired = false; |
| 296 |
goog.events.listen( |
| 297 |
webChannel, goog.net.WebChannel.EventType.ERROR, function(e) { |
| 298 |
eventFired = true; |
| 299 |
assertEquals(goog.net.WebChannel.ErrorStatus.NETWORK_ERROR, e.status); |
| 300 |
}); |
| 301 |
|
| 302 |
webChannel.open(); |
| 303 |
assertFalse(eventFired); |
| 304 |
|
| 305 |
var channel = webChannel.channel_; |
| 306 |
assertNotNull(channel); |
| 307 |
|
| 308 |
simulateErrorEvent(channel); |
| 309 |
assertTrue(eventFired); |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
function testChannelMessage() { |
| 314 |
var webChannelTransport = |
| 315 |
new goog.labs.net.webChannel.WebChannelBaseTransport(); |
| 316 |
webChannel = webChannelTransport.createWebChannel(channelUrl); |
| 317 |
|
| 318 |
var eventFired = false; |
| 319 |
var data = 'foo'; |
| 320 |
goog.events.listen( |
| 321 |
webChannel, goog.net.WebChannel.EventType.MESSAGE, function(e) { |
| 322 |
eventFired = true; |
| 323 |
assertEquals(e.data, data); |
| 324 |
}); |
| 325 |
|
| 326 |
webChannel.open(); |
| 327 |
assertFalse(eventFired); |
| 328 |
|
| 329 |
var channel = webChannel.channel_; |
| 330 |
assertNotNull(channel); |
| 331 |
|
| 332 |
simulateMessageEvent(channel, data); |
| 333 |
assertTrue(eventFired); |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
/** |
| 338 |
* Simulates the WebChannelBase firing the open event for the given channel. |
| 339 |
* @param {!goog.labs.net.webChannel.WebChannelBase} channel The WebChannelBase. |
| 340 |
*/ |
| 341 |
function simulateOpenEvent(channel) { |
| 342 |
assertNotNull(channel.getHandler()); |
| 343 |
channel.getHandler().channelOpened(); |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
/** |
| 348 |
* Simulates the WebChannelBase firing the close event for the given channel. |
| 349 |
* @param {!goog.labs.net.webChannel.WebChannelBase} channel The WebChannelBase. |
| 350 |
*/ |
| 351 |
function simulateCloseEvent(channel) { |
| 352 |
assertNotNull(channel.getHandler()); |
| 353 |
channel.getHandler().channelClosed(); |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
/** |
| 358 |
* Simulates the WebChannelBase firing the error event for the given channel. |
| 359 |
* @param {!goog.labs.net.webChannel.WebChannelBase} channel The WebChannelBase. |
| 360 |
*/ |
| 361 |
function simulateErrorEvent(channel) { |
| 362 |
assertNotNull(channel.getHandler()); |
| 363 |
channel.getHandler().channelError(); |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
/** |
| 368 |
* Simulates the WebChannelBase firing the message event for the given channel. |
| 369 |
* @param {!goog.labs.net.webChannel.WebChannelBase} channel The WebChannelBase. |
| 370 |
* @param {String} data The message data. |
| 371 |
*/ |
| 372 |
function simulateMessageEvent(channel, data) { |
| 373 |
assertNotNull(channel.getHandler()); |
| 374 |
channel.getHandler().channelHandleArray(channel, data); |
| 375 |
} |