First commit

This commit is contained in:
2026-01-12 13:12:46 +01:00
parent b2d9501f6d
commit a1fbd8acf5
4413 changed files with 1245183 additions and 0 deletions

231
node_modules/find-my-way/test/case-insensitive.test.js generated vendored Normal file
View File

@@ -0,0 +1,231 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('../')
test('case insensitive static routes of level 1', t => {
t.plan(1)
const findMyWay = FindMyWay({
caseSensitive: false,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/woo', (req, res, params) => {
t.pass('we should be here')
})
findMyWay.lookup({ method: 'GET', url: '/WOO', headers: {} }, null)
})
test('case insensitive static routes of level 2', t => {
t.plan(1)
const findMyWay = FindMyWay({
caseSensitive: false,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/foo/woo', (req, res, params) => {
t.pass('we should be here')
})
findMyWay.lookup({ method: 'GET', url: '/FoO/WOO', headers: {} }, null)
})
test('case insensitive static routes of level 3', t => {
t.plan(1)
const findMyWay = FindMyWay({
caseSensitive: false,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/foo/bar/woo', (req, res, params) => {
t.pass('we should be here')
})
findMyWay.lookup({ method: 'GET', url: '/Foo/bAR/WoO', headers: {} }, null)
})
test('parametric case insensitive', t => {
t.plan(1)
const findMyWay = FindMyWay({
caseSensitive: false,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/foo/:param', (req, res, params) => {
t.equal(params.param, 'bAR')
})
findMyWay.lookup({ method: 'GET', url: '/Foo/bAR', headers: {} }, null)
})
test('parametric case insensitive with a static part', t => {
t.plan(1)
const findMyWay = FindMyWay({
caseSensitive: false,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/foo/my-:param', (req, res, params) => {
t.equal(params.param, 'bAR')
})
findMyWay.lookup({ method: 'GET', url: '/Foo/MY-bAR', headers: {} }, null)
})
test('parametric case insensitive with capital letter', t => {
t.plan(1)
const findMyWay = FindMyWay({
caseSensitive: false,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/foo/:Param', (req, res, params) => {
t.equal(params.Param, 'bAR')
})
findMyWay.lookup({ method: 'GET', url: '/Foo/bAR', headers: {} }, null)
})
test('case insensitive with capital letter in static path with param', t => {
t.plan(1)
const findMyWay = FindMyWay({
caseSensitive: false,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/Foo/bar/:param', (req, res, params) => {
t.equal(params.param, 'baZ')
})
findMyWay.lookup({ method: 'GET', url: '/foo/bar/baZ', headers: {} }, null)
})
test('case insensitive with multiple paths containing capital letter in static path with param', t => {
/*
* This is a reproduction of the issue documented at
* https://github.com/delvedor/find-my-way/issues/96.
*/
t.plan(2)
const findMyWay = FindMyWay({
caseSensitive: false,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/Foo/bar/:param', (req, res, params) => {
t.equal(params.param, 'baZ')
})
findMyWay.on('GET', '/Foo/baz/:param', (req, res, params) => {
t.equal(params.param, 'baR')
})
findMyWay.lookup({ method: 'GET', url: '/foo/bar/baZ', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/foo/baz/baR', headers: {} }, null)
})
test('case insensitive with multiple mixed-case params within same slash couple', t => {
t.plan(2)
const findMyWay = FindMyWay({
caseSensitive: false,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/foo/:param1-:param2', (req, res, params) => {
t.equal(params.param1, 'My')
t.equal(params.param2, 'bAR')
})
findMyWay.lookup({ method: 'GET', url: '/FOO/My-bAR', headers: {} }, null)
})
test('case insensitive with multiple mixed-case params', t => {
t.plan(2)
const findMyWay = FindMyWay({
caseSensitive: false,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/foo/:param1/:param2', (req, res, params) => {
t.equal(params.param1, 'My')
t.equal(params.param2, 'bAR')
})
findMyWay.lookup({ method: 'GET', url: '/FOO/My/bAR', headers: {} }, null)
})
test('case insensitive with wildcard', t => {
t.plan(1)
const findMyWay = FindMyWay({
caseSensitive: false,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/foo/*', (req, res, params) => {
t.equal(params['*'], 'baR')
})
findMyWay.lookup({ method: 'GET', url: '/FOO/baR', headers: {} }, null)
})
test('parametric case insensitive with multiple routes', t => {
t.plan(6)
const findMyWay = FindMyWay({
caseSensitive: false,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('POST', '/foo/:param/Static/:userId/Save', (req, res, params) => {
t.equal(params.param, 'bAR')
t.equal(params.userId, 'one')
})
findMyWay.on('POST', '/foo/:param/Static/:userId/Update', (req, res, params) => {
t.equal(params.param, 'Bar')
t.equal(params.userId, 'two')
})
findMyWay.on('POST', '/foo/:param/Static/:userId/CANCEL', (req, res, params) => {
t.equal(params.param, 'bAR')
t.equal(params.userId, 'THREE')
})
findMyWay.lookup({ method: 'POST', url: '/foo/bAR/static/one/SAVE', headers: {} }, null)
findMyWay.lookup({ method: 'POST', url: '/fOO/Bar/Static/two/update', headers: {} }, null)
findMyWay.lookup({ method: 'POST', url: '/Foo/bAR/STATIC/THREE/cAnCeL', headers: {} }, null)
})

View File

@@ -0,0 +1,131 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('..')
const noop = () => { }
const customVersioning = {
name: 'version',
// storage factory
storage: function () {
let versions = {}
return {
get: (version) => { return versions[version] || null },
set: (version, store) => { versions[version] = store },
del: (version) => { delete versions[version] },
empty: () => { versions = {} }
}
},
deriveConstraint: (req, ctx) => {
return req.headers.accept
}
}
test('A route could support multiple versions (find) / 1', t => {
t.plan(5)
const findMyWay = FindMyWay({ constraints: { version: customVersioning } })
findMyWay.on('GET', '/', { constraints: { version: 'application/vnd.example.api+json;version=2' } }, noop)
findMyWay.on('GET', '/', { constraints: { version: 'application/vnd.example.api+json;version=3' } }, noop)
t.ok(findMyWay.find('GET', '/', { version: 'application/vnd.example.api+json;version=2' }))
t.ok(findMyWay.find('GET', '/', { version: 'application/vnd.example.api+json;version=3' }))
t.notOk(findMyWay.find('GET', '/', { version: 'application/vnd.example.api+json;version=4' }))
t.notOk(findMyWay.find('GET', '/', { version: 'application/vnd.example.api+json;version=5' }))
t.notOk(findMyWay.find('GET', '/', { version: 'application/vnd.example.api+json;version=6' }))
})
test('A route could support multiple versions (find) / 1 (add strategy outside constructor)', t => {
t.plan(5)
const findMyWay = FindMyWay()
findMyWay.addConstraintStrategy(customVersioning)
findMyWay.on('GET', '/', { constraints: { version: 'application/vnd.example.api+json;version=2' } }, noop)
findMyWay.on('GET', '/', { constraints: { version: 'application/vnd.example.api+json;version=3' } }, noop)
t.ok(findMyWay.find('GET', '/', { version: 'application/vnd.example.api+json;version=2' }))
t.ok(findMyWay.find('GET', '/', { version: 'application/vnd.example.api+json;version=3' }))
t.notOk(findMyWay.find('GET', '/', { version: 'application/vnd.example.api+json;version=4' }))
t.notOk(findMyWay.find('GET', '/', { version: 'application/vnd.example.api+json;version=5' }))
t.notOk(findMyWay.find('GET', '/', { version: 'application/vnd.example.api+json;version=6' }))
})
test('Overriding default strategies uses the custom deriveConstraint function', t => {
t.plan(2)
const findMyWay = FindMyWay({ constraints: { version: customVersioning } })
findMyWay.on('GET', '/', { constraints: { version: 'application/vnd.example.api+json;version=2' } }, (req, res, params) => {
t.equal(req.headers.accept, 'application/vnd.example.api+json;version=2')
})
findMyWay.on('GET', '/', { constraints: { version: 'application/vnd.example.api+json;version=3' } }, (req, res, params) => {
t.equal(req.headers.accept, 'application/vnd.example.api+json;version=3')
})
findMyWay.lookup({
method: 'GET',
url: '/',
headers: { accept: 'application/vnd.example.api+json;version=2' }
})
findMyWay.lookup({
method: 'GET',
url: '/',
headers: { accept: 'application/vnd.example.api+json;version=3' }
})
})
test('Overriding default strategies uses the custom deriveConstraint function (add strategy outside constructor)', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.addConstraintStrategy(customVersioning)
findMyWay.on('GET', '/', { constraints: { version: 'application/vnd.example.api+json;version=2' } }, (req, res, params) => {
t.equal(req.headers.accept, 'application/vnd.example.api+json;version=2')
})
findMyWay.on('GET', '/', { constraints: { version: 'application/vnd.example.api+json;version=3' } }, (req, res, params) => {
t.equal(req.headers.accept, 'application/vnd.example.api+json;version=3')
})
findMyWay.lookup({
method: 'GET',
url: '/',
headers: { accept: 'application/vnd.example.api+json;version=2' }
})
findMyWay.lookup({
method: 'GET',
url: '/',
headers: { accept: 'application/vnd.example.api+json;version=3' }
})
})
test('Overriding custom strategies throws as error (add strategy outside constructor)', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.addConstraintStrategy(customVersioning)
t.throws(() => findMyWay.addConstraintStrategy(customVersioning),
'There already exists a custom constraint with the name version.'
)
})
test('Overriding default strategies after defining a route with constraint', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/', { constraints: { host: 'fastify.io', version: '1.0.0' } }, () => {})
t.throws(() => findMyWay.addConstraintStrategy(customVersioning),
'There already exists a route with version constraint.'
)
})

View File

@@ -0,0 +1,112 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('..')
const rfdc = require('rfdc')({ proto: true })
const customHeaderConstraint = {
name: 'requestedBy',
storage: function () {
const requestedBys = {}
return {
get: (requestedBy) => { return requestedBys[requestedBy] || null },
set: (requestedBy, store) => { requestedBys[requestedBy] = store }
}
},
deriveConstraint: (req, ctx, done) => {
if (req.headers['user-agent'] === 'wrong') {
done(new Error('wrong user-agent'))
return
}
done(null, req.headers['user-agent'])
}
}
test('should derive multiple async constraints', t => {
t.plan(2)
const customHeaderConstraint2 = rfdc(customHeaderConstraint)
customHeaderConstraint2.name = 'requestedBy2'
const router = FindMyWay({ constraints: { requestedBy: customHeaderConstraint, requestedBy2: customHeaderConstraint2 } })
router.on('GET', '/', { constraints: { requestedBy: 'node', requestedBy2: 'node' } }, () => 'asyncHandler')
router.lookup(
{
method: 'GET',
url: '/',
headers: {
'user-agent': 'node'
}
},
null,
(err, result) => {
t.equal(err, null)
t.equal(result, 'asyncHandler')
}
)
})
test('lookup should return an error from deriveConstraint', t => {
t.plan(2)
const router = FindMyWay({ constraints: { requestedBy: customHeaderConstraint } })
router.on('GET', '/', { constraints: { requestedBy: 'node' } }, () => 'asyncHandler')
router.lookup(
{
method: 'GET',
url: '/',
headers: {
'user-agent': 'wrong'
}
},
null,
(err, result) => {
t.same(err, new Error('wrong user-agent'))
t.equal(result, undefined)
}
)
})
test('should derive sync and async constraints', t => {
t.plan(4)
const router = FindMyWay({ constraints: { requestedBy: customHeaderConstraint } })
router.on('GET', '/', { constraints: { version: '1.0.0', requestedBy: 'node' } }, () => 'asyncHandlerV1')
router.on('GET', '/', { constraints: { version: '2.0.0', requestedBy: 'node' } }, () => 'asyncHandlerV2')
router.lookup(
{
method: 'GET',
url: '/',
headers: {
'user-agent': 'node',
'accept-version': '1.0.0'
}
},
null,
(err, result) => {
t.equal(err, null)
t.equal(result, 'asyncHandlerV1')
}
)
router.lookup(
{
method: 'GET',
url: '/',
headers: {
'user-agent': 'node',
'accept-version': '2.0.0'
}
},
null,
(err, result) => {
t.equal(err, null)
t.equal(result, 'asyncHandlerV2')
}
)
})

274
node_modules/find-my-way/test/constraint.custom.test.js generated vendored Normal file
View File

@@ -0,0 +1,274 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('..')
const alpha = () => { }
const beta = () => { }
const gamma = () => { }
const delta = () => { }
const customHeaderConstraint = {
name: 'requestedBy',
storage: function () {
let requestedBys = {}
return {
get: (requestedBy) => { return requestedBys[requestedBy] || null },
set: (requestedBy, store) => { requestedBys[requestedBy] = store },
del: (requestedBy) => { delete requestedBys[requestedBy] },
empty: () => { requestedBys = {} }
}
},
deriveConstraint: (req, ctx) => {
return req.headers['user-agent']
}
}
test('A route could support a custom constraint strategy', t => {
t.plan(3)
const findMyWay = FindMyWay({ constraints: { requestedBy: customHeaderConstraint } })
findMyWay.on('GET', '/', { constraints: { requestedBy: 'curl' } }, alpha)
findMyWay.on('GET', '/', { constraints: { requestedBy: 'wget' } }, beta)
t.equal(findMyWay.find('GET', '/', { requestedBy: 'curl' }).handler, alpha)
t.equal(findMyWay.find('GET', '/', { requestedBy: 'wget' }).handler, beta)
t.notOk(findMyWay.find('GET', '/', { requestedBy: 'chrome' }))
})
test('A route could support a custom constraint strategy (add strategy outside constructor)', t => {
t.plan(3)
const findMyWay = FindMyWay()
findMyWay.addConstraintStrategy(customHeaderConstraint)
findMyWay.on('GET', '/', { constraints: { requestedBy: 'curl' } }, alpha)
findMyWay.on('GET', '/', { constraints: { requestedBy: 'wget' } }, beta)
t.equal(findMyWay.find('GET', '/', { requestedBy: 'curl' }).handler, alpha)
t.equal(findMyWay.find('GET', '/', { requestedBy: 'wget' }).handler, beta)
t.notOk(findMyWay.find('GET', '/', { requestedBy: 'chrome' }))
})
test('A route could support a custom constraint strategy while versioned', t => {
t.plan(8)
const findMyWay = FindMyWay({ constraints: { requestedBy: customHeaderConstraint } })
findMyWay.on('GET', '/', { constraints: { requestedBy: 'curl', version: '1.0.0' } }, alpha)
findMyWay.on('GET', '/', { constraints: { requestedBy: 'curl', version: '2.0.0' } }, beta)
findMyWay.on('GET', '/', { constraints: { requestedBy: 'wget', version: '2.0.0' } }, gamma)
findMyWay.on('GET', '/', { constraints: { requestedBy: 'wget', version: '3.0.0' } }, delta)
t.equal(findMyWay.find('GET', '/', { requestedBy: 'curl', version: '1.x' }).handler, alpha)
t.equal(findMyWay.find('GET', '/', { requestedBy: 'curl', version: '2.x' }).handler, beta)
t.equal(findMyWay.find('GET', '/', { requestedBy: 'wget', version: '2.x' }).handler, gamma)
t.equal(findMyWay.find('GET', '/', { requestedBy: 'wget', version: '3.x' }).handler, delta)
t.notOk(findMyWay.find('GET', '/', { requestedBy: 'chrome' }))
t.notOk(findMyWay.find('GET', '/', { requestedBy: 'chrome', version: '1.x' }))
t.notOk(findMyWay.find('GET', '/', { requestedBy: 'curl', version: '3.x' }))
t.notOk(findMyWay.find('GET', '/', { requestedBy: 'wget', version: '1.x' }))
})
test('A route could support a custom constraint strategy while versioned (add strategy outside constructor)', t => {
t.plan(8)
const findMyWay = FindMyWay()
findMyWay.addConstraintStrategy(customHeaderConstraint)
findMyWay.on('GET', '/', { constraints: { requestedBy: 'curl', version: '1.0.0' } }, alpha)
findMyWay.on('GET', '/', { constraints: { requestedBy: 'curl', version: '2.0.0' } }, beta)
findMyWay.on('GET', '/', { constraints: { requestedBy: 'wget', version: '2.0.0' } }, gamma)
findMyWay.on('GET', '/', { constraints: { requestedBy: 'wget', version: '3.0.0' } }, delta)
t.equal(findMyWay.find('GET', '/', { requestedBy: 'curl', version: '1.x' }).handler, alpha)
t.equal(findMyWay.find('GET', '/', { requestedBy: 'curl', version: '2.x' }).handler, beta)
t.equal(findMyWay.find('GET', '/', { requestedBy: 'wget', version: '2.x' }).handler, gamma)
t.equal(findMyWay.find('GET', '/', { requestedBy: 'wget', version: '3.x' }).handler, delta)
t.notOk(findMyWay.find('GET', '/', { requestedBy: 'chrome' }))
t.notOk(findMyWay.find('GET', '/', { requestedBy: 'chrome', version: '1.x' }))
t.notOk(findMyWay.find('GET', '/', { requestedBy: 'curl', version: '3.x' }))
t.notOk(findMyWay.find('GET', '/', { requestedBy: 'wget', version: '1.x' }))
})
test('A route could support a custom constraint strategy while versioned and host constrained', t => {
t.plan(9)
const findMyWay = FindMyWay({ constraints: { requestedBy: customHeaderConstraint } })
findMyWay.on('GET', '/', { constraints: { requestedBy: 'curl', version: '1.0.0', host: 'fastify.io' } }, alpha)
findMyWay.on('GET', '/', { constraints: { requestedBy: 'curl', version: '2.0.0', host: 'fastify.io' } }, beta)
findMyWay.on('GET', '/', { constraints: { requestedBy: 'curl', version: '2.0.0', host: 'example.io' } }, delta)
t.equal(findMyWay.find('GET', '/', { requestedBy: 'curl', version: '1.x', host: 'fastify.io' }).handler, alpha)
t.equal(findMyWay.find('GET', '/', { requestedBy: 'curl', version: '2.x', host: 'fastify.io' }).handler, beta)
t.equal(findMyWay.find('GET', '/', { requestedBy: 'curl', version: '2.x', host: 'example.io' }).handler, delta)
t.notOk(findMyWay.find('GET', '/', { requestedBy: 'chrome' }))
t.notOk(findMyWay.find('GET', '/', { requestedBy: 'chrome', version: '1.x' }))
t.notOk(findMyWay.find('GET', '/', { requestedBy: 'curl', version: '1.x' }))
t.notOk(findMyWay.find('GET', '/', { requestedBy: 'curl', version: '2.x' }))
t.notOk(findMyWay.find('GET', '/', { requestedBy: 'curl', version: '3.x', host: 'fastify.io' }))
t.notOk(findMyWay.find('GET', '/', { requestedBy: 'curl', version: '1.x', host: 'example.io' }))
})
test('A route could support a custom constraint strategy while versioned and host constrained (add strategy outside constructor)', t => {
t.plan(9)
const findMyWay = FindMyWay()
findMyWay.addConstraintStrategy(customHeaderConstraint)
findMyWay.on('GET', '/', { constraints: { requestedBy: 'curl', version: '1.0.0', host: 'fastify.io' } }, alpha)
findMyWay.on('GET', '/', { constraints: { requestedBy: 'curl', version: '2.0.0', host: 'fastify.io' } }, beta)
findMyWay.on('GET', '/', { constraints: { requestedBy: 'curl', version: '2.0.0', host: 'example.io' } }, delta)
t.equal(findMyWay.find('GET', '/', { requestedBy: 'curl', version: '1.x', host: 'fastify.io' }).handler, alpha)
t.equal(findMyWay.find('GET', '/', { requestedBy: 'curl', version: '2.x', host: 'fastify.io' }).handler, beta)
t.equal(findMyWay.find('GET', '/', { requestedBy: 'curl', version: '2.x', host: 'example.io' }).handler, delta)
t.notOk(findMyWay.find('GET', '/', { requestedBy: 'chrome' }))
t.notOk(findMyWay.find('GET', '/', { requestedBy: 'chrome', version: '1.x' }))
t.notOk(findMyWay.find('GET', '/', { requestedBy: 'curl', version: '1.x' }))
t.notOk(findMyWay.find('GET', '/', { requestedBy: 'curl', version: '2.x' }))
t.notOk(findMyWay.find('GET', '/', { requestedBy: 'curl', version: '3.x', host: 'fastify.io' }))
t.notOk(findMyWay.find('GET', '/', { requestedBy: 'curl', version: '1.x', host: 'example.io' }))
})
test('Custom constraint strategies can set mustMatchWhenDerived flag to true which prevents matches to unconstrained routes when a constraint is derived and there are no other routes', t => {
t.plan(1)
const findMyWay = FindMyWay({
constraints: {
requestedBy: {
...customHeaderConstraint,
mustMatchWhenDerived: true
}
},
defaultRoute (req, res) {
t.pass()
}
})
findMyWay.on('GET', '/', {}, () => t.fail())
findMyWay.lookup({ method: 'GET', url: '/', headers: { 'user-agent': 'node' } }, null)
})
test('Custom constraint strategies can set mustMatchWhenDerived flag to true which prevents matches to unconstrained routes when a constraint is derived and there are no other routes (add strategy outside constructor)', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute (req, res) {
t.pass()
}
})
findMyWay.addConstraintStrategy({
...customHeaderConstraint,
mustMatchWhenDerived: true
})
findMyWay.on('GET', '/', {}, () => t.fail())
findMyWay.lookup({ method: 'GET', url: '/', headers: { 'user-agent': 'node' } }, null)
})
test('Custom constraint strategies can set mustMatchWhenDerived flag to true which prevents matches to unconstrained routes when a constraint is derived when there are constrained routes', t => {
t.plan(1)
const findMyWay = FindMyWay({
constraints: {
requestedBy: {
...customHeaderConstraint,
mustMatchWhenDerived: true
}
},
defaultRoute (req, res) {
t.pass()
}
})
findMyWay.on('GET', '/', {}, () => t.fail())
findMyWay.on('GET', '/', { constraints: { requestedBy: 'curl' } }, () => t.fail())
findMyWay.on('GET', '/', { constraints: { requestedBy: 'wget' } }, () => t.fail())
findMyWay.lookup({ method: 'GET', url: '/', headers: { 'user-agent': 'node' } }, null)
})
test('Custom constraint strategies can set mustMatchWhenDerived flag to true which prevents matches to unconstrained routes when a constraint is derived when there are constrained routes (add strategy outside constructor)', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute (req, res) {
t.pass()
}
})
findMyWay.addConstraintStrategy({
...customHeaderConstraint,
mustMatchWhenDerived: true
})
findMyWay.on('GET', '/', {}, () => t.fail())
findMyWay.on('GET', '/', { constraints: { requestedBy: 'curl' } }, () => t.fail())
findMyWay.on('GET', '/', { constraints: { requestedBy: 'wget' } }, () => t.fail())
findMyWay.lookup({ method: 'GET', url: '/', headers: { 'user-agent': 'node' } }, null)
})
test('Custom constraint strategies can set mustMatchWhenDerived flag to false which allows matches to unconstrained routes when a constraint is derived', t => {
t.plan(1)
const findMyWay = FindMyWay({
constraints: {
requestedBy: {
...customHeaderConstraint,
mustMatchWhenDerived: false
}
},
defaultRoute (req, res) {
t.fail()
}
})
findMyWay.on('GET', '/', {}, () => t.pass())
findMyWay.lookup({ method: 'GET', url: '/', headers: { 'user-agent': 'node' } }, null)
})
test('Custom constraint strategies can set mustMatchWhenDerived flag to false which allows matches to unconstrained routes when a constraint is derived (add strategy outside constructor)', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute (req, res) {
t.pass()
}
})
findMyWay.addConstraintStrategy({
...customHeaderConstraint,
mustMatchWhenDerived: true
})
findMyWay.on('GET', '/', {}, () => t.pass())
findMyWay.lookup({ method: 'GET', url: '/', headers: { 'user-agent': 'node' } }, null)
})
test('Has constraint strategy method test', t => {
t.plan(2)
const findMyWay = FindMyWay()
t.same(findMyWay.hasConstraintStrategy(customHeaderConstraint.name), false)
findMyWay.addConstraintStrategy(customHeaderConstraint)
t.same(findMyWay.hasConstraintStrategy(customHeaderConstraint.name), true)
})

View File

@@ -0,0 +1,290 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('..')
const noop = () => { }
test('A route could support multiple versions (find) / 1', t => {
t.plan(7)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/', { constraints: { version: '1.2.3' } }, noop)
findMyWay.on('GET', '/', { constraints: { version: '3.2.0' } }, noop)
t.ok(findMyWay.find('GET', '/', { version: '1.x' }))
t.ok(findMyWay.find('GET', '/', { version: '1.2.3' }))
t.ok(findMyWay.find('GET', '/', { version: '3.x' }))
t.ok(findMyWay.find('GET', '/', { version: '3.2.0' }))
t.notOk(findMyWay.find('GET', '/', { version: '2.x' }))
t.notOk(findMyWay.find('GET', '/', { version: '2.3.4' }))
t.notOk(findMyWay.find('GET', '/', { version: '3.2.1' }))
})
test('A route could support multiple versions (find) / 2', t => {
t.plan(7)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test', { constraints: { version: '1.2.3' } }, noop)
findMyWay.on('GET', '/test', { constraints: { version: '3.2.0' } }, noop)
t.ok(findMyWay.find('GET', '/test', { version: '1.x' }))
t.ok(findMyWay.find('GET', '/test', { version: '1.2.3' }))
t.ok(findMyWay.find('GET', '/test', { version: '3.x' }))
t.ok(findMyWay.find('GET', '/test', { version: '3.2.0' }))
t.notOk(findMyWay.find('GET', '/test', { version: '2.x' }))
t.notOk(findMyWay.find('GET', '/test', { version: '2.3.4' }))
t.notOk(findMyWay.find('GET', '/test', { version: '3.2.1' }))
})
test('A route could support multiple versions (find) / 3', t => {
t.plan(10)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test/:id/hello', { constraints: { version: '1.2.3' } }, noop)
findMyWay.on('GET', '/test/:id/hello', { constraints: { version: '3.2.0' } }, noop)
findMyWay.on('GET', '/test/name/hello', { constraints: { version: '4.0.0' } }, noop)
t.ok(findMyWay.find('GET', '/test/1234/hello', { version: '1.x' }))
t.ok(findMyWay.find('GET', '/test/1234/hello', { version: '1.2.3' }))
t.ok(findMyWay.find('GET', '/test/1234/hello', { version: '3.x' }))
t.ok(findMyWay.find('GET', '/test/1234/hello', { version: '3.2.0' }))
t.ok(findMyWay.find('GET', '/test/name/hello', { version: '4.x' }))
t.ok(findMyWay.find('GET', '/test/name/hello', { version: '3.x' }))
t.notOk(findMyWay.find('GET', '/test/1234/hello', { version: '2.x' }))
t.notOk(findMyWay.find('GET', '/test/1234/hello', { version: '2.3.4' }))
t.notOk(findMyWay.find('GET', '/test/1234/hello', { version: '3.2.1' }))
t.notOk(findMyWay.find('GET', '/test/1234/hello', { version: '4.x' }))
})
test('A route could support multiple versions (find) / 4', t => {
t.plan(8)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test/*', { constraints: { version: '1.2.3' } }, noop)
findMyWay.on('GET', '/test/hello', { constraints: { version: '3.2.0' } }, noop)
t.ok(findMyWay.find('GET', '/test/1234/hello', { version: '1.x' }))
t.ok(findMyWay.find('GET', '/test/1234/hello', { version: '1.2.3' }))
t.ok(findMyWay.find('GET', '/test/hello', { version: '3.x' }))
t.ok(findMyWay.find('GET', '/test/hello', { version: '3.2.0' }))
t.notOk(findMyWay.find('GET', '/test/1234/hello', { version: '3.2.0' }))
t.notOk(findMyWay.find('GET', '/test/1234/hello', { version: '3.x' }))
t.notOk(findMyWay.find('GET', '/test/1234/hello', { version: '2.x' }))
t.notOk(findMyWay.find('GET', '/test/hello', { version: '2.x' }))
})
test('A route could support multiple versions (find) / 5', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/', { constraints: { version: '1.2.3' } }, () => false)
findMyWay.on('GET', '/', { constraints: { version: '3.2.0' } }, () => true)
t.ok(findMyWay.find('GET', '/', { version: '*' }).handler())
})
test('Find with a version but without versioned routes', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/', noop)
t.notOk(findMyWay.find('GET', '/', { version: '1.x' }))
})
test('A route could support multiple versions (lookup)', t => {
t.plan(7)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
const versions = ['2.x', '2.3.4', '3.2.1']
t.ok(versions.indexOf(req.headers['accept-version']) > -1)
}
})
findMyWay.on('GET', '/', { constraints: { version: '1.2.3' } }, (req, res) => {
const versions = ['1.x', '1.2.3']
t.ok(versions.indexOf(req.headers['accept-version']) > -1)
})
findMyWay.on('GET', '/', { constraints: { version: '3.2.0' } }, (req, res) => {
const versions = ['3.x', '3.2.0']
t.ok(versions.indexOf(req.headers['accept-version']) > -1)
})
findMyWay.lookup({
method: 'GET',
url: '/',
headers: { 'accept-version': '1.x' }
}, null)
findMyWay.lookup({
method: 'GET',
url: '/',
headers: { 'accept-version': '1.2.3' }
}, null)
findMyWay.lookup({
method: 'GET',
url: '/',
headers: { 'accept-version': '3.x' }
}, null)
findMyWay.lookup({
method: 'GET',
url: '/',
headers: { 'accept-version': '3.2.0' }
}, null)
findMyWay.lookup({
method: 'GET',
url: '/',
headers: { 'accept-version': '2.x' }
}, null)
findMyWay.lookup({
method: 'GET',
url: '/',
headers: { 'accept-version': '2.3.4' }
}, null)
findMyWay.lookup({
method: 'GET',
url: '/',
headers: { 'accept-version': '3.2.1' }
}, null)
})
test('It should always choose the highest version of a route', t => {
t.plan(3)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/', { constraints: { version: '2.3.0' } }, (req, res) => {
t.fail('We should not be here')
})
findMyWay.on('GET', '/', { constraints: { version: '2.4.0' } }, (req, res) => {
t.pass('Yeah!')
})
findMyWay.on('GET', '/', { constraints: { version: '3.3.0' } }, (req, res) => {
t.pass('Yeah!')
})
findMyWay.on('GET', '/', { constraints: { version: '3.2.0' } }, (req, res) => {
t.fail('We should not be here')
})
findMyWay.on('GET', '/', { constraints: { version: '3.2.2' } }, (req, res) => {
t.fail('We should not be here')
})
findMyWay.on('GET', '/', { constraints: { version: '4.4.0' } }, (req, res) => {
t.fail('We should not be here')
})
findMyWay.on('GET', '/', { constraints: { version: '4.3.2' } }, (req, res) => {
t.pass('Yeah!')
})
findMyWay.lookup({
method: 'GET',
url: '/',
headers: { 'accept-version': '2.x' }
}, null)
findMyWay.lookup({
method: 'GET',
url: '/',
headers: { 'accept-version': '3.x' }
}, null)
findMyWay.lookup({
method: 'GET',
url: '/',
headers: { 'accept-version': '4.3.x' }
}, null)
})
test('Declare the same route with and without version', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/', noop)
findMyWay.on('GET', '/', { constraints: { version: '1.2.0' } }, noop)
t.ok(findMyWay.find('GET', '/', { version: '1.x' }))
t.ok(findMyWay.find('GET', '/', {}))
})
test('It should throw if you declare multiple times the same route', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/', { constraints: { version: '1.2.3' } }, noop)
try {
findMyWay.on('GET', '/', { constraints: { version: '1.2.3' } }, noop)
t.fail('It should throw')
} catch (err) {
t.equal(err.message, 'Method \'GET\' already declared for route \'/\' with constraints \'{"version":"1.2.3"}\'')
}
})
test('Versioning won\'t work if there are no versioned routes', t => {
t.plan(2)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('We should not be here')
}
})
findMyWay.on('GET', '/', (req, res) => {
t.pass('Yeah!')
})
findMyWay.lookup({
method: 'GET',
url: '/',
headers: { 'accept-version': '2.x' }
}, null)
findMyWay.lookup({
method: 'GET',
url: '/'
}, null)
})
test('Unversioned routes aren\'t triggered when unknown versions are requested', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.ok('We should be here')
}
})
findMyWay.on('GET', '/', (req, res) => {
t.fail('unversioned route shouldnt be hit!')
})
findMyWay.on('GET', '/', { constraints: { version: '1.0.0' } }, (req, res) => {
t.fail('versioned route shouldnt be hit for wrong version!')
})
findMyWay.lookup({
method: 'GET',
url: '/',
headers: { 'accept-version': '2.x' }
}, null)
})

105
node_modules/find-my-way/test/constraint.host.test.js generated vendored Normal file
View File

@@ -0,0 +1,105 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('..')
const alpha = () => { }
const beta = () => { }
const gamma = () => { }
test('A route supports multiple host constraints', t => {
t.plan(4)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/', {}, alpha)
findMyWay.on('GET', '/', { constraints: { host: 'fastify.io' } }, beta)
findMyWay.on('GET', '/', { constraints: { host: 'example.com' } }, gamma)
t.equal(findMyWay.find('GET', '/', {}).handler, alpha)
t.equal(findMyWay.find('GET', '/', { host: 'something-else.io' }).handler, alpha)
t.equal(findMyWay.find('GET', '/', { host: 'fastify.io' }).handler, beta)
t.equal(findMyWay.find('GET', '/', { host: 'example.com' }).handler, gamma)
})
test('A route supports wildcard host constraints', t => {
t.plan(4)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/', { constraints: { host: 'fastify.io' } }, beta)
findMyWay.on('GET', '/', { constraints: { host: /.*\.fastify\.io/ } }, gamma)
t.equal(findMyWay.find('GET', '/', { host: 'fastify.io' }).handler, beta)
t.equal(findMyWay.find('GET', '/', { host: 'foo.fastify.io' }).handler, gamma)
t.equal(findMyWay.find('GET', '/', { host: 'bar.fastify.io' }).handler, gamma)
t.notOk(findMyWay.find('GET', '/', { host: 'example.com' }))
})
test('A route supports multiple host constraints (lookup)', t => {
t.plan(4)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/', {}, (req, res) => {})
findMyWay.on('GET', '/', { constraints: { host: 'fastify.io' } }, (req, res) => {
t.equal(req.headers.host, 'fastify.io')
})
findMyWay.on('GET', '/', { constraints: { host: 'example.com' } }, (req, res) => {
t.equal(req.headers.host, 'example.com')
})
findMyWay.on('GET', '/', { constraints: { host: /.+\.fancy\.ca/ } }, (req, res) => {
t.ok(req.headers.host.endsWith('.fancy.ca'))
})
findMyWay.lookup({
method: 'GET',
url: '/',
headers: { host: 'fastify.io' }
})
findMyWay.lookup({
method: 'GET',
url: '/',
headers: { host: 'example.com' }
})
findMyWay.lookup({
method: 'GET',
url: '/',
headers: { host: 'foo.fancy.ca' }
})
findMyWay.lookup({
method: 'GET',
url: '/',
headers: { host: 'bar.fancy.ca' }
})
})
test('A route supports up to 31 host constraints', (t) => {
t.plan(1)
const findMyWay = FindMyWay()
for (let i = 0; i < 31; i++) {
const host = `h${i.toString().padStart(2, '0')}`
findMyWay.on('GET', '/', { constraints: { host } }, alpha)
}
t.equal(findMyWay.find('GET', '/', { host: 'h01' }).handler, alpha)
})
test('A route throws when constraint limit exceeded', (t) => {
t.plan(1)
const findMyWay = FindMyWay()
for (let i = 0; i < 31; i++) {
const host = `h${i.toString().padStart(2, '0')}`
findMyWay.on('GET', '/', { constraints: { host } }, alpha)
}
t.throws(
() => findMyWay.on('GET', '/', { constraints: { host: 'h31' } }, beta),
'find-my-way supports a maximum of 31 route handlers per node when there are constraints, limit reached'
)
})

109
node_modules/find-my-way/test/constraints.test.js generated vendored Normal file
View File

@@ -0,0 +1,109 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('..')
const alpha = () => { }
const beta = () => { }
const gamma = () => { }
test('A route could support multiple host constraints while versioned', t => {
t.plan(6)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/', { constraints: { host: 'fastify.io', version: '1.1.0' } }, beta)
findMyWay.on('GET', '/', { constraints: { host: 'fastify.io', version: '2.1.0' } }, gamma)
t.equal(findMyWay.find('GET', '/', { host: 'fastify.io', version: '1.x' }).handler, beta)
t.equal(findMyWay.find('GET', '/', { host: 'fastify.io', version: '1.1.x' }).handler, beta)
t.equal(findMyWay.find('GET', '/', { host: 'fastify.io', version: '2.x' }).handler, gamma)
t.equal(findMyWay.find('GET', '/', { host: 'fastify.io', version: '2.1.x' }).handler, gamma)
t.notOk(findMyWay.find('GET', '/', { host: 'fastify.io', version: '3.x' }))
t.notOk(findMyWay.find('GET', '/', { host: 'something-else.io', version: '1.x' }))
})
test('Constrained routes are matched before unconstrainted routes when the constrained route is added last', t => {
t.plan(3)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/', {}, alpha)
findMyWay.on('GET', '/', { constraints: { host: 'fastify.io' } }, beta)
t.equal(findMyWay.find('GET', '/', {}).handler, alpha)
t.equal(findMyWay.find('GET', '/', { host: 'fastify.io' }).handler, beta)
t.equal(findMyWay.find('GET', '/', { host: 'example.com' }).handler, alpha)
})
test('Constrained routes are matched before unconstrainted routes when the constrained route is added first', t => {
t.plan(3)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/', { constraints: { host: 'fastify.io' } }, beta)
findMyWay.on('GET', '/', {}, alpha)
t.equal(findMyWay.find('GET', '/', {}).handler, alpha)
t.equal(findMyWay.find('GET', '/', { host: 'fastify.io' }).handler, beta)
t.equal(findMyWay.find('GET', '/', { host: 'example.com' }).handler, alpha)
})
test('Routes with multiple constraints are matched before routes with one constraint when the doubly-constrained route is added last', t => {
t.plan(3)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/', { constraints: { host: 'fastify.io' } }, alpha)
findMyWay.on('GET', '/', { constraints: { host: 'fastify.io', version: '1.0.0' } }, beta)
t.equal(findMyWay.find('GET', '/', { host: 'fastify.io' }).handler, alpha)
t.equal(findMyWay.find('GET', '/', { host: 'fastify.io', version: '1.0.0' }).handler, beta)
t.equal(findMyWay.find('GET', '/', { host: 'fastify.io', version: '2.0.0' }), null)
})
test('Routes with multiple constraints are matched before routes with one constraint when the doubly-constrained route is added first', t => {
t.plan(3)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/', { constraints: { host: 'fastify.io', version: '1.0.0' } }, beta)
findMyWay.on('GET', '/', { constraints: { host: 'fastify.io' } }, alpha)
t.equal(findMyWay.find('GET', '/', { host: 'fastify.io' }).handler, alpha)
t.equal(findMyWay.find('GET', '/', { host: 'fastify.io', version: '1.0.0' }).handler, beta)
t.equal(findMyWay.find('GET', '/', { host: 'fastify.io', version: '2.0.0' }), null)
})
test('Routes with multiple constraints are matched before routes with one constraint before unconstrained routes', t => {
t.plan(3)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/', { constraints: { host: 'fastify.io', version: '1.0.0' } }, beta)
findMyWay.on('GET', '/', { constraints: { host: 'fastify.io' } }, alpha)
findMyWay.on('GET', '/', { constraints: {} }, gamma)
t.equal(findMyWay.find('GET', '/', { host: 'fastify.io', version: '1.0.0' }).handler, beta)
t.equal(findMyWay.find('GET', '/', { host: 'fastify.io', version: '2.0.0' }), null)
t.equal(findMyWay.find('GET', '/', { host: 'example.io' }).handler, gamma)
})
test('Has constraint strategy method test', t => {
t.plan(6)
const findMyWay = FindMyWay()
t.same(findMyWay.hasConstraintStrategy('version'), false)
t.same(findMyWay.hasConstraintStrategy('host'), false)
findMyWay.on('GET', '/', { constraints: { host: 'fastify.io' } }, () => {})
t.same(findMyWay.hasConstraintStrategy('version'), false)
t.same(findMyWay.hasConstraintStrategy('host'), true)
findMyWay.on('GET', '/', { constraints: { host: 'fastify.io', version: '1.0.0' } }, () => {})
t.same(findMyWay.hasConstraintStrategy('version'), true)
t.same(findMyWay.hasConstraintStrategy('host'), true)
})

View File

@@ -0,0 +1,47 @@
'use strict'
const t = require('tap')
const test = t.test
const querystring = require('fast-querystring')
const FindMyWay = require('../')
test('Custom querystring parser', t => {
t.plan(2)
const findMyWay = FindMyWay({
querystringParser: function (str) {
t.equal(str, 'foo=bar&baz=faz')
return querystring.parse(str)
}
})
findMyWay.on('GET', '/', () => {})
t.same(findMyWay.find('GET', '/?foo=bar&baz=faz').searchParams, { foo: 'bar', baz: 'faz' })
})
test('Custom querystring parser should be called also if there is nothing to parse', t => {
t.plan(2)
const findMyWay = FindMyWay({
querystringParser: function (str) {
t.equal(str, '')
return querystring.parse(str)
}
})
findMyWay.on('GET', '/', () => {})
t.same(findMyWay.find('GET', '/').searchParams, {})
})
test('Querystring without value', t => {
t.plan(2)
const findMyWay = FindMyWay({
querystringParser: function (str) {
t.equal(str, 'foo')
return querystring.parse(str)
}
})
findMyWay.on('GET', '/', () => {})
t.same(findMyWay.find('GET', '/?foo').searchParams, { foo: '' })
})

493
node_modules/find-my-way/test/errors.test.js generated vendored Normal file
View File

@@ -0,0 +1,493 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('../')
test('Method should be a string', t => {
t.plan(1)
const findMyWay = FindMyWay()
try {
findMyWay.on(0, '/test', () => {})
t.fail('method shoukd be a string')
} catch (e) {
t.equal(e.message, 'Method should be a string')
}
})
test('Method should be a string [ignoreTrailingSlash=true]', t => {
t.plan(1)
const findMyWay = FindMyWay({ ignoreTrailingSlash: true })
try {
findMyWay.on(0, '/test', () => {})
t.fail('method shoukd be a string')
} catch (e) {
t.equal(e.message, 'Method should be a string')
}
})
test('Method should be a string [ignoreDuplicateSlashes=true]', t => {
t.plan(1)
const findMyWay = FindMyWay({ ignoreDuplicateSlashes: true })
try {
findMyWay.on(0, '/test', () => {})
t.fail('method shoukd be a string')
} catch (e) {
t.equal(e.message, 'Method should be a string')
}
})
test('Method should be a string (array)', t => {
t.plan(1)
const findMyWay = FindMyWay()
try {
findMyWay.on(['GET', 0], '/test', () => {})
t.fail('method shoukd be a string')
} catch (e) {
t.equal(e.message, 'Method should be a string')
}
})
test('Method should be a string (array) [ignoreTrailingSlash=true]', t => {
t.plan(1)
const findMyWay = FindMyWay({ ignoreTrailingSlash: true })
try {
findMyWay.on(['GET', 0], '/test', () => {})
t.fail('method shoukd be a string')
} catch (e) {
t.equal(e.message, 'Method should be a string')
}
})
test('Method should be a string (array) [ignoreDuplicateSlashes=true]', t => {
t.plan(1)
const findMyWay = FindMyWay({ ignoreDuplicateSlashes: true })
try {
findMyWay.on(['GET', 0], '/test', () => {})
t.fail('method shoukd be a string')
} catch (e) {
t.equal(e.message, 'Method should be a string')
}
})
test('Path should be a string', t => {
t.plan(1)
const findMyWay = FindMyWay()
try {
findMyWay.on('GET', 0, () => {})
t.fail('path should be a string')
} catch (e) {
t.equal(e.message, 'Path should be a string')
}
})
test('Path should be a string [ignoreTrailingSlash=true]', t => {
t.plan(1)
const findMyWay = FindMyWay({ ignoreTrailingSlash: true })
try {
findMyWay.on('GET', 0, () => {})
t.fail('path should be a string')
} catch (e) {
t.equal(e.message, 'Path should be a string')
}
})
test('Path should be a string [ignoreDuplicateSlashes=true]', t => {
t.plan(1)
const findMyWay = FindMyWay({ ignoreDuplicateSlashes: true })
try {
findMyWay.on('GET', 0, () => {})
t.fail('path should be a string')
} catch (e) {
t.equal(e.message, 'Path should be a string')
}
})
test('The path could not be empty', t => {
t.plan(1)
const findMyWay = FindMyWay()
try {
findMyWay.on('GET', '', () => {})
t.fail('The path could not be empty')
} catch (e) {
t.equal(e.message, 'The path could not be empty')
}
})
test('The path could not be empty [ignoreTrailingSlash=true]', t => {
t.plan(1)
const findMyWay = FindMyWay({ ignoreTrailingSlash: true })
try {
findMyWay.on('GET', '', () => {})
t.fail('The path could not be empty')
} catch (e) {
t.equal(e.message, 'The path could not be empty')
}
})
test('The path could not be empty [ignoreDuplicateSlashes=true]', t => {
t.plan(1)
const findMyWay = FindMyWay({ ignoreDuplicateSlashes: true })
try {
findMyWay.on('GET', '', () => {})
t.fail('The path could not be empty')
} catch (e) {
t.equal(e.message, 'The path could not be empty')
}
})
test('The first character of a path should be `/` or `*`', t => {
t.plan(1)
const findMyWay = FindMyWay()
try {
findMyWay.on('GET', 'a', () => {})
t.fail('The first character of a path should be `/` or `*`')
} catch (e) {
t.equal(e.message, 'The first character of a path should be `/` or `*`')
}
})
test('The first character of a path should be `/` or `*` [ignoreTrailingSlash=true]', t => {
t.plan(1)
const findMyWay = FindMyWay({ ignoreTrailingSlash: true })
try {
findMyWay.on('GET', 'a', () => {})
t.fail('The first character of a path should be `/` or `*`')
} catch (e) {
t.equal(e.message, 'The first character of a path should be `/` or `*`')
}
})
test('The first character of a path should be `/` or `*` [ignoreDuplicateSlashes=true]', t => {
t.plan(1)
const findMyWay = FindMyWay({ ignoreDuplicateSlashes: true })
try {
findMyWay.on('GET', 'a', () => {})
t.fail('The first character of a path should be `/` or `*`')
} catch (e) {
t.equal(e.message, 'The first character of a path should be `/` or `*`')
}
})
test('Handler should be a function', t => {
t.plan(1)
const findMyWay = FindMyWay()
try {
findMyWay.on('GET', '/test', 0)
t.fail('handler should be a function')
} catch (e) {
t.equal(e.message, 'Handler should be a function')
}
})
test('Method is not an http method.', t => {
t.plan(1)
const findMyWay = FindMyWay()
try {
findMyWay.on('GETT', '/test', () => {})
t.fail('method is not a valid http method')
} catch (e) {
t.equal(e.message, 'Method \'GETT\' is not an http method.')
}
})
test('Method is not an http method. (array)', t => {
t.plan(1)
const findMyWay = FindMyWay()
try {
findMyWay.on(['POST', 'GETT'], '/test', () => {})
t.fail('method is not a valid http method')
} catch (e) {
t.equal(e.message, 'Method \'GETT\' is not an http method.')
}
})
test('The default route must be a function', t => {
t.plan(1)
try {
FindMyWay({
defaultRoute: '/404'
})
t.fail('default route must be a function')
} catch (e) {
t.equal(e.message, 'The default route must be a function')
}
})
test('Method already declared', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test', () => {})
try {
findMyWay.on('GET', '/test', () => {})
t.fail('method already declared')
} catch (e) {
t.equal(e.message, 'Method \'GET\' already declared for route \'/test\' with constraints \'{}\'')
}
})
test('Method already declared if * is used', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/*', () => {})
try {
findMyWay.on('GET', '*', () => {})
t.fail('should throw error')
} catch (e) {
t.equal(e.message, 'Method \'GET\' already declared for route \'/*\' with constraints \'{}\'')
}
})
test('Method already declared if /* is used', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '*', () => {})
try {
findMyWay.on('GET', '/*', () => {})
t.fail('should throw error')
} catch (e) {
t.equal(e.message, 'Method \'GET\' already declared for route \'/*\' with constraints \'{}\'')
}
})
test('Method already declared [ignoreTrailingSlash=true]', t => {
t.plan(2)
t.test('without trailing slash', t => {
t.plan(2)
const findMyWay = FindMyWay({ ignoreTrailingSlash: true })
findMyWay.on('GET', '/test', () => {})
try {
findMyWay.on('GET', '/test', () => {})
t.fail('method already declared')
} catch (e) {
t.equal(e.message, 'Method \'GET\' already declared for route \'/test\' with constraints \'{}\'')
}
try {
findMyWay.on('GET', '/test/', () => {})
t.fail('method already declared')
} catch (e) {
t.equal(e.message, 'Method \'GET\' already declared for route \'/test\' with constraints \'{}\'')
}
})
t.test('with trailing slash', t => {
t.plan(2)
const findMyWay = FindMyWay({ ignoreTrailingSlash: true })
findMyWay.on('GET', '/test/', () => {})
try {
findMyWay.on('GET', '/test', () => {})
t.fail('method already declared')
} catch (e) {
t.equal(e.message, 'Method \'GET\' already declared for route \'/test\' with constraints \'{}\'')
}
try {
findMyWay.on('GET', '/test/', () => {})
t.fail('method already declared')
} catch (e) {
t.equal(e.message, 'Method \'GET\' already declared for route \'/test\' with constraints \'{}\'')
}
})
})
test('Method already declared [ignoreDuplicateSlashes=true]', t => {
t.plan(2)
t.test('without duplicate slashes', t => {
t.plan(2)
const findMyWay = FindMyWay({ ignoreDuplicateSlashes: true })
findMyWay.on('GET', '/test', () => {})
try {
findMyWay.on('GET', '/test', () => {})
t.fail('method already declared')
} catch (e) {
t.equal(e.message, 'Method \'GET\' already declared for route \'/test\' with constraints \'{}\'')
}
try {
findMyWay.on('GET', '//test', () => {})
t.fail('method already declared')
} catch (e) {
t.equal(e.message, 'Method \'GET\' already declared for route \'/test\' with constraints \'{}\'')
}
})
t.test('with duplicate slashes', t => {
t.plan(2)
const findMyWay = FindMyWay({ ignoreDuplicateSlashes: true })
findMyWay.on('GET', '//test', () => {})
try {
findMyWay.on('GET', '/test', () => {})
t.fail('method already declared')
} catch (e) {
t.equal(e.message, 'Method \'GET\' already declared for route \'/test\' with constraints \'{}\'')
}
try {
findMyWay.on('GET', '//test', () => {})
t.fail('method already declared')
} catch (e) {
t.equal(e.message, 'Method \'GET\' already declared for route \'/test\' with constraints \'{}\'')
}
})
})
test('Method already declared nested route', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test', () => {})
findMyWay.on('GET', '/test/hello', () => {})
findMyWay.on('GET', '/test/world', () => {})
try {
findMyWay.on('GET', '/test/hello', () => {})
t.fail('method already delcared in nested route')
} catch (e) {
t.equal(e.message, 'Method \'GET\' already declared for route \'/test/hello\' with constraints \'{}\'')
}
})
test('Method already declared nested route [ignoreTrailingSlash=true]', t => {
t.plan(2)
t.test('without trailing slash', t => {
t.plan(2)
const findMyWay = FindMyWay({ ignoreTrailingSlash: true })
findMyWay.on('GET', '/test', () => {})
findMyWay.on('GET', '/test/hello', () => {})
findMyWay.on('GET', '/test/world', () => {})
try {
findMyWay.on('GET', '/test/hello', () => {})
t.fail('method already declared')
} catch (e) {
t.equal(e.message, 'Method \'GET\' already declared for route \'/test/hello\' with constraints \'{}\'')
}
try {
findMyWay.on('GET', '/test/hello/', () => {})
t.fail('method already declared')
} catch (e) {
t.equal(e.message, 'Method \'GET\' already declared for route \'/test/hello\' with constraints \'{}\'')
}
})
test('Method already declared with constraints', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test', { constraints: { host: 'fastify.io' } }, () => {})
try {
findMyWay.on('GET', '/test', { constraints: { host: 'fastify.io' } }, () => {})
t.fail('method already declared')
} catch (e) {
t.equal(e.message, 'Method \'GET\' already declared for route \'/test\' with constraints \'{"host":"fastify.io"}\'')
}
})
t.test('with trailing slash', t => {
t.plan(2)
const findMyWay = FindMyWay({ ignoreTrailingSlash: true })
findMyWay.on('GET', '/test/', () => {})
findMyWay.on('GET', '/test/hello/', () => {})
findMyWay.on('GET', '/test/world/', () => {})
try {
findMyWay.on('GET', '/test/hello', () => {})
t.fail('method already declared')
} catch (e) {
t.equal(e.message, 'Method \'GET\' already declared for route \'/test/hello\' with constraints \'{}\'')
}
try {
findMyWay.on('GET', '/test/hello/', () => {})
t.fail('method already declared')
} catch (e) {
t.equal(e.message, 'Method \'GET\' already declared for route \'/test/hello\' with constraints \'{}\'')
}
})
})
test('Method already declared nested route [ignoreDuplicateSlashes=true]', t => {
t.plan(2)
t.test('without duplicate slashes', t => {
t.plan(2)
const findMyWay = FindMyWay({ ignoreDuplicateSlashes: true })
findMyWay.on('GET', '/test', () => {})
findMyWay.on('GET', '/test/hello', () => {})
findMyWay.on('GET', '/test/world', () => {})
try {
findMyWay.on('GET', '/test/hello', () => {})
t.fail('method already declared')
} catch (e) {
t.equal(e.message, 'Method \'GET\' already declared for route \'/test/hello\' with constraints \'{}\'')
}
try {
findMyWay.on('GET', '/test//hello', () => {})
t.fail('method already declared')
} catch (e) {
t.equal(e.message, 'Method \'GET\' already declared for route \'/test/hello\' with constraints \'{}\'')
}
})
t.test('with duplicate slashes', t => {
t.plan(2)
const findMyWay = FindMyWay({ ignoreDuplicateSlashes: true })
findMyWay.on('GET', '/test/', () => {})
findMyWay.on('GET', '/test//hello', () => {})
findMyWay.on('GET', '/test//world', () => {})
try {
findMyWay.on('GET', '/test/hello', () => {})
t.fail('method already declared')
} catch (e) {
t.equal(e.message, 'Method \'GET\' already declared for route \'/test/hello\' with constraints \'{}\'')
}
try {
findMyWay.on('GET', '/test//hello', () => {})
t.fail('method already declared')
} catch (e) {
t.equal(e.message, 'Method \'GET\' already declared for route \'/test/hello\' with constraints \'{}\'')
}
})
})

View File

@@ -0,0 +1,35 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('../')
test('contain param and wildcard together', t => {
t.plan(4)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('we should not be here, the url is: ' + req.url)
}
})
findMyWay.on('GET', '/:lang/item/:id', (req, res, params) => {
t.same(params.lang, 'fr')
t.same(params.id, '12345')
})
findMyWay.on('GET', '/:lang/item/*', (req, res, params) => {
t.same(params.lang, 'fr')
t.same(params['*'], '12345/edit')
})
findMyWay.lookup(
{ method: 'GET', url: '/fr/item/12345', headers: {} },
null
)
findMyWay.lookup(
{ method: 'GET', url: '/fr/item/12345/edit', headers: {} },
null
)
})

View File

@@ -0,0 +1,24 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('../')
test('wildcard should not limit by maxParamLength', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('we should not be here, the url is: ' + req.url)
}
})
findMyWay.on('GET', '*', (req, res, params) => {
t.same(params['*'], '/portfolios/b5859fb9-6c76-4db8-b3d1-337c5be3fd8b/instruments/2a694406-b43f-439d-aa11-0c814805c930/positions')
})
findMyWay.lookup(
{ method: 'GET', url: '/portfolios/b5859fb9-6c76-4db8-b3d1-337c5be3fd8b/instruments/2a694406-b43f-439d-aa11-0c814805c930/positions', headers: {} },
null
)
})

276
node_modules/find-my-way/test/find-route.test.js generated vendored Normal file
View File

@@ -0,0 +1,276 @@
'use strict'
const t = require('tap')
const test = t.test
const rfdc = require('rfdc')({ proto: true })
const FindMyWay = require('..')
function equalRouters (t, router1, router2) {
t.strictSame(router1._opts, router2._opts)
t.same(router1.routes, router2.routes)
t.same(router1.trees, router2.trees)
t.strictSame(router1.constrainer.strategies, router2.constrainer.strategies)
t.strictSame(
router1.constrainer.strategiesInUse,
router2.constrainer.strategiesInUse
)
t.strictSame(
router1.constrainer.asyncStrategiesInUse,
router2.constrainer.asyncStrategiesInUse
)
}
test('findRoute returns null if there is no routes', (t) => {
t.plan(7)
const findMyWay = FindMyWay()
const fundMyWayClone = rfdc(findMyWay)
const route = findMyWay.findRoute('GET', '/example')
t.equal(route, null)
equalRouters(t, findMyWay, fundMyWayClone)
})
test('findRoute returns handler and store for a static route', (t) => {
t.plan(9)
const findMyWay = FindMyWay()
const handler = () => {}
const store = { hello: 'world' }
findMyWay.on('GET', '/example', handler, store)
const fundMyWayClone = rfdc(findMyWay)
const route = findMyWay.findRoute('GET', '/example')
t.equal(route.handler, handler)
t.equal(route.store, store)
t.deepEqual(route.params, [])
equalRouters(t, findMyWay, fundMyWayClone)
})
test('findRoute returns null for a static route', (t) => {
t.plan(7)
const findMyWay = FindMyWay()
const handler = () => {}
findMyWay.on('GET', '/example', handler)
const fundMyWayClone = rfdc(findMyWay)
const route = findMyWay.findRoute('GET', '/example1')
t.equal(route, null)
equalRouters(t, findMyWay, fundMyWayClone)
})
test('findRoute returns handler and params for a parametric route', (t) => {
t.plan(8)
const findMyWay = FindMyWay()
const handler = () => {}
findMyWay.on('GET', '/:param', handler)
const fundMyWayClone = rfdc(findMyWay)
const route = findMyWay.findRoute('GET', '/:param')
t.equal(route.handler, handler)
t.deepEqual(route.params, ['param'])
equalRouters(t, findMyWay, fundMyWayClone)
})
test('findRoute returns null for a parametric route', (t) => {
t.plan(7)
const findMyWay = FindMyWay()
const handler = () => {}
findMyWay.on('GET', '/foo/:param', handler)
const fundMyWayClone = rfdc(findMyWay)
const route = findMyWay.findRoute('GET', '/bar/:param')
t.equal(route, null)
equalRouters(t, findMyWay, fundMyWayClone)
})
test('findRoute returns handler and params for a parametric route with static suffix', (t) => {
t.plan(8)
const findMyWay = FindMyWay()
const handler = () => {}
findMyWay.on('GET', '/:param-static', handler)
const fundMyWayClone = rfdc(findMyWay)
const route = findMyWay.findRoute('GET', '/:param-static')
t.equal(route.handler, handler)
t.deepEqual(route.params, ['param'])
equalRouters(t, findMyWay, fundMyWayClone)
})
test('findRoute returns null for a parametric route with static suffix', (t) => {
t.plan(7)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/:param-static1', () => {})
const fundMyWayClone = rfdc(findMyWay)
const route = findMyWay.findRoute('GET', '/:param-static2')
t.equal(route, null)
equalRouters(t, findMyWay, fundMyWayClone)
})
test('findRoute returns handler and original params even if a param name different', (t) => {
t.plan(8)
const findMyWay = FindMyWay()
const handler = () => {}
findMyWay.on('GET', '/:param1', handler)
const fundMyWayClone = rfdc(findMyWay)
const route = findMyWay.findRoute('GET', '/:param2')
t.equal(route.handler, handler)
t.deepEqual(route.params, ['param1'])
equalRouters(t, findMyWay, fundMyWayClone)
})
test('findRoute returns handler and params for a multi-parametric route', (t) => {
t.plan(8)
const findMyWay = FindMyWay()
const handler = () => {}
findMyWay.on('GET', '/:param1-:param2', handler)
const fundMyWayClone = rfdc(findMyWay)
const route = findMyWay.findRoute('GET', '/:param1-:param2')
t.equal(route.handler, handler)
t.deepEqual(route.params, ['param1', 'param2'])
equalRouters(t, findMyWay, fundMyWayClone)
})
test('findRoute returns null for a multi-parametric route', (t) => {
t.plan(7)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/foo/:param1-:param2/bar1', () => {})
const fundMyWayClone = rfdc(findMyWay)
const route = findMyWay.findRoute('GET', '/foo/:param1-:param2/bar2')
t.equal(route, null)
equalRouters(t, findMyWay, fundMyWayClone)
})
test('findRoute returns handler and regexp param for a regexp route', (t) => {
t.plan(8)
const findMyWay = FindMyWay()
const handler = () => {}
findMyWay.on('GET', '/:param(^\\d+$)', handler)
const fundMyWayClone = rfdc(findMyWay)
const route = findMyWay.findRoute('GET', '/:param(^\\d+$)')
t.equal(route.handler, handler)
t.deepEqual(route.params, ['param'])
equalRouters(t, findMyWay, fundMyWayClone)
})
test('findRoute returns null for a regexp route', (t) => {
t.plan(7)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/:file(^\\S+).png', () => {})
const fundMyWayClone = rfdc(findMyWay)
const route = findMyWay.findRoute('GET', '/:file(^\\D+).png')
t.equal(route, null)
equalRouters(t, findMyWay, fundMyWayClone)
})
test('findRoute returns handler and wildcard param for a wildcard route', (t) => {
t.plan(8)
const findMyWay = FindMyWay()
const handler = () => {}
findMyWay.on('GET', '/example/*', handler)
const fundMyWayClone = rfdc(findMyWay)
const route = findMyWay.findRoute('GET', '/example/*')
t.equal(route.handler, handler)
t.deepEqual(route.params, ['*'])
equalRouters(t, findMyWay, fundMyWayClone)
})
test('findRoute returns null for a wildcard route', (t) => {
t.plan(7)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/foo1/*', () => {})
const fundMyWayClone = rfdc(findMyWay)
const route = findMyWay.findRoute('GET', '/foo2/*')
t.equal(route, null)
equalRouters(t, findMyWay, fundMyWayClone)
})
test('findRoute returns handler for a constrained route', (t) => {
t.plan(9)
const findMyWay = FindMyWay()
const handler = () => {}
findMyWay.on(
'GET',
'/example',
{ constraints: { version: '1.0.0' } },
handler
)
const fundMyWayClone = rfdc(findMyWay)
{
const route = findMyWay.findRoute('GET', '/example')
t.equal(route, null)
}
{
const route = findMyWay.findRoute('GET', '/example', { version: '1.0.0' })
t.equal(route.handler, handler)
}
{
const route = findMyWay.findRoute('GET', '/example', { version: '2.0.0' })
t.equal(route, null)
}
equalRouters(t, findMyWay, fundMyWayClone)
})

17
node_modules/find-my-way/test/find.test.js generated vendored Normal file
View File

@@ -0,0 +1,17 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('..')
test('find calls can pass no constraints', t => {
t.plan(3)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/a', () => {})
findMyWay.on('GET', '/a/b', () => {})
t.ok(findMyWay.find('GET', '/a'))
t.ok(findMyWay.find('GET', '/a/b'))
t.notOk(findMyWay.find('GET', '/a/b/c'))
})

11
node_modules/find-my-way/test/for-in-loop.test.js generated vendored Normal file
View File

@@ -0,0 +1,11 @@
'use strict'
/* eslint no-extend-native: off */
const t = require('tap')
// Something could extend the Array prototype
Array.prototype.test = null
t.doesNotThrow(() => {
require('../')
})

28
node_modules/find-my-way/test/full-url.test.js generated vendored Normal file
View File

@@ -0,0 +1,28 @@
'use strict'
const t = require('tap')
const FindMyWay = require('../')
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/a', (req, res) => {
res.end('{"message":"hello world"}')
})
findMyWay.on('GET', '/a/:id', (req, res) => {
res.end('{"message":"hello world"}')
})
t.same(findMyWay.find('GET', 'http://localhost/a', { host: 'localhost' }), findMyWay.find('GET', '/a', { host: 'localhost' }))
t.same(findMyWay.find('GET', 'http://localhost:8080/a', { host: 'localhost' }), findMyWay.find('GET', '/a', { host: 'localhost' }))
t.same(findMyWay.find('GET', 'http://123.123.123.123/a', {}), findMyWay.find('GET', '/a', {}))
t.same(findMyWay.find('GET', 'https://localhost/a', { host: 'localhost' }), findMyWay.find('GET', '/a', { host: 'localhost' }))
t.same(findMyWay.find('GET', 'http://localhost/a/100', { host: 'localhost' }), findMyWay.find('GET', '/a/100', { host: 'localhost' }))
t.same(findMyWay.find('GET', 'http://localhost:8080/a/100', { host: 'localhost' }), findMyWay.find('GET', '/a/100', { host: 'localhost' }))
t.same(findMyWay.find('GET', 'http://123.123.123.123/a/100', {}), findMyWay.find('GET', '/a/100', {}))
t.same(findMyWay.find('GET', 'https://localhost/a/100', { host: 'localhost' }), findMyWay.find('GET', '/a/100', { host: 'localhost' }))

219
node_modules/find-my-way/test/has-route.test.js generated vendored Normal file
View File

@@ -0,0 +1,219 @@
'use strict'
const t = require('tap')
const test = t.test
const rfdc = require('rfdc')({ proto: true })
const FindMyWay = require('..')
function equalRouters (t, router1, router2) {
t.strictSame(router1._opts, router2._opts)
t.same(router1.routes, router2.routes)
t.same(router1.trees, router2.trees)
t.strictSame(
router1.constrainer.strategies,
router2.constrainer.strategies
)
t.strictSame(
router1.constrainer.strategiesInUse,
router2.constrainer.strategiesInUse
)
t.strictSame(
router1.constrainer.asyncStrategiesInUse,
router2.constrainer.asyncStrategiesInUse
)
}
test('hasRoute returns false if there is no routes', t => {
t.plan(7)
const findMyWay = FindMyWay()
const fundMyWayClone = rfdc(findMyWay)
const hasRoute = findMyWay.hasRoute('GET', '/example')
t.equal(hasRoute, false)
equalRouters(t, findMyWay, fundMyWayClone)
})
test('hasRoute returns true for a static route', t => {
t.plan(7)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/example', () => {})
const fundMyWayClone = rfdc(findMyWay)
const hasRoute = findMyWay.hasRoute('GET', '/example')
t.equal(hasRoute, true)
equalRouters(t, findMyWay, fundMyWayClone)
})
test('hasRoute returns false for a static route', t => {
t.plan(7)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/example', () => {})
const fundMyWayClone = rfdc(findMyWay)
const hasRoute = findMyWay.hasRoute('GET', '/example1')
t.equal(hasRoute, false)
equalRouters(t, findMyWay, fundMyWayClone)
})
test('hasRoute returns true for a parametric route', t => {
t.plan(7)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/:param', () => {})
const fundMyWayClone = rfdc(findMyWay)
const hasRoute = findMyWay.hasRoute('GET', '/:param')
t.equal(hasRoute, true)
equalRouters(t, findMyWay, fundMyWayClone)
})
test('hasRoute returns false for a parametric route', t => {
t.plan(7)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/foo/:param', () => {})
const fundMyWayClone = rfdc(findMyWay)
const hasRoute = findMyWay.hasRoute('GET', '/bar/:param')
t.equal(hasRoute, false)
equalRouters(t, findMyWay, fundMyWayClone)
})
test('hasRoute returns true for a parametric route with static suffix', t => {
t.plan(7)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/:param-static', () => {})
const fundMyWayClone = rfdc(findMyWay)
const hasRoute = findMyWay.hasRoute('GET', '/:param-static')
t.equal(hasRoute, true)
equalRouters(t, findMyWay, fundMyWayClone)
})
test('hasRoute returns false for a parametric route with static suffix', t => {
t.plan(7)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/:param-static1', () => {})
const fundMyWayClone = rfdc(findMyWay)
const hasRoute = findMyWay.hasRoute('GET', '/:param-static2')
t.equal(hasRoute, false)
equalRouters(t, findMyWay, fundMyWayClone)
})
test('hasRoute returns true even if a param name different', t => {
t.plan(7)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/:param1', () => {})
const fundMyWayClone = rfdc(findMyWay)
const hasRoute = findMyWay.hasRoute('GET', '/:param2')
t.equal(hasRoute, true)
equalRouters(t, findMyWay, fundMyWayClone)
})
test('hasRoute returns true for a multi-parametric route', t => {
t.plan(7)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/:param1-:param2', () => {})
const fundMyWayClone = rfdc(findMyWay)
const hasRoute = findMyWay.hasRoute('GET', '/:param1-:param2')
t.equal(hasRoute, true)
equalRouters(t, findMyWay, fundMyWayClone)
})
test('hasRoute returns false for a multi-parametric route', t => {
t.plan(7)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/foo/:param1-:param2/bar1', () => {})
const fundMyWayClone = rfdc(findMyWay)
const hasRoute = findMyWay.hasRoute('GET', '/foo/:param1-:param2/bar2')
t.equal(hasRoute, false)
equalRouters(t, findMyWay, fundMyWayClone)
})
test('hasRoute returns true for a regexp route', t => {
t.plan(7)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/:param(^\\d+$)', () => {})
const fundMyWayClone = rfdc(findMyWay)
const hasRoute = findMyWay.hasRoute('GET', '/:param(^\\d+$)')
t.equal(hasRoute, true)
equalRouters(t, findMyWay, fundMyWayClone)
})
test('hasRoute returns false for a regexp route', t => {
t.plan(7)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/:file(^\\S+).png', () => {})
const fundMyWayClone = rfdc(findMyWay)
const hasRoute = findMyWay.hasRoute('GET', '/:file(^\\D+).png')
t.equal(hasRoute, false)
equalRouters(t, findMyWay, fundMyWayClone)
})
test('hasRoute returns true for a wildcard route', t => {
t.plan(7)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/example/*', () => {})
const fundMyWayClone = rfdc(findMyWay)
const hasRoute = findMyWay.hasRoute('GET', '/example/*')
t.equal(hasRoute, true)
equalRouters(t, findMyWay, fundMyWayClone)
})
test('hasRoute returns false for a wildcard route', t => {
t.plan(7)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/foo1/*', () => {})
const fundMyWayClone = rfdc(findMyWay)
const hasRoute = findMyWay.hasRoute('GET', '/foo2/*')
t.equal(hasRoute, false)
equalRouters(t, findMyWay, fundMyWayClone)
})

27
node_modules/find-my-way/test/host-storage.test.js generated vendored Normal file
View File

@@ -0,0 +1,27 @@
const acceptHostStrategy = require('../lib/strategies/accept-host')
const t = require('tap')
t.test('can get hosts by exact matches', async (t) => {
const storage = acceptHostStrategy.storage()
t.equal(storage.get('fastify.io'), undefined)
storage.set('fastify.io', true)
t.equal(storage.get('fastify.io'), true)
})
t.test('can get hosts by regexp matches', async (t) => {
const storage = acceptHostStrategy.storage()
t.equal(storage.get('fastify.io'), undefined)
storage.set(/.+fastify\.io/, true)
t.equal(storage.get('foo.fastify.io'), true)
t.equal(storage.get('bar.fastify.io'), true)
})
t.test('exact host matches take precendence over regexp matches', async (t) => {
const storage = acceptHostStrategy.storage()
storage.set(/.+fastify\.io/, 'wildcard')
storage.set('auth.fastify.io', 'exact')
t.equal(storage.get('foo.fastify.io'), 'wildcard')
t.equal(storage.get('bar.fastify.io'), 'wildcard')
t.equal(storage.get('auth.fastify.io'), 'exact')
})

View File

@@ -0,0 +1,45 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('../..')
test('A route supports host constraints under http2 protocol', t => {
t.plan(3)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/', {}, (req, res) => {
t.fail()
})
findMyWay.on('GET', '/', { constraints: { host: 'fastify.io' } }, (req, res) => {
t.equal(req.headers[':authority'], 'fastify.io')
})
findMyWay.on('GET', '/', { constraints: { host: /.+\.de/ } }, (req, res) => {
t.ok(req.headers[':authority'].endsWith('.de'))
})
findMyWay.lookup({
method: 'GET',
url: '/',
headers: {
':authority': 'fastify.io'
}
})
findMyWay.lookup({
method: 'GET',
url: '/',
headers: {
':authority': 'fastify.de'
}
})
findMyWay.lookup({
method: 'GET',
url: '/',
headers: {
':authority': 'find-my-way.de'
}
})
})

32
node_modules/find-my-way/test/issue-101.test.js generated vendored Normal file
View File

@@ -0,0 +1,32 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('../')
test('Falling back for node\'s parametric brother', t => {
t.plan(3)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/:namespace/:type/:id', () => {})
findMyWay.on('GET', '/:namespace/jobs/:name/run', () => {})
t.same(
findMyWay.find('GET', '/test_namespace/test_type/test_id').params,
{ namespace: 'test_namespace', type: 'test_type', id: 'test_id' }
)
t.same(
findMyWay.find('GET', '/test_namespace/jobss/test_id').params,
{ namespace: 'test_namespace', type: 'jobss', id: 'test_id' }
)
t.same(
findMyWay.find('GET', '/test_namespace/jobs/test_id').params,
{ namespace: 'test_namespace', type: 'jobs', id: 'test_id' }
)
})

207
node_modules/find-my-way/test/issue-104.test.js generated vendored Normal file
View File

@@ -0,0 +1,207 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('../')
test('Nested static parametric route, url with parameter common prefix > 1', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/a/bbbb', (req, res) => {
res.end('{"message":"hello world"}')
})
findMyWay.on('GET', '/a/bbaa', (req, res) => {
res.end('{"message":"hello world"}')
})
findMyWay.on('GET', '/a/babb', (req, res) => {
res.end('{"message":"hello world"}')
})
findMyWay.on('DELETE', '/a/:id', (req, res) => {
res.end('{"message":"hello world"}')
})
t.same(findMyWay.find('DELETE', '/a/bbar').params, { id: 'bbar' })
})
test('Parametric route, url with parameter common prefix > 1', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/aaa', (req, res) => {
res.end('{"message":"hello world"}')
})
findMyWay.on('GET', '/aabb', (req, res) => {
res.end('{"message":"hello world"}')
})
findMyWay.on('GET', '/abc', (req, res) => {
res.end('{"message":"hello world"}')
})
findMyWay.on('GET', '/:id', (req, res) => {
res.end('{"message":"hello world"}')
})
t.same(findMyWay.find('GET', '/aab').params, { id: 'aab' })
})
test('Parametric route, url with multi parameter common prefix > 1', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/:id/aaa/:id2', (req, res) => {
res.end('{"message":"hello world"}')
})
findMyWay.on('GET', '/:id/aabb/:id2', (req, res) => {
res.end('{"message":"hello world"}')
})
findMyWay.on('GET', '/:id/abc/:id2', (req, res) => {
res.end('{"message":"hello world"}')
})
findMyWay.on('GET', '/:a/:b', (req, res) => {
res.end('{"message":"hello world"}')
})
t.same(findMyWay.find('GET', '/hello/aab').params, { a: 'hello', b: 'aab' })
})
test('Mixed routes, url with parameter common prefix > 1', t => {
t.plan(11)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/test', (req, res, params) => {
res.end('{"hello":"world"}')
})
findMyWay.on('GET', '/testify', (req, res, params) => {
res.end('{"hello":"world"}')
})
findMyWay.on('GET', '/test/hello', (req, res, params) => {
res.end('{"hello":"world"}')
})
findMyWay.on('GET', '/test/hello/test', (req, res, params) => {
res.end('{"hello":"world"}')
})
findMyWay.on('GET', '/te/:a', (req, res, params) => {
res.end('{"hello":"world"}')
})
findMyWay.on('GET', '/test/hello/:b', (req, res, params) => {
res.end('{"hello":"world"}')
})
findMyWay.on('GET', '/:c', (req, res, params) => {
res.end('{"hello":"world"}')
})
findMyWay.on('GET', '/text/hello', (req, res, params) => {
res.end('{"hello":"world"}')
})
findMyWay.on('GET', '/text/:d', (req, res, params) => {
res.end('{"winter":"is here"}')
})
findMyWay.on('GET', '/text/:e/test', (req, res, params) => {
res.end('{"winter":"is here"}')
})
t.same(findMyWay.find('GET', '/test').params, {})
t.same(findMyWay.find('GET', '/testify').params, {})
t.same(findMyWay.find('GET', '/test/hello').params, {})
t.same(findMyWay.find('GET', '/test/hello/test').params, {})
t.same(findMyWay.find('GET', '/te/hello').params, { a: 'hello' })
t.same(findMyWay.find('GET', '/te/').params, { a: '' })
t.same(findMyWay.find('GET', '/testy').params, { c: 'testy' })
t.same(findMyWay.find('GET', '/besty').params, { c: 'besty' })
t.same(findMyWay.find('GET', '/text/hellos/test').params, { e: 'hellos' })
t.same(findMyWay.find('GET', '/te/hello/'), null)
t.same(findMyWay.find('GET', '/te/hellos/testy'), null)
})
test('Parent parametric brother should not rewrite child node parametric brother', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/text/hello', (req, res, params) => {
res.end('{"hello":"world"}')
})
findMyWay.on('GET', '/text/:e/test', (req, res, params) => {
res.end('{"winter":"is here"}')
})
findMyWay.on('GET', '/:c', (req, res, params) => {
res.end('{"hello":"world"}')
})
t.same(findMyWay.find('GET', '/text/hellos/test').params, { e: 'hellos' })
})
test('Mixed parametric routes, with last defined route being static', t => {
t.plan(4)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/test', (req, res, params) => {
res.end('{"hello":"world"}')
})
findMyWay.on('GET', '/test/:a', (req, res, params) => {
res.end('{"hello":"world"}')
})
findMyWay.on('GET', '/test/hello/:b', (req, res, params) => {
res.end('{"hello":"world"}')
})
findMyWay.on('GET', '/test/hello/:c/test', (req, res, params) => {
res.end('{"hello":"world"}')
})
findMyWay.on('GET', '/test/hello/:c/:k', (req, res, params) => {
res.end('{"hello":"world"}')
})
findMyWay.on('GET', '/test/world', (req, res, params) => {
res.end('{"hello":"world"}')
})
t.same(findMyWay.find('GET', '/test/hello').params, { a: 'hello' })
t.same(findMyWay.find('GET', '/test/hello/world/test').params, { c: 'world' })
t.same(findMyWay.find('GET', '/test/hello/world/te').params, { c: 'world', k: 'te' })
t.same(findMyWay.find('GET', '/test/hello/world/testy').params, { c: 'world', k: 'testy' })
})

32
node_modules/find-my-way/test/issue-110.test.js generated vendored Normal file
View File

@@ -0,0 +1,32 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('../')
test('Nested static parametric route, url with parameter common prefix > 1', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/api/foo/b2', (req, res) => {
res.end('{"message":"hello world"}')
})
findMyWay.on('GET', '/api/foo/bar/qux', (req, res) => {
res.end('{"message":"hello world"}')
})
findMyWay.on('GET', '/api/foo/:id/bar', (req, res) => {
res.end('{"message":"hello world"}')
})
findMyWay.on('GET', '/foo', (req, res) => {
res.end('{"message":"hello world"}')
})
t.same(findMyWay.find('GET', '/api/foo/b-123/bar').params, { id: 'b-123' })
})

81
node_modules/find-my-way/test/issue-132.test.js generated vendored Normal file
View File

@@ -0,0 +1,81 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('../')
test('Wildcard mixed with dynamic and common prefix / 1', t => {
t.plan(5)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('OPTIONS', '/*', (req, res, params) => {
t.equal(req.method, 'OPTIONS')
})
findMyWay.on('GET', '/obj/params/*', (req, res, params) => {
t.equal(req.method, 'GET')
})
findMyWay.on('GET', '/obj/:id', (req, res, params) => {
t.equal(req.method, 'GET')
})
findMyWay.on('GET', '/obj_params/*', (req, res, params) => {
t.equal(req.method, 'GET')
})
findMyWay.lookup({ method: 'OPTIONS', url: '/obj/params', headers: {} }, null)
findMyWay.lookup({ method: 'OPTIONS', url: '/obj/params/12', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/obj/params/12', headers: {} }, null)
findMyWay.lookup({ method: 'OPTIONS', url: '/obj_params/12', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/obj_params/12', headers: {} }, null)
})
test('Wildcard mixed with dynamic and common prefix / 2', t => {
t.plan(6)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('OPTIONS', '/*', (req, res, params) => {
t.equal(req.method, 'OPTIONS')
})
findMyWay.on('OPTIONS', '/obj/*', (req, res, params) => {
t.equal(req.method, 'OPTIONS')
})
findMyWay.on('GET', '/obj/params/*', (req, res, params) => {
t.equal(req.method, 'GET')
})
findMyWay.on('GET', '/obj/:id', (req, res, params) => {
t.equal(req.method, 'GET')
})
findMyWay.on('GET', '/obj_params/*', (req, res, params) => {
t.equal(req.method, 'GET')
})
findMyWay.lookup({ method: 'OPTIONS', url: '/obj_params/params', headers: {} }, null)
findMyWay.lookup({ method: 'OPTIONS', url: '/obj/params', headers: {} }, null)
findMyWay.lookup({ method: 'OPTIONS', url: '/obj/params/12', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/obj/params/12', headers: {} }, null)
findMyWay.lookup({ method: 'OPTIONS', url: '/obj_params/12', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/obj_params/12', headers: {} }, null)
})

24
node_modules/find-my-way/test/issue-145.test.js generated vendored Normal file
View File

@@ -0,0 +1,24 @@
'use strict'
const t = require('tap')
const FindMyWay = require('../')
t.test('issue-145', (t) => {
t.plan(8)
const findMyWay = FindMyWay({ ignoreTrailingSlash: true })
const fixedPath = function staticPath () {}
const varPath = function parameterPath () {}
findMyWay.on('GET', '/a/b', fixedPath)
findMyWay.on('GET', '/a/:pam/c', varPath)
t.equal(findMyWay.find('GET', '/a/b').handler, fixedPath)
t.equal(findMyWay.find('GET', '/a/b/').handler, fixedPath)
t.equal(findMyWay.find('GET', '/a/b/c').handler, varPath)
t.equal(findMyWay.find('GET', '/a/b/c/').handler, varPath)
t.equal(findMyWay.find('GET', '/a/foo/c').handler, varPath)
t.equal(findMyWay.find('GET', '/a/foo/c/').handler, varPath)
t.notOk(findMyWay.find('GET', '/a/c'))
t.notOk(findMyWay.find('GET', '/a/c/'))
})

22
node_modules/find-my-way/test/issue-149.test.js generated vendored Normal file
View File

@@ -0,0 +1,22 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('../')
test('Falling back for node\'s parametric brother', t => {
t.plan(3)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/foo/:id', () => {})
findMyWay.on('GET', '/foo/:color/:id', () => {})
findMyWay.on('GET', '/foo/red', () => {})
t.same(findMyWay.find('GET', '/foo/red/123').params, { color: 'red', id: '123' })
t.same(findMyWay.find('GET', '/foo/blue/123').params, { color: 'blue', id: '123' })
t.same(findMyWay.find('GET', '/foo/red').params, {})
})

55
node_modules/find-my-way/test/issue-151.test.js generated vendored Normal file
View File

@@ -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.equal(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.equal(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)
})

22
node_modules/find-my-way/test/issue-154.test.js generated vendored Normal file
View File

@@ -0,0 +1,22 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('..')
const noop = () => {}
test('Should throw when not sending a string', t => {
t.plan(3)
const findMyWay = FindMyWay()
t.throws(() => {
findMyWay.on('GET', '/t1', { constraints: { version: 42 } }, noop)
})
t.throws(() => {
findMyWay.on('GET', '/t2', { constraints: { version: null } }, noop)
})
t.throws(() => {
findMyWay.on('GET', '/t2', { constraints: { version: true } }, noop)
})
})

89
node_modules/find-my-way/test/issue-161.test.js generated vendored Normal file
View File

@@ -0,0 +1,89 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('../')
test('Falling back for node\'s parametric brother without ignoreTrailingSlash', t => {
t.plan(4)
const findMyWay = FindMyWay({
ignoreTrailingSlash: false,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/static/param1', () => {})
findMyWay.on('GET', '/static/param2', () => {})
findMyWay.on('GET', '/static/:paramA/next', () => {})
t.same(findMyWay.find('GET', '/static/param1').params, {})
t.same(findMyWay.find('GET', '/static/param2').params, {})
t.same(findMyWay.find('GET', '/static/paramOther/next').params, { paramA: 'paramOther' })
t.same(findMyWay.find('GET', '/static/param1/next').params, { paramA: 'param1' })
})
test('Falling back for node\'s parametric brother with ignoreTrailingSlash', t => {
t.plan(4)
const findMyWay = FindMyWay({
ignoreTrailingSlash: true,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/static/param1', () => {})
findMyWay.on('GET', '/static/param2', () => {})
findMyWay.on('GET', '/static/:paramA/next', () => {})
t.same(findMyWay.find('GET', '/static/param1').params, {})
t.same(findMyWay.find('GET', '/static/param2').params, {})
t.same(findMyWay.find('GET', '/static/paramOther/next').params, { paramA: 'paramOther' })
t.same(findMyWay.find('GET', '/static/param1/next').params, { paramA: 'param1' })
})
test('Falling back for node\'s parametric brother without ignoreTrailingSlash', t => {
t.plan(4)
const findMyWay = FindMyWay({
ignoreTrailingSlash: false,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/static/param1', () => {})
findMyWay.on('GET', '/static/param2', () => {})
findMyWay.on('GET', '/static/:paramA/next', () => {})
findMyWay.on('GET', '/static/param1/next/param3', () => {})
findMyWay.on('GET', '/static/param1/next/param4', () => {})
findMyWay.on('GET', '/static/:paramA/next/:paramB/other', () => {})
t.same(findMyWay.find('GET', '/static/param1/next/param3').params, {})
t.same(findMyWay.find('GET', '/static/param1/next/param4').params, {})
t.same(findMyWay.find('GET', '/static/paramOther/next/paramOther2/other').params, { paramA: 'paramOther', paramB: 'paramOther2' })
t.same(findMyWay.find('GET', '/static/param1/next/param3/other').params, { paramA: 'param1', paramB: 'param3' })
})
test('Falling back for node\'s parametric brother with ignoreTrailingSlash', t => {
t.plan(4)
const findMyWay = FindMyWay({
ignoreTrailingSlash: true,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/static/param1', () => {})
findMyWay.on('GET', '/static/param2', () => {})
findMyWay.on('GET', '/static/:paramA/next', () => {})
findMyWay.on('GET', '/static/param1/next/param3', () => {})
findMyWay.on('GET', '/static/param1/next/param4', () => {})
findMyWay.on('GET', '/static/:paramA/next/:paramB/other', () => {})
t.same(findMyWay.find('GET', '/static/param1/next/param3').params, {})
t.same(findMyWay.find('GET', '/static/param1/next/param4').params, {})
t.same(findMyWay.find('GET', '/static/paramOther/next/paramOther2/other').params, { paramA: 'paramOther', paramB: 'paramOther2' })
t.same(findMyWay.find('GET', '/static/param1/next/param3/other').params, { paramA: 'param1', paramB: 'param3' })
})

398
node_modules/find-my-way/test/issue-17.test.js generated vendored Normal file
View File

@@ -0,0 +1,398 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('../')
test('Parametric route, request.url contains dash', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/a/:param/b', (req, res, params) => {
t.equal(params.param, 'foo-bar')
})
findMyWay.lookup({ method: 'GET', url: '/a/foo-bar/b', headers: {} }, null)
})
test('Parametric route with fixed suffix', t => {
t.plan(6)
const findMyWay = FindMyWay({
defaultRoute: () => t.fail('Should not be defaultRoute')
})
findMyWay.on('GET', '/a/:param-static', () => {})
findMyWay.on('GET', '/b/:param.static', () => {})
t.same(findMyWay.find('GET', '/a/param-static', {}).params, { param: 'param' })
t.same(findMyWay.find('GET', '/b/param.static', {}).params, { param: 'param' })
t.same(findMyWay.find('GET', '/a/param-param-static', {}).params, { param: 'param-param' })
t.same(findMyWay.find('GET', '/b/param.param.static', {}).params, { param: 'param.param' })
t.same(findMyWay.find('GET', '/a/param.param-static', {}).params, { param: 'param.param' })
t.same(findMyWay.find('GET', '/b/param-param.static', {}).params, { param: 'param-param' })
})
test('Regex param exceeds max parameter length', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.ok('route not matched')
}
})
findMyWay.on('GET', '/a/:param(^\\w{3})', (req, res, params) => {
t.fail('regex match')
})
findMyWay.lookup({ method: 'GET', url: '/a/fool', headers: {} }, null)
})
test('Parametric route with regexp and fixed suffix / 1', t => {
t.plan(4)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.ok('route not matched')
}
})
findMyWay.on('GET', '/a/:param(^\\w{3})bar', (req, res, params) => {
t.fail('regex match')
})
findMyWay.lookup({ method: 'GET', url: '/a/$mebar', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/a/foolol', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/a/foobaz', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/a/foolbar', headers: {} }, null)
})
test('Parametric route with regexp and fixed suffix / 2', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/a/:param(^\\w{3})bar', (req, res, params) => {
t.equal(params.param, 'foo')
})
findMyWay.lookup({ method: 'GET', url: '/a/foobar', headers: {} }, null)
})
test('Parametric route with regexp and fixed suffix / 3', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/a/:param(^\\w{3}-\\w{3})foo', (req, res, params) => {
t.equal(params.param, 'abc-def')
})
findMyWay.lookup({ method: 'GET', url: '/a/abc-deffoo', headers: {} }, null)
})
test('Multi parametric route / 1', t => {
t.plan(4)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/a/:p1-:p2', (req, res, params) => {
t.equal(params.p1, 'foo')
t.equal(params.p2, 'bar')
})
findMyWay.on('GET', '/b/:p1.:p2', (req, res, params) => {
t.equal(params.p1, 'foo')
t.equal(params.p2, 'bar')
})
findMyWay.lookup({ method: 'GET', url: '/a/foo-bar', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/b/foo.bar', headers: {} }, null)
})
test('Multi parametric route / 2', t => {
t.plan(4)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/a/:p1-:p2', (req, res, params) => {
t.equal(params.p1, 'foo-bar')
t.equal(params.p2, 'baz')
})
findMyWay.on('GET', '/b/:p1.:p2', (req, res, params) => {
t.equal(params.p1, 'foo')
t.equal(params.p2, 'bar-baz')
})
findMyWay.lookup({ method: 'GET', url: '/a/foo-bar-baz', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/b/foo.bar-baz', headers: {} }, null)
})
test('Multi parametric route / 3', t => {
t.plan(4)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/a/:p_1-:$p', (req, res, params) => {
t.equal(params.p_1, 'foo')
t.equal(params.$p, 'bar')
})
findMyWay.on('GET', '/b/:p_1.:$p', (req, res, params) => {
t.equal(params.p_1, 'foo')
t.equal(params.$p, 'bar')
})
findMyWay.lookup({ method: 'GET', url: '/a/foo-bar', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/b/foo.bar', headers: {} }, null)
})
test('Multi parametric route / 4', t => {
t.plan(2)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.pass('Everything good')
}
})
findMyWay.on('GET', '/a/:p1-:p2', (req, res, params) => {
t.fail('Should not match this route')
})
findMyWay.on('GET', '/b/:p1.:p2', (req, res, params) => {
t.fail('Should not match this route')
})
findMyWay.lookup({ method: 'GET', url: '/a/foo', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/b/foo', headers: {} }, null)
})
test('Multi parametric route with regexp / 1', t => {
t.plan(2)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/at/:hour(^\\d+)h:minute(^\\d+)m', (req, res, params) => {
t.equal(params.hour, '0')
t.equal(params.minute, '42')
})
findMyWay.lookup({ method: 'GET', url: '/at/0h42m', headers: {} }, null)
})
test('Multi parametric route with colon separator', t => {
t.plan(3)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/:param(.*)::suffix', (req, res, params) => {
t.equal(params.param, 'foo')
})
findMyWay.on('GET', '/:param1(.*)::suffix1-:param2(.*)::suffix2/static', (req, res, params) => {
t.equal(params.param1, 'foo')
t.equal(params.param2, 'bar')
})
findMyWay.lookup({ method: 'GET', url: '/foo:suffix', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/foo:suffix1-bar:suffix2/static', headers: {} }, null)
})
test('Multi parametric route with regexp / 2', t => {
t.plan(8)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/a/:uuid(^[\\d-]{19})-:user(^\\w+)', (req, res, params) => {
t.equal(params.uuid, '1111-2222-3333-4444')
t.equal(params.user, 'foo')
})
findMyWay.on('GET', '/a/:uuid(^[\\d-]{19})-:user(^\\w+)/account', (req, res, params) => {
t.equal(params.uuid, '1111-2222-3333-4445')
t.equal(params.user, 'bar')
})
findMyWay.on('GET', '/b/:uuid(^[\\d-]{19}).:user(^\\w+)', (req, res, params) => {
t.equal(params.uuid, '1111-2222-3333-4444')
t.equal(params.user, 'foo')
})
findMyWay.on('GET', '/b/:uuid(^[\\d-]{19}).:user(^\\w+)/account', (req, res, params) => {
t.equal(params.uuid, '1111-2222-3333-4445')
t.equal(params.user, 'bar')
})
findMyWay.lookup({ method: 'GET', url: '/a/1111-2222-3333-4444-foo', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/a/1111-2222-3333-4445-bar/account', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/b/1111-2222-3333-4444.foo', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/b/1111-2222-3333-4445.bar/account', headers: {} }, null)
})
test('Multi parametric route with fixed suffix', t => {
t.plan(4)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/a/:p1-:p2-baz', (req, res, params) => {
t.equal(params.p1, 'foo')
t.equal(params.p2, 'bar')
})
findMyWay.on('GET', '/b/:p1.:p2-baz', (req, res, params) => {
t.equal(params.p1, 'foo')
t.equal(params.p2, 'bar')
})
findMyWay.lookup({ method: 'GET', url: '/a/foo-bar-baz', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/b/foo.bar-baz', headers: {} }, null)
})
test('Multi parametric route with regexp and fixed suffix', t => {
t.plan(4)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/a/:p1(^\\w+)-:p2(^\\w+)-kuux', (req, res, params) => {
t.equal(params.p1, 'foo')
t.equal(params.p2, 'barbaz')
})
findMyWay.on('GET', '/b/:p1(^\\w+).:p2(^\\w+)-kuux', (req, res, params) => {
t.equal(params.p1, 'foo')
t.equal(params.p2, 'barbaz')
})
findMyWay.lookup({ method: 'GET', url: '/a/foo-barbaz-kuux', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/b/foo.barbaz-kuux', headers: {} }, null)
})
test('Multi parametric route with wildcard', t => {
t.plan(4)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/a/:p1-:p2/*', (req, res, params) => {
t.equal(params.p1, 'foo')
t.equal(params.p2, 'bar')
})
findMyWay.on('GET', '/b/:p1.:p2/*', (req, res, params) => {
t.equal(params.p1, 'foo')
t.equal(params.p2, 'bar')
})
findMyWay.lookup({ method: 'GET', url: '/a/foo-bar/baz', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/b/foo.bar/baz', headers: {} }, null)
})
test('Nested multi parametric route', t => {
t.plan(6)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/a/:p1-:p2/b/:p3', (req, res, params) => {
t.equal(params.p1, 'foo')
t.equal(params.p2, 'bar')
t.equal(params.p3, 'baz')
})
findMyWay.on('GET', '/b/:p1.:p2/b/:p3', (req, res, params) => {
t.equal(params.p1, 'foo')
t.equal(params.p2, 'bar')
t.equal(params.p3, 'baz')
})
findMyWay.lookup({ method: 'GET', url: '/a/foo-bar/b/baz', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/b/foo.bar/b/baz', headers: {} }, null)
})
test('Nested multi parametric route with regexp / 1', t => {
t.plan(6)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/a/:p1(^\\w{3})-:p2(^\\d+)/b/:p3', (req, res, params) => {
t.equal(params.p1, 'foo')
t.equal(params.p2, '42')
t.equal(params.p3, 'bar')
})
findMyWay.on('GET', '/b/:p1(^\\w{3}).:p2(^\\d+)/b/:p3', (req, res, params) => {
t.equal(params.p1, 'foo')
t.equal(params.p2, '42')
t.equal(params.p3, 'bar')
})
findMyWay.lookup({ method: 'GET', url: '/a/foo-42/b/bar', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/b/foo.42/b/bar', headers: {} }, null)
})
test('Nested multi parametric route with regexp / 2', t => {
t.plan(6)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/a/:p1(^\\w{3})-:p2/b/:p3', (req, res, params) => {
t.equal(params.p1, 'foo')
t.equal(params.p2, '42')
t.equal(params.p3, 'bar')
})
findMyWay.on('GET', '/b/:p1(^\\w{3}).:p2/b/:p3', (req, res, params) => {
t.equal(params.p1, 'foo')
t.equal(params.p2, '42')
t.equal(params.p3, 'bar')
})
findMyWay.lookup({ method: 'GET', url: '/a/foo-42/b/bar', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/b/foo.42/b/bar', headers: {} }, null)
})

81
node_modules/find-my-way/test/issue-175.test.js generated vendored Normal file
View File

@@ -0,0 +1,81 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('..')
test('double colon is replaced with single colon, no parameters', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: () => t.fail('should not be default route')
})
function handler (req, res, params) {
t.same(params, {})
}
findMyWay.on('GET', '/name::customVerb', handler)
findMyWay.lookup({ method: 'GET', url: '/name:customVerb' }, null)
})
test('exactly one match for static route with colon', t => {
t.plan(2)
const findMyWay = FindMyWay()
function handler () {}
findMyWay.on('GET', '/name::customVerb', handler)
t.equal(findMyWay.find('GET', '/name:customVerb').handler, handler)
t.equal(findMyWay.find('GET', '/name:test'), null)
})
test('double colon is replaced with single colon, no parameters, same parent node name', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: () => t.fail('should not be default route')
})
findMyWay.on('GET', '/name', () => {
t.fail('should not be parent route')
})
findMyWay.on('GET', '/name::customVerb', (req, res, params) => {
t.same(params, {})
})
findMyWay.lookup({ method: 'GET', url: '/name:customVerb', headers: {} }, null)
})
test('double colon is replaced with single colon, default route, same parent node name', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: () => t.ok('should be default route')
})
findMyWay.on('GET', '/name', () => {
t.fail('should not be parent route')
})
findMyWay.on('GET', '/name::customVerb', () => {
t.fail('should not be child route')
})
findMyWay.lookup({ method: 'GET', url: '/name:wrongCustomVerb', headers: {} }, null)
})
test('double colon is replaced with single colon, with parameters', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: () => t.fail('should not be default route')
})
findMyWay.on('GET', '/name1::customVerb1/:param1/name2::customVerb2:param2', (req, res, params) => {
t.same(params, {
param1: 'value1',
param2: 'value2'
})
})
findMyWay.lookup({ method: 'GET', url: '/name1:customVerb1/value1/name2:customVerb2value2', headers: {} }, null)
})

19
node_modules/find-my-way/test/issue-182.test.js generated vendored Normal file
View File

@@ -0,0 +1,19 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('..')
test('Set method property when splitting node', t => {
t.plan(1)
const findMyWay = FindMyWay()
function handler (req, res, params) {
t.pass()
}
findMyWay.on('GET', '/health-a/health', handler)
findMyWay.on('GET', '/health-b/health', handler)
t.notMatch(findMyWay.prettyPrint(), /undefined/)
})

44
node_modules/find-my-way/test/issue-190.test.js generated vendored Normal file
View File

@@ -0,0 +1,44 @@
'use strict'
const t = require('tap')
const FindMyWay = require('../')
t.test('issue-190', (t) => {
t.plan(6)
const findMyWay = FindMyWay()
let staticCounter = 0
let paramCounter = 0
const staticPath = function staticPath () { staticCounter++ }
const paramPath = function paramPath () { paramCounter++ }
const extraPath = function extraPath () { }
findMyWay.on('GET', '/api/users/award_winners', staticPath)
findMyWay.on('GET', '/api/users/admins', staticPath)
findMyWay.on('GET', '/api/users/:id', paramPath)
findMyWay.on('GET', '/api/:resourceType/foo', extraPath)
t.equal(findMyWay.find('GET', '/api/users/admins').handler, staticPath)
t.equal(findMyWay.find('GET', '/api/users/award_winners').handler, staticPath)
t.equal(findMyWay.find('GET', '/api/users/a766c023-34ec-40d2-923c-e8259a28d2c5').handler, paramPath)
t.equal(findMyWay.find('GET', '/api/users/b766c023-34ec-40d2-923c-e8259a28d2c5').handler, paramPath)
findMyWay.lookup({
method: 'GET',
url: '/api/users/admins',
headers: { }
})
findMyWay.lookup({
method: 'GET',
url: '/api/users/award_winners',
headers: { }
})
findMyWay.lookup({
method: 'GET',
url: '/api/users/a766c023-34ec-40d2-923c-e8259a28d2c5',
headers: { }
})
t.equal(staticCounter, 2)
t.equal(paramCounter, 1)
})

80
node_modules/find-my-way/test/issue-20.test.js generated vendored Normal file
View File

@@ -0,0 +1,80 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('../')
test('Standard case', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be here')
}
})
findMyWay.on('GET', '/a/:param', (req, res, params) => {
t.equal(params.param, 'perfectly-fine-route')
})
findMyWay.lookup({ method: 'GET', url: '/a/perfectly-fine-route', headers: {} }, null)
})
test('Should be 404 / 1', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.pass('Everything good')
}
})
findMyWay.on('GET', '/a/:param', (req, res, params) => {
t.fail('We should not be here')
})
findMyWay.lookup({ method: 'GET', url: '/a', headers: {} }, null)
})
test('Should be 404 / 2', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.pass('Everything good')
}
})
findMyWay.on('GET', '/a/:param', (req, res, params) => {
t.fail('We should not be here')
})
findMyWay.lookup({ method: 'GET', url: '/a-non-existing-route', headers: {} }, null)
})
test('Should be 404 / 3', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.pass('Everything good')
}
})
findMyWay.on('GET', '/a/:param', (req, res, params) => {
t.fail('We should not be here')
})
findMyWay.lookup({ method: 'GET', url: '/a//', headers: {} }, null)
})
test('Should get an empty parameter', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('We should not be here')
}
})
findMyWay.on('GET', '/a/:param', (req, res, params) => {
t.equal(params.param, '')
})
findMyWay.lookup({ method: 'GET', url: '/a/', headers: {} }, null)
})

122
node_modules/find-my-way/test/issue-206.test.js generated vendored Normal file
View File

@@ -0,0 +1,122 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('..')
test('Decode the URL before the routing', t => {
t.plan(8)
const findMyWay = FindMyWay()
function space (req, res, params) {}
function percentTwenty (req, res, params) {}
function percentTwentyfive (req, res, params) {}
findMyWay.on('GET', '/static/:pathParam', () => {})
findMyWay.on('GET', '/[...]/a .html', space)
findMyWay.on('GET', '/[...]/a%20.html', percentTwenty)
findMyWay.on('GET', '/[...]/a%2520.html', percentTwentyfive)
t.equal(findMyWay.find('GET', '/[...]/a .html').handler, space)
t.equal(findMyWay.find('GET', '/%5B...%5D/a .html').handler, space)
t.equal(findMyWay.find('GET', '/[...]/a%20.html').handler, space, 'a%20 decode is a ')
t.equal(findMyWay.find('GET', '/%5B...%5D/a%20.html').handler, space, 'a%20 decode is a ')
t.equal(findMyWay.find('GET', '/[...]/a%2520.html').handler, percentTwenty, 'a%2520 decode is a%20')
t.equal(findMyWay.find('GET', '/%5B...%5D/a%252520.html').handler, percentTwentyfive, 'a%252520.html is a%2520')
t.equal(findMyWay.find('GET', '/[...]/a .html'), null, 'double space')
t.equal(findMyWay.find('GET', '/static/%25E0%A4%A'), null, 'invalid encoded path param')
})
test('double encoding', t => {
t.plan(8)
const findMyWay = FindMyWay()
function pathParam (req, res, params) {
t.same(params, this.expect, 'path param')
t.same(pathParam, this.handler, 'match handler')
}
function regexPathParam (req, res, params) {
t.same(params, this.expect, 'regex param')
t.same(regexPathParam, this.handler, 'match handler')
}
function wildcard (req, res, params) {
t.same(params, this.expect, 'wildcard param')
t.same(wildcard, this.handler, 'match handler')
}
findMyWay.on('GET', '/:pathParam', pathParam)
findMyWay.on('GET', '/reg/:regExeParam(^.*$)', regexPathParam)
findMyWay.on('GET', '/wild/*', wildcard)
findMyWay.lookup(get('/' + doubleEncode('reg/hash# .png')), null,
{ expect: { pathParam: singleEncode('reg/hash# .png') }, handler: pathParam }
)
findMyWay.lookup(get('/' + doubleEncode('special # $ & + , / : ; = ? @')), null,
{ expect: { pathParam: singleEncode('special # $ & + , / : ; = ? @') }, handler: pathParam }
)
findMyWay.lookup(get('/reg/' + doubleEncode('hash# .png')), null,
{ expect: { regExeParam: singleEncode('hash# .png') }, handler: regexPathParam }
)
findMyWay.lookup(get('/wild/' + doubleEncode('mail@mail.it')), null,
{ expect: { '*': singleEncode('mail@mail.it') }, handler: wildcard }
)
function doubleEncode (str) {
return encodeURIComponent(encodeURIComponent(str))
}
function singleEncode (str) {
return encodeURIComponent(str)
}
})
test('Special chars on path parameter', t => {
t.plan(10)
const findMyWay = FindMyWay()
function pathParam (req, res, params) {
t.same(params, this.expect, 'path param')
t.same(pathParam, this.handler, 'match handler')
}
function regexPathParam (req, res, params) {
t.same(params, this.expect, 'regex param')
t.same(regexPathParam, this.handler, 'match handler')
}
function staticEncoded (req, res, params) {
t.same(params, this.expect, 'static match')
t.same(staticEncoded, this.handler, 'match handler')
}
findMyWay.on('GET', '/:pathParam', pathParam)
findMyWay.on('GET', '/reg/:regExeParam(^\\d+) .png', regexPathParam)
findMyWay.on('GET', '/[...]/a%2520.html', staticEncoded)
findMyWay.lookup(get('/%5B...%5D/a%252520.html'), null, { expect: {}, handler: staticEncoded })
findMyWay.lookup(get('/[...].html'), null, { expect: { pathParam: '[...].html' }, handler: pathParam })
findMyWay.lookup(get('/reg/123 .png'), null, { expect: { regExeParam: '123' }, handler: regexPathParam })
findMyWay.lookup(get('/reg%2F123 .png'), null, { expect: { pathParam: 'reg/123 .png' }, handler: pathParam }) // en encoded / is considered a parameter
findMyWay.lookup(get('/reg/123%20.png'), null, { expect: { regExeParam: '123' }, handler: regexPathParam })
})
test('Multi parametric route with encoded colon separator', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/:param(.*)::suffix', (req, res, params) => {
t.equal(params.param, 'foo-bar')
})
findMyWay.lookup({ method: 'GET', url: '/foo-bar%3Asuffix', headers: {} }, null)
})
function get (url) {
return { method: 'GET', url, headers: {} }
}
// http://localhost:3000/parameter with / in it
// http://localhost:3000/parameter%20with%20%2F%20in%20it
// http://localhost:3000/parameter with %252F in it

51
node_modules/find-my-way/test/issue-221.test.js generated vendored Normal file
View File

@@ -0,0 +1,51 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('..')
test('Should return correct param after switching from static route', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/prefix-:id', () => {})
findMyWay.on('GET', '/prefix-111', () => {})
t.same(findMyWay.find('GET', '/prefix-1111').params, { id: '1111' })
})
test('Should return correct param after switching from static route', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/prefix-111', () => {})
findMyWay.on('GET', '/prefix-:id/hello', () => {})
t.same(findMyWay.find('GET', '/prefix-1111/hello').params, { id: '1111' })
})
test('Should return correct param after switching from parametric route', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/prefix-111', () => {})
findMyWay.on('GET', '/prefix-:id/hello', () => {})
findMyWay.on('GET', '/:id', () => {})
t.same(findMyWay.find('GET', '/prefix-1111-hello').params, { id: 'prefix-1111-hello' })
})
test('Should return correct params after switching from parametric route', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test/:param1/test/:param2/prefix-111', () => {})
findMyWay.on('GET', '/test/:param1/test/:param2/prefix-:id/hello', () => {})
findMyWay.on('GET', '/test/:param1/test/:param2/:id', () => {})
t.same(findMyWay.find('GET', '/test/value1/test/value2/prefix-1111-hello').params, {
param1: 'value1',
param2: 'value2',
id: 'prefix-1111-hello'
})
})

95
node_modules/find-my-way/test/issue-234.test.js generated vendored Normal file
View File

@@ -0,0 +1,95 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('..')
test('Match static url without encoding option', t => {
t.plan(2)
const findMyWay = FindMyWay()
const handler = () => {}
findMyWay.on('GET', '/🍌', handler)
t.same(findMyWay.find('GET', '/🍌').handler, handler)
t.same(findMyWay.find('GET', '/%F0%9F%8D%8C').handler, handler)
})
test('Match parametric url with encoding option', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/🍌/:param', () => {})
t.same(findMyWay.find('GET', '/🍌/@').params, { param: '@' })
t.same(findMyWay.find('GET', '/%F0%9F%8D%8C/@').params, { param: '@' })
})
test('Match encoded parametric url with encoding option', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/🍌/:param', () => {})
t.same(findMyWay.find('GET', '/🍌/%23').params, { param: '#' })
t.same(findMyWay.find('GET', '/%F0%9F%8D%8C/%23').params, { param: '#' })
})
test('Decode url components', t => {
t.plan(3)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/:param1/:param2', () => {})
t.same(findMyWay.find('GET', '/foo%23bar/foo%23bar').params, { param1: 'foo#bar', param2: 'foo#bar' })
t.same(findMyWay.find('GET', '/%F0%9F%8D%8C/%F0%9F%8D%8C').params, { param1: '🍌', param2: '🍌' })
t.same(findMyWay.find('GET', '/%F0%9F%8D%8C/foo%23bar').params, { param1: '🍌', param2: 'foo#bar' })
})
test('Decode url components', t => {
t.plan(5)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/foo🍌bar/:param1/:param2', () => {})
findMyWay.on('GET', '/user/:id', () => {})
t.same(findMyWay.find('GET', '/foo%F0%9F%8D%8Cbar/foo%23bar/foo%23bar').params, { param1: 'foo#bar', param2: 'foo#bar' })
t.same(findMyWay.find('GET', '/user/maintainer+tomas').params, { id: 'maintainer+tomas' })
t.same(findMyWay.find('GET', '/user/maintainer%2Btomas').params, { id: 'maintainer+tomas' })
t.same(findMyWay.find('GET', '/user/maintainer%20tomas').params, { id: 'maintainer tomas' })
t.same(findMyWay.find('GET', '/user/maintainer%252Btomas').params, { id: 'maintainer%2Btomas' })
})
test('Decode url components', t => {
t.plan(18)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/:param1', () => {})
t.same(findMyWay.find('GET', '/foo%23bar').params, { param1: 'foo#bar' })
t.same(findMyWay.find('GET', '/foo%24bar').params, { param1: 'foo$bar' })
t.same(findMyWay.find('GET', '/foo%26bar').params, { param1: 'foo&bar' })
t.same(findMyWay.find('GET', '/foo%2bbar').params, { param1: 'foo+bar' })
t.same(findMyWay.find('GET', '/foo%2Bbar').params, { param1: 'foo+bar' })
t.same(findMyWay.find('GET', '/foo%2cbar').params, { param1: 'foo,bar' })
t.same(findMyWay.find('GET', '/foo%2Cbar').params, { param1: 'foo,bar' })
t.same(findMyWay.find('GET', '/foo%2fbar').params, { param1: 'foo/bar' })
t.same(findMyWay.find('GET', '/foo%2Fbar').params, { param1: 'foo/bar' })
t.same(findMyWay.find('GET', '/foo%3abar').params, { param1: 'foo:bar' })
t.same(findMyWay.find('GET', '/foo%3Abar').params, { param1: 'foo:bar' })
t.same(findMyWay.find('GET', '/foo%3bbar').params, { param1: 'foo;bar' })
t.same(findMyWay.find('GET', '/foo%3Bbar').params, { param1: 'foo;bar' })
t.same(findMyWay.find('GET', '/foo%3dbar').params, { param1: 'foo=bar' })
t.same(findMyWay.find('GET', '/foo%3Dbar').params, { param1: 'foo=bar' })
t.same(findMyWay.find('GET', '/foo%3fbar').params, { param1: 'foo?bar' })
t.same(findMyWay.find('GET', '/foo%3Fbar').params, { param1: 'foo?bar' })
t.same(findMyWay.find('GET', '/foo%40bar').params, { param1: 'foo@bar' })
})

120
node_modules/find-my-way/test/issue-238.test.js generated vendored Normal file
View File

@@ -0,0 +1,120 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('../')
test('Multi-parametric tricky path', t => {
t.plan(6)
const findMyWay = FindMyWay({
defaultRoute: () => t.fail('Should not be defaultRoute')
})
findMyWay.on('GET', '/:param1-static-:param2', () => {})
t.same(
findMyWay.find('GET', '/param1-static-param2', {}).params,
{ param1: 'param1', param2: 'param2' }
)
t.same(
findMyWay.find('GET', '/param1.1-param1.2-static-param2.1-param2.2', {}).params,
{ param1: 'param1.1-param1.2', param2: 'param2.1-param2.2' }
)
t.same(
findMyWay.find('GET', '/param1-1-param1-2-static-param2-1-param2-2', {}).params,
{ param1: 'param1-1-param1-2', param2: 'param2-1-param2-2' }
)
t.same(
findMyWay.find('GET', '/static-static-static', {}).params,
{ param1: 'static', param2: 'static' }
)
t.same(
findMyWay.find('GET', '/static-static-static-static', {}).params,
{ param1: 'static', param2: 'static-static' }
)
t.same(
findMyWay.find('GET', '/static-static1-static-static', {}).params,
{ param1: 'static-static1', param2: 'static' }
)
})
test('Multi-parametric nodes with different static ending 1', t => {
t.plan(4)
const findMyWay = FindMyWay({
defaultRoute: () => t.fail('Should not be defaultRoute')
})
const paramHandler = () => {}
const multiParamHandler = () => {}
findMyWay.on('GET', '/v1/foo/:code', paramHandler)
findMyWay.on('GET', '/v1/foo/:code.png', multiParamHandler)
t.same(findMyWay.find('GET', '/v1/foo/hello', {}).handler, paramHandler)
t.same(findMyWay.find('GET', '/v1/foo/hello', {}).params, { code: 'hello' })
t.same(findMyWay.find('GET', '/v1/foo/hello.png', {}).handler, multiParamHandler)
t.same(findMyWay.find('GET', '/v1/foo/hello.png', {}).params, { code: 'hello' })
})
test('Multi-parametric nodes with different static ending 2', t => {
t.plan(4)
const findMyWay = FindMyWay({
defaultRoute: () => t.fail('Should not be defaultRoute')
})
const jpgHandler = () => {}
const pngHandler = () => {}
findMyWay.on('GET', '/v1/foo/:code.jpg', jpgHandler)
findMyWay.on('GET', '/v1/foo/:code.png', pngHandler)
t.same(findMyWay.find('GET', '/v1/foo/hello.jpg', {}).handler, jpgHandler)
t.same(findMyWay.find('GET', '/v1/foo/hello.jpg', {}).params, { code: 'hello' })
t.same(findMyWay.find('GET', '/v1/foo/hello.png', {}).handler, pngHandler)
t.same(findMyWay.find('GET', '/v1/foo/hello.png', {}).params, { code: 'hello' })
})
test('Multi-parametric nodes with different static ending 3', t => {
t.plan(4)
const findMyWay = FindMyWay({
defaultRoute: () => t.fail('Should not be defaultRoute')
})
const jpgHandler = () => {}
const pngHandler = () => {}
findMyWay.on('GET', '/v1/foo/:code.jpg/bar', jpgHandler)
findMyWay.on('GET', '/v1/foo/:code.png/bar', pngHandler)
t.same(findMyWay.find('GET', '/v1/foo/hello.jpg/bar', {}).handler, jpgHandler)
t.same(findMyWay.find('GET', '/v1/foo/hello.jpg/bar', {}).params, { code: 'hello' })
t.same(findMyWay.find('GET', '/v1/foo/hello.png/bar', {}).handler, pngHandler)
t.same(findMyWay.find('GET', '/v1/foo/hello.png/bar', {}).params, { code: 'hello' })
})
test('Multi-parametric nodes with different static ending 4', t => {
t.plan(6)
const findMyWay = FindMyWay({
defaultRoute: () => t.fail('Should not be defaultRoute')
})
const handler = () => {}
const jpgHandler = () => {}
const pngHandler = () => {}
findMyWay.on('GET', '/v1/foo/:code/bar', handler)
findMyWay.on('GET', '/v1/foo/:code.jpg/bar', jpgHandler)
findMyWay.on('GET', '/v1/foo/:code.png/bar', pngHandler)
t.same(findMyWay.find('GET', '/v1/foo/hello/bar', {}).handler, handler)
t.same(findMyWay.find('GET', '/v1/foo/hello/bar', {}).params, { code: 'hello' })
t.same(findMyWay.find('GET', '/v1/foo/hello.jpg/bar', {}).handler, jpgHandler)
t.same(findMyWay.find('GET', '/v1/foo/hello.jpg/bar', {}).params, { code: 'hello' })
t.same(findMyWay.find('GET', '/v1/foo/hello.png/bar', {}).handler, pngHandler)
t.same(findMyWay.find('GET', '/v1/foo/hello.png/bar', {}).params, { code: 'hello' })
})

30
node_modules/find-my-way/test/issue-240.test.js generated vendored Normal file
View File

@@ -0,0 +1,30 @@
'use strict'
const t = require('tap')
const FindMyWay = require('../')
t.test('issue-240: .find matching', (t) => {
t.plan(14)
const findMyWay = FindMyWay({ ignoreDuplicateSlashes: true })
const fixedPath = function staticPath () {}
const varPath = function parameterPath () {}
findMyWay.on('GET', '/a/b', fixedPath)
findMyWay.on('GET', '/a/:pam/c', varPath)
t.equal(findMyWay.find('GET', '/a/b').handler, fixedPath)
t.equal(findMyWay.find('GET', '/a//b').handler, fixedPath)
t.equal(findMyWay.find('GET', '/a/b/c').handler, varPath)
t.equal(findMyWay.find('GET', '/a//b/c').handler, varPath)
t.equal(findMyWay.find('GET', '/a///b/c').handler, varPath)
t.equal(findMyWay.find('GET', '/a//b//c').handler, varPath)
t.equal(findMyWay.find('GET', '/a///b///c').handler, varPath)
t.equal(findMyWay.find('GET', '/a/foo/c').handler, varPath)
t.equal(findMyWay.find('GET', '/a//foo/c').handler, varPath)
t.equal(findMyWay.find('GET', '/a///foo/c').handler, varPath)
t.equal(findMyWay.find('GET', '/a//foo//c').handler, varPath)
t.equal(findMyWay.find('GET', '/a///foo///c').handler, varPath)
t.notOk(findMyWay.find('GET', '/a/c'))
t.notOk(findMyWay.find('GET', '/a//c'))
})

33
node_modules/find-my-way/test/issue-241.test.js generated vendored Normal file
View File

@@ -0,0 +1,33 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('..')
test('Double colon and parametric children', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/::articles', () => {})
findMyWay.on('GET', '/:article_name', () => {})
t.same(findMyWay.find('GET', '/:articles').params, {})
t.same(findMyWay.find('GET', '/articles_param').params, { article_name: 'articles_param' })
})
test('Double colon and parametric children', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/::test::foo/:param/::articles', () => {})
findMyWay.on('GET', '/::test::foo/:param/:article_name', () => {})
t.same(
findMyWay.find('GET', '/:test:foo/param_value1/:articles').params,
{ param: 'param_value1' }
)
t.same(
findMyWay.find('GET', '/:test:foo/param_value2/articles_param').params,
{ param: 'param_value2', article_name: 'articles_param' }
)
})

52
node_modules/find-my-way/test/issue-247.test.js generated vendored Normal file
View File

@@ -0,0 +1,52 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('..')
test('If there are constraints param, router.off method support filter', t => {
t.plan(12)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/a', { constraints: { host: '1' } }, () => {}, { name: 1 })
findMyWay.on('GET', '/a', { constraints: { host: '2', version: '1.0.0' } }, () => {}, { name: 2 })
findMyWay.on('GET', '/a', { constraints: { host: '2', version: '2.0.0' } }, () => {}, { name: 3 })
t.same(findMyWay.find('GET', '/a', { host: '1' }).store, { name: 1 })
t.same(findMyWay.find('GET', '/a', { host: '2', version: '1.0.0' }).store, { name: 2 })
t.same(findMyWay.find('GET', '/a', { host: '2', version: '2.0.0' }).store, { name: 3 })
findMyWay.off('GET', '/a', { host: '1' })
t.same(findMyWay.find('GET', '/a', { host: '1' }), null)
t.same(findMyWay.find('GET', '/a', { host: '2', version: '1.0.0' }).store, { name: 2 })
t.same(findMyWay.find('GET', '/a', { host: '2', version: '2.0.0' }).store, { name: 3 })
findMyWay.off('GET', '/a', { host: '2', version: '1.0.0' })
t.same(findMyWay.find('GET', '/a', { host: '1' }), null)
t.same(findMyWay.find('GET', '/a', { host: '2', version: '1.0.0' }), null)
t.same(findMyWay.find('GET', '/a', { host: '2', version: '2.0.0' }).store, { name: 3 })
findMyWay.off('GET', '/a', { host: '2', version: '2.0.0' })
t.same(findMyWay.find('GET', '/a', { host: '1' }), null)
t.same(findMyWay.find('GET', '/a', { host: '2', version: '1.0.0' }), null)
t.same(findMyWay.find('GET', '/a', { host: '2', version: '2.0.0' }), null)
})
test('If there are no constraints param, router.off method remove all matched router', t => {
t.plan(4)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/a', { constraints: { host: '1' } }, () => {}, { name: 1 })
findMyWay.on('GET', '/a', { constraints: { host: '2' } }, () => {}, { name: 2 })
t.same(findMyWay.find('GET', '/a', { host: '1' }).store, { name: 1 })
t.same(findMyWay.find('GET', '/a', { host: '2' }).store, { name: 2 })
findMyWay.off('GET', '/a')
t.same(findMyWay.find('GET', '/a', { host: '1' }), null)
t.same(findMyWay.find('GET', '/a', { host: '2' }), null)
})

32
node_modules/find-my-way/test/issue-254.test.js generated vendored Normal file
View File

@@ -0,0 +1,32 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('..')
test('Constraints should not be overrided when multiple router is created', t => {
t.plan(1)
const constraint = {
name: 'secret',
storage: function () {
const secrets = {}
return {
get: (secret) => { return secrets[secret] || null },
set: (secret, store) => { secrets[secret] = store }
}
},
deriveConstraint: (req, ctx) => {
return req.headers['x-secret']
},
validate () { return true }
}
const router1 = FindMyWay({ constraints: { secret: constraint } })
FindMyWay()
router1.on('GET', '/', { constraints: { secret: 'alpha' } }, () => {})
router1.find('GET', '/', { secret: 'alpha' })
t.pass('constraints is not overrided')
})

619
node_modules/find-my-way/test/issue-28.test.js generated vendored Normal file
View File

@@ -0,0 +1,619 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('../')
test('wildcard (more complex test)', t => {
t.plan(3)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('we should not be here, the url is: ' + req.url)
}
})
findMyWay.on('GET', '/test/*', (req, res, params) => {
switch (params['*']) {
case 'hello':
t.ok('correct parameter')
break
case 'hello/world':
t.ok('correct parameter')
break
case '':
t.ok('correct parameter')
break
default:
t.fail('wrong parameter: ' + params['*'])
}
})
findMyWay.lookup(
{ method: 'GET', url: '/test/hello', headers: {} },
null
)
findMyWay.lookup(
{ method: 'GET', url: '/test/hello/world', headers: {} },
null
)
findMyWay.lookup(
{ method: 'GET', url: '/test/', headers: {} },
null
)
})
test('Wildcard inside a node with a static route but different method', t => {
t.plan(2)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('we should not be here, the url is: ' + req.url)
}
})
findMyWay.on('GET', '/test/hello', (req, res, params) => {
t.equal(req.method, 'GET')
})
findMyWay.on('OPTIONS', '/*', (req, res, params) => {
t.equal(req.method, 'OPTIONS')
})
findMyWay.lookup(
{ method: 'GET', url: '/test/hello', headers: {} },
null
)
findMyWay.lookup(
{ method: 'OPTIONS', url: '/test/hello', headers: {} },
null
)
})
test('Wildcard inside a node with a static route but different method (more complex case)', t => {
t.plan(5)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
if (req.url === '/test/helloo' && req.method === 'GET') {
t.ok('Everything fine')
} else {
t.fail('we should not be here, the url is: ' + req.url)
}
}
})
findMyWay.on('GET', '/test/hello', (req, res, params) => {
t.equal(req.method, 'GET')
})
findMyWay.on('OPTIONS', '/*', (req, res, params) => {
t.equal(req.method, 'OPTIONS')
})
findMyWay.lookup(
{ method: 'GET', url: '/test/hello', headers: {} },
null
)
findMyWay.lookup(
{ method: 'GET', url: '/test/helloo', headers: {} },
null
)
findMyWay.lookup(
{ method: 'OPTIONS', url: '/test/', headers: {} },
null
)
findMyWay.lookup(
{ method: 'OPTIONS', url: '/test', headers: {} },
null
)
findMyWay.lookup(
{ method: 'OPTIONS', url: '/test/helloo', headers: {} },
null
)
})
test('Wildcard edge cases', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('we should not be here, the url is: ' + req.url)
}
})
findMyWay.on('GET', '/test1/foo', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '/test2/foo', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('OPTIONS', '/*', (req, res, params) => {
t.equal(params['*'], 'test1/foo')
})
findMyWay.lookup(
{ method: 'OPTIONS', url: '/test1/foo', headers: {} },
null
)
})
test('Wildcard edge cases same method', t => {
t.plan(2)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('we should not be here, the url is: ' + req.url)
}
})
findMyWay.on('OPTIONS', '/test1/foo', (req, res, params) => {
t.equal(req.method, 'OPTIONS')
})
findMyWay.on('OPTIONS', '/test2/foo', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('OPTIONS', '/*', (req, res, params) => {
t.equal(params['*'], 'test/foo')
})
findMyWay.lookup(
{ method: 'OPTIONS', url: '/test1/foo', headers: {} },
null
)
findMyWay.lookup(
{ method: 'OPTIONS', url: '/test/foo', headers: {} },
null
)
})
test('Wildcard and parametric edge cases', t => {
t.plan(3)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('we should not be here, the url is: ' + req.url)
}
})
findMyWay.on('OPTIONS', '/test1/foo', (req, res, params) => {
t.equal(req.method, 'OPTIONS')
})
findMyWay.on('OPTIONS', '/test2/foo', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '/:test/foo', (req, res, params) => {
t.equal(params.test, 'example')
})
findMyWay.on('OPTIONS', '/*', (req, res, params) => {
t.equal(params['*'], 'test/foo/hey')
})
findMyWay.lookup(
{ method: 'OPTIONS', url: '/test1/foo', headers: {} },
null
)
findMyWay.lookup(
{ method: 'OPTIONS', url: '/test/foo/hey', headers: {} },
null
)
findMyWay.lookup(
{ method: 'GET', url: '/example/foo', headers: {} },
null
)
})
test('Mixed wildcard and static with same method', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('we should not be here, the url is: ' + req.url)
}
})
findMyWay.on('GET', '/foo1/bar1/baz', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '/foo1/bar2/baz', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '/foo2/bar2/baz', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '*', (req, res, params) => {
t.equal(params['*'], '/foo1/bar1/kuux')
})
findMyWay.lookup(
{ method: 'GET', url: '/foo1/bar1/kuux', headers: {} },
null
)
})
test('Nested wildcards case - 1', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('we should not be here, the url is: ' + req.url)
}
})
findMyWay.on('GET', '*', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '/foo1/*', (req, res, params) => {
t.equal(params['*'], 'bar1/kuux')
})
findMyWay.on('GET', '/foo2/*', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.lookup(
{ method: 'GET', url: '/foo1/bar1/kuux', headers: {} },
null
)
})
test('Nested wildcards case - 2', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('we should not be here, the url is: ' + req.url)
}
})
findMyWay.on('GET', '/foo2/*', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '/foo1/*', (req, res, params) => {
t.equal(params['*'], 'bar1/kuux')
})
findMyWay.on('GET', '*', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.lookup(
{ method: 'GET', url: '/foo1/bar1/kuux', headers: {} },
null
)
})
test('Nested wildcards with parametric and static - 1', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('we should not be here, the url is: ' + req.url)
}
})
findMyWay.on('GET', '*', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '/foo1/*', (req, res, params) => {
t.equal(params['*'], 'bar1/kuux')
})
findMyWay.on('GET', '/foo2/*', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '/foo3/:param', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '/foo4/param', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.lookup(
{ method: 'GET', url: '/foo1/bar1/kuux', headers: {} },
null
)
})
test('Nested wildcards with parametric and static - 2', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('we should not be here, the url is: ' + req.url)
}
})
findMyWay.on('GET', '*', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '/foo1/*', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '/foo2/*', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '/foo3/:param', (req, res, params) => {
t.equal(params.param, 'bar1')
})
findMyWay.on('GET', '/foo4/param', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.lookup(
{ method: 'GET', url: '/foo3/bar1', headers: {} },
null
)
})
test('Nested wildcards with parametric and static - 3', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('we should not be here, the url is: ' + req.url)
}
})
findMyWay.on('GET', '*', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '/foo1/*', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '/foo2/*', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '/foo3/:param', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '/foo4/param', (req, res, params) => {
t.equal(req.url, '/foo4/param')
})
findMyWay.lookup(
{ method: 'GET', url: '/foo4/param', headers: {} },
null
)
})
test('Nested wildcards with parametric and static - 4', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('we should not be here, the url is: ' + req.url)
}
})
findMyWay.on('GET', '*', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '/foo1/*', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '/foo2/*', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '/foo3/:param', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '/foo1/param', (req, res, params) => {
t.equal(req.url, '/foo1/param')
})
findMyWay.lookup(
{ method: 'GET', url: '/foo1/param', headers: {} },
null
)
})
test('Nested wildcards with parametric and static - 5', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('we should not be here, the url is: ' + req.url)
}
})
findMyWay.on('GET', '*', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '/foo1/*', (req, res, params) => {
t.equal(params['*'], 'param/hello/test/long/routee')
})
findMyWay.on('GET', '/foo2/*', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '/foo3/:param', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '/foo1/param/hello/test/long/route', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.lookup(
{ method: 'GET', url: '/foo1/param/hello/test/long/routee', headers: {} },
null
)
})
test('Nested wildcards with parametric and static - 6', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('we should not be here, the url is: ' + req.url)
}
})
findMyWay.on('GET', '*', (req, res, params) => {
t.equal(params['*'], '/foo4/param/hello/test/long/routee')
})
findMyWay.on('GET', '/foo1/*', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '/foo2/*', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '/foo3/:param', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '/foo4/param/hello/test/long/route', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.lookup(
{ method: 'GET', url: '/foo4/param/hello/test/long/routee', headers: {} },
null
)
})
test('Nested wildcards with parametric and static - 7', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('we should not be here, the url is: ' + req.url)
}
})
findMyWay.on('GET', '*', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '/foo1/*', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '/foo2/*', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '/foo3/:param', (req, res, params) => {
t.equal(params.param, 'hello')
})
findMyWay.on('GET', '/foo3/*', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '/foo4/example/hello/test/long/route', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.lookup(
{ method: 'GET', url: '/foo3/hello', headers: {} },
null
)
})
test('Nested wildcards with parametric and static - 8', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('we should not be here, the url is: ' + req.url)
}
})
findMyWay.on('GET', '*', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '/foo1/*', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '/foo2/*', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '/foo3/:param', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '/foo3/*', (req, res, params) => {
t.equal(params['*'], 'hello/world')
})
findMyWay.on('GET', '/foo4/param/hello/test/long/route', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.lookup(
{ method: 'GET', url: '/foo3/hello/world', headers: {} },
null
)
})
test('Wildcard node with constraints', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('we should not be here, the url is: ' + req.url)
}
})
findMyWay.on('GET', '*', { constraints: { host: 'fastify.io' } }, (req, res, params) => {
t.equal(params['*'], '/foo1/foo3')
})
findMyWay.on('GET', '/foo1/*', { constraints: { host: 'something-else.io' } }, (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.on('GET', '/foo1/foo2', (req, res, params) => {
t.fail('we should not be here, the url is: ' + req.url)
})
findMyWay.lookup(
{ method: 'GET', url: '/foo1/foo3', headers: { host: 'fastify.io' } },
null
)
})
test('Wildcard must be the last character in the route', (t) => {
t.plan(6)
const expectedError = new Error('Wildcard must be the last character in the route')
const findMyWay = FindMyWay()
t.throws(() => findMyWay.on('GET', '*1', () => {}), expectedError)
t.throws(() => findMyWay.on('GET', '*/', () => {}), expectedError)
t.throws(() => findMyWay.on('GET', '*?', () => {}), expectedError)
t.throws(() => findMyWay.on('GET', '/foo*123', () => {}), expectedError)
t.throws(() => findMyWay.on('GET', '/foo*?', () => {}), expectedError)
t.throws(() => findMyWay.on('GET', '/foo*/', () => {}), expectedError)
})

15
node_modules/find-my-way/test/issue-280.test.js generated vendored Normal file
View File

@@ -0,0 +1,15 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('../')
test('Wildcard route match when regexp route fails', (t) => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/:a(a)', () => {})
findMyWay.on('GET', '/*', () => {})
t.same(findMyWay.find('GET', '/b', {}).params, { '*': 'b' })
})

38
node_modules/find-my-way/test/issue-285.test.js generated vendored Normal file
View File

@@ -0,0 +1,38 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('../')
test('Parametric regex match with similar routes', (t) => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/:a(a)', () => {})
findMyWay.on('GET', '/:param/static', () => {})
t.same(findMyWay.find('GET', '/a', {}).params, { a: 'a' })
t.same(findMyWay.find('GET', '/param/static', {}).params, { param: 'param' })
})
test('Parametric regex match with similar routes', (t) => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/:a(a)', () => {})
findMyWay.on('GET', '/:b(b)/static', () => {})
t.same(findMyWay.find('GET', '/a', {}).params, { a: 'a' })
t.same(findMyWay.find('GET', '/b/static', {}).params, { b: 'b' })
})
test('Parametric regex match with similar routes', (t) => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/:a(a)/static', { constraints: { version: '1.0.0' } }, () => {})
findMyWay.on('GET', '/:b(b)/static', { constraints: { version: '2.0.0' } }, () => {})
t.same(findMyWay.find('GET', '/a/static', { version: '1.0.0' }).params, { a: 'a' })
t.same(findMyWay.find('GET', '/b/static', { version: '2.0.0' }).params, { b: 'b' })
})

