Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fixes #3648 - URL must be a string #3653

Merged
merged 12 commits into from
Jan 25, 2022
5 changes: 5 additions & 0 deletions lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,11 @@ const codes = {
500,
TypeError
),
FST_ERR_INVALID_URL: createError(
'FST_ERR_INVALID_URL',
"URL must be a string. Received '%s'",
400
),

/**
* again listen when close server
Expand Down
7 changes: 6 additions & 1 deletion lib/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const {
const {
FST_ERR_SCH_VALIDATION_BUILD,
FST_ERR_SCH_SERIALIZATION_BUILD,
FST_ERR_DEFAULT_ROUTE_INVALID_TYPE
FST_ERR_DEFAULT_ROUTE_INVALID_TYPE,
FST_ERR_INVALID_URL
} = require('./errors')

const {
Expand Down Expand Up @@ -99,6 +100,10 @@ function buildRouting (options) {

// Convert shorthand to extended route declaration
function prepareRoute (method, url, options, handler) {
if (typeof url !== 'string') {
throw new FST_ERR_INVALID_URL(typeof url)
}

if (!handler && typeof options === 'function') {
handler = options // for support over direct function calls such as fastify.get() options are reused as the handler
options = {}
Expand Down
12 changes: 12 additions & 0 deletions test/route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const sget = require('simple-get').concat
const joi = require('@hapi/joi')
const Fastify = require('..')
const proxyquire = require('proxyquire')
const { FST_ERR_INVALID_URL } = require('../lib/errors')

test('route', t => {
t.plan(9)
Expand Down Expand Up @@ -1268,3 +1269,14 @@ test('Correct error message is produced if "path" option is used', t => {
})
}, new Error('Error Handler for POST:/test route, if defined, must be a function'))
})

test('invalid url attribute - non string URL', t => {
t.plan(1)
const fastify = Fastify()

try {
fastify.get(/^\/(donations|skills|blogs)/, () => {})
} catch (error) {
t.equal(error.code, FST_ERR_INVALID_URL().code)
}
})