wzp
2021-05-13 7d694a9113118daec5be7ac224dab46a3b20f106
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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.deepEqual(
      { test: 'text' },
      params
    )
  })
  findMyWay.on('GET', '/text/hello', noop)
 
  findMyWay.lookup({ url: '/text', method: 'GET', headers: {} })
})