232
node_modules/find-my-way/test/issue-330.test.js generated vendored Normal file
View File

@@ -0,0 +1,232 @@
const t = require('tap')
const test = t.test
const FindMyWay = require('..')
const proxyquire = require('proxyquire')
const HandlerStorage = require('../lib/handler-storage')
const Constrainer = require('../lib/constrainer')
const { safeDecodeURIComponent } = require('../lib/url-sanitizer')
const acceptVersionStrategy = require('../lib/strategies/accept-version')
const httpMethodStrategy = require('../lib/strategies/http-method')
test('FULL_PATH_REGEXP and OPTIONAL_PARAM_REGEXP should be considered safe', (t) => {
t.plan(1)
t.doesNotThrow(() => require('..'))
})
test('should throw an error for unsafe FULL_PATH_REGEXP', (t) => {
t.plan(1)
t.throws(() => proxyquire('..', {
'safe-regex2': () => false
}), new Error('the FULL_PATH_REGEXP is not safe, update this module'))
})
test('Should throw an error for unsafe OPTIONAL_PARAM_REGEXP', (t) => {
t.plan(1)
let callCount = 0
t.throws(() => proxyquire('..', {
'safe-regex2': () => {
return ++callCount < 2
}
}), new Error('the OPTIONAL_PARAM_REGEXP is not safe, update this module'))
})
test('double colon does not define parametric node', (t) => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/::id', () => {})
const route1 = findMyWay.findRoute('GET', '/::id')
t.strictSame(route1.params, [])
findMyWay.on('GET', '/:foo(\\d+)::bar', () => {})
const route2 = findMyWay.findRoute('GET', '/:foo(\\d+)::bar')
t.strictSame(route2.params, ['foo'])
})
test('case insensitive static routes', (t) => {
t.plan(3)
const findMyWay = FindMyWay({
caseSensitive: false
})
findMyWay.on('GET', '/foo', () => {})
findMyWay.on('GET', '/foo/bar', () => {})
findMyWay.on('GET', '/foo/bar/baz', () => {})
t.ok(findMyWay.findRoute('GET', '/FoO'))
t.ok(findMyWay.findRoute('GET', '/FOo/Bar'))
t.ok(findMyWay.findRoute('GET', '/fOo/Bar/bAZ'))
})
test('wildcard must be the last character in the route', (t) => {
t.plan(3)
const expectedError = new Error('Wildcard must be the last character in the route')
const findMyWay = FindMyWay()
findMyWay.on('GET', '*', () => {})
t.throws(() => findMyWay.findRoute('GET', '*1'), expectedError)
t.throws(() => findMyWay.findRoute('GET', '*/'), expectedError)
t.throws(() => findMyWay.findRoute('GET', '*?'), expectedError)
})
test('does not find the route if maxParamLength is exceeded', t => {
t.plan(2)
const findMyWay = FindMyWay({
maxParamLength: 2
})
findMyWay.on('GET', '/:id(\\d+)', () => {})
t.equal(findMyWay.find('GET', '/123'), null)
t.ok(findMyWay.find('GET', '/12'))
})
test('Should check if a regex is safe to use', (t) => {
t.plan(1)
const findMyWay = FindMyWay()
// we must pass a safe regex to register the route
// findRoute will still throws the expected assertion error if we try to access it with unsafe reggex
findMyWay.on('GET', '/test/:id(\\d+)', () => {})
const unSafeRegex = /(x+x+)+y/
t.throws(() => findMyWay.findRoute('GET', `/test/:id(${unSafeRegex.toString()})`), {
message: "The regex '(/(x+x+)+y/)' is not safe!"
})
})
test('Disable safe regex check', (t) => {
t.plan(1)
const findMyWay = FindMyWay({ allowUnsafeRegex: true })
const unSafeRegex = /(x+x+)+y/
findMyWay.on('GET', `/test2/:id(${unSafeRegex.toString()})`, () => {})
t.doesNotThrow(() => findMyWay.findRoute('GET', `/test2/:id(${unSafeRegex.toString()})`))
})
test('throws error if no strategy registered for constraint key', (t) => {
t.plan(2)
const constrainer = new Constrainer()
const error = new Error('No strategy registered for constraint key invalid-constraint')
t.throws(() => constrainer.newStoreForConstraint('invalid-constraint'), error)
t.throws(() => constrainer.validateConstraints({ 'invalid-constraint': 'foo' }), error)
})
test('throws error if pass an undefined constraint value', (t) => {
t.plan(1)
const constrainer = new Constrainer()
const error = new Error('Can\'t pass an undefined constraint value, must pass null or no key at all')
t.throws(() => constrainer.validateConstraints({ key: undefined }), error)
})
test('Constrainer.noteUsage', (t) => {
t.plan(3)
const constrainer = new Constrainer()
t.equal(constrainer.strategiesInUse.size, 0)
constrainer.noteUsage()
t.equal(constrainer.strategiesInUse.size, 0)
constrainer.noteUsage({ host: 'fastify.io' })
t.equal(constrainer.strategiesInUse.size, 1)
})
test('Cannot derive constraints without active strategies.', (t) => {
t.plan(1)
const constrainer = new Constrainer()
const before = constrainer.deriveSyncConstraints
constrainer._buildDeriveConstraints()
t.sameStrict(constrainer.deriveSyncConstraints, before)
})
test('getMatchingHandler should return null if not compiled', (t) => {
t.plan(1)
const handlerStorage = new HandlerStorage()
t.equal(handlerStorage.getMatchingHandler({ foo: 'bar' }), null)
})
test('safeDecodeURIComponent should replace %3x to null for every x that is not a valid lowchar', (t) => {
t.plan(1)
t.equal(safeDecodeURIComponent('Hello%3xWorld'), 'HellonullWorld')
})
test('SemVerStore version should be a string', (t) => {
t.plan(1)
const Storage = acceptVersionStrategy.storage
t.throws(() => new Storage().set(1), new TypeError('Version should be a string'))
})
test('SemVerStore.maxMajor should increase automatically', (t) => {
t.plan(3)
const Storage = acceptVersionStrategy.storage
const storage = new Storage()
t.equal(storage.maxMajor, 0)
storage.set('2')
t.equal(storage.maxMajor, 2)
storage.set('1')
t.equal(storage.maxMajor, 2)
})
test('SemVerStore.maxPatches should increase automatically', (t) => {
t.plan(3)
const Storage = acceptVersionStrategy.storage
const storage = new Storage()
storage.set('2.0.0')
t.sameStrict(storage.maxPatches, { '2.0': 0 })
storage.set('2.0.2')
t.sameStrict(storage.maxPatches, { '2.0': 2 })
storage.set('2.0.1')
t.sameStrict(storage.maxPatches, { '2.0': 2 })
})
test('Major version must be a numeric value', t => {
t.plan(1)
const findMyWay = FindMyWay()
t.throws(() => findMyWay.on('GET', '/test', { constraints: { version: 'x' } }, () => {}),
new TypeError('Major version must be a numeric value'))
})
test('httpMethodStrategy storage handles set and get operations correctly', (t) => {
t.plan(2)
const storage = httpMethodStrategy.storage()
t.equal(storage.get('foo'), null)
storage.set('foo', { bar: 'baz' })
t.strictSame(storage.get('foo'), { bar: 'baz' })
})
test('if buildPrettyMeta argument is undefined, will return an object', (t) => {
t.plan(1)
const findMyWay = FindMyWay()
t.sameStrict(findMyWay.buildPrettyMeta(), {})
})

