Skip to content

Commit

Permalink
fix: make port optional when parsing URL (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnyb authored Sep 24, 2024
1 parent 0fa05e7 commit 5cfef3f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function fastifyUrlData (fastify, options, next) {
const host = this.hostname
const port = this.port
const path = this.headers[':path'] || this.raw.url
const urlData = parse(`${scheme}://${host}:${port}${path}`)
const urlData = parse(`${scheme}://${host}${port ? ':' + port : ''}${path}`)
if (key) return urlData[key]
return urlData
})
Expand Down
16 changes: 16 additions & 0 deletions test/tests.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,19 @@ test('parses a full URI ignoring X-Forwarded-Host when trustProxy is not set', (

t.teardown(() => fastify.close())
})

test('should parse path without a port specified', async (t) => {
t.plan(2)
const fastify = Fastify()
fastify
.register(plugin)

fastify.get('/', (req, reply) => {
const path = req.urlData('path')
reply.send('That worked, path is ' + path)
})

const res = await fastify.inject({ url: '/', headers: { host: 'localhost' } })
t.equal(res.statusCode, 200)
t.equal(res.body, 'That worked, path is /')
})

0 comments on commit 5cfef3f

Please sign in to comment.