|
| 1 | +var assert = require('assert'); |
| 2 | +var sinon = require('sinon'); |
| 3 | +var support = require('../support'); |
| 4 | +var UncaughtExceptionHandler = main.server.UncaughtExceptionHandler; |
| 5 | + |
| 6 | +describe("UncaughtExceptionHandler", function () { |
| 7 | + describe("instantiating", function () { |
| 8 | + it("sets self.app_name", function () { |
| 9 | + var app_name = support.random.string(); |
| 10 | + var handler = new UncaughtExceptionHandler({app_name: app_name}); |
| 11 | + assert.strictEqual(handler.app_name, app_name); |
| 12 | + }); |
| 13 | + |
| 14 | + it("sets self.app_name to '' by default", function () { |
| 15 | + var handler = new UncaughtExceptionHandler(); |
| 16 | + assert.strictEqual(handler.app_name, ''); |
| 17 | + }); |
| 18 | + }); |
| 19 | + |
| 20 | + describe("handle()", function () { |
| 21 | + var handler; |
| 22 | + var app_name; |
| 23 | + var error; |
| 24 | + var req = {}; |
| 25 | + var res = { |
| 26 | + header: function () {}, |
| 27 | + status: function () {}, |
| 28 | + json: function () {} |
| 29 | + }; |
| 30 | + |
| 31 | + var route = {spec: {method: 'POST', path: '/foo/bar'}}; |
| 32 | + |
| 33 | + beforeEach(function () { |
| 34 | + app_name = support.random.string(); |
| 35 | + handler = new UncaughtExceptionHandler({app_name: app_name}); |
| 36 | + error = support.fake_error(); |
| 37 | + }); |
| 38 | + |
| 39 | + context("whe no error passed in", function () { |
| 40 | + it("doesn't log to console", function () { |
| 41 | + sinon.stub(console, 'log'); |
| 42 | + |
| 43 | + handler.handle(req, res, route); |
| 44 | + assert.ok(!console.log.called); |
| 45 | + |
| 46 | + console.log.restore(); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + it("logs meta info to the console", function () { |
| 51 | + sinon.stub(console, 'log'); |
| 52 | + |
| 53 | + handler.handle(req, res, route, error); |
| 54 | + var expected = /\[app\].*uncaught exception/i; |
| 55 | + assert.ok(console.log.calledWithMatch(expected)); |
| 56 | + |
| 57 | + console.log.restore(); |
| 58 | + }); |
| 59 | + |
| 60 | + it("logs route info to the console", function () { |
| 61 | + sinon.stub(console, 'log'); |
| 62 | + |
| 63 | + handler.handle(req, res, route, error); |
| 64 | + var expected = 'Route: ' + route.spec.method + ' ' + route.spec.path; |
| 65 | + assert.ok(console.log.calledWith(expected)); |
| 66 | + |
| 67 | + console.log.restore(); |
| 68 | + }); |
| 69 | + |
| 70 | + it("doesn't log route info if no route passed in", function () { |
| 71 | + sinon.stub(console, 'log'); |
| 72 | + |
| 73 | + handler.handle(req, res, null, error); |
| 74 | + var route_regex = /Route/; |
| 75 | + assert.ok(!console.log.calledWithMatch(route_regex)); |
| 76 | + |
| 77 | + console.log.restore(); |
| 78 | + }); |
| 79 | + |
| 80 | + it("logs error to the console", function () { |
| 81 | + sinon.stub(console, 'log'); |
| 82 | + |
| 83 | + handler.handle(req, res, route, error); |
| 84 | + assert.ok(console.log.calledWith(error)); |
| 85 | + |
| 86 | + console.log.restore(); |
| 87 | + }); |
| 88 | + |
| 89 | + it("logs error's stack to the console", function () { |
| 90 | + sinon.stub(console, 'log'); |
| 91 | + |
| 92 | + handler.handle(req, res, route, error); |
| 93 | + assert.ok(console.log.calledWith(error.stack)); |
| 94 | + |
| 95 | + console.log.restore(); |
| 96 | + }); |
| 97 | + |
| 98 | + it("doesn't throw an error when no app_name is set", function () { |
| 99 | + sinon.stub(console, 'log'); |
| 100 | + handler = new UncaughtExceptionHandler(); |
| 101 | + |
| 102 | + handler.handle(req, res, route, error); |
| 103 | + assert.ok(console.log.calledWith(error)); |
| 104 | + |
| 105 | + console.log.restore(); |
| 106 | + }); |
| 107 | + |
| 108 | + it("calls res.json() with error", function () { |
| 109 | + sinon.stub(console, 'log'); |
| 110 | + sinon.stub(res, 'json'); |
| 111 | + |
| 112 | + handler.handle(req, res, route, error); |
| 113 | + assert.ok(res.json.calledWith(error)); |
| 114 | + |
| 115 | + res.json.restore(); |
| 116 | + console.log.restore(); |
| 117 | + }); |
| 118 | + }); |
| 119 | +}); |
0 commit comments