150
node_modules/find-my-way/test/issue-44.test.js generated vendored Normal file
View File

@@ -0,0 +1,150 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('../')
test('Parametric and static with shared prefix / 1', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/woo', (req, res, params) => {
t.fail('we should not be here')
})
findMyWay.on('GET', '/:param', (req, res, params) => {
t.equal(params.param, 'winter')
})
findMyWay.lookup({ method: 'GET', url: '/winter', headers: {} }, null)
})
test('Parametric and static with shared prefix / 2', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/woo', (req, res, params) => {
t.ok('we should be here')
})
findMyWay.on('GET', '/:param', (req, res, params) => {
t.fail('we should not be here')
})
findMyWay.lookup({ method: 'GET', url: '/woo', headers: {} }, null)
})
test('Parametric and static with shared prefix (nested)', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.ok('We should be here')
}
})
findMyWay.on('GET', '/woo', (req, res, params) => {
t.fail('we should not be here')
})
findMyWay.on('GET', '/:param', (req, res, params) => {
t.fail('we should not be here')
})
findMyWay.lookup({ method: 'GET', url: '/winter/coming', headers: {} }, null)
})
test('Parametric and static with shared prefix and different suffix', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('We should not be here')
}
})
findMyWay.on('GET', '/example/shared/nested/test', (req, res, params) => {
t.fail('We should not be here')
})
findMyWay.on('GET', '/example/:param/nested/other', (req, res, params) => {
t.ok('We should be here')
})
findMyWay.lookup({ method: 'GET', url: '/example/shared/nested/other', headers: {} }, null)
})
test('Parametric and static with shared prefix (with wildcard)', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/woo', (req, res, params) => {
t.fail('we should not be here')
})
findMyWay.on('GET', '/:param', (req, res, params) => {
t.equal(params.param, 'winter')
})
findMyWay.on('GET', '/*', (req, res, params) => {
t.fail('we should not be here')
})
findMyWay.lookup({ method: 'GET', url: '/winter', headers: {} }, null)
})
test('Parametric and static with shared prefix (nested with wildcard)', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/woo', (req, res, params) => {
t.fail('we should not be here')
})
findMyWay.on('GET', '/:param', (req, res, params) => {
t.fail('we should not be here')
})
findMyWay.on('GET', '/*', (req, res, params) => {
t.equal(params['*'], 'winter/coming')
})
findMyWay.lookup({ method: 'GET', url: '/winter/coming', headers: {} }, null)
})
test('Parametric and static with shared prefix (nested with split)', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('we should not be here')
}
})
findMyWay.on('GET', '/woo', (req, res, params) => {
t.fail('we should not be here')
})
findMyWay.on('GET', '/:param', (req, res, params) => {
t.equal(params.param, 'winter')
})
findMyWay.on('GET', '/wo', (req, res, params) => {
t.fail('we should not be here')
})
findMyWay.lookup({ method: 'GET', url: '/winter', headers: {} }, null)
})

