Skip to content

Commit

Permalink
feat: added express test and example output in README
Browse files Browse the repository at this point in the history
  • Loading branch information
niftylettuce committed Jun 14, 2019
1 parent fc9f1fc commit ff9aa8a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ app.get('/', (req, res, next) => {
[MIT](LICENSE) © [Nick Baugh](http://niftylettuce.com/)


##
##

[npm]: https://www.npmjs.com/

Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,8 @@ const parseRequest = (config = {}) => {
result.request.timestamp = new Date(req[pinoHttpStartTime]).toISOString();
else if (req._startTime instanceof Date)
result.request.timestamp = req._startTime.toISOString();
else if (typeof req._startTime === 'number')
result.request.timestamp = new Date(req._startTime).toISOString();

// add `request.duration`
if (typeof headers['X-Response-Time'] === 'string')
Expand Down
14 changes: 13 additions & 1 deletion test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ test('parses start time and start date as date', t => {
t.true(typeof obj.request.duration === 'number');
});

test('works with morgan req._startTime', t => {
test('works with morgan req._startTime date', t => {
const req = {};
req._startTime = new Date();
req.headers = {
Expand All @@ -107,6 +107,18 @@ test('works with morgan req._startTime', t => {
t.true(typeof obj.request.duration === 'number');
});

test('works with morgan req._startTime number', t => {
const req = {};
req._startTime = Date.now();
req.headers = {
'X-Response-Time': '500 ms'
};
const obj = parseRequest({ req });
t.true(typeof obj.duration === 'number');
t.true(typeof obj.request.timestamp === 'string');
t.true(typeof obj.request.duration === 'number');
});

test('parses start time and start date as number', t => {
const req = {};
req[startTime] = Date.now();
Expand Down

0 comments on commit ff9aa8a

Please sign in to comment.