Skip to content

Commit b4cb4f5

Browse files
dylannildelvedor
authored andcommitted
fix #151 Wildcard route should not be blocked by Parametric with diff… (#152)
1 parent a5d041f commit b4cb4f5

File tree

4 files changed

+77
-3
lines changed

4 files changed

+77
-3
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const isRegexSafe = require('safe-regex2')
1818
const Node = require('./node')
1919
const NODE_TYPES = Node.prototype.types
2020
const httpMethods = http.METHODS
21-
const FULL_PATH_REGEXP = /^https?:\/\/.*\//
21+
const FULL_PATH_REGEXP = /^https?:\/\/.*?\//
2222

2323
if (!isRegexSafe(FULL_PATH_REGEXP)) {
2424
throw new Error('the FULL_PATH_REGEXP is not safe, update this module')

node.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,12 @@ Node.prototype.findChild = function (path, method) {
122122
}
123123
}
124124

125-
child = this.children[':'] || this.children['*']
125+
child = this.children[':']
126+
if (child !== undefined && (child.numberOfChildren > 0 || child.handlers[method] !== null)) {
127+
return child
128+
}
129+
130+
child = this.children['*']
126131
if (child !== undefined && (child.numberOfChildren > 0 || child.handlers[method] !== null)) {
127132
return child
128133
}
@@ -138,7 +143,12 @@ Node.prototype.findVersionChild = function (version, path, method) {
138143
}
139144
}
140145

141-
child = this.children[':'] || this.children['*']
146+
child = this.children[':']
147+
if (child !== undefined && (child.numberOfChildren > 0 || child.getVersionHandler(version, method) !== null)) {
148+
return child
149+
}
150+
151+
child = this.children['*']
142152
if (child !== undefined && (child.numberOfChildren > 0 || child.getVersionHandler(version, method) !== null)) {
143153
return child
144154
}

test/full-url.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,16 @@ findMyWay.on('GET', '/a', (req, res) => {
1313
res.end('{"message":"hello world"}')
1414
})
1515

16+
findMyWay.on('GET', '/a/:id', (req, res) => {
17+
res.end('{"message":"hello world"}')
18+
})
19+
1620
t.deepEqual(findMyWay.find('GET', 'http://localhost/a'), findMyWay.find('GET', '/a'))
1721
t.deepEqual(findMyWay.find('GET', 'http://localhost:8080/a'), findMyWay.find('GET', '/a'))
1822
t.deepEqual(findMyWay.find('GET', 'http://123.123.123.123/a'), findMyWay.find('GET', '/a'))
1923
t.deepEqual(findMyWay.find('GET', 'https://localhost/a'), findMyWay.find('GET', '/a'))
24+
25+
t.deepEqual(findMyWay.find('GET', 'http://localhost/a/100'), findMyWay.find('GET', '/a/100'))
26+
t.deepEqual(findMyWay.find('GET', 'http://localhost:8080/a/100'), findMyWay.find('GET', '/a/100'))
27+
t.deepEqual(findMyWay.find('GET', 'http://123.123.123.123/a/100'), findMyWay.find('GET', '/a/100'))
28+
t.deepEqual(findMyWay.find('GET', 'https://localhost/a/100'), findMyWay.find('GET', '/a/100'))

test/issue-151.test.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict'
2+
3+
const t = require('tap')
4+
const test = t.test
5+
const FindMyWay = require('../')
6+
7+
test('Wildcard route should not be blocked by Parametric with different method / 1', t => {
8+
t.plan(1)
9+
const findMyWay = FindMyWay({
10+
defaultRoute: (req, res) => {
11+
t.fail('Should not be defaultRoute')
12+
}
13+
})
14+
15+
findMyWay.on('OPTIONS', '/*', (req, res, params) => {
16+
t.fail('Should not be here')
17+
})
18+
19+
findMyWay.on('OPTIONS', '/obj/*', (req, res, params) => {
20+
t.strictEqual(req.method, 'OPTIONS')
21+
})
22+
23+
findMyWay.on('GET', '/obj/:id', (req, res, params) => {
24+
t.fail('Should not be GET')
25+
})
26+
27+
findMyWay.lookup({ method: 'OPTIONS', url: '/obj/params', headers: {} }, null)
28+
})
29+
30+
test('Wildcard route should not be blocked by Parametric with different method / 2', t => {
31+
t.plan(1)
32+
const findMyWay = FindMyWay({
33+
defaultRoute: (req, res) => {
34+
t.fail('Should not be defaultRoute')
35+
}
36+
})
37+
38+
findMyWay.on('OPTIONS', '/*', { version: '1.2.3' }, (req, res, params) => {
39+
t.fail('Should not be here')
40+
})
41+
42+
findMyWay.on('OPTIONS', '/obj/*', { version: '1.2.3' }, (req, res, params) => {
43+
t.strictEqual(req.method, 'OPTIONS')
44+
})
45+
46+
findMyWay.on('GET', '/obj/:id', { version: '1.2.3' }, (req, res, params) => {
47+
t.fail('Should not be GET')
48+
})
49+
50+
findMyWay.lookup({
51+
method: 'OPTIONS',
52+
url: '/obj/params',
53+
headers: { 'accept-version': '1.2.3' }
54+
}, null)
55+
})

0 commit comments

Comments
 (0)