76
node_modules/find-my-way/test/issue-46.test.js generated vendored Normal file
View File

@@ -0,0 +1,76 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('../')
test('If the prefixLen is higher than the pathLen we should not save the wildcard child', t => {
t.plan(3)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.get('/static/*', () => {})
t.same(findMyWay.find('GET', '/static/').params, { '*': '' })
t.same(findMyWay.find('GET', '/static/hello').params, { '*': 'hello' })
t.same(findMyWay.find('GET', '/static'), null)
})
test('If the prefixLen is higher than the pathLen we should not save the wildcard child (mixed routes)', t => {
t.plan(3)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.get('/static/*', () => {})
findMyWay.get('/simple', () => {})
findMyWay.get('/simple/:bar', () => {})
findMyWay.get('/hello', () => {})
t.same(findMyWay.find('GET', '/static/').params, { '*': '' })
t.same(findMyWay.find('GET', '/static/hello').params, { '*': 'hello' })
t.same(findMyWay.find('GET', '/static'), null)
})
test('If the prefixLen is higher than the pathLen we should not save the wildcard child (with a root wildcard)', t => {
t.plan(3)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.get('*', () => {})
findMyWay.get('/static/*', () => {})
findMyWay.get('/simple', () => {})
findMyWay.get('/simple/:bar', () => {})
findMyWay.get('/hello', () => {})
t.same(findMyWay.find('GET', '/static/').params, { '*': '' })
t.same(findMyWay.find('GET', '/static/hello').params, { '*': 'hello' })
t.same(findMyWay.find('GET', '/static').params, { '*': '/static' })
})
test('If the prefixLen is higher than the pathLen we should not save the wildcard child (404)', t => {
t.plan(4)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.get('/static/*', () => {})
findMyWay.get('/simple', () => {})
findMyWay.get('/simple/:bar', () => {})
findMyWay.get('/hello', () => {})
t.same(findMyWay.find('GET', '/stati'), null)
t.same(findMyWay.find('GET', '/staticc'), null)
t.same(findMyWay.find('GET', '/stati/hello'), null)
t.same(findMyWay.find('GET', '/staticc/hello'), null)
})

