From f788e3956cf2daf521dbe36fdb8d64a2a8dbb5c8 Mon Sep 17 00:00:00 2001 From: Scott Gress Date: Tue, 15 Nov 2016 16:42:54 -0600 Subject: [PATCH] Add default startRequestTimer middleware to stack automatically --- lib/hooks/http/index.js | 1 - lib/hooks/http/initialize.js | 3 +- .../middleware.startRequestTimer.test.js | 69 +++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 test/integration/middleware.startRequestTimer.test.js diff --git a/lib/hooks/http/index.js b/lib/hooks/http/index.js index 536f57b15..71057872c 100644 --- a/lib/hooks/http/index.js +++ b/lib/hooks/http/index.js @@ -49,7 +49,6 @@ module.exports = function(sails) { http: { middleware: { order: [ - 'startRequestTimer', 'cookieParser', 'session', 'bodyParser', diff --git a/lib/hooks/http/initialize.js b/lib/hooks/http/initialize.js index f563bb3bd..7c07819a4 100644 --- a/lib/hooks/http/initialize.js +++ b/lib/hooks/http/initialize.js @@ -221,7 +221,8 @@ module.exports = function(sails) { var configuredHttpMiddlewareFns = getConfiguredHttpMiddlewareFns(expressApp, sails); // Split the middleware order into "pre-router" and "post-router" middleware. - var preRouterMiddleware = []; + // The internal "startRequestTimer" always comes first. + var preRouterMiddleware = ['startRequestTimer']; var postRouterMiddleware = null; _.each(sails.config.http.middleware.order, function(middlewareKey) { if (middlewareKey === 'router') { postRouterMiddleware = []; } diff --git a/test/integration/middleware.startRequestTimer.test.js b/test/integration/middleware.startRequestTimer.test.js new file mode 100644 index 000000000..4afd5ba1e --- /dev/null +++ b/test/integration/middleware.startRequestTimer.test.js @@ -0,0 +1,69 @@ +var _ = require('@sailshq/lodash'); +var request = require('request'); +var Sails = require('../../lib').Sails; +var assert = require('assert'); +var fs = require('fs-extra'); +var request = require('request'); +var appHelper = require('./helpers/appHelper'); +var path = require('path'); + +describe('middleware :: ', function() { + + describe('startRequestTimer :: ', function() { + + var appName = 'testApp'; + var sailsApp; + + before(function(done) { + appHelper.build(done); + }); + + after(function() { + process.chdir('../'); + appHelper.teardown(); + }); + + describe('default startRequestTimer middleware', function() { + + before(function(done) { + appHelper.lift({ + hooks: { + pubsub: false + }, + routes: { + '/time': function(req, res) { + assert(req._startTime); + assert(req._startTime instanceof Date); + res.send(); + } + } + }, function(err, _sailsApp) { + if (err) { return done(err); } + sailsApp = _sailsApp; + return done(); + }); + }); + + it('should add a _startTime to the request object', function(done) { + + request( + { + method: 'GET', + uri: 'http://localhost:1342/time', + }, + function(err, response, body) { + return done(err); + } + ); + + }); + + after(function(done) { + sailsApp.lower(done); + }); + + }); + + }); + +});