Skip to content

Commit

Permalink
Merge pull request #48 from jimmiehansson/feature/express-additional-…
Browse files Browse the repository at this point in the history
…values

Add logic and tests to complement PR #25
  • Loading branch information
jsumners authored Jan 19, 2021
2 parents 7ffa0e7 + 9575bab commit 99102a8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/req.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ const pinoReqProto = Object.create({}, {
writable: true,
value: ''
},
query: {
enumerable: true,
writable: true,
value: ''
},
params: {
enumerable: true,
writable: true,
value: ''
},
headers: {
enumerable: true,
writable: true,
Expand Down Expand Up @@ -61,6 +71,8 @@ function reqSerializer (req) {
// req.originalUrl is for expressjs compat.
if (req.originalUrl) {
_req.url = req.originalUrl
_req.query = req.query
_req.params = req.params
} else {
// req.url.path is for hapi compat.
_req.url = req.path || (req.url ? (req.url.path || req.url) : undefined)
Expand Down
40 changes: 40 additions & 0 deletions test/req.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,43 @@ test('req.remotePort will be obtained from request info.remotePort if available'
res.end()
}
})

test('req.query is available', function (t) {
t.plan(1)

const server = http.createServer(handler)
server.unref()
server.listen(0, () => {
http.get(server.address(), () => {})
})

t.tearDown(() => server.close())

function handler (req, res) {
req.originalUrl = '/test'
req.query = '/foo?bar=foobar&bar=foo'
const serialized = serializers.reqSerializer(req)
t.is(serialized.query, '/foo?bar=foobar&bar=foo')
res.end()
}
})

test('req.params is available', function (t) {
t.plan(1)

const server = http.createServer(handler)
server.unref()
server.listen(0, () => {
http.get(server.address(), () => {})
})

t.tearDown(() => server.close())

function handler (req, res) {
req.originalUrl = '/test'
req.params = '/foo/bar'
const serialized = serializers.reqSerializer(req)
t.is(serialized.params, '/foo/bar')
res.end()
}
})

0 comments on commit 99102a8

Please sign in to comment.