PageRenderTime 37ms CodeModel.GetById 29ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/node_modules/newrelic/test/integration/versioned/connect-1/connect-error-intercept.tap.js

https://github.com/hayes/framework
JavaScript | 122 lines | 91 code | 28 blank | 3 comment | 0 complexity | 16819b2ca65a07307b5fa07189103bbf MD5 | raw file
  1'use strict';
  2
  3var path   = require ('path')
  4  , tap    = require('tap')
  5  , test   = tap.test
  6  , helper = require(path.join(__dirname, '..', '..', '..', 'lib', 'agent_helper'))
  7  ;
  8
  9// connect is a loudmouth without this
 10process.env.NODE_ENV = 'test';
 11
 12test("intercepting errors with connect 1", function (t) {
 13  t.plan(3);
 14
 15  t.test("should wrap handlers with proxies", function (t) {
 16    var agent   = helper.instrumentMockedAgent()
 17      , connect = require('connect')
 18      , app     = connect()
 19      ;
 20
 21    this.tearDown(function () {
 22      helper.unloadAgent(agent);
 23    });
 24
 25    function nop () {}
 26
 27    app.use(nop);
 28
 29    t.ok(app.stack, "there's a stack of handlers defined");
 30    // 2 because of the error handler
 31    t.equal(app.stack.length, 2, "have test middleware + error interceptor");
 32
 33    var wrapNop = app.stack[0];
 34    t.equal(wrapNop.route, '', "nop handler defaults to all routes");
 35    t.ok(wrapNop.handle, "have nop handle passed above");
 36    t.equal(wrapNop.handle.name, 'wrappedConnectHandle', "nop is wrapped");
 37    t.equal(wrapNop.handle.__NR_original, nop, "found nop in wrapper");
 38
 39    // implementation detail: the sentinel
 40    var interceptor = app.stack[1];
 41    t.equal(interceptor.route, '', "interceptor catches all routes");
 42    t.ok(interceptor.handle, "interceptor has a handler");
 43    t.equal(interceptor.handle.name, 'sentinel', "error-wrapping sentinel found");
 44
 45    t.end();
 46  });
 47
 48  t.test("should have only one error interceptor in the middleware stack", function (t) {
 49    var agent   = helper.instrumentMockedAgent()
 50      , connect = require('connect')
 51      , app     = connect()
 52      ;
 53
 54    this.tearDown(function () {
 55      helper.unloadAgent(agent);
 56    });
 57
 58    app.use(connect.bodyParser());
 59    t.equal(app.stack.length, 2, "2 handlers after 1st add");
 60    t.equal(app.stack[app.stack.length - 1].handle.name, 'sentinel', "sentinel found");
 61
 62    app.use(connect.cookieParser());
 63    t.equal(app.stack.length, 3, "3 handlers after 2nd add");
 64    t.equal(app.stack[app.stack.length - 1].handle.name, 'sentinel', "sentinel found");
 65
 66    app.use(connect.csrf());
 67    t.equal(app.stack.length, 4, "4 handlers after 3rd add");
 68    t.equal(app.stack[app.stack.length - 1].handle.name, 'sentinel', "sentinel found");
 69
 70    app.use(connect.logger());
 71    t.equal(app.stack.length, 5, "5 handlers after 4th add");
 72    t.equal(app.stack[app.stack.length - 1].handle.name, 'sentinel', "sentinel found");
 73
 74    t.end();
 75  });
 76
 77  t.test("should trace any errors that occur while executing a middleware stack",
 78         function (t) {
 79    var agent   = helper.instrumentMockedAgent()
 80      , connect = require('connect')
 81      , app     = connect()
 82      ;
 83
 84    this.tearDown(function () {
 85      helper.unloadAgent(agent);
 86    });
 87
 88    function wiggleware(req, res, next) {
 89      var harbl = null;
 90      harbl.bargl(); // OHHH NOOOOO
 91
 92      return next(); // will never get here
 93    }
 94
 95    var stubReq = {
 96      url : '/test',
 97      method : 'GET'
 98    };
 99
100    var stubRes = {
101      headers : {},
102      setHeader : function (name, value) {
103        stubRes.headers[name] = value;
104      },
105      end : function () {
106        stubRes._end = Array.prototype.slice(arguments);
107      }
108    };
109
110    app.use(wiggleware);
111    app.handle(stubReq, stubRes);
112
113    var errors = agent.errors.errors; // FIXME: redundancy is dumb
114    t.equal(errors.length, 1, "the error got traced");
115
116    var error = errors[0];
117    t.equal(error.length, 5, "format for traced error is correct");
118    t.equal(error[3], 'TypeError', "got the correct class for the error");
119
120    t.end();
121  });
122});