Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(request): date() and time() methods return value #1576

Merged
merged 1 commit into from Dec 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 5 additions & 8 deletions lib/request.js
Expand Up @@ -241,14 +241,9 @@ function patch(Request) {
* @memberof Request
* @instance
* @function date
* @returns {Date} date
* @returns {Date} date when request began being processed
*/
Request.prototype.date = function date() {
if (this._date !== undefined) {
return this._date;
}

this._date = new Date(this._time);
return this._date;
};

Expand Down Expand Up @@ -410,10 +405,12 @@ function patch(Request) {
* @memberof Request
* @instance
* @function time
* @returns {Number} time
* @returns {Number} time when request began being processed in epoch:
* ellapsed milliseconds since
* January 1, 1970, 00:00:00 UTC
*/
Request.prototype.time = function time() {
return this._time;
return this._date.getTime();
};

/**
Expand Down
1 change: 1 addition & 0 deletions lib/server.js
Expand Up @@ -1210,6 +1210,7 @@ Server.prototype._run = function _run(req, res, route, chain, cb) {
Server.prototype._setupRequest = function _setupRequest(req, res) {
var self = this;
req.log = res.log = self.log;
req._date = new Date();
req._time = process.hrtime();
req.serverName = self.name;

Expand Down
32 changes: 32 additions & 0 deletions test/request.test.js
Expand Up @@ -191,3 +191,35 @@ test('should provide route object', function(t) {
t.end();
});
});

test('should provide time when request started', function(t) {
SERVER.get('/ping/:name', function(req, res, next) {
t.equal(typeof req.time(), 'number');
t.ok(req.time() > Date.now() - 1000);
t.ok(req.time() <= Date.now());
res.send('ok');
return next();
});

CLIENT.get('/ping/lagavulin', function(err, _, res) {
t.ifError(err);
t.equal(res.statusCode, 200);
t.end();
});
});

test('should provide date when request started', function(t) {
SERVER.get('/ping/:name', function(req, res, next) {
t.ok(req.date() instanceof Date);
t.ok(req.date().getTime() > Date.now() - 1000);
t.ok(req.date().getTime() <= Date.now());
res.send('ok');
return next();
});

CLIENT.get('/ping/lagavulin', function(err, _, res) {
t.ifError(err);
t.equal(res.statusCode, 200);
t.end();
});
});