diff --git a/index.js b/index.js index d3865033..1d6311a5 100644 --- a/index.js +++ b/index.js @@ -18,7 +18,7 @@ const isRegexSafe = require('safe-regex2') const Node = require('./node') const NODE_TYPES = Node.prototype.types const httpMethods = http.METHODS -const FULL_PATH_REGEXP = /^https?:\/\/.*\// +const FULL_PATH_REGEXP = /^https?:\/\/.*?\// if (!isRegexSafe(FULL_PATH_REGEXP)) { throw new Error('the FULL_PATH_REGEXP is not safe, update this module') diff --git a/node.js b/node.js index 59811045..a38088cc 100644 --- a/node.js +++ b/node.js @@ -122,7 +122,12 @@ Node.prototype.findChild = function (path, method) { } } - child = this.children[':'] || this.children['*'] + child = this.children[':'] + if (child !== undefined && (child.numberOfChildren > 0 || child.handlers[method] !== null)) { + return child + } + + child = this.children['*'] if (child !== undefined && (child.numberOfChildren > 0 || child.handlers[method] !== null)) { return child } @@ -138,7 +143,12 @@ Node.prototype.findVersionChild = function (version, path, method) { } } - child = this.children[':'] || this.children['*'] + child = this.children[':'] + if (child !== undefined && (child.numberOfChildren > 0 || child.getVersionHandler(version, method) !== null)) { + return child + } + + child = this.children['*'] if (child !== undefined && (child.numberOfChildren > 0 || child.getVersionHandler(version, method) !== null)) { return child } diff --git a/test/full-url.test.js b/test/full-url.test.js index b16bd62b..d24b86a0 100644 --- a/test/full-url.test.js +++ b/test/full-url.test.js @@ -13,7 +13,16 @@ findMyWay.on('GET', '/a', (req, res) => { res.end('{"message":"hello world"}') }) +findMyWay.on('GET', '/a/:id', (req, res) => { + res.end('{"message":"hello world"}') +}) + t.deepEqual(findMyWay.find('GET', 'http://localhost/a'), findMyWay.find('GET', '/a')) t.deepEqual(findMyWay.find('GET', 'http://localhost:8080/a'), findMyWay.find('GET', '/a')) t.deepEqual(findMyWay.find('GET', 'http://123.123.123.123/a'), findMyWay.find('GET', '/a')) t.deepEqual(findMyWay.find('GET', 'https://localhost/a'), findMyWay.find('GET', '/a')) + +t.deepEqual(findMyWay.find('GET', 'http://localhost/a/100'), findMyWay.find('GET', '/a/100')) +t.deepEqual(findMyWay.find('GET', 'http://localhost:8080/a/100'), findMyWay.find('GET', '/a/100')) +t.deepEqual(findMyWay.find('GET', 'http://123.123.123.123/a/100'), findMyWay.find('GET', '/a/100')) +t.deepEqual(findMyWay.find('GET', 'https://localhost/a/100'), findMyWay.find('GET', '/a/100')) diff --git a/test/issue-151.test.js b/test/issue-151.test.js new file mode 100644 index 00000000..2725486c --- /dev/null +++ b/test/issue-151.test.js @@ -0,0 +1,55 @@ +'use strict' + +const t = require('tap') +const test = t.test +const FindMyWay = require('../') + +test('Wildcard route should not be blocked by Parametric with different method / 1', t => { + t.plan(1) + const findMyWay = FindMyWay({ + defaultRoute: (req, res) => { + t.fail('Should not be defaultRoute') + } + }) + + findMyWay.on('OPTIONS', '/*', (req, res, params) => { + t.fail('Should not be here') + }) + + findMyWay.on('OPTIONS', '/obj/*', (req, res, params) => { + t.strictEqual(req.method, 'OPTIONS') + }) + + findMyWay.on('GET', '/obj/:id', (req, res, params) => { + t.fail('Should not be GET') + }) + + findMyWay.lookup({ method: 'OPTIONS', url: '/obj/params', headers: {} }, null) +}) + +test('Wildcard route should not be blocked by Parametric with different method / 2', t => { + t.plan(1) + const findMyWay = FindMyWay({ + defaultRoute: (req, res) => { + t.fail('Should not be defaultRoute') + } + }) + + findMyWay.on('OPTIONS', '/*', { version: '1.2.3' }, (req, res, params) => { + t.fail('Should not be here') + }) + + findMyWay.on('OPTIONS', '/obj/*', { version: '1.2.3' }, (req, res, params) => { + t.strictEqual(req.method, 'OPTIONS') + }) + + findMyWay.on('GET', '/obj/:id', { version: '1.2.3' }, (req, res, params) => { + t.fail('Should not be GET') + }) + + findMyWay.lookup({ + method: 'OPTIONS', + url: '/obj/params', + headers: { 'accept-version': '1.2.3' } + }, null) +})