| 1 |
// Copyright 2009 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.debug.ErrorReporterTest'); |
| 16 |
goog.setTestOnly('goog.debug.ErrorReporterTest'); |
| 17 |
|
| 18 |
goog.require('goog.debug.Error'); |
| 19 |
goog.require('goog.debug.ErrorReporter'); |
| 20 |
goog.require('goog.debug.errorcontext'); |
| 21 |
goog.require('goog.events'); |
| 22 |
goog.require('goog.functions'); |
| 23 |
goog.require('goog.testing.PropertyReplacer'); |
| 24 |
goog.require('goog.testing.TestCase'); |
| 25 |
goog.require('goog.testing.jsunit'); |
| 26 |
goog.require('goog.userAgent'); |
| 27 |
goog.require('goog.userAgent.product'); |
| 28 |
|
| 29 |
MockXhrIo = function() {}; |
| 30 |
|
| 31 |
MockXhrIo.prototype.onReadyStateChangeEntryPoint_ = function() {}; |
| 32 |
|
| 33 |
MockXhrIo.protectEntryPoints = function() {}; |
| 34 |
|
| 35 |
MockXhrIo.lastUrl = null; |
| 36 |
|
| 37 |
MockXhrIo.send = function( |
| 38 |
url, opt_callback, opt_method, opt_content, opt_headers, opt_timeInterval) { |
| 39 |
MockXhrIo.lastUrl = url; |
| 40 |
MockXhrIo.lastContent = opt_content; |
| 41 |
MockXhrIo.lastHeaders = opt_headers; |
| 42 |
}; |
| 43 |
|
| 44 |
var errorReporter; |
| 45 |
var originalSetTimeout = window.setTimeout; |
| 46 |
var stubs = new goog.testing.PropertyReplacer(); |
| 47 |
var url = 'http://www.your.tst/more/bogus.js'; |
| 48 |
var encodedUrl = 'http%3A%2F%2Fwww.your.tst%2Fmore%2Fbogus.js'; |
| 49 |
|
| 50 |
function setUp() { |
| 51 |
// TODO(b/25875505): Fix unreported assertions (go/failonunreportedasserts). |
| 52 |
goog.testing.TestCase.getActiveTestCase().failOnUnreportedAsserts = false; |
| 53 |
|
| 54 |
stubs.set(goog.net, 'XhrIo', MockXhrIo); |
| 55 |
goog.debug.ErrorReporter.ALLOW_AUTO_PROTECT = true; |
| 56 |
} |
| 57 |
|
| 58 |
function tearDown() { |
| 59 |
goog.dispose(errorReporter); |
| 60 |
stubs.reset(); |
| 61 |
MockXhrIo.lastUrl = null; |
| 62 |
} |
| 63 |
|
| 64 |
function throwAnErrorWith(script, line, message, opt_stack) { |
| 65 |
var error = {message: message, fileName: script, lineNumber: line}; |
| 66 |
if (opt_stack) { |
| 67 |
error['stack'] = opt_stack; |
| 68 |
} |
| 69 |
|
| 70 |
throw error; |
| 71 |
} |
| 72 |
|
| 73 |
function testsendErrorReport() { |
| 74 |
errorReporter = new goog.debug.ErrorReporter('/log'); |
| 75 |
errorReporter.sendErrorReport('message', 'filename.js', 123, 'trace'); |
| 76 |
|
| 77 |
assertEquals( |
| 78 |
'/log?script=filename.js&error=message&line=123', MockXhrIo.lastUrl); |
| 79 |
assertEquals('trace=trace', MockXhrIo.lastContent); |
| 80 |
} |
| 81 |
|
| 82 |
function testsendErrorReportWithCustomSender() { |
| 83 |
var uri = null; |
| 84 |
var method = null; |
| 85 |
var content = null; |
| 86 |
var headers = null; |
| 87 |
function mockXhrSender(uriIn, methodIn, contentIn, headersIn) { |
| 88 |
uri = uriIn; |
| 89 |
method = methodIn; |
| 90 |
content = contentIn; |
| 91 |
headers = headersIn; |
| 92 |
} |
| 93 |
|
| 94 |
errorReporter = new goog.debug.ErrorReporter('/log'); |
| 95 |
errorReporter.setXhrSender(mockXhrSender); |
| 96 |
errorReporter.sendErrorReport('message', 'filename.js', 123, 'trace'); |
| 97 |
|
| 98 |
assertEquals('/log?script=filename.js&error=message&line=123', uri); |
| 99 |
assertEquals('POST', method); |
| 100 |
assertEquals('trace=trace', content); |
| 101 |
assertUndefined(headers); |
| 102 |
} |
| 103 |
|
| 104 |
function testsendErrorReport_noTrace() { |
| 105 |
errorReporter = new goog.debug.ErrorReporter('/log'); |
| 106 |
errorReporter.sendErrorReport('message', 'filename.js', 123); |
| 107 |
|
| 108 |
assertEquals( |
| 109 |
'/log?script=filename.js&error=message&line=123', MockXhrIo.lastUrl); |
| 110 |
assertEquals('', MockXhrIo.lastContent); |
| 111 |
} |
| 112 |
|
| 113 |
function test_nonInternetExplorerSendErrorReport() { |
| 114 |
if (goog.userAgent.product.SAFARI) { |
| 115 |
// TODO(b/20733468): Disabled so we can get the rest of the Closure test |
| 116 |
// suite running in a continuous build. Will investigate later. |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
stubs.set(goog.userAgent, 'IE', false); |
| 121 |
stubs.set(goog.global, 'setTimeout', function(fcn, time) { fcn.call(); }); |
| 122 |
|
| 123 |
errorReporter = goog.debug.ErrorReporter.install('/errorreporter'); |
| 124 |
|
| 125 |
var errorFunction = goog.partial(throwAnErrorWith, url, 5, 'Hello :)'); |
| 126 |
|
| 127 |
try { |
| 128 |
goog.global.setTimeout(errorFunction, 0); |
| 129 |
} catch (e) { |
| 130 |
// Expected. The error is rethrown after sending. |
| 131 |
} |
| 132 |
|
| 133 |
assertEquals( |
| 134 |
'/errorreporter?script=' + encodedUrl + '&error=Hello%20%3A)&line=5', |
| 135 |
MockXhrIo.lastUrl); |
| 136 |
assertEquals('trace=Not%20available', MockXhrIo.lastContent); |
| 137 |
} |
| 138 |
|
| 139 |
function test_internetExplorerSendErrorReport() { |
| 140 |
stubs.set(goog.userAgent, 'IE', true); |
| 141 |
stubs.set(goog.userAgent, 'isVersionOrHigher', goog.functions.FALSE); |
| 142 |
|
| 143 |
// Remove test runner's onerror handler so the test doesn't fail. |
| 144 |
stubs.set(goog.global, 'onerror', null); |
| 145 |
|
| 146 |
errorReporter = goog.debug.ErrorReporter.install('/errorreporter'); |
| 147 |
goog.global.onerror('Goodbye :(', url, 22); |
| 148 |
assertEquals( |
| 149 |
'/errorreporter?script=' + encodedUrl + '&error=Goodbye%20%3A(&line=22', |
| 150 |
MockXhrIo.lastUrl); |
| 151 |
assertEquals('trace=Not%20available', MockXhrIo.lastContent); |
| 152 |
} |
| 153 |
|
| 154 |
function test_setLoggingHeaders() { |
| 155 |
stubs.set(goog.userAgent, 'IE', true); |
| 156 |
stubs.set(goog.userAgent, 'isVersionOrHigher', goog.functions.FALSE); |
| 157 |
// Remove test runner's onerror handler so the test doesn't fail. |
| 158 |
stubs.set(goog.global, 'onerror', null); |
| 159 |
|
| 160 |
errorReporter = goog.debug.ErrorReporter.install('/errorreporter'); |
| 161 |
errorReporter.setLoggingHeaders('header!'); |
| 162 |
goog.global.onerror('Goodbye :(', 'http://www.your.tst/more/bogus.js', 22); |
| 163 |
assertEquals('header!', MockXhrIo.lastHeaders); |
| 164 |
} |
| 165 |
|
| 166 |
function test_nonInternetExplorerSendErrorReportWithTrace() { |
| 167 |
stubs.set(goog.userAgent, 'IE', false); |
| 168 |
stubs.set(goog.global, 'setTimeout', function(fcn, time) { fcn.call(); }); |
| 169 |
|
| 170 |
errorReporter = goog.debug.ErrorReporter.install('/errorreporter'); |
| 171 |
|
| 172 |
var trace = 'Error(\"Something Wrong\")@:0\n' + |
| 173 |
'$MF$E$Nx$([object Object])@http://a.b.c:83/a/f.js:901\n' + |
| 174 |
'([object Object])@http://a.b.c:813/a/f.js:37'; |
| 175 |
|
| 176 |
|
| 177 |
var errorFunction = goog.partial(throwAnErrorWith, url, 5, 'Hello :)', trace); |
| 178 |
|
| 179 |
try { |
| 180 |
goog.global.setTimeout(errorFunction, 0); |
| 181 |
} catch (e) { |
| 182 |
// Expected. The error is rethrown after sending. |
| 183 |
} |
| 184 |
|
| 185 |
assertEquals( |
| 186 |
'/errorreporter?script=' + encodedUrl + '&error=Hello%20%3A)&line=5', |
| 187 |
MockXhrIo.lastUrl); |
| 188 |
assertEquals( |
| 189 |
'trace=' + |
| 190 |
'Error(%22Something%20Wrong%22)%40%3A0%0A' + |
| 191 |
'%24MF%24E%24Nx%24(%5Bobject%20Object%5D)%40' + |
| 192 |
'http%3A%2F%2Fa.b.c%3A83%2Fa%2Ff.js%3A901%0A' + |
| 193 |
'(%5Bobject%20Object%5D)%40http%3A%2F%2Fa.b.c%3A813%2Fa%2Ff.js%3A37', |
| 194 |
MockXhrIo.lastContent); |
| 195 |
} |
| 196 |
|
| 197 |
function testProtectAdditionalEntryPoint_nonIE() { |
| 198 |
stubs.set(goog.userAgent, 'IE', false); |
| 199 |
|
| 200 |
errorReporter = goog.debug.ErrorReporter.install('/errorreporter'); |
| 201 |
var fn = function() {}; |
| 202 |
var protectedFn = errorReporter.protectAdditionalEntryPoint(fn); |
| 203 |
assertNotNull(protectedFn); |
| 204 |
assertNotEquals(fn, protectedFn); |
| 205 |
} |
| 206 |
|
| 207 |
function testProtectAdditionalEntryPoint_IE() { |
| 208 |
stubs.set(goog.userAgent, 'IE', true); |
| 209 |
stubs.set(goog.userAgent, 'isVersionOrHigher', goog.functions.FALSE); |
| 210 |
|
| 211 |
errorReporter = goog.debug.ErrorReporter.install('/errorreporter'); |
| 212 |
var fn = function() {}; |
| 213 |
var protectedFn = errorReporter.protectAdditionalEntryPoint(fn); |
| 214 |
assertNull(protectedFn); |
| 215 |
} |
| 216 |
|
| 217 |
function testHandleException_dispatchesEvent() { |
| 218 |
errorReporter = goog.debug.ErrorReporter.install('/errorreporter'); |
| 219 |
var loggedErrors = 0; |
| 220 |
goog.events.listen( |
| 221 |
errorReporter, goog.debug.ErrorReporter.ExceptionEvent.TYPE, |
| 222 |
function(event) { |
| 223 |
assertNotNullNorUndefined(event.error); |
| 224 |
loggedErrors++; |
| 225 |
}); |
| 226 |
errorReporter.handleException(new Error()); |
| 227 |
errorReporter.handleException(new Error()); |
| 228 |
assertEquals( |
| 229 |
'Expected 2 errors. ' + |
| 230 |
'(Ensure an exception was not swallowed.)', |
| 231 |
2, loggedErrors); |
| 232 |
} |
| 233 |
|
| 234 |
function testHandleException_includesContext() { |
| 235 |
errorReporter = goog.debug.ErrorReporter.install('/errorreporter'); |
| 236 |
var loggedErrors = 0; |
| 237 |
var testError = new Error('test error'); |
| 238 |
var testContext = {'contextParam': 'contextValue'}; |
| 239 |
goog.events.listen( |
| 240 |
errorReporter, goog.debug.ErrorReporter.ExceptionEvent.TYPE, |
| 241 |
function(event) { |
| 242 |
assertNotNullNorUndefined(event.error); |
| 243 |
assertObjectEquals({contextParam: 'contextValue'}, event.context); |
| 244 |
loggedErrors++; |
| 245 |
}); |
| 246 |
errorReporter.handleException(testError, testContext); |
| 247 |
assertEquals( |
| 248 |
'Expected 1 error. ' + |
| 249 |
'(Ensure an exception was not swallowed.)', |
| 250 |
1, loggedErrors); |
| 251 |
} |
| 252 |
|
| 253 |
function testContextProvider() { |
| 254 |
errorReporter = goog.debug.ErrorReporter.install( |
| 255 |
'/errorreporter', |
| 256 |
function(error, context) { context.providedContext = 'value'; }); |
| 257 |
var loggedErrors = 0; |
| 258 |
var testError = new Error('test error'); |
| 259 |
goog.events.listen( |
| 260 |
errorReporter, goog.debug.ErrorReporter.ExceptionEvent.TYPE, |
| 261 |
function(event) { |
| 262 |
assertNotNullNorUndefined(event.error); |
| 263 |
assertObjectEquals({providedContext: 'value'}, event.context); |
| 264 |
loggedErrors++; |
| 265 |
}); |
| 266 |
errorReporter.handleException(testError); |
| 267 |
assertEquals( |
| 268 |
'Expected 1 error. ' + |
| 269 |
'(Ensure an exception was not swallowed.)', |
| 270 |
1, loggedErrors); |
| 271 |
} |
| 272 |
|
| 273 |
function testContextProvider_withOtherContext() { |
| 274 |
errorReporter = goog.debug.ErrorReporter.install( |
| 275 |
'/errorreporter', |
| 276 |
function(error, context) { context.providedContext = 'value'; }); |
| 277 |
var loggedErrors = 0; |
| 278 |
var testError = new Error('test error'); |
| 279 |
goog.events.listen( |
| 280 |
errorReporter, goog.debug.ErrorReporter.ExceptionEvent.TYPE, |
| 281 |
function(event) { |
| 282 |
assertNotNullNorUndefined(event.error); |
| 283 |
assertObjectEquals( |
| 284 |
{providedContext: 'value', otherContext: 'value'}, event.context); |
| 285 |
loggedErrors++; |
| 286 |
}); |
| 287 |
errorReporter.handleException(testError, {'otherContext': 'value'}); |
| 288 |
assertEquals( |
| 289 |
'Expected 1 error. ' + |
| 290 |
'(Ensure an exception was not swallowed.)', |
| 291 |
1, loggedErrors); |
| 292 |
} |
| 293 |
|
| 294 |
function testErrorWithContext() { |
| 295 |
errorReporter = goog.debug.ErrorReporter.install('/errorreporter'); |
| 296 |
var loggedErrors = 0; |
| 297 |
var testError = new Error('test error'); |
| 298 |
goog.debug.errorcontext.addErrorContext(testError, 'key1', 'value1'); |
| 299 |
goog.debug.errorcontext.addErrorContext(testError, 'animalType', 'dog'); |
| 300 |
goog.events.listen( |
| 301 |
errorReporter, goog.debug.ErrorReporter.ExceptionEvent.TYPE, |
| 302 |
function(event) { |
| 303 |
assertNotNullNorUndefined(event.error); |
| 304 |
assertObjectEquals({key1: 'value1', animalType: 'dog'}, event.context); |
| 305 |
loggedErrors++; |
| 306 |
}); |
| 307 |
errorReporter.handleException(testError); |
| 308 |
assertEquals( |
| 309 |
'Expected 1 error. ' + |
| 310 |
'(Ensure an exception was not swallowed.)', |
| 311 |
1, loggedErrors); |
| 312 |
} |
| 313 |
|
| 314 |
function testErrorWithDifferentContextSources() { |
| 315 |
errorReporter = goog.debug.ErrorReporter.install( |
| 316 |
'/errorreporter', function(error, context) { |
| 317 |
context.providedContext = 'provided ctx'; |
| 318 |
}); |
| 319 |
var loggedErrors = 0; |
| 320 |
var testError = new Error('test error'); |
| 321 |
goog.debug.errorcontext.addErrorContext( |
| 322 |
testError, 'addErrorContext', 'some value'); |
| 323 |
goog.events.listen( |
| 324 |
errorReporter, goog.debug.ErrorReporter.ExceptionEvent.TYPE, |
| 325 |
function(event) { |
| 326 |
assertNotNullNorUndefined(event.error); |
| 327 |
assertObjectEquals( |
| 328 |
{ |
| 329 |
addErrorContext: 'some value', |
| 330 |
providedContext: 'provided ctx', |
| 331 |
handleExceptionContext: 'another value' |
| 332 |
}, |
| 333 |
event.context); |
| 334 |
loggedErrors++; |
| 335 |
}); |
| 336 |
errorReporter.handleException( |
| 337 |
testError, {handleExceptionContext: 'another value'}); |
| 338 |
assertEquals( |
| 339 |
'Expected 1 error. ' + |
| 340 |
'(Ensure an exception was not swallowed.)', |
| 341 |
1, loggedErrors); |
| 342 |
} |
| 343 |
|
| 344 |
function testHandleException_ignoresExceptionsDuringEventDispatch() { |
| 345 |
errorReporter = goog.debug.ErrorReporter.install('/errorreporter'); |
| 346 |
goog.events.listen( |
| 347 |
errorReporter, goog.debug.ErrorReporter.ExceptionEvent.TYPE, |
| 348 |
function(event) { fail('This exception should be swallowed.'); }); |
| 349 |
errorReporter.handleException(new Error()); |
| 350 |
} |
| 351 |
|
| 352 |
function testHandleException_doNotReportErrorToServer() { |
| 353 |
var error = new goog.debug.Error(); |
| 354 |
error.reportErrorToServer = false; |
| 355 |
errorReporter.handleException(error); |
| 356 |
assertNull(MockXhrIo.lastUrl); |
| 357 |
} |
| 358 |
|
| 359 |
function testDisposal() { |
| 360 |
errorReporter = goog.debug.ErrorReporter.install('/errorreporter'); |
| 361 |
if (!goog.userAgent.IE) { |
| 362 |
assertNotEquals(originalSetTimeout, window.setTimeout); |
| 363 |
} |
| 364 |
goog.dispose(errorReporter); |
| 365 |
assertEquals(originalSetTimeout, window.setTimeout); |
| 366 |
} |
| 367 |
|
| 368 |
function testSetContextPrefix() { |
| 369 |
errorReporter = new goog.debug.ErrorReporter('/log'); |
| 370 |
errorReporter.setContextPrefix('baz.'); |
| 371 |
errorReporter.sendErrorReport( |
| 372 |
'message', 'filename.js', 123, 'trace', {'foo': 'bar'}); |
| 373 |
assertEquals('trace=trace&baz.foo=bar', MockXhrIo.lastContent); |
| 374 |
} |
| 375 |
|
| 376 |
function testTruncationLimit() { |
| 377 |
errorReporter = new goog.debug.ErrorReporter('/log'); |
| 378 |
errorReporter.setTruncationLimit(6); |
| 379 |
errorReporter.sendErrorReport( |
| 380 |
'message', 'filename.js', 123, 'trace', {'foo': 'bar'}); |
| 381 |
assertEquals('trace=', MockXhrIo.lastContent); |
| 382 |
} |
| 383 |
|
| 384 |
function testZeroTruncationLimit() { |
| 385 |
errorReporter = new goog.debug.ErrorReporter('/log'); |
| 386 |
errorReporter.setTruncationLimit(0); |
| 387 |
errorReporter.sendErrorReport( |
| 388 |
'message', 'filename.js', 123, 'trace', {'foo': 'bar'}); |
| 389 |
assertEquals('', MockXhrIo.lastContent); |
| 390 |
} |
| 391 |
|
| 392 |
function testTruncationLimitLargerThanBody() { |
| 393 |
errorReporter = new goog.debug.ErrorReporter('/log'); |
| 394 |
errorReporter.setTruncationLimit(9999); |
| 395 |
errorReporter.sendErrorReport( |
| 396 |
'message', 'filename.js', 123, 'trace', {'foo': 'bar'}); |
| 397 |
assertEquals('trace=trace&context.foo=bar', MockXhrIo.lastContent); |
| 398 |
} |
| 399 |
|
| 400 |
function testSetNegativeTruncationLimit() { |
| 401 |
errorReporter = new goog.debug.ErrorReporter('/log'); |
| 402 |
assertThrows(function() { errorReporter.setTruncationLimit(-10); }); |
| 403 |
} |
| 404 |
|
| 405 |
function testSetTruncationLimitNull() { |
| 406 |
errorReporter = new goog.debug.ErrorReporter('/log'); |
| 407 |
errorReporter.setTruncationLimit(null); |
| 408 |
errorReporter.sendErrorReport( |
| 409 |
'message', 'filename.js', 123, 'trace', {'foo': 'bar'}); |
| 410 |
assertEquals('trace=trace&context.foo=bar', MockXhrIo.lastContent); |
| 411 |
} |
| 412 |
|
| 413 |
function testAttemptAutoProtectWithAllowAutoProtectOff() { |
| 414 |
goog.debug.ErrorReporter.ALLOW_AUTO_PROTECT = false; |
| 415 |
assertThrows(function() { |
| 416 |
errorReporter = |
| 417 |
new goog.debug.ErrorReporter('/log', function(e, context) {}, false); |
| 418 |
}); |
| 419 |
} |
| 420 |
|
| 421 |
function testSetAdditionalArgumentsArgsEmptyObject() { |
| 422 |
errorReporter = new goog.debug.ErrorReporter('/log'); |
| 423 |
errorReporter.setAdditionalArguments({}); |
| 424 |
errorReporter.sendErrorReport( |
| 425 |
'message', 'filename.js', 123, 'trace', {'foo': 'bar'}); |
| 426 |
assertEquals( |
| 427 |
'/log?script=filename.js&error=message&line=123', MockXhrIo.lastUrl); |
| 428 |
} |
| 429 |
|
| 430 |
function testSetAdditionalArgumentsSingleArgument() { |
| 431 |
errorReporter = new goog.debug.ErrorReporter('/log'); |
| 432 |
errorReporter.setAdditionalArguments({'extra': 'arg'}); |
| 433 |
errorReporter.sendErrorReport( |
| 434 |
'message', 'filename.js', 123, 'trace', {'foo': 'bar'}); |
| 435 |
assertEquals( |
| 436 |
'/log?script=filename.js&error=message&line=123&extra=arg', |
| 437 |
MockXhrIo.lastUrl); |
| 438 |
} |
| 439 |
|
| 440 |
function testSetAdditionalArgumentsMultipleArguments() { |
| 441 |
errorReporter = new goog.debug.ErrorReporter('/log'); |
| 442 |
errorReporter.setAdditionalArguments({'extra': 'arg', 'cat': 'dog'}); |
| 443 |
errorReporter.sendErrorReport( |
| 444 |
'message', 'filename.js', 123, 'trace', {'foo': 'bar'}); |
| 445 |
assertEquals( |
| 446 |
'/log?script=filename.js&error=message&line=123&extra=arg&cat=dog', |
| 447 |
MockXhrIo.lastUrl); |
| 448 |
} |