Skip to content

Commit 6cbb025

Browse files
authored
Merge commit from fork
1 parent c831020 commit 6cbb025

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

‎src/hono-base.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ class Hono<
364364
const pathPrefixLength = mergedPath === '/' ? 0 : mergedPath.length
365365
return (request) => {
366366
const url = new URL(request.url)
367-
url.pathname = url.pathname.slice(pathPrefixLength) || '/'
367+
url.pathname = this.getPath(request).slice(pathPrefixLength) || '/'
368368
return new Request(url, request)
369369
}
370370
})()

‎src/hono.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2685,6 +2685,40 @@ describe('app.mount()', () => {
26852685
})
26862686
})
26872687

2688+
describe('With encoded paths', () => {
2689+
const anotherApp = (req: Request) => new Response(getPath(req))
2690+
2691+
it('Should strip a decoded non-ASCII mount prefix', async () => {
2692+
const app = new Hono()
2693+
app.mount('/api/é', anotherApp)
2694+
2695+
const res = await app.request('/api/%C3%A9/hello')
2696+
2697+
expect(res.status).toBe(200)
2698+
expect(await res.text()).toBe('/hello')
2699+
})
2700+
2701+
it('Should preserve an encoded slash as a literal path segment after stripping the prefix', async () => {
2702+
const app = new Hono()
2703+
app.mount('/api/v1', anotherApp)
2704+
2705+
const res = await app.request('/api/v1/admin%2Fsecret')
2706+
2707+
expect(res.status).toBe(200)
2708+
expect(await res.text()).toBe('/admin%2Fsecret')
2709+
})
2710+
2711+
it('Should preserve encoded percent characters after stripping the prefix', async () => {
2712+
const app = new Hono()
2713+
app.mount('/api', anotherApp)
2714+
2715+
const res = await app.request('/api/foo%252Fbar')
2716+
2717+
expect(res.status).toBe(200)
2718+
expect(await res.text()).toBe('/foo%252Fbar')
2719+
})
2720+
})
2721+
26882722
describe('With fetch', () => {
26892723
const anotherApp = async (req: Request, env: {}, executionContext: ExecutionContext) => {
26902724
const path = getPath(req)

0 commit comments

Comments
 (0)