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

Assert on unsupported route method #4494

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/route.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const Assert = require('assert');
const Http = require('http');

const Bounce = require('@hapi/bounce');
const Catbox = require('@hapi/catbox');
Expand All @@ -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 {
Expand All @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions test/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down