Skip to content
Myr

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

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

* fix(linear-router): don't match an empty path segment as a param `/u/:id/posts` matched `/u//posts` and set `id` to an empty string. RegExpRouter, TrieRouter and PatternRouter all return 404 for that path. The value extraction already rejected an empty segment at the end of the path, but not one in the middle: `path.indexOf('/', pos + 1)` returns `pos + 1` for `//`, so the slice was empty and the match continued. Rejects the mid-path case the same way, and adds the case to the shared suite where all four routers pass it. * refactor: reject an empty param value in one place ---------

authored by Harshit Prakash · committed by GitHub · parent f5a5346 · Browse files · 2 files+21 added−3 removed
This commit

State

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

Trailers

  • Co-authored-by: Yusuke Wada <yusuke@kamawada.com>

Diff stats

2 files+21 added−3 removed

  • tests:+17 added−0 removed
  • other:+4 added−3 removed

2 files+21 added−3 removed

modified:src/router/linear-router/router.ts+4 added-3 removed
modified:src/router/common.case.test.ts+17 added
876 unmodified lines
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')
217 unmodified lines