Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(request): date() and time() methods return value (#1576)
  • Loading branch information
hekike authored and William Blankenship committed Dec 11, 2017
1 parent ea563b7 commit 4c2cb1a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
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();
});
});

0 comments on commit 4c2cb1a

Please sign in to comment.