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

fix #151 Wildcard route should not be blocked by Parametric with diff… #152

Merged
merged 2 commits into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 12 additions & 2 deletions node.js
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
55 changes: 55 additions & 0 deletions 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)
})