Skip to content

Commit

Permalink
Fixes #3648 - URL must be a string (#3653)
Browse files Browse the repository at this point in the history
* Fixed #3648 - URL must be a string

* Revert "Fixed #3648 - URL must be a string"

This reverts commit c5cd59f.

* Fixed #3648 - URL must be a string

* Fixed GitHub Linter Error

* Fixed GitHub Linter Error

* Fixed GitHub Linter Error

* Fixed GitHub Linter Error

* Update lib/route.js

Co-authored-by: Manuel Spigolon <behemoth89@gmail.com>

* Update route.test.js

* Addressed Review comments - Used Error codes. Removed unecessary url existence check

* Removed unecessary logger statement

Co-authored-by: Manuel Spigolon <behemoth89@gmail.com>
  • Loading branch information
VigneshMurugan and Eomm committed Jan 25, 2022
1 parent 4928abc commit 25147fe
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/errors.js
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
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
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)
}
})

0 comments on commit 25147fe

Please sign in to comment.