fix(router): `PatternRouter` supports a hostname, added tests (#1114)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* fix(router): `PatternRouter` supports a hostname, added tests * denoify
authored by Yusuke Wada · committed by GitHub · parent 4fde03e · Browse files · 6 files·+82 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
6 files+82 added−2 removed
- tests:+80 added−0 removed
- other:+2 added−2 removed
Provenance
- Commit
84f8bc6- Tree
240acf7- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
4fde03e
6 files changed+82 added−2 removed
modified:deno_dist/router/pattern-router/router.ts+1 added-1 removed
12 unmodified lines1314151617171819202149 unmodified lines12 unmodified lines if (endsWithWildcard) { path = path.slice(0, -2) }
const parts = path.match(/\/(:\w+(?:{[^}]+})?)|\/[^\/\?]+|(\?)/g) || [] const parts = path.match(/\/?(:\w+(?:{[^}]+})?)|\/?[^\/\?]+|(\?)/g) || [] if (parts[parts.length - 1] === '?') { this.add(method, parts.slice(0, parts.length - 2).join(''), handler) parts.pop() }49 unmodified linesmodified:src/router/linear-router/router.test.ts+20 added
273 unmodified lines274 router.match('GET', '/posts/get')275 }).toThrowError(/Duplicate param name/)276 })277 })278+279+describe('Routing with a hostname', () => {280+ const router = new LinearRouter<string>()281+ router.add('get', 'www1.example.com/hello', 'www1')282+ router.add('get', 'www2.example.com/hello', 'www2')283+ it('GET www1.example.com/hello', () => {284+ const res = router.match('get', 'www1.example.com/hello')285+ expect(res).not.toBeNull()286+ expect(res?.handlers).toEqual(['www1'])287+ })288+ it('GET www2.example.com/hello', () => {289+ const res = router.match('get', 'www2.example.com/hello')290+ expect(res).not.toBeNull()291+ expect(res?.handlers).toEqual(['www2'])292+ })293+ it('GET /hello', () => {294+ const res = router.match('get', '/hello')295+ expect(res).toBeNull()296+ })297+})modified:src/router/pattern-router/router.test.ts+20 added
447 unmodified lines448 expect(res?.handlers).toEqual(['slug', 'no-slug-b'])449 expect(res?.params['slug']).toBe('b')450 })451 })452+453+describe('Routing with a hostname', () => {454+ const router = new PatternRouter<string>()455+ router.add('get', 'www1.example.com/hello', 'www1')456+ router.add('get', 'www2.example.com/hello', 'www2')457+ it('GET www1.example.com/hello', () => {458+ const res = router.match('get', 'www1.example.com/hello')459+ expect(res).not.toBeNull()460+ expect(res?.handlers).toEqual(['www1'])461+ })462+ it('GET www2.example.com/hello', () => {463+ const res = router.match('get', 'www2.example.com/hello')464+ expect(res).not.toBeNull()465+ expect(res?.handlers).toEqual(['www2'])466+ })467+ it('GET /hello', () => {468+ const res = router.match('get', '/hello')469+ expect(res).toBeNull()470+ })471+})modified:src/router/pattern-router/router.ts+1 added-1 removed
12 unmodified lines1314151617171819202149 unmodified lines12 unmodified lines if (endsWithWildcard) { path = path.slice(0, -2) }
const parts = path.match(/\/(:\w+(?:{[^}]+})?)|\/[^\/\?]+|(\?)/g) || [] const parts = path.match(/\/?(:\w+(?:{[^}]+})?)|\/?[^\/\?]+|(\?)/g) || [] if (parts[parts.length - 1] === '?') { this.add(method, parts.slice(0, parts.length - 2).join(''), handler) parts.pop() }49 unmodified linesmodified:src/router/reg-exp-router/router.test.ts+20 added
469 unmodified lines470 expect(res).not.toBeNull()471 expect(res?.handlers).toEqual(['wildcard', 'star2', 'bar'])472 })473 })474+475+describe('Routing with a hostname', () => {476+ const router = new RegExpRouter<string>()477+ router.add('get', 'www1.example.com/hello', 'www1')478+ router.add('get', 'www2.example.com/hello', 'www2')479+ it('GET www1.example.com/hello', () => {480+ const res = router.match('get', 'www1.example.com/hello')481+ expect(res).not.toBeNull()482+ expect(res?.handlers).toEqual(['www1'])483+ })484+ it('GET www2.example.com/hello', () => {485+ const res = router.match('get', 'www2.example.com/hello')486+ expect(res).not.toBeNull()487+ expect(res?.handlers).toEqual(['www2'])488+ })489+ it('GET /hello', () => {490+ const res = router.match('get', '/hello')491+ expect(res).toBeNull()492+ })493+})modified:src/router/trie-router/router.test.ts+20 added
169 unmodified lines170 expect(res?.handlers).toEqual(['slug', 'no-slug-b'])171 expect(res?.params['slug']).toBe('b')172 })173 })174+175+describe('Routing with a hostname', () => {176+ const router = new TrieRouter<string>()177+ router.add('get', 'www1.example.com/hello', 'www1')178+ router.add('get', 'www2.example.com/hello', 'www2')179+ it('GET www1.example.com/hello', () => {180+ const res = router.match('get', 'www1.example.com/hello')181+ expect(res).not.toBeNull()182+ expect(res?.handlers).toEqual(['www1'])183+ })184+ it('GET www2.example.com/hello', () => {185+ const res = router.match('get', 'www2.example.com/hello')186+ expect(res).not.toBeNull()187+ expect(res?.handlers).toEqual(['www2'])188+ })189+ it('GET /hello', () => {190+ const res = router.match('get', '/hello')191+ expect(res).toBeNull()192+ })193+})