109
node_modules/find-my-way/test/issue-49.test.js generated vendored Normal file
View File

@@ -0,0 +1,109 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('../')
const noop = () => {}
test('Defining static route after parametric - 1', t => {
t.plan(3)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/static', noop)
findMyWay.on('GET', '/:param', noop)
t.ok(findMyWay.find('GET', '/static'))
t.ok(findMyWay.find('GET', '/para'))
t.ok(findMyWay.find('GET', '/s'))
})
test('Defining static route after parametric - 2', t => {
t.plan(3)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/:param', noop)
findMyWay.on('GET', '/static', noop)
t.ok(findMyWay.find('GET', '/static'))
t.ok(findMyWay.find('GET', '/para'))
t.ok(findMyWay.find('GET', '/s'))
})
test('Defining static route after parametric - 3', t => {
t.plan(4)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/:param', noop)
findMyWay.on('GET', '/static', noop)
findMyWay.on('GET', '/other', noop)
t.ok(findMyWay.find('GET', '/static'))
t.ok(findMyWay.find('GET', '/para'))
t.ok(findMyWay.find('GET', '/s'))
t.ok(findMyWay.find('GET', '/o'))
})
test('Defining static route after parametric - 4', t => {
t.plan(4)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/static', noop)
findMyWay.on('GET', '/other', noop)
findMyWay.on('GET', '/:param', noop)
t.ok(findMyWay.find('GET', '/static'))
t.ok(findMyWay.find('GET', '/para'))
t.ok(findMyWay.find('GET', '/s'))
t.ok(findMyWay.find('GET', '/o'))
})
test('Defining static route after parametric - 5', t => {
t.plan(4)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/static', noop)
findMyWay.on('GET', '/:param', noop)
findMyWay.on('GET', '/other', noop)
t.ok(findMyWay.find('GET', '/static'))
t.ok(findMyWay.find('GET', '/para'))
t.ok(findMyWay.find('GET', '/s'))
t.ok(findMyWay.find('GET', '/o'))
})
test('Should produce the same tree - 1', t => {
t.plan(1)
const findMyWay1 = FindMyWay()
const findMyWay2 = FindMyWay()
findMyWay1.on('GET', '/static', noop)
findMyWay1.on('GET', '/:param', noop)
findMyWay2.on('GET', '/:param', noop)
findMyWay2.on('GET', '/static', noop)
t.equal(findMyWay1.tree, findMyWay2.tree)
})
test('Should produce the same tree - 2', t => {
t.plan(3)
const findMyWay1 = FindMyWay()
const findMyWay2 = FindMyWay()
const findMyWay3 = FindMyWay()
findMyWay1.on('GET', '/:param', noop)
findMyWay1.on('GET', '/static', noop)
findMyWay1.on('GET', '/other', noop)
findMyWay2.on('GET', '/static', noop)
findMyWay2.on('GET', '/:param', noop)
findMyWay2.on('GET', '/other', noop)
findMyWay3.on('GET', '/static', noop)
findMyWay3.on('GET', '/other', noop)
findMyWay3.on('GET', '/:param', noop)
t.equal(findMyWay1.tree, findMyWay2.tree)
t.equal(findMyWay2.tree, findMyWay3.tree)
t.equal(findMyWay1.tree, findMyWay3.tree)
})

132
node_modules/find-my-way/test/issue-59.test.js generated vendored Normal file
View File

@@ -0,0 +1,132 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('../')
const noop = () => {}
test('single-character prefix', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/b/', noop)
findMyWay.on('GET', '/b/bulk', noop)
t.equal(findMyWay.find('GET', '/bulk'), null)
})
test('multi-character prefix', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/bu/', noop)
findMyWay.on('GET', '/bu/bulk', noop)
t.equal(findMyWay.find('GET', '/bulk'), null)
})
test('static / 1', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/bb/', noop)
findMyWay.on('GET', '/bb/bulk', noop)
t.equal(findMyWay.find('GET', '/bulk'), null)
})
test('static / 2', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/bb/ff/', noop)
findMyWay.on('GET', '/bb/ff/bulk', noop)
t.equal(findMyWay.find('GET', '/bulk'), null)
t.equal(findMyWay.find('GET', '/ff/bulk'), null)
})
test('static / 3', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/bb/ff/', noop)
findMyWay.on('GET', '/bb/ff/bulk', noop)
findMyWay.on('GET', '/bb/ff/gg/bulk', noop)
findMyWay.on('GET', '/bb/ff/bulk/bulk', noop)
t.equal(findMyWay.find('GET', '/bulk'), null)
})
test('with parameter / 1', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/:foo/', noop)
findMyWay.on('GET', '/:foo/bulk', noop)
t.equal(findMyWay.find('GET', '/bulk'), null)
})
test('with parameter / 2', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/bb/', noop)
findMyWay.on('GET', '/bb/:foo', noop)
t.equal(findMyWay.find('GET', '/bulk'), null)
})
test('with parameter / 3', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/bb/ff/', noop)
findMyWay.on('GET', '/bb/ff/:foo', noop)
t.equal(findMyWay.find('GET', '/bulk'), null)
})
test('with parameter / 4', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/bb/:foo/', noop)
findMyWay.on('GET', '/bb/:foo/bulk', noop)
t.equal(findMyWay.find('GET', '/bulk'), null)
})
test('with parameter / 5', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/bb/:foo/aa/', noop)
findMyWay.on('GET', '/bb/:foo/aa/bulk', noop)
t.equal(findMyWay.find('GET', '/bulk'), null)
t.equal(findMyWay.find('GET', '/bb/foo/bulk'), null)
})
test('with parameter / 6', t => {
t.plan(3)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/static/:parametric/static/:parametric', noop)
findMyWay.on('GET', '/static/:parametric/static/:parametric/bulk', noop)
t.equal(findMyWay.find('GET', '/bulk'), null)
t.equal(findMyWay.find('GET', '/static/foo/bulk'), null)
t.not(findMyWay.find('GET', '/static/foo/static/bulk'), null)
})
test('wildcard / 1', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/bb/', noop)
findMyWay.on('GET', '/bb/*', noop)
t.equal(findMyWay.find('GET', '/bulk'), null)
})

28
node_modules/find-my-way/test/issue-62.test.js generated vendored Normal file
View File

@@ -0,0 +1,28 @@
'use strict'
const t = require('tap')
const FindMyWay = require('../')
const noop = function () {}
t.test('issue-62', (t) => {
t.plan(2)
const findMyWay = FindMyWay({ allowUnsafeRegex: true })
findMyWay.on('GET', '/foo/:id(([a-f0-9]{3},?)+)', noop)
t.notOk(findMyWay.find('GET', '/foo/qwerty'))
t.ok(findMyWay.find('GET', '/foo/bac,1ea'))
})
t.test('issue-62 - escape chars', (t) => {
const findMyWay = FindMyWay()
t.plan(2)
findMyWay.get('/foo/:param(\\([a-f0-9]{3}\\))', noop)
t.notOk(findMyWay.find('GET', '/foo/abc'))
t.ok(findMyWay.find('GET', '/foo/(abc)', {}))
})

23
node_modules/find-my-way/test/issue-63.test.js generated vendored Normal file
View File

@@ -0,0 +1,23 @@
'use strict'
const t = require('tap')
const factory = require('../')
const noop = function () {}
t.test('issue-63', (t) => {
t.plan(2)
const fmw = factory()
t.throws(function () {
fmw.on('GET', '/foo/:id(a', noop)
})
try {
fmw.on('GET', '/foo/:id(a', noop)
t.fail('should fail')
} catch (err) {
t.equal(err.message, 'Invalid regexp expression in "/foo/:id(a"')
}
})

51
node_modules/find-my-way/test/issue-67.test.js generated vendored Normal file
View File

@@ -0,0 +1,51 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('../')
const noop = () => {}
test('static routes', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/b/', noop)
findMyWay.on('GET', '/b/bulk', noop)
findMyWay.on('GET', '/b/ulk', noop)
t.equal(findMyWay.find('GET', '/bulk'), null)
})
test('parametric routes', t => {
t.plan(5)
const findMyWay = FindMyWay()
function foo () { }
findMyWay.on('GET', '/foo/:fooParam', foo)
findMyWay.on('GET', '/foo/bar/:barParam', noop)
findMyWay.on('GET', '/foo/search', noop)
findMyWay.on('GET', '/foo/submit', noop)
t.equal(findMyWay.find('GET', '/foo/awesome-parameter').handler, foo)
t.equal(findMyWay.find('GET', '/foo/b-first-character').handler, foo)
t.equal(findMyWay.find('GET', '/foo/s-first-character').handler, foo)
t.equal(findMyWay.find('GET', '/foo/se-prefix').handler, foo)
t.equal(findMyWay.find('GET', '/foo/sx-prefix').handler, foo)
})
test('parametric with common prefix', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test', noop)
findMyWay.on('GET', '/:test', (req, res, params) => {
t.same(
{ test: 'text' },
params
)
})
findMyWay.on('GET', '/text/hello', noop)
findMyWay.lookup({ url: '/text', method: 'GET', headers: {} })
})

20
node_modules/find-my-way/test/issue-93.test.js generated vendored Normal file
View File

@@ -0,0 +1,20 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('../')
const noop = () => {}
test('Should keep semver store when split node', t => {
t.plan(4)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/t1', { constraints: { version: '1.0.0' } }, noop)
findMyWay.on('GET', '/t2', { constraints: { version: '2.1.0' } }, noop)
t.ok(findMyWay.find('GET', '/t1', { version: '1.0.0' }))
t.ok(findMyWay.find('GET', '/t2', { version: '2.x' }))
t.notOk(findMyWay.find('GET', '/t1', { version: '2.x' }))
t.notOk(findMyWay.find('GET', '/t2', { version: '1.0.0' }))
})

30
node_modules/find-my-way/test/lookup-async.test.js generated vendored Normal file
View File

@@ -0,0 +1,30 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('..')
test('should return result in the done callback', t => {
t.plan(2)
const router = FindMyWay()
router.on('GET', '/', () => 'asyncHandlerResult')
router.lookup({ method: 'GET', url: '/' }, null, (err, result) => {
t.equal(err, null)
t.equal(result, 'asyncHandlerResult')
})
})
test('should return an error in the done callback', t => {
t.plan(2)
const router = FindMyWay()
const error = new Error('ASYNC_HANDLER_ERROR')
router.on('GET', '/', () => { throw error })
router.lookup({ method: 'GET', url: '/' }, null, (err, result) => {
t.equal(err, error)
t.equal(result, undefined)
})
})

59
node_modules/find-my-way/test/lookup.test.js generated vendored Normal file
View File

@@ -0,0 +1,59 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('..')
test('lookup calls route handler with no context', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/example', function handle (req, res, params) {
// without context, this will be the result object returned from router.find
t.equal(this.handler, handle)
})
findMyWay.lookup({ method: 'GET', url: '/example', headers: {} }, null)
})
test('lookup calls route handler with context as scope', t => {
t.plan(1)
const findMyWay = FindMyWay()
const ctx = { foo: 'bar' }
findMyWay.on('GET', '/example', function handle (req, res, params) {
t.equal(this, ctx)
})
findMyWay.lookup({ method: 'GET', url: '/example', headers: {} }, null, ctx)
})
test('lookup calls default route handler with no context', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute (req, res) {
// without context, the default route's scope is the router itself
t.equal(this, findMyWay)
}
})
findMyWay.lookup({ method: 'GET', url: '/example', headers: {} }, null)
})
test('lookup calls default route handler with context as scope', t => {
t.plan(1)
const ctx = { foo: 'bar' }
const findMyWay = FindMyWay({
defaultRoute (req, res) {
t.equal(this, ctx)
}
})
findMyWay.lookup({ method: 'GET', url: '/example', headers: {} }, null, ctx)
})

18
node_modules/find-my-way/test/matching-order.test.js generated vendored Normal file
View File

@@ -0,0 +1,18 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('..')
test('Matching order', t => {
t.plan(3)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/foo/bar/static', { constraints: { host: 'test' } }, () => {})
findMyWay.on('GET', '/foo/bar/*', () => {})
findMyWay.on('GET', '/foo/:param/static', () => {})
t.same(findMyWay.find('GET', '/foo/bar/static', { host: 'test' }).params, {})
t.same(findMyWay.find('GET', '/foo/bar/static').params, { '*': 'static' })
t.same(findMyWay.find('GET', '/foo/value/static').params, { param: 'value' })
})

45
node_modules/find-my-way/test/max-param-length.test.js generated vendored Normal file
View File

@@ -0,0 +1,45 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('../')
test('maxParamLength default value is 500', t => {
t.plan(1)
const findMyWay = FindMyWay()
t.equal(findMyWay.maxParamLength, 100)
})
test('maxParamLength should set the maximum length for a parametric route', t => {
t.plan(1)
const findMyWay = FindMyWay({ maxParamLength: 10 })
findMyWay.on('GET', '/test/:param', () => {})
t.same(findMyWay.find('GET', '/test/123456789abcd'), null)
})
test('maxParamLength should set the maximum length for a parametric (regex) route', t => {
t.plan(1)
const findMyWay = FindMyWay({ maxParamLength: 10 })
findMyWay.on('GET', '/test/:param(^\\d+$)', () => {})
t.same(findMyWay.find('GET', '/test/123456789abcd'), null)
})
test('maxParamLength should set the maximum length for a parametric (multi) route', t => {
t.plan(1)
const findMyWay = FindMyWay({ maxParamLength: 10 })
findMyWay.on('GET', '/test/:param-bar', () => {})
t.same(findMyWay.find('GET', '/test/123456789abcd'), null)
})
test('maxParamLength should set the maximum length for a parametric (regex with suffix) route', t => {
t.plan(1)
const findMyWay = FindMyWay({ maxParamLength: 10 })
findMyWay.on('GET', '/test/:param(^\\w{3})bar', () => {})
t.same(findMyWay.find('GET', '/test/123456789abcd'), null)
})

830
node_modules/find-my-way/test/methods.test.js generated vendored Normal file
View File

