| 1 |
// Copyright 2007 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.JsonpTest'); |
| 16 |
goog.setTestOnly('goog.net.JsonpTest'); |
| 17 |
|
| 18 |
goog.require('goog.html.TrustedResourceUrl'); |
| 19 |
goog.require('goog.net.Jsonp'); |
| 20 |
goog.require('goog.string.Const'); |
| 21 |
goog.require('goog.testing.PropertyReplacer'); |
| 22 |
goog.require('goog.testing.jsunit'); |
| 23 |
goog.require('goog.testing.recordFunction'); |
| 24 |
goog.require('goog.userAgent'); |
| 25 |
|
| 26 |
// Global vars to facilitate a shared set up function. |
| 27 |
|
| 28 |
var timeoutWasCalled; |
| 29 |
var timeoutHandler; |
| 30 |
|
| 31 |
var fakeUrl = 'https://fake-site.eek/'; |
| 32 |
var fakeTrustedUrl = |
| 33 |
goog.html.TrustedResourceUrl.fromConstant(goog.string.Const.from(fakeUrl)); |
| 34 |
|
| 35 |
var originalTimeout; |
| 36 |
function setUp() { |
| 37 |
timeoutWasCalled = false; |
| 38 |
timeoutHandler = null; |
| 39 |
originalTimeout = window.setTimeout; |
| 40 |
window.setTimeout = function(handler, time) { |
| 41 |
timeoutWasCalled = true; |
| 42 |
timeoutHandler = handler; |
| 43 |
}; |
| 44 |
} |
| 45 |
|
| 46 |
// Firefox throws a JS error when a script is not found. We catch that here and |
| 47 |
// ensure the test case doesn't fail because of it. |
| 48 |
var originalOnError = window.onerror; |
| 49 |
window.onerror = function(msg, url, line) { |
| 50 |
// TODO(user): Safari 3 on the farm returns an object instead of the typical |
| 51 |
// params. Pass through errors for safari for now. |
| 52 |
if (goog.userAgent.WEBKIT || |
| 53 |
msg == 'Error loading script' && url.indexOf('fake-site') != -1) { |
| 54 |
return true; |
| 55 |
} else { |
| 56 |
return originalOnError && originalOnError(msg, url, line); |
| 57 |
} |
| 58 |
}; |
| 59 |
|
| 60 |
function tearDown() { |
| 61 |
window.setTimeout = originalTimeout; |
| 62 |
} |
| 63 |
|
| 64 |
// Quick function records the before-state of the DOM, and then return a |
| 65 |
// a function to check that XDC isn't leaving stuff behind. |
| 66 |
function newCleanupGuard() { |
| 67 |
var bodyChildCount = document.body.childNodes.length; |
| 68 |
|
| 69 |
return function() { |
| 70 |
// let any timeout queues finish before we check these: |
| 71 |
window.setTimeout(function() { |
| 72 |
var propCounter = 0; |
| 73 |
|
| 74 |
// All callbacks should have been deleted or be the null function. |
| 75 |
for (var key in goog.global) { |
| 76 |
// NOTES: callbacks are stored on goog.global with property |
| 77 |
// name prefixed with goog.net.Jsonp.CALLBACKS. |
| 78 |
if (key.indexOf(goog.net.Jsonp.CALLBACKS) == 0) { |
| 79 |
var callbackId = goog.net.Jsonp.getCallbackId_(key); |
| 80 |
if (goog.global[callbackId] && |
| 81 |
goog.global[callbackId] != goog.nullFunction) { |
| 82 |
propCounter++; |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
assertEquals( |
| 88 |
'script cleanup', bodyChildCount, document.body.childNodes.length); |
| 89 |
assertEquals('window jsonp array empty', 0, propCounter); |
| 90 |
}, 0); |
| 91 |
}; |
| 92 |
} |
| 93 |
|
| 94 |
function getScriptElement(result) { |
| 95 |
return result.deferred_.defaultScope_.script_; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
// Check that send function is sane when things go well. |
| 100 |
function testSend() { |
| 101 |
var replyReceived; |
| 102 |
var jsonp = new goog.net.Jsonp(fakeTrustedUrl); |
| 103 |
|
| 104 |
var checkCleanup = newCleanupGuard(); |
| 105 |
|
| 106 |
var userCallback = function(data) { replyReceived = data; }; |
| 107 |
|
| 108 |
var payload = {atisket: 'atasket', basket: 'yellow'}; |
| 109 |
var result = jsonp.send(payload, userCallback); |
| 110 |
|
| 111 |
var script = getScriptElement(result); |
| 112 |
|
| 113 |
assertNotNull('script created', script); |
| 114 |
assertEquals('encoding is utf-8', 'UTF-8', script.charset); |
| 115 |
|
| 116 |
// Check that the URL matches our payload. |
| 117 |
assertTrue('payload in url', script.src.indexOf('basket=yellow') > -1); |
| 118 |
assertTrue('server url', script.src.indexOf(fakeUrl) == 0); |
| 119 |
|
| 120 |
// Now, we have to track down the name of the callback function, so we can |
| 121 |
// call that to simulate a returned request + verify that the callback |
| 122 |
// function does not break if it receives a second unexpected parameter. |
| 123 |
var callbackName = /callback=([^&]+)/.exec(script.src)[1]; |
| 124 |
var callbackFunc = eval(callbackName); |
| 125 |
callbackFunc( |
| 126 |
{some: 'data', another: ['data', 'right', 'here']}, 'unexpected'); |
| 127 |
assertEquals('input was received', 'right', replyReceived.another[1]); |
| 128 |
|
| 129 |
// Because the callbackFunc calls cleanUp_ and that calls setTimeout which |
| 130 |
// we have overwritten, we have to call the timeoutHandler to actually do |
| 131 |
// the cleaning. |
| 132 |
timeoutHandler(); |
| 133 |
|
| 134 |
checkCleanup(); |
| 135 |
timeoutHandler(); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
// Check that send function is sane when things go well. |
| 140 |
function testSendWhenCallbackHasTwoParameters() { |
| 141 |
var replyReceived, replyReceived2; |
| 142 |
var jsonp = new goog.net.Jsonp(fakeTrustedUrl); |
| 143 |
|
| 144 |
var checkCleanup = newCleanupGuard(); |
| 145 |
|
| 146 |
var userCallback = function(data, opt_data2) { |
| 147 |
replyReceived = data; |
| 148 |
replyReceived2 = opt_data2; |
| 149 |
}; |
| 150 |
|
| 151 |
var payload = {atisket: 'atasket', basket: 'yellow'}; |
| 152 |
var result = jsonp.send(payload, userCallback); |
| 153 |
var script = getScriptElement(result); |
| 154 |
|
| 155 |
// Test a callback function that receives two parameters. |
| 156 |
var callbackName = /callback=([^&]+)/.exec(script.src)[1]; |
| 157 |
var callbackFunc = eval(callbackName); |
| 158 |
callbackFunc('param1', {some: 'data', another: ['data', 'right', 'here']}); |
| 159 |
assertEquals('input was received', 'param1', replyReceived); |
| 160 |
assertEquals('second input was received', 'right', replyReceived2.another[1]); |
| 161 |
|
| 162 |
// Because the callbackFunc calls cleanUp_ and that calls setTimeout which |
| 163 |
// we have overwritten, we have to call the timeoutHandler to actually do |
| 164 |
// the cleaning. |
| 165 |
timeoutHandler(); |
| 166 |
|
| 167 |
checkCleanup(); |
| 168 |
timeoutHandler(); |
| 169 |
} |
| 170 |
|
| 171 |
// Check that send function works correctly when callback param value is |
| 172 |
// specified. |
| 173 |
function testSendWithCallbackParamValue() { |
| 174 |
var replyReceived; |
| 175 |
var jsonp = new goog.net.Jsonp(fakeTrustedUrl); |
| 176 |
|
| 177 |
var checkCleanup = newCleanupGuard(); |
| 178 |
|
| 179 |
var userCallback = function(data) { replyReceived = data; }; |
| 180 |
|
| 181 |
var payload = {atisket: 'atasket', basket: 'yellow'}; |
| 182 |
var result = jsonp.send(payload, userCallback, undefined, 'dummyId'); |
| 183 |
|
| 184 |
var script = getScriptElement(result); |
| 185 |
|
| 186 |
assertNotNull('script created', script); |
| 187 |
assertEquals('encoding is utf-8', 'UTF-8', script.charset); |
| 188 |
|
| 189 |
// Check that the URL matches our payload. |
| 190 |
assertTrue('payload in url', script.src.indexOf('basket=yellow') > -1); |
| 191 |
assertTrue( |
| 192 |
'dummyId in url', |
| 193 |
script.src.indexOf( |
| 194 |
'callback=' + goog.net.Jsonp.getCallbackId_('dummyId')) > -1); |
| 195 |
assertTrue('server url', script.src.indexOf(fakeUrl) == 0); |
| 196 |
|
| 197 |
// Now, we simulate a returned request using the known callback function |
| 198 |
// name. |
| 199 |
var callbackFunc = |
| 200 |
eval('callback=' + goog.net.Jsonp.getCallbackId_('dummyId')); |
| 201 |
callbackFunc({some: 'data', another: ['data', 'right', 'here']}); |
| 202 |
assertEquals('input was received', 'right', replyReceived.another[1]); |
| 203 |
|
| 204 |
// Because the callbackFunc calls cleanUp_ and that calls setTimeout which |
| 205 |
// we have overwritten, we have to call the timeoutHandler to actually do |
| 206 |
// the cleaning. |
| 207 |
timeoutHandler(); |
| 208 |
|
| 209 |
checkCleanup(); |
| 210 |
timeoutHandler(); |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
// Check that the send function is sane when the thing goes south. |
| 215 |
function testSendFailure() { |
| 216 |
var replyReceived = false; |
| 217 |
var errorReplyReceived = false; |
| 218 |
|
| 219 |
var jsonp = new goog.net.Jsonp(fakeTrustedUrl); |
| 220 |
|
| 221 |
var checkCleanup = newCleanupGuard(); |
| 222 |
|
| 223 |
var userCallback = function(data) { replyReceived = data; }; |
| 224 |
var userErrorCallback = function(data) { errorReplyReceived = data; }; |
| 225 |
|
| 226 |
var payload = {justa: 'test'}; |
| 227 |
|
| 228 |
jsonp.send(payload, userCallback, userErrorCallback); |
| 229 |
|
| 230 |
assertTrue('timeout called', timeoutWasCalled); |
| 231 |
|
| 232 |
// Now, simulate the time running out, so we go into error mode. |
| 233 |
// After jsonp.send(), the timeoutHandler now is the Jsonp.cleanUp_ function. |
| 234 |
timeoutHandler(); |
| 235 |
// But that function also calls a setTimeout(), so it changes the timeout |
| 236 |
// handler once again, so to actually clean up we have to call the |
| 237 |
// timeoutHandler() once again. Fun! |
| 238 |
timeoutHandler(); |
| 239 |
|
| 240 |
assertFalse('standard callback not called', replyReceived); |
| 241 |
|
| 242 |
// The user's error handler should be called back with the same payload |
| 243 |
// passed back to it. |
| 244 |
assertEquals('error handler called', 'test', errorReplyReceived.justa); |
| 245 |
|
| 246 |
// Check that the relevant cleanup has occurred. |
| 247 |
checkCleanup(); |
| 248 |
// Check cleanup just calls setTimeout so we have to call the handler to |
| 249 |
// actually check that the cleanup worked. |
| 250 |
timeoutHandler(); |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
// Check that a cancel call works and cleans up after itself. |
| 255 |
function testCancel() { |
| 256 |
var checkCleanup = newCleanupGuard(); |
| 257 |
|
| 258 |
var successCalled = false; |
| 259 |
var successCallback = function() { successCalled = true; }; |
| 260 |
|
| 261 |
// Send and cancel a request, then make sure it was cleaned up. |
| 262 |
var jsonp = new goog.net.Jsonp(fakeTrustedUrl); |
| 263 |
var requestObject = jsonp.send({test: 'foo'}, successCallback); |
| 264 |
jsonp.cancel(requestObject); |
| 265 |
|
| 266 |
for (var key in goog.global[goog.net.Jsonp.CALLBACKS]) { |
| 267 |
// NOTES: callbacks are stored on goog.global with property |
| 268 |
// name prefixed with goog.net.Jsonp.CALLBACKS. |
| 269 |
if (key.indexOf('goog.net.Jsonp.CALLBACKS') == 0) { |
| 270 |
var callbackId = goog.net.Jsonp.getCallbackId_(key); |
| 271 |
assertNotEquals( |
| 272 |
'The success callback should have been removed', |
| 273 |
goog.global[callbackId], successCallback); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
// Make sure cancelling removes the script tag |
| 278 |
checkCleanup(); |
| 279 |
timeoutHandler(); |
| 280 |
} |
| 281 |
|
| 282 |
function testPayloadParameters() { |
| 283 |
var checkCleanup = newCleanupGuard(); |
| 284 |
|
| 285 |
var jsonp = new goog.net.Jsonp(fakeTrustedUrl); |
| 286 |
var result = jsonp.send({'foo': 3, 'bar': 'baz'}); |
| 287 |
|
| 288 |
var script = getScriptElement(result); |
| 289 |
assertEquals( |
| 290 |
'Payload parameters should have been added to url.', |
| 291 |
fakeUrl + '?foo=3&bar=baz', script.src); |
| 292 |
|
| 293 |
checkCleanup(); |
| 294 |
timeoutHandler(); |
| 295 |
} |
| 296 |
|
| 297 |
function testNonce() { |
| 298 |
var checkCleanup = newCleanupGuard(); |
| 299 |
|
| 300 |
var jsonp = new goog.net.Jsonp(fakeTrustedUrl); |
| 301 |
jsonp.setNonce('foo'); |
| 302 |
var result = jsonp.send(); |
| 303 |
|
| 304 |
var script = getScriptElement(result); |
| 305 |
assertEquals( |
| 306 |
'Nonce attribute should have been added to script element.', 'foo', |
| 307 |
(script['nonce'] || script.getAttribute('nonce'))); |
| 308 |
|
| 309 |
checkCleanup(); |
| 310 |
timeoutHandler(); |
| 311 |
} |
| 312 |
|
| 313 |
function testOptionalPayload() { |
| 314 |
var checkCleanup = newCleanupGuard(); |
| 315 |
|
| 316 |
var errorCallback = goog.testing.recordFunction(); |
| 317 |
|
| 318 |
var stubs = new goog.testing.PropertyReplacer(); |
| 319 |
stubs.set( |
| 320 |
goog.global, 'setTimeout', function(errorHandler) { errorHandler(); }); |
| 321 |
|
| 322 |
var jsonp = new goog.net.Jsonp(fakeTrustedUrl); |
| 323 |
var result = jsonp.send(null, null, errorCallback); |
| 324 |
|
| 325 |
var script = getScriptElement(result); |
| 326 |
assertEquals( |
| 327 |
'Parameters should not have been added to url.', fakeUrl, script.src); |
| 328 |
|
| 329 |
// Clear the script hooks because we triggered the error manually. |
| 330 |
script.onload = goog.nullFunction; |
| 331 |
script.onerror = goog.nullFunction; |
| 332 |
script.onreadystatechange = goog.nullFunction; |
| 333 |
|
| 334 |
var errorCallbackArguments = errorCallback.getLastCall().getArguments(); |
| 335 |
assertEquals(1, errorCallbackArguments.length); |
| 336 |
assertObjectEquals({}, errorCallbackArguments[0]); |
| 337 |
|
| 338 |
checkCleanup(); |
| 339 |
stubs.reset(); |
| 340 |
} |