diff --git a/lib/req.js b/lib/req.js index 9d6651e..1e5551c 100644 --- a/lib/req.js +++ b/lib/req.js @@ -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 diff --git a/test/req.test.js b/test/req.test.js index 22c7503..610e5db 100644 --- a/test/req.test.js +++ b/test/req.test.js @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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() } })