From f30d54d36a818755b9c0c35f8daf4f718cd61680 Mon Sep 17 00:00:00 2001 From: Gil Pedersen Date: Thu, 21 Mar 2024 13:04:15 +0100 Subject: [PATCH] Assert on unsupported route method --- lib/route.js | 6 +++++- test/route.js | 9 +++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/route.js b/lib/route.js index 0fb4e0d49..6f2c254e8 100755 --- a/lib/route.js +++ b/lib/route.js @@ -1,6 +1,7 @@ 'use strict'; const Assert = require('assert'); +const Http = require('http'); const Bounce = require('@hapi/bounce'); const Catbox = require('@hapi/catbox'); @@ -19,7 +20,9 @@ const Streams = require('./streams'); const Validation = require('./validation'); -const internals = {}; +const internals = { + httpVerbs: new Set([...Http.METHODS.map((s) => s.toLowerCase()), '*', '_special']) +}; exports = module.exports = internals.Route = class { @@ -35,6 +38,7 @@ exports = module.exports = internals.Route = class { const method = route.method.toLowerCase(); Hoek.assert(method !== 'head', 'Cannot set HEAD route:', route.path); + Hoek.assert(internals.httpVerbs.has(method), 'Unsupported "method" for route:', route.method, route.path); const path = realm.modifiers.route.prefix ? realm.modifiers.route.prefix + (route.path !== '/' ? route.path : '') : route.path; Hoek.assert(path === '/' || path[path.length - 1] !== '/' || !core.settings.router.stripTrailingSlash, 'Path cannot end with a trailing slash when configured to strip:', route.method, route.path); diff --git a/test/route.js b/test/route.js index e8dae012b..bb4aca3f5 100755 --- a/test/route.js +++ b/test/route.js @@ -85,6 +85,15 @@ describe('Route', () => { }).to.throw(/Invalid route options/); }); + it('throws an error when a route uses an engine unsupported method', () => { + + expect(() => { + + const server = Hapi.server(); + server.route({ method: 'HAPIIII', path: '/', handler: () => null }); + }).to.throw(/Unsupported "method" for route/); + }); + it('throws an error when a route uses the HEAD method', () => { expect(() => {