Skip to content

Commit

Permalink
guard against req.url being falsy
Browse files Browse the repository at this point in the history
it's tempting to simply stop looking at req.url.path altogether, since that was introduced for hapi 17 in pinojs@83d58a7#diff-2c4782cb20f7f0d7ca90c7bc04939f4420c4bdb42fdce618984da393ba9202f7R60 .

but since pinojs#29 we look at req.path first, which is safer in hapi, so hapi doesn't care about req.url.path anymore.

but perhaps someone else does depend on req.url.path . so for now, continuing to look at req.url.path , which means a bit more guarding in the req serializer.
  • Loading branch information
rektide committed Feb 23, 2022
1 parent e7d2109 commit 01be47f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion lib/req.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function reqSerializer (req) {
} else {
const path = req.path
// path for safe hapi compat.
_req.url = typeof path === 'string' ? path : (req.url.path || req.url)
_req.url = typeof path === 'string' ? path : req.url
}
_req.headers = req.headers
_req.remoteAddress = connection && connection.remoteAddress
Expand Down
30 changes: 15 additions & 15 deletions test/req.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ test('req.url will be obtained from input request req.path when input request ur
}
})

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

const server = http.createServer(handler)
Expand All @@ -189,14 +189,14 @@ test('req.url will be obtained from input request url.path when input request ur
t.teardown(() => server.close())

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

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

const server = http.createServer(handler)
Expand All @@ -208,14 +208,13 @@ test('req.url will be obtained from input request url when input request url is
t.teardown(() => server.close())

function handler (req, res) {
req.url = '/test'
const serialized = serializers.reqSerializer(req)
t.equal(serialized.url, '/test')
t.equal(serialized.url, '/')
res.end()
}
})

test('req.url will be empty when input request path and url are not defined', function (t) {
test('req.url will be obtained from input request originalUrl when available', function (t) {
t.plan(1)

const server = http.createServer(handler)
Expand All @@ -227,13 +226,14 @@ test('req.url will be empty when input request path and url are not defined', fu
t.teardown(() => server.close())

function handler (req, res) {
req.originalUrl = '/test'
const serialized = serializers.reqSerializer(req)
t.equal(serialized.url, '/')
t.equal(serialized.url, '/test')
res.end()
}
})

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

const server = http.createServer(handler)
Expand All @@ -245,14 +245,17 @@ test('req.url will be obtained from input request originalUrl when available', f
t.teardown(() => server.close())

function handler (req, res) {
req.originalUrl = '/test'
req.path = function () {
throw new Error('unexpected invocation')
}
req.url = '/test'
const serialized = serializers.reqSerializer(req)
t.equal(serialized.url, '/test')
res.end()
}
})

test('req.url will be obtained from input request url when req path is a function', function (t) {
test('req.url being undefined does not throw an error', function (t) {
t.plan(1)

const server = http.createServer(handler)
Expand All @@ -264,12 +267,9 @@ test('req.url will be obtained from input request url when req path is a functio
t.teardown(() => server.close())

function handler (req, res) {
req.path = function () {
throw new Error('unexpected invocation')
}
req.url = '/test'
req.url = undefined
const serialized = serializers.reqSerializer(req)
t.equal(serialized.url, '/test')
t.equal(serialized.url, undefined)
res.end()
}
})
Expand Down

0 comments on commit 01be47f

Please # to comment.