Skip to content

Commit

Permalink
[master] prefer req.path instead of req.url.path (#29)
Browse files Browse the repository at this point in the history
* [master] prefer req.path instead of req.url.path

* [master] add fallback when req path is not present, added unit test

Co-authored-by: Hugo Ortiz <huortiz@paypal.com>
  • Loading branch information
hugoerortiz and Hugo Ortiz authored Aug 14, 2020
1 parent da08596 commit 3f60a62
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/req.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function reqSerializer (req) {
_req.url = req.originalUrl
} else {
// req.url.path is for hapi compat.
_req.url = req.url ? (req.url.path || req.url) : undefined
_req.url = req.path || (req.url ? (req.url.path || req.url) : undefined)
}
_req.headers = req.headers
_req.remoteAddress = connection && connection.remoteAddress
Expand Down
58 changes: 57 additions & 1 deletion test/req.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,25 @@ test('req.id has a non-function value with custom id function', function (t) {
}
})

test('req.url will be obtained from input request req.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.path = '/test'
var serialized = serializers.reqSerializer(req)
t.is(serialized.url, '/test')
res.end()
}
})

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

Expand All @@ -170,13 +189,50 @@ 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 = { path: '/test' }
var serialized = serializers.reqSerializer(req)
t.is(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) {
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 = '/test'
var serialized = serializers.reqSerializer(req)
t.is(serialized.url, '/test')
res.end()
}
})

test('req.url will be empty when input request path and url are not defined', 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) {
var serialized = serializers.reqSerializer(req)
t.is(serialized.url, '/')
res.end()
}
})

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

Expand Down

0 comments on commit 3f60a62

Please # to comment.