Skip to content
Myr

fix(trie-router): match empty wildcard remainder after regexp param (#5102)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(trie-router): match empty wildcard remainder after regexp param * test(router): cover empty wildcard remainder after regexp param * test(router): add test for regexp wildcard * test: fix test name for regex route (may be a typo)

authored by Taku Amano · committed by GitHub · parent ab590e4 · Browse files · 4 files+49 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+49 added−2 removed

  • tests:+38 added−2 removed
  • other:+11 added−0 removed

4 files+49 added−2 removed

modified:src/router/linear-router/router.test.ts+2 added
modified:src/router/trie-router/node.test.ts+9 added-2 removed
408 unmodified lines
409 describe('Regexp', () => {410 const node = new Node()411 node.insert('get', '/regex-abc/:id{[0-9]+}/*', 'middleware a')412 node.insert('get', '/regex-abc/:id{[0-9]+}/def', 'regexp')413 it('/regexp-abc/123/def', () => {413 it('/regex-abc/123/def', () => {414 const [res] = node.search('get', '/regex-abc/123/def')415 expect(res.length).toBe(2)416 expect(res[0][0]).toEqual('middleware a')417 expect(res[0][1]['id']).toBe('123')418 expect(res[1][0]).toEqual('regexp')419 expect(res[1][1]['id']).toBe('123')420 })421 it('/regexp-abc/123', () => {421 it('/regex-abc/123/ghi', () => {422 const [res] = node.search('get', '/regex-abc/123/ghi')423 expect(res.length).toBe(1)424 expect(res[0][0]).toEqual('middleware a')425 expect(res[0][1]['id']).toBe('123')426 })427 it('/regex-abc/123', () => {428 const [res] = node.search('get', '/regex-abc/123')429 expect(res.length).toBe(1)430 expect(res[0][0]).toEqual('middleware a')431 expect(res[0][1]['id']).toBe('123')432 })433 })434 describe('Trailing slash', () => {435 const node = new Node()436 node.insert('get', '/book', 'GET /book')
387 unmodified lines
modified:src/router/trie-router/node.ts+11 added
185 unmodified lines
186 if (m) {187 params[name] = m[0]188 this.#pushHandlerSets(handlerSets, child, method, node.#params, params)189190 // '/:id{[0-9]+}/*' => match '/123'191 if (m[0].length === restPathString.length && child.#children['*']) {192 this.#pushHandlerSets(193 handlerSets,194 child.#children['*'],195 method,196 node.#params,197 params198 )199 }200201 if (hasChildren(child.#children)) {202 child.#params = params203 const componentCount = m[0].match(/\//)?.length ?? 0204 const targetCurNodes = (curNodesQueue[componentCount] ||= [])
41 unmodified lines
modified:src/router/common.case.test.ts+27 added
548 unmodified lines
549 expect(res[0].params['dir']).toEqual('foo')550 })551 })552553 describe('Capture regex param with trailing wildcard on empty remainder', () => {554 beforeEach(() => {555 router.add('GET', '/:id{[0-9]+}/*', 'regexp')556 })557558 it('GET /123', () => {559 const res = match('GET', '/123')560 expect(res.length).toBe(1)561 expect(res[0].handler).toEqual('regexp')562 expect(res[0].params['id']).toBe('123')563 })564 })565566 describe('Capture regex param with trailing wildcard and sibling route', () => {567 beforeEach(() => {568 router.add('GET', '/regex-abc/:id{[0-9]+}/*', 'middleware')569 router.add('GET', '/regex-abc/:id{[0-9]+}/def', 'regexp')570 })571572 it('GET /regex-abc/123/ghi', () => {573 const res = match('GET', '/regex-abc/123/ghi')574 expect(res.length).toBe(1)575 expect(res[0].handler).toEqual('middleware')576 expect(res[0].params['id']).toBe('123')577 })578 })579580 describe('Capture complex multiple directories', () => {581 beforeEach(() => {582 router.add('GET', '/:first{.+}/middle-a/:reference?', '1')583 router.add('GET', '/:first{.+}/middle-b/end-c/:uuid', '2')
326 unmodified lines