Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[master] prefer req.path instead of req.url.path #29

Merged
merged 2 commits into from
Aug 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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