From 9b2fcafbe691b3b92637e4ff61186222f78d6759 Mon Sep 17 00:00:00 2001 From: Gabriel Massadas Date: Tue, 5 Sep 2023 16:16:32 +0100 Subject: [PATCH] Fix special characters in request params --- src/Router.spec.ts | 9 +++++++++ src/Router.ts | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Router.spec.ts b/src/Router.spec.ts index 1e504680..14f314dd 100644 --- a/src/Router.spec.ts +++ b/src/Router.spec.ts @@ -409,6 +409,15 @@ describe('Router', () => { router.get('/foo', vi.fn()).get('/foo', vi.fn()) }).not.toThrow() }) + + it('allows special characters in params', async () => { + const specialRouter = Router() + specialRouter.get('/foo/:id', (request) => new Response(`id: ${request.params.id}`)) + + const response = await specialRouter.handle(buildRequest({ path: '/foo/user%3A1234' })) + + expect(await response.text()).toBe('id: user:1234') + }) }) describe(`.handle({ method = 'GET', url }, ...args)`, () => { diff --git a/src/Router.ts b/src/Router.ts index 3daa6d39..74b69484 100644 --- a/src/Router.ts +++ b/src/Router.ts @@ -94,7 +94,7 @@ export const Router = < }), routes, async handle (request: RequestLike, ...args) { - let response, match, url = new URL(request.url), query: any = request.query = { __proto__: null } + let response, match, url = new URL(decodeURIComponent(request.url)), query: any = request.query = { __proto__: null } for (let [k, v] of url.searchParams) { query[k] = query[k] === undefined ? v : [query[k], v].flat() }