PageRenderTime 45ms CodeModel.GetById 17ms 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
Possible License(s): MIT, MPL-2.0-no-copyleft-exception
  1. 'use strict';
  2. var path = require ('path')
  3. , tap = require('tap')
  4. , test = tap.test
  5. , helper = require(path.join(__dirname, '..', '..', '..', 'lib', 'agent_helper'))
  6. ;
  7. // connect is a loudmouth without this
  8. process.env.NODE_ENV = 'test';
  9. test("intercepting errors with connect 1", function (t) {
  10. t.plan(3);
  11. t.test("should wrap handlers with proxies", function (t) {
  12. var agent = helper.instrumentMockedAgent()
  13. , connect = require('connect')
  14. , app = connect()
  15. ;
  16. this.tearDown(function () {
  17. helper.unloadAgent(agent);
  18. });
  19. function nop () {}
  20. app.use(nop);
  21. t.ok(app.stack, "there's a stack of handlers defined");
  22. // 2 because of the error handler
  23. t.equal(app.stack.length, 2, "have test middleware + error interceptor");
  24. var wrapNop = app.stack[0];
  25. t.equal(wrapNop.route, '', "nop handler defaults to all routes");
  26. t.ok(wrapNop.handle, "have nop handle passed above");
  27. t.equal(wrapNop.handle.name, 'wrappedConnectHandle', "nop is wrapped");
  28. t.equal(wrapNop.handle.__NR_original, nop, "found nop in wrapper");
  29. // implementation detail: the sentinel
  30. var interceptor = app.stack[1];
  31. t.equal(interceptor.route, '', "interceptor catches all routes");
  32. t.ok(interceptor.handle, "interceptor has a handler");
  33. t.equal(interceptor.handle.name, 'sentinel', "error-wrapping sentinel found");
  34. t.end();
  35. });
  36. t.test("should have only one error interceptor in the middleware stack", function (t) {
  37. var agent = helper.instrumentMockedAgent()
  38. , connect = require('connect')
  39. , app = connect()
  40. ;
  41. this.tearDown(function () {
  42. helper.unloadAgent(agent);
  43. });
  44. app.use(connect.bodyParser());
  45. t.equal(app.stack.length, 2, "2 handlers after 1st add");
  46. t.equal(app.stack[app.stack.length - 1].handle.name, 'sentinel', "sentinel found");
  47. app.use(connect.cookieParser());
  48. t.equal(app.stack.length, 3, "3 handlers after 2nd add");
  49. t.equal(app.stack[app.stack.length - 1].handle.name, 'sentinel', "sentinel found");
  50. app.use(connect.csrf());
  51. t.equal(app.stack.length, 4, "4 handlers after 3rd add");
  52. t.equal(app.stack[app.stack.length - 1].handle.name, 'sentinel', "sentinel found");
  53. app.use(connect.logger());
  54. t.equal(app.stack.length, 5, "5 handlers after 4th add");
  55. t.equal(app.stack[app.stack.length - 1].handle.name, 'sentinel', "sentinel found");
  56. t.end();
  57. });
  58. t.test("should trace any errors that occur while executing a middleware stack",
  59. function (t) {
  60. var agent = helper.instrumentMockedAgent()
  61. , connect = require('connect')
  62. , app = connect()
  63. ;
  64. this.tearDown(function () {
  65. helper.unloadAgent(agent);
  66. });
  67. function wiggleware(req, res, next) {
  68. var harbl = null;
  69. harbl.bargl(); // OHHH NOOOOO
  70. return next(); // will never get here
  71. }
  72. var stubReq = {
  73. url : '/test',
  74. method : 'GET'
  75. };
  76. var stubRes = {
  77. headers : {},
  78. setHeader : function (name, value) {
  79. stubRes.headers[name] = value;
  80. },
  81. end : function () {
  82. stubRes._end = Array.prototype.slice(arguments);
  83. }
  84. };
  85. app.use(wiggleware);
  86. app.handle(stubReq, stubRes);
  87. var errors = agent.errors.errors; // FIXME: redundancy is dumb
  88. t.equal(errors.length, 1, "the error got traced");
  89. var error = errors[0];
  90. t.equal(error.length, 5, "format for traced error is correct");
  91. t.equal(error[3], 'TypeError', "got the correct class for the error");
  92. t.end();
  93. });
  94. });