@@ -0,0 +1,830 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('../')
test('the router is an object with methods', t => {
t.plan(4)
const findMyWay = FindMyWay()
t.equal(typeof findMyWay.on, 'function')
t.equal(typeof findMyWay.off, 'function')
t.equal(typeof findMyWay.lookup, 'function')
t.equal(typeof findMyWay.find, 'function')
})
test('on throws for invalid method', t => {
t.plan(1)
const findMyWay = FindMyWay()
t.throws(() => {
findMyWay.on('INVALID', '/a/b')
})
})
test('on throws for invalid path', t => {
t.plan(3)
const findMyWay = FindMyWay()
// Non string
t.throws(() => {
findMyWay.on('GET', 1)
})
// Empty
t.throws(() => {
findMyWay.on('GET', '')
})
// Doesn't start with / or *
t.throws(() => {
findMyWay.on('GET', 'invalid')
})
})
test('register a route', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test', () => {
t.ok('inside the handler')
})
findMyWay.lookup({ method: 'GET', url: '/test', headers: {} }, null)
})
test('register a route with multiple methods', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on(['GET', 'POST'], '/test', () => {
t.ok('inside the handler')
})
findMyWay.lookup({ method: 'GET', url: '/test', headers: {} }, null)
findMyWay.lookup({ method: 'POST', url: '/test', headers: {} }, null)
})
test('does not register /test/*/ when ignoreTrailingSlash is true', t => {
t.plan(1)
const findMyWay = FindMyWay({
ignoreTrailingSlash: true
})
findMyWay.on('GET', '/test/*', () => {})
t.equal(
findMyWay.routes.filter((r) => r.path.includes('/test')).length,
1
)
})
test('off throws for invalid method', t => {
t.plan(1)
const findMyWay = FindMyWay()
t.throws(() => {
findMyWay.off('INVALID', '/a/b')
})
})
test('off throws for invalid path', t => {
t.plan(3)
const findMyWay = FindMyWay()
// Non string
t.throws(() => {
findMyWay.off('GET', 1)
})
// Empty
t.throws(() => {
findMyWay.off('GET', '')
})
// Doesn't start with / or *
t.throws(() => {
findMyWay.off('GET', 'invalid')
})
})
test('off with nested wildcards with parametric and static', t => {
t.plan(3)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('we should not be here, the url is: ' + req.url)
}
})
findMyWay.on('GET', '*', (req, res, params) => {
t.equal(params['*'], '/foo2/first/second')
})
findMyWay.on('GET', '/foo1/*', () => {})
findMyWay.on('GET', '/foo2/*', () => {})
findMyWay.on('GET', '/foo3/:param', () => {})
findMyWay.on('GET', '/foo3/*', () => {})
findMyWay.on('GET', '/foo4/param/hello/test/long/route', () => {})
const route1 = findMyWay.find('GET', '/foo3/first/second')
t.equal(route1.params['*'], 'first/second')
findMyWay.off('GET', '/foo3/*')
const route2 = findMyWay.find('GET', '/foo3/first/second')
t.equal(route2.params['*'], '/foo3/first/second')
findMyWay.off('GET', '/foo2/*')
findMyWay.lookup(
{ method: 'GET', url: '/foo2/first/second', headers: {} },
null
)
})
test('off removes all routes when ignoreTrailingSlash is true', t => {
t.plan(6)
const findMyWay = FindMyWay({
ignoreTrailingSlash: true
})
findMyWay.on('GET', '/test1/', () => {})
t.equal(findMyWay.routes.length, 1)
findMyWay.on('GET', '/test2', () => {})
t.equal(findMyWay.routes.length, 2)
findMyWay.off('GET', '/test1')
t.equal(findMyWay.routes.length, 1)
t.equal(
findMyWay.routes.filter((r) => r.path === '/test2').length,
1
)
t.equal(
findMyWay.routes.filter((r) => r.path === '/test2/').length,
0
)
findMyWay.off('GET', '/test2/')
t.equal(findMyWay.routes.length, 0)
})
test('off removes all routes when ignoreDuplicateSlashes is true', t => {
t.plan(6)
const findMyWay = FindMyWay({
ignoreDuplicateSlashes: true
})
findMyWay.on('GET', '//test1', () => {})
t.equal(findMyWay.routes.length, 1)
findMyWay.on('GET', '/test2', () => {})
t.equal(findMyWay.routes.length, 2)
findMyWay.off('GET', '/test1')
t.equal(findMyWay.routes.length, 1)
t.equal(
findMyWay.routes.filter((r) => r.path === '/test2').length,
1
)
t.equal(
findMyWay.routes.filter((r) => r.path === '//test2').length,
0
)
findMyWay.off('GET', '//test2')
t.equal(findMyWay.routes.length, 0)
})
test('deregister a route without children', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/a', () => {})
findMyWay.on('GET', '/a/b', () => {})
findMyWay.off('GET', '/a/b')
t.ok(findMyWay.find('GET', '/a'))
t.notOk(findMyWay.find('GET', '/a/b'))
})
test('deregister a route with children', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/a', () => {})
findMyWay.on('GET', '/a/b', () => {})
findMyWay.off('GET', '/a')
t.notOk(findMyWay.find('GET', '/a'))
t.ok(findMyWay.find('GET', '/a/b'))
})
test('deregister a route by method', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on(['GET', 'POST'], '/a', () => {})
findMyWay.off('GET', '/a')
t.notOk(findMyWay.find('GET', '/a'))
t.ok(findMyWay.find('POST', '/a'))
})
test('deregister a route with multiple methods', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on(['GET', 'POST'], '/a', () => {})
findMyWay.off(['GET', 'POST'], '/a')
t.notOk(findMyWay.find('GET', '/a'))
t.notOk(findMyWay.find('POST', '/a'))
})
test('reset a router', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on(['GET', 'POST'], '/a', () => {})
findMyWay.reset()
t.notOk(findMyWay.find('GET', '/a'))
t.notOk(findMyWay.find('POST', '/a'))
})
test('default route', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: () => {
t.ok('inside the default route')
}
})
findMyWay.lookup({ method: 'GET', url: '/test', headers: {} }, null)
})
test('parametric route', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test/:id', (req, res, params) => {
t.equal(params.id, 'hello')
})
findMyWay.lookup({ method: 'GET', url: '/test/hello', headers: {} }, null)
})
test('multiple parametric route', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test/:id', (req, res, params) => {
t.equal(params.id, 'hello')
})
findMyWay.on('GET', '/other-test/:id', (req, res, params) => {
t.equal(params.id, 'world')
})
findMyWay.lookup({ method: 'GET', url: '/test/hello', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/other-test/world', headers: {} }, null)
})
test('multiple parametric route with the same prefix', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test/:id', (req, res, params) => {
t.equal(params.id, 'hello')
})
findMyWay.on('GET', '/test/:id/world', (req, res, params) => {
t.equal(params.id, 'world')
})
findMyWay.lookup({ method: 'GET', url: '/test/hello', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/test/world/world', headers: {} }, null)
})
test('nested parametric route', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test/:hello/test/:world', (req, res, params) => {
t.equal(params.hello, 'hello')
t.equal(params.world, 'world')
})
findMyWay.lookup({ method: 'GET', url: '/test/hello/test/world', headers: {} }, null)
})
test('nested parametric route with same prefix', t => {
t.plan(3)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test', (req, res, params) => {
t.ok('inside route')
})
findMyWay.on('GET', '/test/:hello/test/:world', (req, res, params) => {
t.equal(params.hello, 'hello')
t.equal(params.world, 'world')
})
findMyWay.lookup({ method: 'GET', url: '/test', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/test/hello/test/world', headers: {} }, null)
})
test('long route', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/abc/def/ghi/lmn/opq/rst/uvz', (req, res, params) => {
t.ok('inside long path')
})
findMyWay.lookup({ method: 'GET', url: '/abc/def/ghi/lmn/opq/rst/uvz', headers: {} }, null)
})
test('long parametric route', t => {
t.plan(3)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/abc/:def/ghi/:lmn/opq/:rst/uvz', (req, res, params) => {
t.equal(params.def, 'def')
t.equal(params.lmn, 'lmn')
t.equal(params.rst, 'rst')
})
findMyWay.lookup({ method: 'GET', url: '/abc/def/ghi/lmn/opq/rst/uvz', headers: {} }, null)
})
test('long parametric route with common prefix', t => {
t.plan(9)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/', (req, res, params) => {
throw new Error('I shoul not be here')
})
findMyWay.on('GET', '/abc', (req, res, params) => {
throw new Error('I shoul not be here')
})
findMyWay.on('GET', '/abc/:def', (req, res, params) => {
t.equal(params.def, 'def')
})
findMyWay.on('GET', '/abc/:def/ghi/:lmn', (req, res, params) => {
t.equal(params.def, 'def')
t.equal(params.lmn, 'lmn')
})
findMyWay.on('GET', '/abc/:def/ghi/:lmn/opq/:rst', (req, res, params) => {
t.equal(params.def, 'def')
t.equal(params.lmn, 'lmn')
t.equal(params.rst, 'rst')
})
findMyWay.on('GET', '/abc/:def/ghi/:lmn/opq/:rst/uvz', (req, res, params) => {
t.equal(params.def, 'def')
t.equal(params.lmn, 'lmn')
t.equal(params.rst, 'rst')
})
findMyWay.lookup({ method: 'GET', url: '/abc/def', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/abc/def/ghi/lmn', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/abc/def/ghi/lmn/opq/rst', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/abc/def/ghi/lmn/opq/rst/uvz', headers: {} }, null)
})
test('common prefix', t => {
t.plan(4)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/f', (req, res, params) => {
t.ok('inside route')
})
findMyWay.on('GET', '/ff', (req, res, params) => {
t.ok('inside route')
})
findMyWay.on('GET', '/ffa', (req, res, params) => {
t.ok('inside route')
})
findMyWay.on('GET', '/ffb', (req, res, params) => {
t.ok('inside route')
})
findMyWay.lookup({ method: 'GET', url: '/f', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/ff', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/ffa', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/ffb', headers: {} }, null)
})
test('wildcard', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test/*', (req, res, params) => {
t.equal(params['*'], 'hello')
})
findMyWay.lookup(
{ method: 'GET', url: '/test/hello', headers: {} },
null
)
})
test('catch all wildcard', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '*', (req, res, params) => {
t.equal(params['*'], '/test/hello')
})
findMyWay.lookup(
{ method: 'GET', url: '/test/hello', headers: {} },
null
)
})
test('find should return the route', t => {
t.plan(1)
const findMyWay = FindMyWay()
const fn = () => {}
findMyWay.on('GET', '/test', fn)
t.same(
findMyWay.find('GET', '/test'),
{ handler: fn, params: {}, store: null, searchParams: {} }
)
})
test('find should return the route with params', t => {
t.plan(1)
const findMyWay = FindMyWay()
const fn = () => {}
findMyWay.on('GET', '/test/:id', fn)
t.same(
findMyWay.find('GET', '/test/hello'),
{ handler: fn, params: { id: 'hello' }, store: null, searchParams: {} }
)
})
test('find should return a null handler if the route does not exist', t => {
t.plan(1)
const findMyWay = FindMyWay()
t.same(
findMyWay.find('GET', '/test'),
null
)
})
test('should decode the uri - parametric', t => {
t.plan(1)
const findMyWay = FindMyWay()
const fn = () => {}
findMyWay.on('GET', '/test/:id', fn)
t.same(
findMyWay.find('GET', '/test/he%2Fllo'),
{ handler: fn, params: { id: 'he/llo' }, store: null, searchParams: {} }
)
})
test('should decode the uri - wildcard', t => {
t.plan(1)
const findMyWay = FindMyWay()
const fn = () => {}
findMyWay.on('GET', '/test/*', fn)
t.same(
findMyWay.find('GET', '/test/he%2Fllo'),
{ handler: fn, params: { '*': 'he/llo' }, store: null, searchParams: {} }
)
})
test('safe decodeURIComponent', t => {
t.plan(1)
const findMyWay = FindMyWay()
const fn = () => {}
findMyWay.on('GET', '/test/:id', fn)
t.same(
findMyWay.find('GET', '/test/hel%"Flo'),
null
)
})
test('safe decodeURIComponent - nested route', t => {
t.plan(1)
const findMyWay = FindMyWay()
const fn = () => {}
findMyWay.on('GET', '/test/hello/world/:id/blah', fn)
t.same(
findMyWay.find('GET', '/test/hello/world/hel%"Flo/blah'),
null
)
})
test('safe decodeURIComponent - wildcard', t => {
t.plan(1)
const findMyWay = FindMyWay()
const fn = () => {}
findMyWay.on('GET', '/test/*', fn)
t.same(
findMyWay.find('GET', '/test/hel%"Flo'),
null
)
})
test('static routes should be inserted before parametric / 1', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test/hello', () => {
t.pass('inside correct handler')
})
findMyWay.on('GET', '/test/:id', () => {
t.fail('wrong handler')
})
findMyWay.lookup({ method: 'GET', url: '/test/hello', headers: {} }, null)
})
test('static routes should be inserted before parametric / 2', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test/:id', () => {
t.fail('wrong handler')
})
findMyWay.on('GET', '/test/hello', () => {
t.pass('inside correct handler')
})
findMyWay.lookup({ method: 'GET', url: '/test/hello', headers: {} }, null)
})
test('static routes should be inserted before parametric / 3', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/:id', () => {
t.fail('wrong handler')
})
findMyWay.on('GET', '/test', () => {
t.ok('inside correct handler')
})
findMyWay.on('GET', '/test/:id', () => {
t.fail('wrong handler')
})
findMyWay.on('GET', '/test/hello', () => {
t.ok('inside correct handler')
})
findMyWay.lookup({ method: 'GET', url: '/test', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/test/hello', headers: {} }, null)
})
test('static routes should be inserted before parametric / 4', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/:id', () => {
t.ok('inside correct handler')
})
findMyWay.on('GET', '/test', () => {
t.fail('wrong handler')
})
findMyWay.on('GET', '/test/:id', () => {
t.ok('inside correct handler')
})
findMyWay.on('GET', '/test/hello', () => {
t.fail('wrong handler')
})
findMyWay.lookup({ method: 'GET', url: '/test/id', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/id', headers: {} }, null)
})
test('Static parametric with shared part of the path', t => {
t.plan(2)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.equal(req.url, '/example/shared/nested/oopss')
}
})
findMyWay.on('GET', '/example/shared/nested/test', (req, res, params) => {
t.fail('We should not be here')
})
findMyWay.on('GET', '/example/:param/nested/oops', (req, res, params) => {
t.equal(params.param, 'other')
})
findMyWay.lookup({ method: 'GET', url: '/example/shared/nested/oopss', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/example/other/nested/oops', headers: {} }, null)
})
test('parametric route with different method', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test/:id', (req, res, params) => {
t.equal(params.id, 'hello')
})
findMyWay.on('POST', '/test/:other', (req, res, params) => {
t.equal(params.other, 'world')
})
findMyWay.lookup({ method: 'GET', url: '/test/hello', headers: {} }, null)
findMyWay.lookup({ method: 'POST', url: '/test/world', headers: {} }, null)
})
test('params does not keep the object reference', t => {
t.plan(2)
const findMyWay = FindMyWay()
let first = true
findMyWay.on('GET', '/test/:id', (req, res, params) => {
if (first) {
setTimeout(() => {
t.equal(params.id, 'hello')
}, 10)
} else {
setTimeout(() => {
t.equal(params.id, 'world')
}, 10)
}
first = false
})
findMyWay.lookup({ method: 'GET', url: '/test/hello', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/test/world', headers: {} }, null)
})
test('Unsupported method (static)', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.pass('Everything ok')
}
})
findMyWay.on('GET', '/', (req, res, params) => {
t.fail('We should not be here')
})
findMyWay.lookup({ method: 'TROLL', url: '/', headers: {} }, null)
})
test('Unsupported method (wildcard)', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.pass('Everything ok')
}
})
findMyWay.on('GET', '*', (req, res, params) => {
t.fail('We should not be here')
})
findMyWay.lookup({ method: 'TROLL', url: '/hello/world', headers: {} }, null)
})
test('Unsupported method (static find)', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/', () => {})
t.same(findMyWay.find('TROLL', '/'), null)
})
test('Unsupported method (wildcard find)', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '*', () => {})
t.same(findMyWay.find('TROLL', '/hello/world'), null)
})
test('register all known HTTP methods', t => {
t.plan(6)
const findMyWay = FindMyWay()
const httpMethods = require('../lib/http-methods')
const handlers = {}
for (const i in httpMethods) {
const m = httpMethods[i]
handlers[m] = function myHandler () {}
findMyWay.on(m, '/test', handlers[m])
}
t.ok(findMyWay.find('COPY', '/test'))
t.equal(findMyWay.find('COPY', '/test').handler, handlers.COPY)
t.ok(findMyWay.find('SUBSCRIBE', '/test'))
t.equal(findMyWay.find('SUBSCRIBE', '/test').handler, handlers.SUBSCRIBE)
t.ok(findMyWay.find('M-SEARCH', '/test'))
t.equal(findMyWay.find('M-SEARCH', '/test').handler, handlers['M-SEARCH'])
})
test('off removes all routes without checking constraints if no constraints are specified', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test', {}, (req, res) => {})
findMyWay.on('GET', '/test', { constraints: { host: 'example.com' } }, (req, res) => {})
findMyWay.off('GET', '/test')
t.equal(findMyWay.routes.length, 0)
})
test('off removes only constrainted routes if constraints are specified', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test', {}, (req, res) => {})
findMyWay.on('GET', '/test', { constraints: { host: 'example.com' } }, (req, res) => {})
findMyWay.off('GET', '/test', { host: 'example.com' })
t.equal(findMyWay.routes.length, 1)
t.notOk(findMyWay.routes[0].opts.constraints)
})
test('off removes no routes if provided constraints does not match any registered route', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test', {}, (req, res) => {})
findMyWay.on('GET', '/test', { constraints: { version: '2.x' } }, (req, res) => {})
findMyWay.on('GET', '/test', { constraints: { version: '3.x' } }, (req, res) => {})
findMyWay.off('GET', '/test', { version: '1.x' })
t.equal(findMyWay.routes.length, 3)
})
test('off validates that constraints is an object or undefined', t => {
t.plan(6)
const findMyWay = FindMyWay()
t.throws(() => findMyWay.off('GET', '/', 2))
t.throws(() => findMyWay.off('GET', '/', 'should throw'))
t.throws(() => findMyWay.off('GET', '/', []))
t.doesNotThrow(() => findMyWay.off('GET', '/', undefined))
t.doesNotThrow(() => findMyWay.off('GET', '/', {}))
t.doesNotThrow(() => findMyWay.off('GET', '/'))
})
test('off removes only unconstrainted route if an empty object is given as constraints', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.get('/', {}, () => {})
findMyWay.get('/', { constraints: { host: 'fastify.io' } }, () => {})
findMyWay.off('GET', '/', {})
t.equal(findMyWay.routes.length, 1)
t.equal(findMyWay.routes[0].opts.constraints.host, 'fastify.io')
})

73
node_modules/find-my-way/test/on-bad-url.test.js generated vendored Normal file
View File

@@ -0,0 +1,73 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('../')
test('If onBadUrl is defined, then a bad url should be handled differently (find)', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
},
onBadUrl: (path, req, res) => {
t.equal(path, '/%world', { todo: 'this is not executed' })
}
})
findMyWay.on('GET', '/hello/:id', (req, res) => {
t.fail('Should not be here')
})
const handle = findMyWay.find('GET', '/hello/%world')
t.not(handle, null)
})
test('If onBadUrl is defined, then a bad url should be handled differently (lookup)', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
},
onBadUrl: (path, req, res) => {
t.equal(path, '/hello/%world')
}
})
findMyWay.on('GET', '/hello/:id', (req, res) => {
t.fail('Should not be here')
})
findMyWay.lookup({ method: 'GET', url: '/hello/%world', headers: {} }, null)
})
test('If onBadUrl is not defined, then we should call the defaultRoute (find)', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/hello/:id', (req, res) => {
t.fail('Should not be here')
})
const handle = findMyWay.find('GET', '/hello/%world')
t.equal(handle, null)
})
test('If onBadUrl is not defined, then we should call the defaultRoute (lookup)', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.pass('Everything fine')
}
})
findMyWay.on('GET', '/hello/:id', (req, res) => {
t.fail('Should not be here')
})
findMyWay.lookup({ method: 'GET', url: '/hello/%world', headers: {} }, null)
})

217
node_modules/find-my-way/test/optional-params.test.js generated vendored Normal file
View File

@@ -0,0 +1,217 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('../')
test('Test route with optional parameter', (t) => {
t.plan(2)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/a/:param/b/:optional?', (req, res, params) => {
if (params.optional) {
t.equal(params.optional, 'foo')
} else {
t.equal(params.optional, undefined)
}
})
findMyWay.lookup({ method: 'GET', url: '/a/foo-bar/b', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/a/foo-bar/b/foo', headers: {} }, null)
})
test('Test for duplicate route with optional param', (t) => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/foo/:bar?', (req, res, params) => {})
try {
findMyWay.on('GET', '/foo', (req, res, params) => {})
t.fail('method is already declared for route with optional param')
} catch (e) {
t.equal(e.message, 'Method \'GET\' already declared for route \'/foo\' with constraints \'{}\'')
}
})
test('Test for param with ? not at the end', (t) => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
try {
findMyWay.on('GET', '/foo/:bar?/baz', (req, res, params) => {})
t.fail('Optional Param in the middle of the path is not allowed')
} catch (e) {
t.equal(e.message, 'Optional Parameter needs to be the last parameter of the path')
}
})
test('Multi parametric route with optional param', (t) => {
t.plan(2)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/a/:p1-:p2?', (req, res, params) => {
if (params.p1 && params.p2) {
t.equal(params.p1, 'foo-bar')
t.equal(params.p2, 'baz')
}
})
findMyWay.lookup({ method: 'GET', url: '/a/foo-bar-baz', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/a', headers: {} }, null)
})
test('Optional Parameter with ignoreTrailingSlash = true', (t) => {
t.plan(4)
const findMyWay = FindMyWay({
ignoreTrailingSlash: true,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/test/hello/:optional?', (req, res, params) => {
if (params.optional) {
t.equal(params.optional, 'foo')
} else {
t.equal(params.optional, undefined)
}
})
findMyWay.lookup({ method: 'GET', url: '/test/hello/', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/test/hello', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/test/hello/foo', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/test/hello/foo/', headers: {} }, null)
})
test('Optional Parameter with ignoreTrailingSlash = false', (t) => {
t.plan(4)
const findMyWay = FindMyWay({
ignoreTrailingSlash: false,
defaultRoute: (req, res) => {
t.match(req.url, '/test/hello/foo/')
}
})
findMyWay.on('GET', '/test/hello/:optional?', (req, res, params) => {
if (req.url === '/test/hello/') {
t.same(params, { optional: '' })
} else if (req.url === '/test/hello') {
t.same(params, {})
} else if (req.url === '/test/hello/foo') {
t.same(params, { optional: 'foo' })
}
})
findMyWay.lookup({ method: 'GET', url: '/test/hello/', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/test/hello', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/test/hello/foo', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/test/hello/foo/', headers: {} }, null)
})
test('Optional Parameter with ignoreDuplicateSlashes = true', (t) => {
t.plan(4)
const findMyWay = FindMyWay({
ignoreDuplicateSlashes: true,
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/test/hello/:optional?', (req, res, params) => {
if (params.optional) {
t.equal(params.optional, 'foo')
} else {
t.equal(params.optional, undefined)
}
})
findMyWay.lookup({ method: 'GET', url: '/test//hello', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/test/hello', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/test/hello/foo', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/test//hello//foo', headers: {} }, null)
})
test('Optional Parameter with ignoreDuplicateSlashes = false', (t) => {
t.plan(4)
const findMyWay = FindMyWay({
ignoreDuplicateSlashes: false,
defaultRoute: (req, res) => {
if (req.url === '/test//hello') {
t.same(req.params, undefined)
} else if (req.url === '/test//hello/foo') {
t.same(req.params, undefined)
}
}
})
findMyWay.on('GET', '/test/hello/:optional?', (req, res, params) => {
if (req.url === '/test/hello/') {
t.same(params, { optional: '' })
} else if (req.url === '/test/hello') {
t.same(params, {})
} else if (req.url === '/test/hello/foo') {
t.same(params, { optional: 'foo' })
}
})
findMyWay.lookup({ method: 'GET', url: '/test//hello', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/test/hello', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/test/hello/foo', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/test//hello/foo', headers: {} }, null)
})
test('deregister a route with optional param', (t) => {
t.plan(4)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/a/:param/b/:optional?', (req, res, params) => {})
t.ok(findMyWay.find('GET', '/a/:param/b'))
t.ok(findMyWay.find('GET', '/a/:param/b/:optional'))
findMyWay.off('GET', '/a/:param/b/:optional?')
t.notOk(findMyWay.find('GET', '/a/:param/b'))
t.notOk(findMyWay.find('GET', '/a/:param/b/:optional'))
})
test('optional parameter on root', (t) => {
t.plan(2)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
t.fail('Should not be defaultRoute')
}
})
findMyWay.on('GET', '/:optional?', (req, res, params) => {
if (params.optional) {
t.equal(params.optional, 'foo')
} else {
t.equal(params.optional, undefined)
}
})
findMyWay.lookup({ method: 'GET', url: '/', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/foo', headers: {} }, null)
})

127
node_modules/find-my-way/test/params-collisions.test.js generated vendored Normal file
View File

@@ -0,0 +1,127 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('..')
test('should setup parametric and regexp node', t => {
t.plan(2)
const findMyWay = FindMyWay()
const paramHandler = () => {}
const regexpHandler = () => {}
findMyWay.on('GET', '/foo/:bar', paramHandler)
findMyWay.on('GET', '/foo/:bar(123)', regexpHandler)
t.equal(findMyWay.find('GET', '/foo/value').handler, paramHandler)
t.equal(findMyWay.find('GET', '/foo/123').handler, regexpHandler)
})
test('should setup parametric and multi-parametric node', t => {
t.plan(2)
const findMyWay = FindMyWay()
const paramHandler = () => {}
const regexpHandler = () => {}
findMyWay.on('GET', '/foo/:bar', paramHandler)
findMyWay.on('GET', '/foo/:bar.png', regexpHandler)
t.equal(findMyWay.find('GET', '/foo/value').handler, paramHandler)
t.equal(findMyWay.find('GET', '/foo/value.png').handler, regexpHandler)
})
test('should throw when set upping two parametric nodes', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/foo/:bar', () => {})
t.throws(() => findMyWay.on('GET', '/foo/:baz', () => {}))
})
test('should throw when set upping two regexp nodes', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/foo/:bar(123)', () => {})
t.throws(() => findMyWay.on('GET', '/foo/:bar(456)', () => {}))
})
test('should set up two parametric nodes with static ending', t => {
t.plan(2)
const findMyWay = FindMyWay()
const paramHandler1 = () => {}
const paramHandler2 = () => {}
findMyWay.on('GET', '/foo/:bar.png', paramHandler1)
findMyWay.on('GET', '/foo/:bar.jpeg', paramHandler2)
t.equal(findMyWay.find('GET', '/foo/value.png').handler, paramHandler1)
t.equal(findMyWay.find('GET', '/foo/value.jpeg').handler, paramHandler2)
})
test('should set up two regexp nodes with static ending', t => {
t.plan(2)
const findMyWay = FindMyWay()
const paramHandler1 = () => {}
const paramHandler2 = () => {}
findMyWay.on('GET', '/foo/:bar(123).png', paramHandler1)
findMyWay.on('GET', '/foo/:bar(456).jpeg', paramHandler2)
t.equal(findMyWay.find('GET', '/foo/123.png').handler, paramHandler1)
t.equal(findMyWay.find('GET', '/foo/456.jpeg').handler, paramHandler2)
})
test('node with longer static suffix should have higher priority', t => {
t.plan(2)
const findMyWay = FindMyWay()
const paramHandler1 = () => {}
const paramHandler2 = () => {}
findMyWay.on('GET', '/foo/:bar.png', paramHandler1)
findMyWay.on('GET', '/foo/:bar.png.png', paramHandler2)
t.equal(findMyWay.find('GET', '/foo/value.png').handler, paramHandler1)
t.equal(findMyWay.find('GET', '/foo/value.png.png').handler, paramHandler2)
})
test('node with longer static suffix should have higher priority', t => {
t.plan(2)
const findMyWay = FindMyWay()
const paramHandler1 = () => {}
const paramHandler2 = () => {}
findMyWay.on('GET', '/foo/:bar.png.png', paramHandler2)
findMyWay.on('GET', '/foo/:bar.png', paramHandler1)
t.equal(findMyWay.find('GET', '/foo/value.png').handler, paramHandler1)
t.equal(findMyWay.find('GET', '/foo/value.png.png').handler, paramHandler2)
})
test('should set up regexp node and node with static ending', t => {
t.plan(2)
const regexHandler = () => {}
const multiParamHandler = () => {}
const findMyWay = FindMyWay()
findMyWay.on('GET', '/foo/:bar(123)', regexHandler)
findMyWay.on('GET', '/foo/:bar(123).jpeg', multiParamHandler)
t.equal(findMyWay.find('GET', '/foo/123.jpeg').handler, multiParamHandler)
t.equal(findMyWay.find('GET', '/foo/123').handler, regexHandler)
})

View File

@@ -0,0 +1,53 @@
'use strict'
const t = require('tap')
const FindMyWay = require('../')
t.test('path params match', (t) => {
t.plan(24)
const findMyWay = FindMyWay({ ignoreTrailingSlash: true, ignoreDuplicateSlashes: true })
const b1Path = function b1StaticPath () {}
const b2Path = function b2StaticPath () {}
const cPath = function cStaticPath () {}
const paramPath = function parameterPath () {}
findMyWay.on('GET', '/ab1', b1Path)
findMyWay.on('GET', '/ab2', b2Path)
findMyWay.on('GET', '/ac', cPath)
findMyWay.on('GET', '/:pam', paramPath)
t.equal(findMyWay.find('GET', '/ab1').handler, b1Path)
t.equal(findMyWay.find('GET', '/ab1/').handler, b1Path)
t.equal(findMyWay.find('GET', '//ab1').handler, b1Path)
t.equal(findMyWay.find('GET', '//ab1//').handler, b1Path)
t.equal(findMyWay.find('GET', '/ab2').handler, b2Path)
t.equal(findMyWay.find('GET', '/ab2/').handler, b2Path)
t.equal(findMyWay.find('GET', '//ab2').handler, b2Path)
t.equal(findMyWay.find('GET', '//ab2//').handler, b2Path)
t.equal(findMyWay.find('GET', '/ac').handler, cPath)
t.equal(findMyWay.find('GET', '/ac/').handler, cPath)
t.equal(findMyWay.find('GET', '//ac').handler, cPath)
t.equal(findMyWay.find('GET', '//ac//').handler, cPath)
t.equal(findMyWay.find('GET', '/foo').handler, paramPath)
t.equal(findMyWay.find('GET', '/foo/').handler, paramPath)
t.equal(findMyWay.find('GET', '//foo').handler, paramPath)
t.equal(findMyWay.find('GET', '//foo//').handler, paramPath)
const noTrailingSlashRet = findMyWay.find('GET', '/abcdef')
t.equal(noTrailingSlashRet.handler, paramPath)
t.same(noTrailingSlashRet.params, { pam: 'abcdef' })
const trailingSlashRet = findMyWay.find('GET', '/abcdef/')
t.equal(trailingSlashRet.handler, paramPath)
t.same(trailingSlashRet.params, { pam: 'abcdef' })
const noDuplicateSlashRet = findMyWay.find('GET', '/abcdef')
t.equal(noDuplicateSlashRet.handler, paramPath)
t.same(noDuplicateSlashRet.params, { pam: 'abcdef' })
const duplicateSlashRet = findMyWay.find('GET', '//abcdef')
t.equal(duplicateSlashRet.handler, paramPath)
t.same(duplicateSlashRet.params, { pam: 'abcdef' })
})

597
node_modules/find-my-way/test/pretty-print-tree.test.js generated vendored Normal file
View File

@@ -0,0 +1,597 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('../')
test('pretty print - empty tree', t => {
t.plan(2)
const findMyWay = FindMyWay()
const tree = findMyWay.prettyPrint({ method: 'GET' })
const expected = '(empty tree)'
t.equal(typeof tree, 'string')
t.equal(tree, expected)
})
test('pretty print - static routes', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test', () => {})
findMyWay.on('GET', '/test/hello', () => {})
findMyWay.on('GET', '/hello/world', () => {})
const tree = findMyWay.prettyPrint({ method: 'GET' })
const expected = `\
└── /
├── test (GET)
│ └── /hello (GET)
└── hello/world (GET)
`
t.equal(typeof tree, 'string')
t.equal(tree, expected)
})
test('pretty print - parametric routes', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test', () => {})
findMyWay.on('GET', '/test/:hello', () => {})
findMyWay.on('GET', '/hello/:world', () => {})
const tree = findMyWay.prettyPrint({ method: 'GET' })
const expected = `\
└── /
├── test (GET)
│ └── /
│ └── :hello (GET)
└── hello/
└── :world (GET)
`
t.equal(typeof tree, 'string')
t.equal(tree, expected)
})
test('pretty print - parametric routes', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/static', () => {})
findMyWay.on('GET', '/static/:param/suffix1', () => {})
findMyWay.on('GET', '/static/:param(123)/suffix2', () => {})
findMyWay.on('GET', '/static/:param(123).end/suffix3', () => {})
findMyWay.on('GET', '/static/:param1(123).:param2(456)/suffix4', () => {})
const tree = findMyWay.prettyPrint({ method: 'GET' })
const expected = `\
└── /
└── static (GET)
└── /
├── :param(123).end
│ └── /suffix3 (GET)
├── :param(123)
│ └── /suffix2 (GET)
├── :param1(123).:param2(456)
│ └── /suffix4 (GET)
└── :param
└── /suffix1 (GET)
`
t.equal(typeof tree, 'string')
t.equal(tree, expected)
})
test('pretty print - parametric routes', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/static', () => {})
findMyWay.on('GET', '/static/:param/suffix1', () => {})
findMyWay.on('GET', '/static/:param(123)/suffix2', () => {})
findMyWay.on('GET', '/static/:param(123).end/suffix3', () => {})
findMyWay.on('GET', '/static/:param1(123).:param2(456)/suffix4', () => {})
const tree = findMyWay.prettyPrint({ method: 'GET', commonPrefix: false })
const expected = `\
└── /static (GET)
├── /:param(123).end/suffix3 (GET)
├── /:param(123)/suffix2 (GET)
├── /:param1(123).:param2(456)/suffix4 (GET)
└── /:param/suffix1 (GET)
`
t.equal(typeof tree, 'string')
t.equal(tree, expected)
})
test('pretty print - mixed parametric routes', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test', () => {})
findMyWay.on('GET', '/test/:hello', () => {})
findMyWay.on('POST', '/test/:hello', () => {})
findMyWay.on('GET', '/test/:hello/world', () => {})
const tree = findMyWay.prettyPrint({ method: 'GET' })
const expected = `\
└── /
└── test (GET)
└── /
└── :hello (GET)
└── /world (GET)
`
t.equal(typeof tree, 'string')
t.equal(tree, expected)
})
test('pretty print - wildcard routes', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test', () => {})
findMyWay.on('GET', '/test/*', () => {})
findMyWay.on('GET', '/hello/*', () => {})
const tree = findMyWay.prettyPrint({ method: 'GET' })
const expected = `\
└── /
├── test (GET)
│ └── /
│ └── * (GET)
└── hello/
└── * (GET)
`
t.equal(typeof tree, 'string')
t.equal(tree, expected)
})
test('pretty print - parametric routes with same parent and followed by a static route which has the same prefix with the former routes', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test', () => {})
findMyWay.on('GET', '/test/hello/:id', () => {})
findMyWay.on('POST', '/test/hello/:id', () => {})
findMyWay.on('GET', '/test/helloworld', () => {})
const tree = findMyWay.prettyPrint({ method: 'GET' })
const expected = `\
└── /
└── test (GET)
└── /hello
├── /
│ └── :id (GET)
└── world (GET)
`
t.equal(typeof tree, 'string')
t.equal(tree, expected)
})
test('pretty print - constrained parametric routes', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test', () => {})
findMyWay.on('GET', '/test', { constraints: { host: 'auth.fastify.io' } }, () => {})
findMyWay.on('GET', '/test/:hello', () => {})
findMyWay.on('GET', '/test/:hello', { constraints: { version: '1.1.2' } }, () => {})
findMyWay.on('GET', '/test/:hello', { constraints: { version: '2.0.0' } }, () => {})
const tree = findMyWay.prettyPrint({ method: 'GET' })
const expected = `\
└── /
└── test (GET)
test (GET) {"host":"auth.fastify.io"}
└── /
└── :hello (GET)
:hello (GET) {"version":"1.1.2"}
:hello (GET) {"version":"2.0.0"}
`
t.equal(typeof tree, 'string')
t.equal(tree, expected)
})
test('pretty print - multiple parameters are drawn appropriately', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test', () => {})
// routes with a nested parameter (i.e. no handler for the /:param) were breaking the display
findMyWay.on('GET', '/test/:hello/there/:ladies', () => {})
findMyWay.on('GET', '/test/:hello/there/:ladies/and/:gents', () => {})
findMyWay.on('GET', '/test/are/:you/:ready/to/:rock', () => {})
const tree = findMyWay.prettyPrint({ method: 'GET', commonPrefix: false })
const expected = `\
└── /test (GET)
├── /are/:you/:ready/to/:rock (GET)
└── /:hello/there/:ladies (GET)
└── /and/:gents (GET)
`
t.equal(typeof tree, 'string')
t.equal(tree, expected)
})
test('pretty print commonPrefix - use routes array to draw flattened routes', t => {
t.plan(4)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test', () => {})
findMyWay.on('GET', '/test/hello', () => {})
findMyWay.on('GET', '/testing', () => {})
findMyWay.on('GET', '/testing/:param', () => {})
findMyWay.on('GET', '/update', () => {})
const radixTree = findMyWay.prettyPrint({ method: 'GET', commonPrefix: true })
const arrayTree = findMyWay.prettyPrint({ method: 'GET', commonPrefix: false })
const radixExpected = `\
└── /
├── test (GET)
│ ├── /hello (GET)
│ └── ing (GET)
│ └── /
│ └── :param (GET)
└── update (GET)
`
const arrayExpected = `\
├── /test (GET)
│ ├── /hello (GET)
│ └── ing (GET)
│ └── /:param (GET)
└── /update (GET)
`
t.equal(typeof radixTree, 'string')
t.equal(radixTree, radixExpected)
t.equal(typeof arrayTree, 'string')
t.equal(arrayTree, arrayExpected)
})
test('pretty print commonPrefix - handle wildcard root', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '*', () => {})
findMyWay.on('GET', '/test/hello', () => {})
findMyWay.on('GET', '/testing', () => {})
findMyWay.on('GET', '/testing/:param', () => {})
findMyWay.on('PUT', '/update', () => {})
const arrayTree = findMyWay.prettyPrint({ method: 'GET', commonPrefix: false })
const arrayExpected = `\
├── /test/hello (GET)
├── /testing (GET)
│ └── /:param (GET)
└── * (GET)
`
t.equal(typeof arrayTree, 'string')
t.equal(arrayTree, arrayExpected)
})
test('pretty print commonPrefix - handle wildcard root', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '*', () => {})
findMyWay.on('GET', '/test/hello', () => {})
findMyWay.on('GET', '/testing', () => {})
findMyWay.on('GET', '/testing/:param', () => {})
findMyWay.on('PUT', '/update', () => {})
const radixTree = findMyWay.prettyPrint({ method: 'GET' })
const radixExpected = `\
└── (empty root node)
├── /
│ └── test
│ ├── /hello (GET)
│ └── ing (GET)
│ └── /
│ └── :param (GET)
└── * (GET)
`
t.equal(typeof radixTree, 'string')
t.equal(radixTree, radixExpected)
})
test('pretty print commonPrefix - handle constrained routes', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test', () => {})
findMyWay.on('GET', '/test', { constraints: { host: 'auth.fastify.io' } }, () => {})
findMyWay.on('GET', '/test/:hello', () => {})
findMyWay.on('PUT', '/test/:hello', () => {})
findMyWay.on('GET', '/test/:hello', { constraints: { version: '1.1.2' } }, () => {})
findMyWay.on('GET', '/test/:hello', { constraints: { version: '2.0.0' } }, () => {})
const arrayTree = findMyWay.prettyPrint({ method: 'GET', commonPrefix: false })
const arrayExpected = `\
└── /test (GET)
/test (GET) {"host":"auth.fastify.io"}
└── /:hello (GET)
/:hello (GET) {"version":"1.1.2"}
/:hello (GET) {"version":"2.0.0"}
`
t.equal(typeof arrayTree, 'string')
t.equal(arrayTree, arrayExpected)
})
test('pretty print includeMeta - commonPrefix: true', t => {
t.plan(6)
const findMyWay = FindMyWay()
const namedFunction = () => {}
const store = {
onRequest: [() => {}, namedFunction],
onTimeout: [() => {}],
genericMeta: 'meta',
mixedMeta: ['mixed items', { an: 'object' }],
objectMeta: { one: '1', two: 2 },
functionMeta: namedFunction
}
store[Symbol('symbolKey')] = Symbol('symbolValue')
findMyWay.on('GET', '/test', () => {}, store)
findMyWay.on('GET', '/test', { constraints: { host: 'auth.fastify.io' } }, () => {}, store)
findMyWay.on('GET', '/testing/:hello', () => {}, store)
findMyWay.on('PUT', '/tested/:hello', () => {}, store)
findMyWay.on('GET', '/test/:hello', { constraints: { version: '1.1.2' } }, () => {})
findMyWay.on('GET', '/test/:hello', { constraints: { version: '2.0.0' } }, () => {})
const radixTree = findMyWay.prettyPrint({
method: 'GET',
commonPrefix: true,
includeMeta: true
})
const radixTreeExpected = `\
└── /
└── test (GET)
• (onRequest) ["anonymous()","namedFunction()"]
• (onTimeout) ["anonymous()"]
• (genericMeta) "meta"
• (mixedMeta) ["mixed items",{"an":"object"}]
• (objectMeta) {"one":"1","two":2}
• (functionMeta) "namedFunction()"
• (Symbol(symbolKey)) "Symbol(symbolValue)"
test (GET) {"host":"auth.fastify.io"}
• (onRequest) ["anonymous()","namedFunction()"]
• (onTimeout) ["anonymous()"]
• (genericMeta) "meta"
• (mixedMeta) ["mixed items",{"an":"object"}]
• (objectMeta) {"one":"1","two":2}
• (functionMeta) "namedFunction()"
• (Symbol(symbolKey)) "Symbol(symbolValue)"
├── ing/
│ └── :hello (GET)
│ • (onRequest) ["anonymous()","namedFunction()"]
│ • (onTimeout) ["anonymous()"]
│ • (genericMeta) "meta"
│ • (mixedMeta) ["mixed items",{"an":"object"}]
│ • (objectMeta) {"one":"1","two":2}
│ • (functionMeta) "namedFunction()"
│ • (Symbol(symbolKey)) "Symbol(symbolValue)"
└── /
└── :hello (GET) {"version":"1.1.2"}
:hello (GET) {"version":"2.0.0"}
`
const radixTreeSpecific = findMyWay.prettyPrint({
method: 'GET',
commonPrefix: true,
includeMeta: ['onTimeout', 'objectMeta', 'nonExistent']
})
const radixTreeSpecificExpected = `\
└── /
└── test (GET)
• (onTimeout) ["anonymous()"]
• (objectMeta) {"one":"1","two":2}
test (GET) {"host":"auth.fastify.io"}
• (onTimeout) ["anonymous()"]
• (objectMeta) {"one":"1","two":2}
├── ing/
│ └── :hello (GET)
│ • (onTimeout) ["anonymous()"]
│ • (objectMeta) {"one":"1","two":2}
└── /
└── :hello (GET) {"version":"1.1.2"}
:hello (GET) {"version":"2.0.0"}
`
const radixTreeNoMeta = findMyWay.prettyPrint({
method: 'GET',
commonPrefix: true,
includeMeta: false
})
const radixTreeNoMetaExpected = `\
└── /
└── test (GET)
test (GET) {"host":"auth.fastify.io"}
├── ing/
│ └── :hello (GET)
└── /
└── :hello (GET) {"version":"1.1.2"}
:hello (GET) {"version":"2.0.0"}
`
t.equal(typeof radixTree, 'string')
t.equal(radixTree, radixTreeExpected)
t.equal(typeof radixTreeSpecific, 'string')
t.equal(radixTreeSpecific, radixTreeSpecificExpected)
t.equal(typeof radixTreeNoMeta, 'string')
t.equal(radixTreeNoMeta, radixTreeNoMetaExpected)
})
test('pretty print includeMeta - commonPrefix: false', t => {
t.plan(6)
const findMyWay = FindMyWay()
const namedFunction = () => {}
const store = {
onRequest: [() => {}, namedFunction],
onTimeout: [() => {}],
genericMeta: 'meta',
mixedMeta: ['mixed items', { an: 'object' }],
objectMeta: { one: '1', two: 2 },
functionMeta: namedFunction
}
store[Symbol('symbolKey')] = Symbol('symbolValue')
findMyWay.on('GET', '/test', () => {}, store)
findMyWay.on('GET', '/test', { constraints: { host: 'auth.fastify.io' } }, () => {}, store)
findMyWay.on('GET', '/testing/:hello', () => {}, store)
findMyWay.on('PUT', '/tested/:hello', () => {}, store)
findMyWay.on('GET', '/test/:hello', { constraints: { version: '1.1.2' } }, () => {})
findMyWay.on('GET', '/test/:hello', { constraints: { version: '2.0.0' } }, () => {})
const arrayTree = findMyWay.prettyPrint({
method: 'GET',
commonPrefix: false,
includeMeta: true
})
const arrayExpected = `\
└── /test (GET)
• (onRequest) ["anonymous()","namedFunction()"]
• (onTimeout) ["anonymous()"]
• (genericMeta) "meta"
• (mixedMeta) ["mixed items",{"an":"object"}]
• (objectMeta) {"one":"1","two":2}
• (functionMeta) "namedFunction()"
• (Symbol(symbolKey)) "Symbol(symbolValue)"
/test (GET) {"host":"auth.fastify.io"}
• (onRequest) ["anonymous()","namedFunction()"]
• (onTimeout) ["anonymous()"]
• (genericMeta) "meta"
• (mixedMeta) ["mixed items",{"an":"object"}]
• (objectMeta) {"one":"1","two":2}
• (functionMeta) "namedFunction()"
• (Symbol(symbolKey)) "Symbol(symbolValue)"
├── ing/:hello (GET)
│ • (onRequest) ["anonymous()","namedFunction()"]
│ • (onTimeout) ["anonymous()"]
│ • (genericMeta) "meta"
│ • (mixedMeta) ["mixed items",{"an":"object"}]
│ • (objectMeta) {"one":"1","two":2}
│ • (functionMeta) "namedFunction()"
│ • (Symbol(symbolKey)) "Symbol(symbolValue)"
└── /:hello (GET) {"version":"1.1.2"}
/:hello (GET) {"version":"2.0.0"}
`
const arraySpecific = findMyWay.prettyPrint({
method: 'GET',
commonPrefix: false,
includeMeta: ['onRequest', 'mixedMeta', 'nonExistent']
})
const arraySpecificExpected = `\
└── /test (GET)
• (onRequest) ["anonymous()","namedFunction()"]
• (mixedMeta) ["mixed items",{"an":"object"}]
/test (GET) {"host":"auth.fastify.io"}
• (onRequest) ["anonymous()","namedFunction()"]
• (mixedMeta) ["mixed items",{"an":"object"}]
├── ing/:hello (GET)
│ • (onRequest) ["anonymous()","namedFunction()"]
│ • (mixedMeta) ["mixed items",{"an":"object"}]
└── /:hello (GET) {"version":"1.1.2"}
/:hello (GET) {"version":"2.0.0"}
`
const arrayNoMeta = findMyWay.prettyPrint({
method: 'GET',
commonPrefix: false,
includeMeta: false
})
const arrayNoMetaExpected = `\
└── /test (GET)
/test (GET) {"host":"auth.fastify.io"}
├── ing/:hello (GET)
└── /:hello (GET) {"version":"1.1.2"}
/:hello (GET) {"version":"2.0.0"}
`
t.equal(typeof arrayTree, 'string')
t.equal(arrayTree, arrayExpected)
t.equal(typeof arraySpecific, 'string')
t.equal(arraySpecific, arraySpecificExpected)
t.equal(typeof arrayNoMeta, 'string')
t.equal(arrayNoMeta, arrayNoMetaExpected)
})
test('pretty print includeMeta - buildPrettyMeta function', t => {
t.plan(4)
const findMyWay = FindMyWay({
buildPrettyMeta: route => {
return { metaKey: route.method === 'GET' ? route.path : 'not a GET route' }
}
})
const namedFunction = () => {}
const store = {
onRequest: [() => {}, namedFunction],
onTimeout: [() => {}],
genericMeta: 'meta',
mixedMeta: ['mixed items', { an: 'object' }],
objectMeta: { one: '1', two: 2 },
functionMeta: namedFunction
}
store[Symbol('symbolKey')] = Symbol('symbolValue')
findMyWay.on('GET', '/test', () => {}, store)
findMyWay.on('GET', '/test', { constraints: { host: 'auth.fastify.io' } }, () => {}, store)
findMyWay.on('GET', '/test/:hello', () => {}, store)
findMyWay.on('PUT', '/test/:hello', () => {}, store)
findMyWay.on('GET', '/test/:hello', { constraints: { version: '1.1.2' } }, () => {})
findMyWay.on('GET', '/test/:hello', { constraints: { version: '2.0.0' } }, () => {})
const arrayTree = findMyWay.prettyPrint({
method: 'GET',
commonPrefix: false,
includeMeta: true
})
const arrayExpected = `\
└── /test (GET)
• (metaKey) "/test"
/test (GET) {"host":"auth.fastify.io"}
• (metaKey) "/test"
└── /:hello (GET)
• (metaKey) "/test/:hello"
/:hello (GET) {"version":"1.1.2"}
• (metaKey) "/test/:hello"
/:hello (GET) {"version":"2.0.0"}
• (metaKey) "/test/:hello"
`
const radixTree = findMyWay.prettyPrint({
method: 'GET',
includeMeta: true
})
const radixExpected = `\
└── /
└── test (GET)
• (metaKey) "/test"
test (GET) {"host":"auth.fastify.io"}
• (metaKey) "/test"
└── /
└── :hello (GET)
• (metaKey) "/test/:hello"
:hello (GET) {"version":"1.1.2"}
• (metaKey) "/test/:hello"
:hello (GET) {"version":"2.0.0"}
• (metaKey) "/test/:hello"
`
t.equal(typeof arrayTree, 'string')
t.equal(arrayTree, arrayExpected)
t.equal(typeof radixTree, 'string')
t.equal(radixTree, radixExpected)
})

681
node_modules/find-my-way/test/pretty-print.test.js generated vendored Normal file
View File

@@ -0,0 +1,681 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('../')
test('pretty print - empty tree', t => {
t.plan(2)
const findMyWay = FindMyWay()
const tree = findMyWay.prettyPrint()
const expected = '(empty tree)'
t.equal(typeof tree, 'string')
t.equal(tree, expected)
})
test('pretty print - static routes', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test', () => {})
findMyWay.on('GET', '/test/hello', () => {})
findMyWay.on('GET', '/hello/world', () => {})
const tree = findMyWay.prettyPrint()
const expected = `\
└── /
├── test (GET)
│ └── /hello (GET)
└── hello/world (GET)
`
t.equal(typeof tree, 'string')
t.equal(tree, expected)
})
test('pretty print - parametric routes', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test', () => {})
findMyWay.on('GET', '/test/:hello', () => {})
findMyWay.on('GET', '/hello/:world', () => {})
const tree = findMyWay.prettyPrint()
const expected = `\
└── /
├── test (GET)
│ └── /
│ └── :hello (GET)
└── hello/
└── :world (GET)
`
t.equal(typeof tree, 'string')
t.equal(tree, expected)
})
test('pretty print - parametric routes', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/static', () => {})
findMyWay.on('GET', '/static/:param/suffix1', () => {})
findMyWay.on('GET', '/static/:param(123)/suffix2', () => {})
findMyWay.on('GET', '/static/:param(123).end/suffix3', () => {})
findMyWay.on('GET', '/static/:param1(123).:param2(456)/suffix4', () => {})
const tree = findMyWay.prettyPrint()
const expected = `\
└── /
└── static (GET)
└── /
├── :param(123).end
│ └── /suffix3 (GET)
├── :param(123)
│ └── /suffix2 (GET)
├── :param1(123).:param2(456)
│ └── /suffix4 (GET)
└── :param
└── /suffix1 (GET)
`
t.equal(typeof tree, 'string')
t.equal(tree, expected)
})
test('pretty print - parametric routes', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/static', () => {})
findMyWay.on('GET', '/static/:param/suffix1', () => {})
findMyWay.on('GET', '/static/:param(123)/suffix2', () => {})
findMyWay.on('GET', '/static/:param(123).end/suffix3', () => {})
findMyWay.on('GET', '/static/:param1(123).:param2(456)/suffix4', () => {})
const tree = findMyWay.prettyPrint({ commonPrefix: false })
const expected = `\
└── /static (GET)
├── /:param(123).end/suffix3 (GET)
├── /:param(123)/suffix2 (GET)
├── /:param1(123).:param2(456)/suffix4 (GET)
└── /:param/suffix1 (GET)
`
t.equal(typeof tree, 'string')
t.equal(tree, expected)
})
test('pretty print - mixed parametric routes', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test', () => {})
findMyWay.on('GET', '/test/:hello', () => {})
findMyWay.on('POST', '/test/:hello', () => {})
findMyWay.on('GET', '/test/:hello/world', () => {})
const tree = findMyWay.prettyPrint()
const expected = `\
└── /
└── test (GET)
└── /
└── :hello (GET, POST)
└── /world (GET)
`
t.equal(typeof tree, 'string')
t.equal(tree, expected)
})
test('pretty print - wildcard routes', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test', () => {})
findMyWay.on('GET', '/test/*', () => {})
findMyWay.on('GET', '/hello/*', () => {})
const tree = findMyWay.prettyPrint()
const expected = `\
└── /
├── test (GET)
│ └── /
│ └── * (GET)
└── hello/
└── * (GET)
`
t.equal(typeof tree, 'string')
t.equal(tree, expected)
})
test('pretty print - parametric routes with same parent and followed by a static route which has the same prefix with the former routes', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test', () => {})
findMyWay.on('GET', '/test/hello/:id', () => {})
findMyWay.on('POST', '/test/hello/:id', () => {})
findMyWay.on('GET', '/test/helloworld', () => {})
const tree = findMyWay.prettyPrint()
const expected = `\
└── /
└── test (GET)
└── /hello
├── /
│ └── :id (GET, POST)
└── world (GET)
`
t.equal(typeof tree, 'string')
t.equal(tree, expected)
})
test('pretty print - constrained parametric routes', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test', () => {})
findMyWay.on('GET', '/test', { constraints: { host: 'auth.fastify.io' } }, () => {})
findMyWay.on('GET', '/test/:hello', () => {})
findMyWay.on('GET', '/test/:hello', { constraints: { version: '1.1.2' } }, () => {})
findMyWay.on('GET', '/test/:hello', { constraints: { version: '2.0.0' } }, () => {})
const tree = findMyWay.prettyPrint()
const expected = `\
└── /
└── test (GET)
test (GET) {"host":"auth.fastify.io"}
└── /
└── :hello (GET)
:hello (GET) {"version":"1.1.2"}
:hello (GET) {"version":"2.0.0"}
`
t.equal(typeof tree, 'string')
t.equal(tree, expected)
})
test('pretty print - multiple parameters are drawn appropriately', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test', () => {})
// routes with a nested parameter (i.e. no handler for the /:param) were breaking the display
findMyWay.on('GET', '/test/:hello/there/:ladies', () => {})
findMyWay.on('GET', '/test/:hello/there/:ladies/and/:gents', () => {})
findMyWay.on('GET', '/test/are/:you/:ready/to/:rock', () => {})
const tree = findMyWay.prettyPrint({ commonPrefix: false })
const expected = `\
└── /test (GET)
├── /are/:you/:ready/to/:rock (GET)
└── /:hello/there/:ladies (GET)
└── /and/:gents (GET)
`
t.equal(typeof tree, 'string')
t.equal(tree, expected)
})
test('pretty print - multiple parameters are drawn appropriately', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test', () => {})
// routes with a nested parameter (i.e. no handler for the /:param) were breaking the display
findMyWay.on('GET', '/test/:hello/there/:ladies', () => {})
findMyWay.on('GET', '/test/:hello/there/:ladies/and/:gents', () => {})
findMyWay.on('GET', '/test/are/:you/:ready/to/:rock', () => {})
const tree = findMyWay.prettyPrint({ commonPrefix: false })
const expected = `\
└── /test (GET)
├── /are/:you/:ready/to/:rock (GET)
└── /:hello/there/:ladies (GET)
└── /and/:gents (GET)
`
t.equal(typeof tree, 'string')
t.equal(tree, expected)
})
test('pretty print commonPrefix - use routes array to draw flattened routes', t => {
t.plan(4)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test', () => {})
findMyWay.on('GET', '/test/hello', () => {})
findMyWay.on('GET', '/testing', () => {})
findMyWay.on('GET', '/testing/:param', () => {})
findMyWay.on('PUT', '/update', () => {})
const radixTree = findMyWay.prettyPrint({ commonPrefix: true })
const arrayTree = findMyWay.prettyPrint({ commonPrefix: false })
const radixExpected = `\
└── /
├── test (GET)
│ ├── /hello (GET)
│ └── ing (GET)
│ └── /
│ └── :param (GET)
└── update (PUT)
`
const arrayExpected = `\
├── /test (GET)
│ ├── /hello (GET)
│ └── ing (GET)
│ └── /:param (GET)
└── /update (PUT)
`
t.equal(typeof radixTree, 'string')
t.equal(radixTree, radixExpected)
t.equal(typeof arrayTree, 'string')
t.equal(arrayTree, arrayExpected)
})
test('pretty print commonPrefix - handle wildcard root', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('OPTIONS', '*', () => {})
findMyWay.on('GET', '/test/hello', () => {})
findMyWay.on('GET', '/testing', () => {})
findMyWay.on('GET', '/testing/:param', () => {})
findMyWay.on('PUT', '/update', () => {})
const arrayTree = findMyWay.prettyPrint({ commonPrefix: false })
const arrayExpected = `\
├── /test/hello (GET)
├── /testing (GET)
│ └── /:param (GET)
├── /update (PUT)
└── * (OPTIONS)
`
t.equal(typeof arrayTree, 'string')
t.equal(arrayTree, arrayExpected)
})
test('pretty print commonPrefix - handle wildcard root', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '*', () => {})
findMyWay.on('GET', '/test/hello', () => {})
findMyWay.on('GET', '/testing', () => {})
findMyWay.on('GET', '/testing/:param', () => {})
findMyWay.on('PUT', '/update', () => {})
const radixTree = findMyWay.prettyPrint()
const radixExpected = `\
└── (empty root node)
├── /
│ ├── test
│ │ ├── /hello (GET)
│ │ └── ing (GET)
│ │ └── /
│ │ └── :param (GET)
│ └── update (PUT)
└── * (GET)
`
t.equal(typeof radixTree, 'string')
t.equal(radixTree, radixExpected)
})
test('pretty print commonPrefix - handle constrained routes', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test', () => {})
findMyWay.on('GET', '/test', { constraints: { host: 'auth.fastify.io' } }, () => {})
findMyWay.on('GET', '/test/:hello', () => {})
findMyWay.on('PUT', '/test/:hello', () => {})
findMyWay.on('GET', '/test/:hello', { constraints: { version: '1.1.2' } }, () => {})
findMyWay.on('GET', '/test/:hello', { constraints: { version: '2.0.0' } }, () => {})
const arrayTree = findMyWay.prettyPrint({ commonPrefix: false })
const arrayExpected = `\
└── /test (GET)
/test (GET) {"host":"auth.fastify.io"}
└── /:hello (GET, PUT)
/:hello (GET) {"version":"1.1.2"}
/:hello (GET) {"version":"2.0.0"}
`
t.equal(typeof arrayTree, 'string')
t.equal(arrayTree, arrayExpected)
})
test('pretty print commonPrefix - handle method constraint', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.addConstraintStrategy({
name: 'method',
storage: function () {
const handlers = {}
return {
get: (type) => { return handlers[type] || null },
set: (type, store) => { handlers[type] = store }
}
},
deriveConstraint: (req) => req.headers['x-method'],
mustMatchWhenDerived: true
})
findMyWay.on('GET', '/test', () => {})
findMyWay.on('GET', '/test', { constraints: { method: 'foo' } }, () => {})
findMyWay.on('GET', '/test/:hello', () => {})
findMyWay.on('PUT', '/test/:hello', () => {})
findMyWay.on('GET', '/test/:hello', { constraints: { method: 'bar' } }, () => {})
findMyWay.on('GET', '/test/:hello', { constraints: { method: 'baz' } }, () => {})
const arrayTree = findMyWay.prettyPrint({
commonPrefix: false,
methodConstraintName: 'methodOverride'
})
const arrayExpected = `\
└── /test (GET)
/test (GET) {"method":"foo"}
└── /:hello (GET, PUT)
/:hello (GET) {"method":"bar"}
/:hello (GET) {"method":"baz"}
`
t.equal(typeof arrayTree, 'string')
t.equal(arrayTree, arrayExpected)
})
test('pretty print includeMeta - commonPrefix: true', t => {
t.plan(6)
const findMyWay = FindMyWay()
const namedFunction = () => {}
const store = {
onRequest: [() => {}, namedFunction],
onTimeout: [() => {}],
genericMeta: 'meta',
mixedMeta: ['mixed items', { an: 'object' }],
objectMeta: { one: '1', two: 2 },
functionMeta: namedFunction
}
store[Symbol('symbolKey')] = Symbol('symbolValue')
findMyWay.on('GET', '/test', () => {}, store)
findMyWay.on('GET', '/test', { constraints: { host: 'auth.fastify.io' } }, () => {}, store)
findMyWay.on('GET', '/testing/:hello', () => {}, store)
findMyWay.on('PUT', '/tested/:hello', () => {}, store)
findMyWay.on('GET', '/test/:hello', { constraints: { version: '1.1.2' } }, () => {})
findMyWay.on('GET', '/test/:hello', { constraints: { version: '2.0.0' } }, () => {})
const radixTree = findMyWay.prettyPrint({ commonPrefix: true, includeMeta: true })
const radixTreeExpected = `\
└── /
└── test (GET)
• (onRequest) ["anonymous()","namedFunction()"]
• (onTimeout) ["anonymous()"]
• (genericMeta) "meta"
• (mixedMeta) ["mixed items",{"an":"object"}]
• (objectMeta) {"one":"1","two":2}
• (functionMeta) "namedFunction()"
• (Symbol(symbolKey)) "Symbol(symbolValue)"
test (GET) {"host":"auth.fastify.io"}
• (onRequest) ["anonymous()","namedFunction()"]
• (onTimeout) ["anonymous()"]
• (genericMeta) "meta"
• (mixedMeta) ["mixed items",{"an":"object"}]
• (objectMeta) {"one":"1","two":2}
• (functionMeta) "namedFunction()"
• (Symbol(symbolKey)) "Symbol(symbolValue)"
├── ing/
│ └── :hello (GET)
│ • (onRequest) ["anonymous()","namedFunction()"]
│ • (onTimeout) ["anonymous()"]
│ • (genericMeta) "meta"
│ • (mixedMeta) ["mixed items",{"an":"object"}]
│ • (objectMeta) {"one":"1","two":2}
│ • (functionMeta) "namedFunction()"
│ • (Symbol(symbolKey)) "Symbol(symbolValue)"
├── ed/
│ └── :hello (PUT)
│ • (onRequest) ["anonymous()","namedFunction()"]
│ • (onTimeout) ["anonymous()"]
│ • (genericMeta) "meta"
│ • (mixedMeta) ["mixed items",{"an":"object"}]
│ • (objectMeta) {"one":"1","two":2}
│ • (functionMeta) "namedFunction()"
│ • (Symbol(symbolKey)) "Symbol(symbolValue)"
└── /
└── :hello (GET) {"version":"1.1.2"}
:hello (GET) {"version":"2.0.0"}
`
const radixTreeSpecific = findMyWay.prettyPrint({ commonPrefix: true, includeMeta: ['onTimeout', 'objectMeta', 'nonExistent'] })
const radixTreeSpecificExpected = `\
└── /
└── test (GET)
• (onTimeout) ["anonymous()"]
• (objectMeta) {"one":"1","two":2}
test (GET) {"host":"auth.fastify.io"}
• (onTimeout) ["anonymous()"]
• (objectMeta) {"one":"1","two":2}
├── ing/
│ └── :hello (GET)
│ • (onTimeout) ["anonymous()"]
│ • (objectMeta) {"one":"1","two":2}
├── ed/
│ └── :hello (PUT)
│ • (onTimeout) ["anonymous()"]
│ • (objectMeta) {"one":"1","two":2}
└── /
└── :hello (GET) {"version":"1.1.2"}
:hello (GET) {"version":"2.0.0"}
`
const radixTreeNoMeta = findMyWay.prettyPrint({ commonPrefix: true, includeMeta: false })
const radixTreeNoMetaExpected = `\
└── /
└── test (GET)
test (GET) {"host":"auth.fastify.io"}
├── ing/
│ └── :hello (GET)
├── ed/
│ └── :hello (PUT)
└── /
└── :hello (GET) {"version":"1.1.2"}
:hello (GET) {"version":"2.0.0"}
`
t.equal(typeof radixTree, 'string')
t.equal(radixTree, radixTreeExpected)
t.equal(typeof radixTreeSpecific, 'string')
t.equal(radixTreeSpecific, radixTreeSpecificExpected)
t.equal(typeof radixTreeNoMeta, 'string')
t.equal(radixTreeNoMeta, radixTreeNoMetaExpected)
})
test('pretty print includeMeta - commonPrefix: false', t => {
t.plan(6)
const findMyWay = FindMyWay()
const namedFunction = () => {}
const store = {
onRequest: [() => {}, namedFunction],
onTimeout: [() => {}],
onError: null,
onRegister: undefined,
genericMeta: 'meta',
mixedMeta: ['mixed items', { an: 'object' }],
objectMeta: { one: '1', two: 2 },
functionMeta: namedFunction
}
store[Symbol('symbolKey')] = Symbol('symbolValue')
findMyWay.on('GET', '/test', () => {}, store)
findMyWay.on('GET', '/test', { constraints: { host: 'auth.fastify.io' } }, () => {}, store)
findMyWay.on('GET', '/testing/:hello', () => {}, store)
findMyWay.on('PUT', '/tested/:hello', () => {}, store)
findMyWay.on('GET', '/test/:hello', { constraints: { version: '1.1.2' } }, () => {})
findMyWay.on('GET', '/test/:hello', { constraints: { version: '2.0.0' } }, () => {})
const arrayTree = findMyWay.prettyPrint({ commonPrefix: false, includeMeta: true })
const arrayExpected = `\
└── /test (GET)
• (onRequest) ["anonymous()","namedFunction()"]
• (onTimeout) ["anonymous()"]
• (genericMeta) "meta"
• (mixedMeta) ["mixed items",{"an":"object"}]
• (objectMeta) {"one":"1","two":2}
• (functionMeta) "namedFunction()"
• (Symbol(symbolKey)) "Symbol(symbolValue)"
/test (GET) {"host":"auth.fastify.io"}
• (onRequest) ["anonymous()","namedFunction()"]
• (onTimeout) ["anonymous()"]
• (genericMeta) "meta"
• (mixedMeta) ["mixed items",{"an":"object"}]
• (objectMeta) {"one":"1","two":2}
• (functionMeta) "namedFunction()"
• (Symbol(symbolKey)) "Symbol(symbolValue)"
├── ing/:hello (GET)
│ • (onRequest) ["anonymous()","namedFunction()"]
│ • (onTimeout) ["anonymous()"]
│ • (genericMeta) "meta"
│ • (mixedMeta) ["mixed items",{"an":"object"}]
│ • (objectMeta) {"one":"1","two":2}
│ • (functionMeta) "namedFunction()"
│ • (Symbol(symbolKey)) "Symbol(symbolValue)"
├── ed/:hello (PUT)
│ • (onRequest) ["anonymous()","namedFunction()"]
│ • (onTimeout) ["anonymous()"]
│ • (genericMeta) "meta"
│ • (mixedMeta) ["mixed items",{"an":"object"}]
│ • (objectMeta) {"one":"1","two":2}
│ • (functionMeta) "namedFunction()"
│ • (Symbol(symbolKey)) "Symbol(symbolValue)"
└── /:hello (GET) {"version":"1.1.2"}
/:hello (GET) {"version":"2.0.0"}
`
const arraySpecific = findMyWay.prettyPrint({ commonPrefix: false, includeMeta: ['onRequest', 'mixedMeta', 'nonExistent'] })
const arraySpecificExpected = `\
└── /test (GET)
• (onRequest) ["anonymous()","namedFunction()"]
• (mixedMeta) ["mixed items",{"an":"object"}]
/test (GET) {"host":"auth.fastify.io"}
• (onRequest) ["anonymous()","namedFunction()"]
• (mixedMeta) ["mixed items",{"an":"object"}]
├── ing/:hello (GET)
│ • (onRequest) ["anonymous()","namedFunction()"]
│ • (mixedMeta) ["mixed items",{"an":"object"}]
├── ed/:hello (PUT)
│ • (onRequest) ["anonymous()","namedFunction()"]
│ • (mixedMeta) ["mixed items",{"an":"object"}]
└── /:hello (GET) {"version":"1.1.2"}
/:hello (GET) {"version":"2.0.0"}
`
const arrayNoMeta = findMyWay.prettyPrint({ commonPrefix: false, includeMeta: false })
const arrayNoMetaExpected = `\
└── /test (GET)
/test (GET) {"host":"auth.fastify.io"}
├── ing/:hello (GET)
├── ed/:hello (PUT)
└── /:hello (GET) {"version":"1.1.2"}
/:hello (GET) {"version":"2.0.0"}
`
t.equal(typeof arrayTree, 'string')
t.equal(arrayTree, arrayExpected)
t.equal(typeof arraySpecific, 'string')
t.equal(arraySpecific, arraySpecificExpected)
t.equal(typeof arrayNoMeta, 'string')
t.equal(arrayNoMeta, arrayNoMetaExpected)
})
test('pretty print includeMeta - buildPrettyMeta function', t => {
t.plan(4)
const findMyWay = FindMyWay({
buildPrettyMeta: route => {
return { metaKey: route.method === 'PUT' ? 'Hide PUT route path' : route.path }
}
})
const namedFunction = () => {}
const store = {
onRequest: [() => {}, namedFunction],
onTimeout: [() => {}],
genericMeta: 'meta',
mixedMeta: ['mixed items', { an: 'object' }],
objectMeta: { one: '1', two: 2 },
functionMeta: namedFunction
}
store[Symbol('symbolKey')] = Symbol('symbolValue')
findMyWay.on('GET', '/test', () => {}, store)
findMyWay.on('GET', '/test', { constraints: { host: 'auth.fastify.io' } }, () => {}, store)
findMyWay.on('GET', '/test/:hello', () => {}, store)
findMyWay.on('PUT', '/test/:hello', () => {}, store)
findMyWay.on('POST', '/test/:hello', () => {}, store)
findMyWay.on('GET', '/test/:hello', { constraints: { version: '1.1.2' } }, () => {})
findMyWay.on('GET', '/test/:hello', { constraints: { version: '2.0.0' } }, () => {})
const arrayTree = findMyWay.prettyPrint({ commonPrefix: false, includeMeta: true })
const arrayExpected = `\
└── /test (GET)
• (metaKey) "/test"
/test (GET) {"host":"auth.fastify.io"}
• (metaKey) "/test"
└── /:hello (GET, POST)
• (metaKey) "/test/:hello"
/:hello (PUT)
• (metaKey) "Hide PUT route path"
/:hello (GET) {"version":"1.1.2"}
• (metaKey) "/test/:hello"
/:hello (GET) {"version":"2.0.0"}
• (metaKey) "/test/:hello"
`
const radixTree = findMyWay.prettyPrint({ includeMeta: true })
const radixExpected = `\
└── /
└── test (GET)
• (metaKey) "/test"
test (GET) {"host":"auth.fastify.io"}
• (metaKey) "/test"
└── /
└── :hello (GET, POST)
• (metaKey) "/test/:hello"
:hello (PUT)
• (metaKey) "Hide PUT route path"
:hello (GET) {"version":"1.1.2"}
• (metaKey) "/test/:hello"
:hello (GET) {"version":"2.0.0"}
• (metaKey) "/test/:hello"
`
t.equal(typeof arrayTree, 'string')
t.equal(arrayTree, arrayExpected)
t.equal(typeof radixTree, 'string')
t.equal(radixTree, radixExpected)
})
test('pretty print - print all methods', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.all('/test', () => {})
const tree = findMyWay.prettyPrint()
const expected = `\
└── /
└── test (ACL, BIND, CHECKOUT, CONNECT, COPY, DELETE, GET, HEAD, LINK, LOCK, \
M-SEARCH, MERGE, MKACTIVITY, MKCALENDAR, MKCOL, MOVE, NOTIFY, OPTIONS, PATCH, \
POST, PROPFIND, PROPPATCH, PURGE, PUT, REBIND, REPORT, SEARCH, SOURCE, SUBSCRIBE, \
TRACE, UNBIND, UNLINK, UNLOCK, UNSUBSCRIBE)
`
t.equal(typeof tree, 'string')
t.equal(tree, expected)
})

55
node_modules/find-my-way/test/querystring.test.js generated vendored Normal file
View File

@@ -0,0 +1,55 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('../')
test('should sanitize the url - query', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test', (req, res, params, store, query) => {
t.same(query, { hello: 'world' })
t.ok('inside the handler')
})
findMyWay.lookup({ method: 'GET', url: '/test?hello=world', headers: {} }, null)
})
test('should sanitize the url - hash', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test', (req, res, params, store, query) => {
t.same(query, { hello: '' })
t.ok('inside the handler')
})
findMyWay.lookup({ method: 'GET', url: '/test#hello', headers: {} }, null)
})
test('handles path and query separated by ; with useSemicolonDelimiter enabled', t => {
t.plan(2)
const findMyWay = FindMyWay({
useSemicolonDelimiter: true
})
findMyWay.on('GET', '/test', (req, res, params, store, query) => {
t.same(query, { jsessionid: '123456' })
t.ok('inside the handler')
})
findMyWay.lookup({ method: 'GET', url: '/test;jsessionid=123456', headers: {} }, null)
})
test('handles path and query separated by ? using ; in the path', t => {
t.plan(2)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test;jsessionid=123456', (req, res, params, store, query) => {
t.same(query, { foo: 'bar' })
t.ok('inside the handler')
})
findMyWay.lookup({ method: 'GET', url: '/test;jsessionid=123456?foo=bar', headers: {} }, null)
})

271
node_modules/find-my-way/test/regex.test.js generated vendored Normal file
View File

@@ -0,0 +1,271 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('../')
test('route with matching regex', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: () => {
t.fail('route not matched')
}
})
findMyWay.on('GET', '/test/:id(^\\d+$)', () => {
t.ok('regex match')
})
findMyWay.lookup({ method: 'GET', url: '/test/12', headers: {} }, null)
})
test('route without matching regex', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: () => {
t.ok('route not matched')
}
})
findMyWay.on('GET', '/test/:id(^\\d+$)', () => {
t.fail('regex match')
})
findMyWay.lookup({ method: 'GET', url: '/test/test', headers: {} }, null)
})
test('route with an extension regex 2', t => {
t.plan(2)
const findMyWay = FindMyWay({
defaultRoute: (req) => {
t.fail(`route not matched: ${req.url}`)
}
})
findMyWay.on('GET', '/test/S/:file(^\\S+).png', () => {
t.ok('regex match')
})
findMyWay.on('GET', '/test/D/:file(^\\D+).png', () => {
t.ok('regex match')
})
findMyWay.lookup({ method: 'GET', url: '/test/S/foo.png', headers: {} }, null)
findMyWay.lookup({ method: 'GET', url: '/test/D/foo.png', headers: {} }, null)
})
test('nested route with matching regex', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: () => {
t.fail('route not matched')
}
})
findMyWay.on('GET', '/test/:id(^\\d+$)/hello', () => {
t.ok('regex match')
})
findMyWay.lookup({ method: 'GET', url: '/test/12/hello', headers: {} }, null)
})
test('mixed nested route with matching regex', t => {
t.plan(2)
const findMyWay = FindMyWay({
defaultRoute: () => {
t.fail('route not matched')
}
})
findMyWay.on('GET', '/test/:id(^\\d+$)/hello/:world', (req, res, params) => {
t.equal(params.id, '12')
t.equal(params.world, 'world')
})
findMyWay.lookup({ method: 'GET', url: '/test/12/hello/world', headers: {} }, null)
})
test('mixed nested route with double matching regex', t => {
t.plan(2)
const findMyWay = FindMyWay({
defaultRoute: () => {
t.fail('route not matched')
}
})
findMyWay.on('GET', '/test/:id(^\\d+$)/hello/:world(^\\d+$)', (req, res, params) => {
t.equal(params.id, '12')
t.equal(params.world, '15')
})
findMyWay.lookup({ method: 'GET', url: '/test/12/hello/15', headers: {} }, null)
})
test('mixed nested route without double matching regex', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: () => {
t.ok('route not matched')
}
})
findMyWay.on('GET', '/test/:id(^\\d+$)/hello/:world(^\\d+$)', (req, res, params) => {
t.fail('route mathed')
})
findMyWay.lookup({ method: 'GET', url: '/test/12/hello/test', headers: {} }, null)
})
test('route with an extension regex', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: () => {
t.fail('route not matched')
}
})
findMyWay.on('GET', '/test/:file(^\\d+).png', () => {
t.ok('regex match')
})
findMyWay.lookup({ method: 'GET', url: '/test/12.png', headers: {} }, null)
})
test('route with an extension regex - no match', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: () => {
t.ok('route not matched')
}
})
findMyWay.on('GET', '/test/:file(^\\d+).png', () => {
t.fail('regex match')
})
findMyWay.lookup({ method: 'GET', url: '/test/aa.png', headers: {} }, null)
})
test('safe decodeURIComponent', t => {
t.plan(1)
const findMyWay = FindMyWay({
defaultRoute: () => {
t.ok('route not matched')
}
})
findMyWay.on('GET', '/test/:id(^\\d+$)', () => {
t.fail('we should not be here')
})
t.same(
findMyWay.find('GET', '/test/hel%"Flo', {}),
null
)
})
test('Should check if a regex is safe to use', t => {
t.plan(13)
const noop = () => {}
// https://github.com/substack/safe-regex/blob/master/test/regex.js
const good = [
/\bOakland\b/,
/\b(Oakland|San Francisco)\b/i,
/^\d+1337\d+$/i,
/^\d+(1337|404)\d+$/i,
/^\d+(1337|404)*\d+$/i,
RegExp(Array(26).join('a?') + Array(26).join('a'))
]
const bad = [
/^(a?){25}(a){25}$/,
RegExp(Array(27).join('a?') + Array(27).join('a')),
/(x+x+)+y/,
/foo|(x+x+)+y/,
/(a+){10}y/,
/(a+){2}y/,
/(.*){1,32000}[bc]/
]
const findMyWay = FindMyWay()
good.forEach(regex => {
try {
findMyWay.on('GET', `/test/:id(${regex.toString()})`, noop)
t.pass('ok')
findMyWay.off('GET', `/test/:id(${regex.toString()})`)
} catch (err) {
t.fail(err)
}
})
bad.forEach(regex => {
try {
findMyWay.on('GET', `/test/:id(${regex.toString()})`, noop)
t.fail('should throw')
} catch (err) {
t.ok(err)
}
})
})
test('Disable safe regex check', t => {
t.plan(13)
const noop = () => {}
// https://github.com/substack/safe-regex/blob/master/test/regex.js
const good = [
/\bOakland\b/,
/\b(Oakland|San Francisco)\b/i,
/^\d+1337\d+$/i,
/^\d+(1337|404)\d+$/i,
/^\d+(1337|404)*\d+$/i,
RegExp(Array(26).join('a?') + Array(26).join('a'))
]
const bad = [
/^(a?){25}(a){25}$/,
RegExp(Array(27).join('a?') + Array(27).join('a')),
/(x+x+)+y/,
/foo|(x+x+)+y/,
/(a+){10}y/,
/(a+){2}y/,
/(.*){1,32000}[bc]/
]
const findMyWay = FindMyWay({ allowUnsafeRegex: true })
good.forEach(regex => {
try {
findMyWay.on('GET', `/test/:id(${regex.toString()})`, noop)
t.pass('ok')
findMyWay.off('GET', `/test/:id(${regex.toString()})`)
} catch (err) {
t.fail(err)
}
})
bad.forEach(regex => {
try {
findMyWay.on('GET', `/test/:id(${regex.toString()})`, noop)
t.pass('ok')
findMyWay.off('GET', `/test/:id(${regex.toString()})`)
} catch (err) {
t.fail(err)
}
})
})
test('prevent back-tracking', (t) => {
t.plan(0)
t.setTimeout(20)
const findMyWay = FindMyWay({
defaultRoute: () => {
t.fail('route not matched')
}
})
findMyWay.on('GET', '/:foo-:bar-', (req, res, params) => {})
findMyWay.find('GET', '/' + '-'.repeat(16000) + 'a', { host: 'fastify.io' })
})

View File

@@ -0,0 +1,46 @@
'use strict'
const { test } = require('tap')
const FindMyWay = require('../')
function initializeRoutes (router, handler, quantity) {
for (const x of Array(quantity).keys()) {
router.on('GET', '/test-route-' + x, handler)
}
return router
}
test('verify routes registered', t => {
const quantity = 5
// 1 (check length) + quantity of routes * quantity of tests per route
t.plan(1 + (quantity * 1))
let findMyWay = FindMyWay()
const defaultHandler = (req, res, params) => res.end(JSON.stringify({ hello: 'world' }))
findMyWay = initializeRoutes(findMyWay, defaultHandler, quantity)
t.equal(findMyWay.routes.length, quantity)
findMyWay.routes.forEach((route, idx) => {
t.match(route, {
method: 'GET',
path: '/test-route-' + idx,
opts: {},
handler: defaultHandler,
store: undefined
})
})
})
test('verify routes registered and deregister', t => {
// 1 (check length) + quantity of routes * quantity of tests per route
t.plan(2)
let findMyWay = FindMyWay()
const quantity = 2
const defaultHandler = (req, res, params) => res.end(JSON.stringify({ hello: 'world' }))
findMyWay = initializeRoutes(findMyWay, defaultHandler, quantity)
t.equal(findMyWay.routes.length, quantity)
findMyWay.off('GET', '/test-route-0')
t.equal(findMyWay.routes.length, quantity - 1)
})

437
node_modules/find-my-way/test/server.test.js generated vendored Normal file
View File

@@ -0,0 +1,437 @@
'use strict'
const t = require('tap')
const test = t.test
const http = require('http')
const FindMyWay = require('../')
test('basic router with http server', t => {
t.plan(6)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test', (req, res, params) => {
t.ok(req)
t.ok(res)
t.ok(params)
res.end(JSON.stringify({ hello: 'world' }))
})
const server = http.createServer((req, res) => {
findMyWay.lookup(req, res)
})
server.listen(0, err => {
t.error(err)
server.unref()
http.get('http://localhost:' + server.address().port + '/test', async (res) => {
let body = ''
for await (const chunk of res) {
body += chunk
}
t.equal(res.statusCode, 200)
t.same(JSON.parse(body), { hello: 'world' })
})
})
})
test('router with params with http server', t => {
t.plan(6)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test/:id', (req, res, params) => {
t.ok(req)
t.ok(res)
t.equal(params.id, 'hello')
res.end(JSON.stringify({ hello: 'world' }))
})
const server = http.createServer((req, res) => {
findMyWay.lookup(req, res)
})
server.listen(0, err => {
t.error(err)
server.unref()
http.get('http://localhost:' + server.address().port + '/test/hello', async (res) => {
let body = ''
for await (const chunk of res) {
body += chunk
}
t.equal(res.statusCode, 200)
t.same(JSON.parse(body), { hello: 'world' })
})
})
})
test('default route', t => {
t.plan(2)
const findMyWay = FindMyWay({
defaultRoute: (req, res) => {
res.statusCode = 404
res.end()
}
})
const server = http.createServer((req, res) => {
findMyWay.lookup(req, res)
})
server.listen(0, err => {
t.error(err)
server.unref()
http.get('http://localhost:' + server.address().port, async (res) => {
t.equal(res.statusCode, 404)
})
})
})
test('automatic default route', t => {
t.plan(2)
const findMyWay = FindMyWay()
const server = http.createServer((req, res) => {
findMyWay.lookup(req, res)
})
server.listen(0, err => {
t.error(err)
server.unref()
http.get('http://localhost:' + server.address().port, async (res) => {
t.equal(res.statusCode, 404)
})
})
})
test('maps two routes when trailing slash should be trimmed', t => {
t.plan(21)
const findMyWay = FindMyWay({
ignoreTrailingSlash: true
})
findMyWay.on('GET', '/test/', (req, res, params) => {
t.ok(req)
t.ok(res)
t.ok(params)
res.end('test')
})
findMyWay.on('GET', '/othertest', (req, res, params) => {
t.ok(req)
t.ok(res)
t.ok(params)
res.end('othertest')
})
const server = http.createServer((req, res) => {
findMyWay.lookup(req, res)
})
server.listen(0, err => {
t.error(err)
server.unref()
const baseURL = 'http://localhost:' + server.address().port
http.get(baseURL + '/test/', async (res) => {
let body = ''
for await (const chunk of res) {
body += chunk
}
t.equal(res.statusCode, 200)
t.same(body, 'test')
})
http.get(baseURL + '/test', async (res) => {
let body = ''
for await (const chunk of res) {
body += chunk
}
t.equal(res.statusCode, 200)
t.same(body, 'test')
})
http.get(baseURL + '/othertest', async (res) => {
let body = ''
for await (const chunk of res) {
body += chunk
}
t.equal(res.statusCode, 200)
t.same(body, 'othertest')
})
http.get(baseURL + '/othertest/', async (res) => {
let body = ''
for await (const chunk of res) {
body += chunk
}
t.equal(res.statusCode, 200)
t.same(body, 'othertest')
})
})
})
test('does not trim trailing slash when ignoreTrailingSlash is false', t => {
t.plan(7)
const findMyWay = FindMyWay({
ignoreTrailingSlash: false
})
findMyWay.on('GET', '/test/', (req, res, params) => {
t.ok(req)
t.ok(res)
t.ok(params)
res.end('test')
})
const server = http.createServer((req, res) => {
findMyWay.lookup(req, res)
})
server.listen(0, err => {
t.error(err)
server.unref()
const baseURL = 'http://localhost:' + server.address().port
http.get(baseURL + '/test/', async (res) => {
let body = ''
for await (const chunk of res) {
body += chunk
}
t.equal(res.statusCode, 200)
t.same(body, 'test')
})
http.get(baseURL + '/test', async (res) => {
t.equal(res.statusCode, 404)
})
})
})
test('does not map // when ignoreTrailingSlash is true', t => {
t.plan(7)
const findMyWay = FindMyWay({
ignoreTrailingSlash: false
})
findMyWay.on('GET', '/', (req, res, params) => {
t.ok(req)
t.ok(res)
t.ok(params)
res.end('test')
})
const server = http.createServer((req, res) => {
findMyWay.lookup(req, res)
})
server.listen(0, err => {
t.error(err)
server.unref()
const baseURL = 'http://localhost:' + server.address().port
http.get(baseURL + '/', async (res) => {
let body = ''
for await (const chunk of res) {
body += chunk
}
t.equal(res.statusCode, 200)
t.same(body, 'test')
})
http.get(baseURL + '//', async (res) => {
t.equal(res.statusCode, 404)
})
})
})
test('maps two routes when duplicate slashes should be trimmed', t => {
t.plan(21)
const findMyWay = FindMyWay({
ignoreDuplicateSlashes: true
})
findMyWay.on('GET', '//test', (req, res, params) => {
t.ok(req)
t.ok(res)
t.ok(params)
res.end('test')
})
findMyWay.on('GET', '/othertest', (req, res, params) => {
t.ok(req)
t.ok(res)
t.ok(params)
res.end('othertest')
})
const server = http.createServer((req, res) => {
findMyWay.lookup(req, res)
})
server.listen(0, err => {
t.error(err)
server.unref()
const baseURL = 'http://localhost:' + server.address().port
http.get(baseURL + '//test', async (res) => {
let body = ''
for await (const chunk of res) {
body += chunk
}
t.equal(res.statusCode, 200)
t.same(body, 'test')
})
http.get(baseURL + '/test', async (res) => {
let body = ''
for await (const chunk of res) {
body += chunk
}
t.equal(res.statusCode, 200)
t.same(body, 'test')
})
http.get(baseURL + '/othertest', async (res) => {
let body = ''
for await (const chunk of res) {
body += chunk
}
t.equal(res.statusCode, 200)
t.same(body, 'othertest')
})
http.get(baseURL + '//othertest', async (res) => {
let body = ''
for await (const chunk of res) {
body += chunk
}
t.equal(res.statusCode, 200)
t.same(body, 'othertest')
})
})
})
test('does not trim duplicate slashes when ignoreDuplicateSlashes is false', t => {
t.plan(7)
const findMyWay = FindMyWay({
ignoreDuplicateSlashes: false
})
findMyWay.on('GET', '//test', (req, res, params) => {
t.ok(req)
t.ok(res)
t.ok(params)
res.end('test')
})
const server = http.createServer((req, res) => {
findMyWay.lookup(req, res)
})
server.listen(0, err => {
t.error(err)
server.unref()
const baseURL = 'http://localhost:' + server.address().port
http.get(baseURL + '//test', async (res) => {
let body = ''
for await (const chunk of res) {
body += chunk
}
t.equal(res.statusCode, 200)
t.same(body, 'test')
})
http.get(baseURL + '/test', async (res) => {
t.equal(res.statusCode, 404)
})
})
})
test('does map // when ignoreDuplicateSlashes is true', t => {
t.plan(11)
const findMyWay = FindMyWay({
ignoreDuplicateSlashes: true
})
findMyWay.on('GET', '/', (req, res, params) => {
t.ok(req)
t.ok(res)
t.ok(params)
res.end('test')
})
const server = http.createServer((req, res) => {
findMyWay.lookup(req, res)
})
server.listen(0, err => {
t.error(err)
server.unref()
const baseURL = 'http://localhost:' + server.address().port
http.get(baseURL + '/', async (res) => {
let body = ''
for await (const chunk of res) {
body += chunk
}
t.equal(res.statusCode, 200)
t.same(body, 'test')
})
http.get(baseURL + '//', async (res) => {
let body = ''
for await (const chunk of res) {
body += chunk
}
t.equal(res.statusCode, 200)
t.same(body, 'test')
})
})
})
test('versioned routes', t => {
t.plan(3)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test', { constraints: { version: '1.2.3' } }, (req, res, params) => {
res.end('ok')
})
const server = http.createServer((req, res) => {
findMyWay.lookup(req, res)
})
server.listen(0, err => {
t.error(err)
server.unref()
http.get(
'http://localhost:' + server.address().port + '/test',
{
headers: { 'Accept-Version': '1.x' }
},
async (res) => {
t.equal(res.statusCode, 200)
}
)
http.get(
'http://localhost:' + server.address().port + '/test',
{
headers: { 'Accept-Version': '2.x' }
},
async (res) => {
t.equal(res.statusCode, 404)
}
)
})
})

47
node_modules/find-my-way/test/shorthands.test.js generated vendored Normal file
View File

@@ -0,0 +1,47 @@
'use strict'
const httpMethods = require('../lib/http-methods')
const t = require('tap')
const test = t.test
const FindMyWay = require('../')
t.test('should support shorthand', t => {
t.plan(httpMethods.length)
for (const i in httpMethods) {
const m = httpMethods[i]
const methodName = m.toLowerCase()
t.test('`.' + methodName + '`', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay[methodName]('/test', () => {
t.ok('inside the handler')
})
findMyWay.lookup({ method: m, url: '/test', headers: {} }, null)
})
}
})
test('should support `.all` shorthand', t => {
t.plan(11)
const findMyWay = FindMyWay()
findMyWay.all('/test', () => {
t.ok('inside the handler')
})
findMyWay.lookup({ method: 'GET', url: '/test', headers: {} }, null)
findMyWay.lookup({ method: 'DELETE', url: '/test', headers: {} }, null)
findMyWay.lookup({ method: 'HEAD', url: '/test', headers: {} }, null)
findMyWay.lookup({ method: 'PATCH', url: '/test', headers: {} }, null)
findMyWay.lookup({ method: 'POST', url: '/test', headers: {} }, null)
findMyWay.lookup({ method: 'PUT', url: '/test', headers: {} }, null)
findMyWay.lookup({ method: 'OPTIONS', url: '/test', headers: {} }, null)
findMyWay.lookup({ method: 'TRACE', url: '/test', headers: {} }, null)
findMyWay.lookup({ method: 'CONNECT', url: '/test', headers: {} }, null)
findMyWay.lookup({ method: 'COPY', url: '/test', headers: {} }, null)
findMyWay.lookup({ method: 'SUBSCRIBE', url: '/test', headers: {} }, null)
})

50
node_modules/find-my-way/test/store.test.js generated vendored Normal file
View File

@@ -0,0 +1,50 @@
'use strict'
const t = require('tap')
const test = t.test
const FindMyWay = require('../')
test('handler should have the store object', t => {
t.plan(1)
const findMyWay = FindMyWay()
findMyWay.on('GET', '/test', (req, res, params, store) => {
t.equal(store.hello, 'world')
}, { hello: 'world' })
findMyWay.lookup({ method: 'GET', url: '/test', headers: {} }, null)
})
test('find a store object', t => {
t.plan(1)
const findMyWay = FindMyWay()
const fn = () => {}
findMyWay.on('GET', '/test', fn, { hello: 'world' })
t.same(findMyWay.find('GET', '/test'), {
handler: fn,
params: {},
store: { hello: 'world' },
searchParams: {}
})
})
test('update the store', t => {
t.plan(2)
const findMyWay = FindMyWay()
let bool = false
findMyWay.on('GET', '/test', (req, res, params, store) => {
if (!bool) {
t.equal(store.hello, 'world')
store.hello = 'hello'
bool = true
findMyWay.lookup({ method: 'GET', url: '/test', headers: {} }, null)
} else {
t.equal(store.hello, 'hello')
}
}, { hello: 'world' })
findMyWay.lookup({ method: 'GET', url: '/test', headers: {} }, null)
})

174
node_modules/find-my-way/test/types/router.test-d.ts generated vendored Normal file
View File

@@ -0,0 +1,174 @@
import { expectType } from 'tsd'
import * as Router from '../../'
import { Http2ServerRequest, Http2ServerResponse } from 'http2'
import { IncomingMessage, ServerResponse } from 'http'
let http1Req!: IncomingMessage;
let http1Res!: ServerResponse;
let http2Req!: Http2ServerRequest;
let http2Res!: Http2ServerResponse;
// HTTP1
{
let handler: Router.Handler<Router.HTTPVersion.V1>
const router = Router({
ignoreTrailingSlash: true,
ignoreDuplicateSlashes: true,
allowUnsafeRegex: false,
caseSensitive: false,
maxParamLength: 42,
defaultRoute (http1Req, http1Res) {},
onBadUrl (path, http1Req, http1Res) {},
constraints: {
foo: {
name: 'foo',
mustMatchWhenDerived: true,
storage () {
return {
get (version) { return handler },
set (version, handler) {}
}
},
deriveConstraint(req) { return '1.0.0' },
validate(value) { if (typeof value === "string") { throw new Error("invalid")} }
}
}
})
expectType<Router.Instance<Router.HTTPVersion.V1>>(router)
expectType<void>(router.on('GET', '/', () => {}))
expectType<void>(router.on(['GET', 'POST'], '/', () => {}))
expectType<void>(router.on('GET', '/', { constraints: { version: '1.0.0' }}, () => {}))
expectType<void>(router.on('GET', '/', () => {}, {}))
expectType<void>(router.on('GET', '/', {constraints: { version: '1.0.0' }}, () => {}, {}))
expectType<void>(router.get('/', () => {}))
expectType<void>(router.get('/', { constraints: { version: '1.0.0' }}, () => {}))
expectType<void>(router.get('/', () => {}, {}))
expectType<void>(router.get('/', { constraints: { version: '1.0.0' }}, () => {}, {}))
expectType<void>(router.off('GET', '/'))
expectType<void>(router.off(['GET', 'POST'], '/'))
expectType<any>(router.lookup(http1Req, http1Res))
expectType<Router.FindResult<Router.HTTPVersion.V1> | null>(router.find('GET', '/'))
expectType<Router.FindResult<Router.HTTPVersion.V1> | null>(router.find('GET', '/', {}))
expectType<Router.FindResult<Router.HTTPVersion.V1> | null>(router.find('GET', '/', {version: '1.0.0'}))
expectType<Router.FindRouteResult<Router.HTTPVersion.V1> | null>(router.findRoute('GET', '/'));
expectType<Router.FindRouteResult<Router.HTTPVersion.V1> | null>(router.findRoute('GET', '/', {}));
expectType<Router.FindRouteResult<Router.HTTPVersion.V1> | null>(router.findRoute('GET', '/', {version: '1.0.0'}));
expectType<void>(router.reset())
expectType<string>(router.prettyPrint())
expectType<string>(router.prettyPrint({ method: 'GET' }))
expectType<string>(router.prettyPrint({ commonPrefix: false }))
expectType<string>(router.prettyPrint({ commonPrefix: true }))
expectType<string>(router.prettyPrint({ includeMeta: true }))
expectType<string>(router.prettyPrint({ includeMeta: ['test', Symbol('test')] }))
}
// HTTP2
{
const constraints: { [key: string]: Router.ConstraintStrategy<Router.HTTPVersion.V2, string> } = {
foo: {
name: 'foo',
mustMatchWhenDerived: true,
storage () {
return {
get (version) { return handler },
set (version, handler) {}
}
},
deriveConstraint(req) { return '1.0.0' },
validate(value) { if (typeof value === "string") { throw new Error("invalid")} }
}
}
let handler: Router.Handler<Router.HTTPVersion.V2>
const router = Router<Router.HTTPVersion.V2>({
ignoreTrailingSlash: true,
ignoreDuplicateSlashes: true,
allowUnsafeRegex: false,
caseSensitive: false,
maxParamLength: 42,
defaultRoute (http1Req, http1Res) {},
onBadUrl (path, http1Req, http1Res) {},
constraints
})
expectType<Router.Instance<Router.HTTPVersion.V2>>(router)
expectType<void>(router.on('GET', '/', () => {}))
expectType<void>(router.on(['GET', 'POST'], '/', () => {}))
expectType<void>(router.on('GET', '/', { constraints: { version: '1.0.0' }}, () => {}))
expectType<void>(router.on('GET', '/', () => {}, {}))
expectType<void>(router.on('GET', '/', { constraints: { version: '1.0.0' }}, () => {}, {}))
expectType<void>(router.addConstraintStrategy(constraints.foo))
expectType<void>(router.get('/', () => {}))
expectType<void>(router.get('/', { constraints: { version: '1.0.0' }}, () => {}))
expectType<void>(router.get('/', () => {}, {}))
expectType<void>(router.get('/', { constraints: { version: '1.0.0' }}, () => {}, {}))
expectType<void>(router.off('GET', '/'))
expectType<void>(router.off(['GET', 'POST'], '/'))
expectType<any>(router.lookup(http2Req, http2Res))
expectType<Router.FindResult<Router.HTTPVersion.V2> | null>(router.find('GET', '/', {}))
expectType<Router.FindResult<Router.HTTPVersion.V2> | null>(router.find('GET', '/', {version: '1.0.0', host: 'fastify.io'}))
expectType<void>(router.reset())
expectType<string>(router.prettyPrint())
}
// Custom Constraint
{
let handler: Router.Handler<Router.HTTPVersion.V1>
interface AcceptAndContentType { accept?: string, contentType?: string }
const customConstraintWithObject: Router.ConstraintStrategy<Router.HTTPVersion.V1, AcceptAndContentType> = {
name: "customConstraintWithObject",
deriveConstraint<Context>(req: Router.Req<Router.HTTPVersion.V1>, ctx: Context | undefined): AcceptAndContentType {
return {
accept: req.headers.accept,
contentType: req.headers["content-type"]
}
},
validate(value: unknown): void {},
storage () {
return {
get (version) { return handler },
set (version, handler) {}
}
}
}
const storageWithObject = customConstraintWithObject.storage()
const acceptAndContentType: AcceptAndContentType = { accept: 'application/json', contentType: 'application/xml' }
expectType<AcceptAndContentType>(customConstraintWithObject.deriveConstraint(http1Req, http1Res))
expectType<Router.Handler<Router.HTTPVersion.V1> | null>(storageWithObject.get(acceptAndContentType));
expectType<void>(storageWithObject.set(acceptAndContentType, () => {}));
const customConstraintWithDefault: Router.ConstraintStrategy<Router.HTTPVersion.V1> = {
name: "customConstraintWithObject",
deriveConstraint<Context>(req: Router.Req<Router.HTTPVersion.V1>, ctx: Context | undefined): string {
return req.headers.accept ?? ''
},
storage () {
return {
get (version) { return handler },
set (version, handler) {}
}
}
}
const storageWithDefault = customConstraintWithDefault.storage()
expectType<string>(customConstraintWithDefault.deriveConstraint(http1Req, http1Res))
expectType<Router.Handler<Router.HTTPVersion.V1> | null>(storageWithDefault.get(''));
expectType<void>(storageWithDefault.set('', () => {}));
}