Skip to content

Commit

Permalink
Add default startRequestTimer middleware to stack automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
sgress454 committed Nov 15, 2016
1 parent 701ed99 commit f788e39
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
1 change: 0 additions & 1 deletion lib/hooks/http/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ module.exports = function(sails) {
http: {
middleware: {
order: [
'startRequestTimer',
'cookieParser',
'session',
'bodyParser',
Expand Down
3 changes: 2 additions & 1 deletion lib/hooks/http/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []; }
Expand Down
69 changes: 69 additions & 0 deletions test/integration/middleware.startRequestTimer.test.js
Original file line number Diff line number Diff line change
@@ -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);
});

});

});

});

0 comments on commit f788e39

Please sign in to comment.