Skip to content
Myr

common.case.test.ts

This file

Path

src/router/common.case.test.ts

1,118 lines 路 36.8 KiB 路 TypeScript

Last changed by

fix(linear-router): don't match an empty path segment as a param (#5373)Harshit Prakashbefore adoption00ee875

History of this file

Take it

1import type { RunnerTestSuite } from 'vitest'2import type { ParamIndexMap, Params, Router } from '../router'34const getSuiteHierarchy = (suite?: RunnerTestSuite) => {5  const res: RunnerTestSuite[] = []6  let s: RunnerTestSuite | undefined = suite7  while (s) {8    res.unshift(s)9    s = s.suite10  }11  return res12}1314export const runTest = ({15  skip = [],16  newRouter,17}: {18  skip?: {19    reason: string20    tests: string[]21  }[]22  newRouter: <T>() => Router<T>23}) => {24  describe('Common', () => {25    type Match = (method: string, path: string) => { handler: string; params: Params }[]26    let router: Router<string>27    let match: Match2829    beforeEach(({ task, skip: skipTask }) => {30      const suites = getSuiteHierarchy(task.suite)31      const name = [...suites.slice(2).map((s) => s.name), task.name].join(' > ')32      const isSkip = skip.find((s) => s.tests.includes(name))33      if (isSkip) {34        console.log(`Skip: ${isSkip.reason}`)35        skipTask()36        return37      }3839      router = newRouter()40      match = (method: string, path: string) => {41        const [matchRes, stash] = router.match(method, path)42        const res = matchRes.map((r) =>43          stash44            ? {45                handler: r[0],46                params: Object.keys(r[1]).reduce(47                  (acc, key) => {48                    acc[key] = stash[(r[1] as ParamIndexMap)[key]]49                    return acc50                  },51                  Object.create(null) as Params52                ),53              }54            : { handler: r[0], params: r[1] as Params }55        )56        return res57      }58    })5960    describe('Basic Usage', () => {61      beforeEach(() => {62        router.add('GET', '/hello', 'get hello')63        router.add('POST', '/hello', 'post hello')64        router.add('PURGE', '/hello', 'purge hello')65      })6667      it('GET, post hello', async () => {68        let res = match('GET', '/hello')69        expect(res.length).toBe(1)70        expect(res[0].handler).toEqual('get hello')71        res = match('POST', '/hello')72        expect(res.length).toBe(1)73        expect(res[0].handler).toEqual('post hello')74        res = match('PURGE', '/hello')75        expect(res.length).toBe(1)76        expect(res[0].handler).toEqual('purge hello')77        res = match('PUT', '/hello')78        expect(res.length).toBe(0)79        res = match('GET', '/')80        expect(res.length).toBe(0)81      })82    })8384    describe('Reserved words', () => {85      it('Reserved words and named parameter', async () => {86        router.add('GET', '/entry/:constructor', 'get entry')87        const res = match('GET', '/entry/123')88        expect(res.length).toBe(1)89        expect(res[0].handler).toEqual('get entry')90        expect(res[0].params['constructor']).toBe('123')91      })9293      it('Reserved words and wildcard', async () => {94        router.add('GET', '/wild/*/card', 'get wildcard')95        const res = match('GET', '/wild/constructor/card')96        expect(res.length).toBe(1)97        expect(res[0].handler).toEqual('get wildcard')98      })99100      it('Reserved words and optional named parameter', async () => {101        router.add('GET', '/api/animals/:constructor?', 'animals')102        const res = match('GET', '/api/animals')103        expect(res.length).toBe(1)104        expect(res[0].handler).toEqual('animals')105        expect(res[0].params['constructor']).toBeUndefined()106      })107    })108109    describe('Complex', () => {110      it('Named Param', async () => {111        router.add('GET', '/entry/:id', 'get entry')112        let res = match('GET', '/entry/123')113114        expect(res.length).toBe(1)115        expect(res[0].handler).toEqual('get entry')116        expect(res[0].params['id']).toBe('123')117118        res = match('GET', '/entry-123')119        expect(res.length).toBe(0)120      })121122      it('Wildcard', async () => {123        router.add('GET', '/wild/*/card', 'get wildcard')124        const res = match('GET', '/wild/xxx/card')125        expect(res.length).toBe(1)126        expect(res[0].handler).toEqual('get wildcard')127      })128129      it('Default', async () => {130        router.add('GET', '/api/abc', 'get api')131        router.add('GET', '/api/*', 'fallback')132        let res = match('GET', '/api/abc')133        expect(res.length).toBe(2)134        expect(res[0].handler).toEqual('get api')135        expect(res[1].handler).toEqual('fallback')136        res = match('GET', '/api/def')137        expect(res.length).toBe(1)138        expect(res[0].handler).toEqual('fallback')139      })140141      it('Regexp', async () => {142        router.add('GET', '/post/:date{[0-9]+}/:title{[a-z]+}', 'get post')143        let res = match('GET', '/post/20210101/hello')144        expect(res.length).toBe(1)145        expect(res[0].handler).toEqual('get post')146        expect(res[0].params['date']).toBe('20210101')147        expect(res[0].params['title']).toBe('hello')148        res = match('GET', '/post/onetwothree')149        expect(res.length).toBe(0)150        res = match('GET', '/post/123/123')151        expect(res.length).toBe(0)152      })153154      it('Parameter with {.*} regexp', () => {155        router.add('GET', '/files/:name{.*}', 'file')156        let res = match('GET', '/files')157        expect(res.length).toBe(0)158159        res = match('GET', '/files/a')160        expect(res.length).toBe(1)161        expect(res[0].handler).toEqual('file')162        expect(res[0].params['name']).toEqual('a')163164        res = match('GET', '/files/a/b')165        expect(res.length).toBe(1)166        expect(res[0].handler).toEqual('file')167        expect(res[0].params['name']).toEqual('a/b')168169        res = match('GET', '/files/')170        expect(res.length).toBe(1)171        expect(res[0].handler).toEqual('file')172      })173174      it('/*', async () => {175        router.add('GET', '/api/*', 'auth middleware')176        router.add('GET', '/api', 'top')177        router.add('GET', '/api/posts', 'posts')178        router.add('GET', '/api/*', 'fallback')179180        let res = match('GET', '/api')181        expect(res.length).toBe(3)182        expect(res[0].handler).toEqual('auth middleware')183        expect(res[1].handler).toEqual('top')184        expect(res[2].handler).toEqual('fallback')185        res = match('GET', '/api/posts')186        expect(res.length).toBe(3)187        expect(res[0].handler).toEqual('auth middleware')188        expect(res[1].handler).toEqual('posts')189        expect(res[2].handler).toEqual('fallback')190      })191    })192193    describe('Registration order', () => {194      it('middleware -> handler', async () => {195        router.add('GET', '*', 'bar')196        router.add('GET', '/:type/:action', 'foo')197        const res = match('GET', '/posts/123')198        expect(res.length).toBe(2)199        expect(res[0].handler).toEqual('bar')200        expect(res[1].handler).toEqual('foo')201      })202203      it('handler -> fallback', async () => {204        router.add('GET', '/:type/:action', 'foo')205        router.add('GET', '*', 'fallback')206        const res = match('GET', '/posts/123')207        expect(res.length).toBe(2)208        expect(res[0].handler).toEqual('foo')209        expect(res[1].handler).toEqual('fallback')210      })211    })212213    describe('Multi match', () => {214      describe('Blog', () => {215        beforeEach(() => {216          router.add('ALL', '*', 'middleware a')217          router.add('GET', '*', 'middleware b')218          router.add('GET', '/entry', 'get entries')219          router.add('POST', '/entry/*', 'middleware c')220          router.add('POST', '/entry', 'post entry')221          router.add('GET', '/entry/:id', 'get entry')222          router.add('GET', '/entry/:id/comment/:comment_id', 'get comment')223        })224225        it('GET /', async () => {226          const res = match('GET', '/')227          expect(res.length).toBe(2)228          expect(res[0].handler).toEqual('middleware a')229          expect(res[1].handler).toEqual('middleware b')230        })231        it('GET /entry/123', async () => {232          const res = match('GET', '/entry/123')233          expect(res.length).toBe(3)234          expect(res[0].handler).toEqual('middleware a')235          expect(res[0].params['id']).toBe(undefined)236          expect(res[1].handler).toEqual('middleware b')237          expect(res[1].params['id']).toBe(undefined)238          expect(res[2].handler).toEqual('get entry')239          expect(res[2].params['id']).toBe('123')240        })241        it('GET /entry/123/comment/456', async () => {242          const res = match('GET', '/entry/123/comment/456')243          expect(res.length).toBe(3)244          expect(res[0].handler).toEqual('middleware a')245          expect(res[0].params['id']).toBe(undefined)246          expect(res[0].params['comment_id']).toBe(undefined)247          expect(res[1].handler).toEqual('middleware b')248          expect(res[1].params['id']).toBe(undefined)249          expect(res[1].params['comment_id']).toBe(undefined)250          expect(res[2].handler).toEqual('get comment')251          expect(res[2].params['id']).toBe('123')252          expect(res[2].params['comment_id']).toBe('456')253        })254        it('POST /entry', async () => {255          const res = match('POST', '/entry')256          expect(res.length).toBe(3)257          expect(res[0].handler).toEqual('middleware a')258          expect(res[1].handler).toEqual('middleware c')259          expect(res[2].handler).toEqual('post entry')260        })261        it('DELETE /entry', async () => {262          const res = match('DELETE', '/entry')263          expect(res.length).toBe(1)264          expect(res[0].handler).toEqual('middleware a')265        })266      })267268      describe('`params` per a handler', () => {269        beforeEach(() => {270          router.add('ALL', '*', 'middleware a')271          router.add('GET', '/entry/:id/*', 'middleware b')272          router.add('GET', '/entry/:id/:action', 'action')273        })274275        it('GET /entry/123/show', async () => {276          const res = match('GET', '/entry/123/show')277          expect(res.length).toBe(3)278          expect(res[0].handler).toEqual('middleware a')279          expect(res[0].params['id']).toBe(undefined)280          expect(res[0].params['action']).toBe(undefined)281          expect(res[1].handler).toEqual('middleware b')282          expect(res[1].params['id']).toBe('123')283          expect(res[1].params['comment_id']).toBe(undefined)284          expect(res[2].handler).toEqual('action')285          expect(res[2].params['id']).toBe('123')286          expect(res[2].params['action']).toBe('show')287        })288      })289290      it('hierarchy', () => {291        router.add('GET', '/posts/:id/comments/:comment_id', 'foo')292        router.add('GET', '/posts/:id', 'bar')293        expect(() => {294          router.match('GET', '/')295        }).not.toThrow()296      })297    })298299    describe('Duplicate param name', () => {300      it('self', () => {301        router.add('GET', '/:id/:id', 'foo')302        const res = match('GET', '/123/456')303        expect(res.length).toBe(1)304        expect(res[0].handler).toEqual('foo')305        expect(res[0].params['id']).toBe('123')306      })307308      it('parent', () => {309        router.add('GET', '/:id/:action', 'foo')310        router.add('GET', '/posts/:id', 'bar')311        const res = match('GET', '/posts/get')312        expect(res.length).toBe(2)313        expect(res[0].handler).toEqual('foo')314        expect(res[0].params['id']).toBe('posts')315        expect(res[0].params['action']).toBe('get')316        expect(res[1].handler).toEqual('bar')317        expect(res[1].params['id']).toBe('get')318      })319320      it('child', () => {321        router.add('GET', '/posts/:id', 'foo')322        router.add('GET', '/:id/:action', 'bar')323        const res = match('GET', '/posts/get')324        expect(res.length).toBe(2)325        expect(res[0].handler).toEqual('foo')326        expect(res[0].params['id']).toBe('get')327        expect(res[1].handler).toEqual('bar')328        expect(res[1].params['id']).toBe('posts')329        expect(res[1].params['action']).toBe('get')330      })331    })332333    describe('page', () => {334      it('GET /page', async () => {335        router.add('GET', '/page', 'page')336        router.add('ALL', '*', 'fallback') // or '*'337338        const res = match('GET', '/page')339        expect(res.length).toBe(2)340        expect(res[0].handler).toEqual('page')341        expect(res[1].handler).toEqual('fallback')342      })343    })344345    describe('star', () => {346      beforeEach(() => {347        router.add('GET', '/', '/')348        router.add('GET', '/*', '/*')349        router.add('GET', '*', '*')350351        router.add('GET', '/x', '/x')352        router.add('GET', '/x/*', '/x/*')353      })354355      it('top', async () => {356        const res = match('GET', '/')357        expect(res.length).toBe(3)358        expect(res[0].handler).toEqual('/')359        expect(res[1].handler).toEqual('/*')360        expect(res[2].handler).toEqual('*')361      })362363      it('Under a certain path', async () => {364        const res = match('GET', '/x')365        expect(res.length).toBe(4)366        expect(res[0].handler).toEqual('/*')367        expect(res[1].handler).toEqual('*')368        expect(res[2].handler).toEqual('/x')369        expect(res[3].handler).toEqual('/x/*')370      })371    })372373    describe('Suffix wildcard', () => {374      beforeEach(() => {375        router.add('GET', '/assets*', 'assets')376      })377378      it('GET /assets', async () => {379        const res = match('GET', '/assets')380        expect(res.length).toBe(1)381        expect(res[0].handler).toEqual('assets')382      })383384      it('GET /assets-v2', async () => {385        const res = match('GET', '/assets-v2')386        expect(res.length).toBe(1)387        expect(res[0].handler).toEqual('assets')388      })389390      it('GET /assets/app.js', async () => {391        const res = match('GET', '/assets/app.js')392        expect(res.length).toBe(1)393        expect(res[0].handler).toEqual('assets')394      })395396      it('GET /asset', async () => {397        const res = match('GET', '/asset')398        expect(res.length).toBe(0)399      })400401      it.each([402        ['/assets*', '/assets/app.js'],403        ['/assets/app.js', '/assets*'],404      ])('associates %s before %s', async (first, second) => {405        router.add('POST', first, first)406        router.add('POST', second, second)407        const res = match('POST', '/assets/app.js')408        expect(res.map(({ handler }) => handler)).toEqual([first, second])409      })410    })411412    describe('Trailing wildcard', () => {413      beforeEach(() => {414        router.add('GET', '/path/*', 'path')415      })416417      it('GET /path', async () => {418        const res = match('GET', '/path')419        expect(res.length).toBe(1)420        expect(res[0].handler).toEqual('path')421      })422423      it('GET /path/to/file', async () => {424        const res = match('GET', '/path/to/file')425        expect(res.length).toBe(1)426        expect(res[0].handler).toEqual('path')427      })428429      it('GET /pathfoo', async () => {430        const res = match('GET', '/pathfoo')431        expect(res.length).toBe(0)432      })433    })434435    describe('Trailing wildcard after a label', () => {436      beforeEach(() => {437        router.add('GET', '/:name/*', 'middleware')438        router.add('GET', '/:id', 'handler')439      })440441      it('GET /abc', async () => {442        const res = match('GET', '/abc')443        expect(res.length).toBe(2)444        expect(res[0]).toEqual({ handler: 'middleware', params: { name: 'abc' } })445        expect(res[1]).toEqual({ handler: 'handler', params: { id: 'abc' } })446      })447448      it('GET /abc/sub', async () => {449        const res = match('GET', '/abc/sub')450        expect(res).toEqual([{ handler: 'middleware', params: { name: 'abc' } }])451      })452    })453454    describe('Trailing wildcard after a pattern label', () => {455      beforeEach(() => {456        router.add('GET', '/:name{[a-z]+}/*', 'middleware')457        router.add('GET', '/:id{[a-z]+}', 'handler')458      })459460      it('GET /abc', async () => {461        const res = match('GET', '/abc')462        expect(res.length).toBe(2)463        expect(res[0]).toEqual({ handler: 'middleware', params: { name: 'abc' } })464        expect(res[1]).toEqual({ handler: 'handler', params: { id: 'abc' } })465      })466467      it('GET /reverse/abc in reverse registration order', async () => {468        router = newRouter()469        router.add('GET', '/reverse/:id{[a-z]+}', 'handler')470        router.add('GET', '/reverse/:name{[a-z]+}/*', 'middleware')471        const res = match('GET', '/reverse/abc')472        expect(res.map(({ handler }) => handler)).toEqual(['handler', 'middleware'])473      })474475      it('POST /all/abc with ALL middleware', async () => {476        router = newRouter()477        router.add('ALL', '/all/:name{[a-z]+}/*', 'middleware')478        router.add('POST', '/all/:id{[a-z]+}', 'handler')479        const res = match('POST', '/all/abc')480        expect(res.map(({ handler }) => handler)).toEqual(['middleware', 'handler'])481      })482483      it('GET /posts/2024/comments with nested braces', async () => {484        router = newRouter()485        router.add('GET', '/posts/:year{[0-9]{4}}/*', 'middleware')486        router.add('GET', '/posts/:yr{[0-9]{4}}/comments', 'handler')487        const res = match('GET', '/posts/2024/comments')488        expect(res).toEqual([489          { handler: 'middleware', params: { year: '2024' } },490          { handler: 'handler', params: { yr: '2024' } },491        ])492      })493494      it('GET /files/foo/detail with regexp meta characters', async () => {495        router = newRouter()496        router.add('GET', '/files/:kind{(?:foo|bar)}/*', 'middleware')497        router.add('GET', '/files/:type{(?:foo|bar)}/detail', 'handler')498        const res = match('GET', '/files/foo/detail')499        expect(res).toEqual([500          { handler: 'middleware', params: { kind: 'foo' } },501          { handler: 'handler', params: { type: 'foo' } },502        ])503      })504505      it('GET /user/123/profile with the default pattern', async () => {506        router = newRouter()507        router.add('GET', '/user/:userId{[^/]+}/*', 'middleware')508        router.add('GET', '/user/:id/profile', 'handler')509        const res = match('GET', '/user/123/profile')510        expect(res).toEqual([511          { handler: 'middleware', params: { userId: '123' } },512          { handler: 'handler', params: { id: '123' } },513        ])514      })515516      it('GET /user/123/profile with the default pattern in reverse registration order', async () => {517        router = newRouter()518        router.add('GET', '/user/:id/profile', 'handler')519        router.add('GET', '/user/:userId{[^/]+}/*', 'middleware')520        const res = match('GET', '/user/123/profile')521        expect(res).toEqual([522          { handler: 'handler', params: { id: '123' } },523          { handler: 'middleware', params: { userId: '123' } },524        ])525      })526    })527528    describe('Trailing wildcard after a middle wildcard', () => {529      beforeEach(() => {530        router.add('GET', '/a/*/b/*', 'b')531      })532533      it('GET /a/x/b', async () => {534        const res = match('GET', '/a/x/b')535        expect(res.length).toBe(1)536        expect(res[0].handler).toEqual('b')537      })538539      it('GET /a/x/b/c', async () => {540        const res = match('GET', '/a/x/b/c')541        expect(res.length).toBe(1)542        expect(res[0].handler).toEqual('b')543      })544545      it('GET /a/x/bz', async () => {546        const res = match('GET', '/a/x/bz')547        expect(res.length).toBe(0)548      })549    })550551    describe('Optional route', () => {552      beforeEach(() => {553        router.add('GET', '/api/animals/:type?', 'animals')554        router.add('GET', '/v1/:version?/:platform?', 'result')555      })556557      it('GET /api/animals/dog', async () => {558        const res = match('GET', '/api/animals/dog')559        expect(res.length).toBe(1)560        expect(res[0].handler).toEqual('animals')561        expect(res[0].params['type']).toBe('dog')562      })563      it('GET /api/animals', async () => {564        const res = match('GET', '/api/animals')565        expect(res.length).toBe(1)566        expect(res[0].handler).toEqual('animals')567        expect(res[0].params['type']).toBeUndefined()568      })569      it('GET /v1/123/abc', () => {570        const res = match('GET', '/v1/123/abc')571        expect(res.length).toBe(1)572        expect(res[0].handler).toEqual('result')573        expect(res[0].params['version']).toBe('123')574        expect(res[0].params['platform']).toBe('abc')575      })576      it('GET /v1/123', () => {577        const res = match('GET', '/v1/123')578        expect(res.length).toBe(1)579        expect(res[0].handler).toEqual('result')580        expect(res[0].params['version']).toBe('123')581        expect(res[0].params['platform']).toBeUndefined()582      })583      it('GET /v1', () => {584        const res = match('GET', '/v1')585        expect(res.length).toBe(1)586        expect(res[0].handler).toEqual('result')587        expect(res[0].params['version']).toBeUndefined()588        expect(res[0].params['platform']).toBeUndefined()589      })590    })591592    describe('All', () => {593      beforeEach(() => {594        router.add('GET', '/hello', 'get hello')595        router.add('ALL', '/all', 'get all')596      })597598      it('GET, all hello', async () => {599        const res = match('GET', '/all')600        expect(res.length).toBe(1)601      })602    })603604    describe('long prefix, then star', () => {605      describe('GET only', () => {606        beforeEach(() => {607          router.add('GET', '/long/prefix/*', 'long-prefix')608          router.add('GET', '/long/*', 'long')609          router.add('GET', '*', 'star1')610          router.add('GET', '*', 'star2')611        })612613        it('GET /', () => {614          const res = match('GET', '/')615          expect(res.length).toBe(2)616          expect(res[0].handler).toEqual('star1')617          expect(res[1].handler).toEqual('star2')618        })619620        it('GET /long/prefix', () => {621          const res = match('GET', '/long/prefix')622          expect(res.length).toBe(4)623          expect(res[0].handler).toEqual('long-prefix')624          expect(res[1].handler).toEqual('long')625          expect(res[2].handler).toEqual('star1')626          expect(res[3].handler).toEqual('star2')627        })628629        it('GET /long/prefix/test', () => {630          const res = match('GET', '/long/prefix/test')631          expect(res.length).toBe(4)632          expect(res[0].handler).toEqual('long-prefix')633          expect(res[1].handler).toEqual('long')634          expect(res[2].handler).toEqual('star1')635          expect(res[3].handler).toEqual('star2')636        })637      })638639      describe('ALL and GET', () => {640        beforeEach(() => {641          router.add('ALL', '/long/prefix/*', 'long-prefix')642          router.add('ALL', '/long/*', 'long')643          router.add('GET', '*', 'star1')644          router.add('GET', '*', 'star2')645        })646647        it('GET /', () => {648          const res = match('GET', '/')649          expect(res.length).toBe(2)650          expect(res[0].handler).toEqual('star1')651          expect(res[1].handler).toEqual('star2')652        })653654        it('GET /long/prefix', () => {655          const res = match('GET', '/long/prefix')656          expect(res.length).toBe(4)657          expect(res[0].handler).toEqual('long-prefix')658          expect(res[1].handler).toEqual('long')659          expect(res[2].handler).toEqual('star1')660          expect(res[3].handler).toEqual('star2')661        })662663        it('GET /long/prefix/test', () => {664          const res = match('GET', '/long/prefix/test')665          expect(res.length).toBe(4)666          expect(res[0].handler).toEqual('long-prefix')667          expect(res[1].handler).toEqual('long')668          expect(res[2].handler).toEqual('star1')669          expect(res[3].handler).toEqual('star2')670        })671      })672    })673674    describe('Including slashes', () => {675      beforeEach(() => {676        router.add('GET', '/js/:filename{[a-z0-9/]+.js}', 'any file')677      })678679      it('GET /js/main.js', () => {680        router.add('GET', '/js/main.js', 'main.js')681682        const res = match('GET', '/js/main.js')683        expect(res.length).toBe(2)684        expect(res[0].handler).toEqual('any file')685        expect(res[0].params['filename']).toEqual('main.js')686        expect(res[1].handler).toEqual('main.js')687        expect(res[1].params['filename']).toEqual(undefined)688      })689690      it('GET /js/chunk/123.js', () => {691        const res = match('GET', '/js/chunk/123.js')692        expect(res.length).toBe(1)693        expect(res[0].handler).toEqual('any file')694        expect(res[0].params['filename']).toEqual('chunk/123.js')695      })696697      it('GET /js/chunk/nest/123.js', () => {698        const res = match('GET', '/js/chunk/nest/123.js')699        expect(res.length).toBe(1)700        expect(res[0].handler).toEqual('any file')701        expect(res[0].params['filename']).toEqual('chunk/nest/123.js')702      })703    })704705    describe('Capture simple multiple directories', () => {706      beforeEach(() => {707        router.add('GET', '/:dirs{.+}/file.html', 'file.html')708      })709710      it('GET /foo/bar/file.html', () => {711        const res = match('GET', '/foo/bar/file.html')712        expect(res.length).toBe(1)713        expect(res[0].handler).toEqual('file.html')714        expect(res[0].params['dirs']).toEqual('foo/bar')715      })716717      it('GET /foo/bar/baz/file.html', () => {718        const res = match('GET', '/foo/bar/baz/file.html')719        expect(res.length).toBe(1)720        expect(res[0].handler).toEqual('file.html')721        expect(res[0].params['dirs']).toEqual('foo/bar/baz')722      })723724      it('GET /a/b/c/d/file.html', () => {725        const res = match('GET', '/a/b/c/d/file.html')726        expect(res.length).toBe(1)727        expect(res[0].handler).toEqual('file.html')728        expect(res[0].params['dirs']).toEqual('a/b/c/d')729      })730    })731732    describe('Capture regex pattern has trailing wildcard', () => {733      beforeEach(() => {734        router.add('GET', '/:dir{[a-z]+}/*/file.html', 'file.html')735      })736737      it('GET /foo/bar/file.html', () => {738        const res = match('GET', '/foo/bar/file.html')739        expect(res.length).toBe(1)740        expect(res[0].handler).toEqual('file.html')741        expect(res[0].params['dir']).toEqual('foo')742      })743    })744745    describe('Capture regex param with trailing wildcard on empty remainder', () => {746      beforeEach(() => {747        router.add('GET', '/:id{[0-9]+}/*', 'regexp')748      })749750      it('GET /123', () => {751        const res = match('GET', '/123')752        expect(res.length).toBe(1)753        expect(res[0].handler).toEqual('regexp')754        expect(res[0].params['id']).toBe('123')755      })756    })757758    describe('Capture regex param with trailing wildcard and sibling route', () => {759      beforeEach(() => {760        router.add('GET', '/regex-abc/:id{[0-9]+}/*', 'middleware')761        router.add('GET', '/regex-abc/:id{[0-9]+}/def', 'regexp')762      })763764      it('GET /regex-abc/123/ghi', () => {765        const res = match('GET', '/regex-abc/123/ghi')766        expect(res.length).toBe(1)767        expect(res[0].handler).toEqual('middleware')768        expect(res[0].params['id']).toBe('123')769      })770    })771772    describe('Capture complex multiple directories', () => {773      beforeEach(() => {774        router.add('GET', '/:first{.+}/middle-a/:reference?', '1')775        router.add('GET', '/:first{.+}/middle-b/end-c/:uuid', '2')776        router.add('GET', '/:first{.+}/middle-b/:digest', '3')777      })778779      it('GET /part1/middle-b/latest', () => {780        const res = match('GET', '/part1/middle-b/latest')781        expect(res.length).toBe(1)782        expect(res[0].handler).toEqual('3')783        expect(res[0].params['first']).toEqual('part1')784        expect(res[0].params['digest']).toEqual('latest')785      })786787      it('GET /part1/middle-b/end-c/latest', () => {788        const res = match('GET', '/part1/middle-b/end-c/latest')789        expect(res.length).toBe(1)790        expect(res[0].handler).toEqual('2')791        expect(res[0].params['first']).toEqual('part1')792        expect(res[0].params['uuid']).toEqual('latest')793      })794    })795796    describe('Capture multiple directories and optional', () => {797      beforeEach(() => {798        router.add('GET', '/:prefix{.+}/contents/:id?', 'contents')799      })800801      it('GET /foo/bar/contents', () => {802        const res = match('GET', '/foo/bar/contents')803        expect(res.length).toBe(1)804        expect(res[0].handler).toEqual('contents')805        expect(res[0].params['prefix']).toEqual('foo/bar')806        expect(res[0].params['id']).toEqual(undefined)807      })808809      it('GET /foo/bar/contents/123', () => {810        const res = match('GET', '/foo/bar/contents/123')811        expect(res.length).toBe(1)812        expect(res[0].handler).toEqual('contents')813        expect(res[0].params['prefix']).toEqual('foo/bar')814        expect(res[0].params['id']).toEqual('123')815      })816    })817818    describe('non ascii characters', () => {819      beforeEach(() => {820        router.add('ALL', '/$/*', 'middleware $')821        router.add('GET', '/$/:name', 'get $ name')822        router.add('ALL', '/()/*', 'middleware ()')823        router.add('GET', '/()/:name', 'get () name')824      })825826      it('GET /$/hono', () => {827        const res = match('GET', '/$/hono')828        expect(res.length).toBe(2)829        expect(res[0].handler).toEqual('middleware $')830        expect(res[0].params).toEqual({})831        expect(res[1].handler).toEqual('get $ name')832        expect(res[1].params['name']).toEqual('hono')833      })834835      it('GET /()/hono', () => {836        const res = match('GET', '/()/hono')837        expect(res.length).toBe(2)838        expect(res[0].handler).toEqual('middleware ()')839        expect(res[0].params).toEqual({})840        expect(res[1].handler).toEqual('get () name')841        expect(res[1].params['name']).toEqual('hono')842      })843    })844845    describe('REST API', () => {846      beforeEach(() => {847        router.add('GET', '/users/:username{[a-z]+}', 'profile')848        router.add('GET', '/users/:username{[a-z]+}/posts', 'posts')849      })850851      it('GET /users/hono', () => {852        const res = match('GET', '/users/hono')853        expect(res.length).toBe(1)854        expect(res[0].handler).toEqual('profile')855      })856857      it('GET /users/hono/posts', () => {858        const res = match('GET', '/users/hono/posts')859        expect(res.length).toBe(1)860        expect(res[0].handler).toEqual('posts')861      })862    })863864    describe('Trailing slash', () => {865      beforeEach(() => {866        router.add('GET', '/book', 'GET /book')867        router.add('GET', '/book/:id', 'GET /book/:id')868      })869870      it('GET /book', () => {871        const res = match('GET', '/book')872        expect(res.length).toBe(1)873        expect(res[0].handler).toEqual('GET /book')874      })875      it('GET /book/', () => {876        const res = match('GET', '/book/')877        expect(res.length).toBe(0)878      })879    })880881    describe('Empty path segment', () => {882      beforeEach(() => {883        router.add('GET', '/u/:id/posts', 'posts')884      })885886      it('GET /u/42/posts', () => {887        const res = match('GET', '/u/42/posts')888        expect(res.length).toBe(1)889        expect(res[0].params['id']).toBe('42')890      })891892      it('GET /u//posts does not match with an empty param', () => {893        const res = match('GET', '/u//posts')894        expect(res.length).toBe(0)895      })896    })897898    describe('Same path', () => {899      beforeEach(() => {900        router.add('GET', '/hey', 'Middleware A')901        router.add('GET', '/hey', 'Middleware B')902      })903904      it('GET /hey', () => {905        const res = match('GET', '/hey')906        expect(res.length).toBe(2)907        expect(res[0].handler).toEqual('Middleware A')908        expect(res[1].handler).toEqual('Middleware B')909      })910    })911912    describe('Routing with a hostname', () => {913      beforeEach(() => {914        router.add('GET', 'www1.example.com/hello', 'www1')915        router.add('GET', 'www2.example.com/hello', 'www2')916      })917      it('GET www1.example.com/hello', () => {918        const res = match('GET', 'www1.example.com/hello')919        expect(res.length).toBe(1)920        expect(res[0].handler).toEqual('www1')921      })922      it('GET www2.example.com/hello', () => {923        const res = match('GET', 'www2.example.com/hello')924        expect(res.length).toBe(1)925        expect(res[0].handler).toEqual('www2')926      })927      it('GET /hello', () => {928        const res = match('GET', '/hello')929        expect(res.length).toBe(0)930      })931    })932933    describe('static routes of ALL and GET', () => {934      beforeEach(() => {935        router.add('ALL', '/foo', 'foo')936        router.add('GET', '/bar', 'bar')937      })938939      it('get /foo', () => {940        const res = match('GET', '/foo')941        expect(res[0].handler).toEqual('foo')942      })943    })944945    describe('ALL and Star', () => {946      beforeEach(() => {947        router.add('ALL', '/x', '/x')948        router.add('GET', '*', 'star')949      })950951      it('Should return /x and star', async () => {952        const res = match('GET', '/x')953        expect(res.length).toBe(2)954        expect(res[0].handler).toEqual('/x')955        expect(res[1].handler).toEqual('star')956      })957    })958959    describe('GET star, ALL static, GET star...', () => {960      beforeEach(() => {961        router.add('GET', '*', 'star1')962        router.add('ALL', '/x', '/x')963        router.add('GET', '*', 'star2')964        router.add('GET', '*', 'star3')965      })966967      it('Should return /x and star', async () => {968        const res = match('GET', '/x')969        expect(res.length).toBe(4)970        expect(res[0].handler).toEqual('star1')971        expect(res[1].handler).toEqual('/x')972        expect(res[2].handler).toEqual('star2')973        expect(res[3].handler).toEqual('star3')974      })975    })976977    // https://github.com/honojs/hono/issues/699978    describe('GET star, GET static, ALL star...', () => {979      beforeEach(() => {980        router.add('GET', '/y/*', 'star1')981        router.add('GET', '/y/a', 'a')982        router.add('ALL', '/y/b/*', 'star2')983        router.add('GET', '/y/b/bar', 'bar')984      })985986      it('Should return star1, star2, and bar', async () => {987        const res = match('GET', '/y/b/bar')988        expect(res.length).toBe(3)989        expect(res[0].handler).toEqual('star1')990        expect(res[1].handler).toEqual('star2')991        expect(res[2].handler).toEqual('bar')992      })993    })994995    describe('ALL star, ALL star, GET static, ALL star...', () => {996      beforeEach(() => {997        router.add('ALL', '*', 'wildcard')998        router.add('ALL', '/a/*', 'star1')999        router.add('GET', '/a/foo', 'foo')1000        router.add('ALL', '/b/*', 'star2')1001        router.add('GET', '/b/bar', 'bar')1002      })10031004      it('Should return wildcard, star2 and bar', async () => {1005        const res = match('GET', '/b/bar')1006        expect(res.length).toBe(3)1007        expect(res[0].handler).toEqual('wildcard')1008        expect(res[1].handler).toEqual('star2')1009        expect(res[2].handler).toEqual('bar')1010      })1011    })10121013    describe('Capture Group', () => {1014      describe('Simple capturing group', () => {1015        beforeEach(() => {1016          router.add('get', '/foo/:capture{(bar|baz)}', 'ok')1017        })10181019        it('GET /foo/bar', () => {1020          const res = match('get', '/foo/bar')1021          expect(res.length).toBe(1)1022          expect(res[0].handler).toBe('ok')1023          expect(res[0].params['capture']).toBe('bar')1024        })10251026        it('GET /foo/baz', () => {1027          const res = match('get', '/foo/baz')1028          expect(res.length).toBe(1)1029          expect(res[0].handler).toBe('ok')1030          expect(res[0].params['capture']).toBe('baz')1031        })10321033        it('GET /foo/qux', () => {1034          const res = match('get', '/foo/qux')1035          expect(res.length).toBe(0)1036        })1037      })10381039      describe('Non-capturing group', () => {1040        beforeEach(() => {1041          router.add('get', '/foo/:capture{(?:bar|baz)}', 'ok')1042        })10431044        it('GET /foo/bar', () => {1045          const res = match('get', '/foo/bar')1046          expect(res.length).toBe(1)1047          expect(res[0].handler).toBe('ok')1048          expect(res[0].params['capture']).toBe('bar')1049        })10501051        it('GET /foo/baz', () => {1052          const res = match('get', '/foo/baz')1053          expect(res.length).toBe(1)1054          expect(res[0].handler).toBe('ok')1055          expect(res[0].params['capture']).toBe('baz')1056        })10571058        it('GET /foo/qux', () => {1059          const res = match('get', '/foo/qux')1060          expect(res.length).toBe(0)1061        })1062      })10631064      describe('Non-capturing group with prefix', () => {1065        beforeEach(() => {1066          router.add('get', '/foo/:capture{ba(?:r|z)}', 'ok')1067        })10681069        it('GET /foo/bar', () => {1070          const res = match('get', '/foo/bar')1071          expect(res.length).toBe(1)1072          expect(res[0].handler).toBe('ok')1073          expect(res[0].params['capture']).toBe('bar')1074        })10751076        it('GET /foo/baz', () => {1077          const res = match('get', '/foo/baz')1078          expect(res.length).toBe(1)1079          expect(res[0].handler).toBe('ok')1080          expect(res[0].params['capture']).toBe('baz')1081        })10821083        it('GET /foo/qux', () => {1084          const res = match('get', '/foo/qux')1085          expect(res.length).toBe(0)1086        })1087      })10881089      describe('Complex capturing group', () => {1090        it('GET request', () => {1091          router.add('get', '/foo/:capture{ba(r|z)}', 'ok')10921093          const res = match('get', '/foo/bar')1094          expect(res.length).toBe(1)1095          expect(res[0].handler).toBe('ok')1096        })1097      })1098    })10991100    describe('Unknown method', () => {1101      beforeEach(() => {1102        router.add('GET', '/', 'index')1103        router.add('ALL', '/all', 'all')1104      })11051106      it('UNKNOWN_METHOD /', () => {1107        const res = match('UNKNOWN_METHOD', '/')1108        expect(res.length).toBe(0)1109      })11101111      it('UNKNOWN_METHOD /all', () => {1112        const res = match('UNKNOWN_METHOD', '/all')1113        expect(res.length).toBe(1)1114        expect(res[0].handler).toBe('all')1115      })1116    })1117  })1118}