Skip to content

Commit

Permalink
Merge pull request #5 from pinojs/hapi-support
Browse files Browse the repository at this point in the history
hapi 17 support
  • Loading branch information
David Mark Clements authored Mar 28, 2018
2 parents 4b0ce4f + 63876ff commit 6f07ed1
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
8 changes: 5 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ The default `request` serializer. Returns and object:

```js
{
id: 'string', // Default is an empty string. Attach a synchronous function
// to the input `request` that returns an identifier to have
id: 'string', // Default is an empty string, unless there is an `id` property
// already attached to the `request` object or to the `request.info`
// object. Attach a synchronous function
// to the `request.id` that returns an identifier to have
// the value filled.
method: 'string',
url: 'string',
url: 'string', // the request pathname (as per req.url in core HTTP)
headers: Object,
remoteAddress: 'string',
remotePort: Number,
Expand Down
7 changes: 5 additions & 2 deletions lib/req.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,15 @@ Object.defineProperty(pinoReqProto, rawSymbol, {
function reqSerializer (req) {
var connection = req.connection
const _req = Object.create(pinoReqProto)
_req.id = typeof req.id === 'function' ? req.id() : req.id
// req.info is for hapi compat.
_req.id = (typeof req.id === 'function' ? req.id() : (req.id || (req.info && req.info.id))) || ''
_req.method = req.method
_req.url = req.url
// req.url.path is for hapi compat.
_req.url = req.url ? (req.url.path || req.url) : undefined
_req.headers = req.headers
_req.remoteAddress = connection && connection.remoteAddress
_req.remotePort = connection && connection.remotePort
// req.raw is for hapi compat/equivalence
_req.raw = req.raw || req
return _req
}
Expand Down
58 changes: 58 additions & 0 deletions test/req.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,26 @@ test('req.raw is available', function (t) {
}
})

test('req.raw will be obtained in from input request raw property if input request raw property is truthy', function (t) {
t.plan(2)

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

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

function handler (req, res) {
req.raw = { req: {foo: 'foo'}, res: {} }
var serialized = serializers.reqSerializer(req)
t.ok(serialized.raw)
t.is(serialized.raw.req.foo, 'foo')
res.end()
}
})

test('req.id has a non-function value', function (t) {
t.plan(1)

Expand All @@ -81,6 +101,25 @@ test('req.id has a non-function value', function (t) {
}
})

test('req.id will be obtained from input request info.id when input request id does not exist', function (t) {
t.plan(1)

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

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

function handler (req, res) {
req.info = {id: 'test'}
var serialized = serializers.reqSerializer(req)
t.is(serialized.id, 'test')
res.end()
}
})

test('req.id has a non-function value with custom id function', function (t) {
t.plan(2)

Expand All @@ -101,6 +140,25 @@ test('req.id has a non-function value with custom id function', function (t) {
}
})

test('req.url will be obtained from input request url.path when input request url is an object', function (t) {
t.plan(1)

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

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

function handler (req, res) {
req.url = {path: '/test'}
var serialized = serializers.reqSerializer(req)
t.is(serialized.url, '/test')
res.end()
}
})

test('can wrap request serializers', function (t) {
t.plan(3)

Expand Down

0 comments on commit 6f07ed1

Please sign in to comment.