0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-30 01:56:18 +01:00

fix(app): app.mount() supports / (#1119)

* fix(app): `app.mount()` supports `/`

* chore: denoify
This commit is contained in:
Yusuke Wada 2023-05-20 15:29:58 +09:00 committed by GitHub
parent 0ffd795ec6
commit 127fa30dce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 4 deletions

View File

@ -205,7 +205,9 @@ class Hono<E extends Env = Env, S = {}, BasePath extends string = '/'> extends d
applicationHandler: (request: Request, ...args: any) => Response | Promise<Response>,
optionHandler?: (c: Context) => unknown
): Hono<E, S, BasePath> {
const pathPrefixLength = mergePath(this._basePath, path).length
const mergedPath = mergePath(this._basePath, path)
const pathPrefixLength = mergedPath === '/' ? 0 : mergedPath.length
const handler: MiddlewareHandler = async (c, next) => {
let executionContext: ExecutionContext | undefined = undefined
try {

View File

@ -205,7 +205,9 @@ class Hono<E extends Env = Env, S = {}, BasePath extends string = '/'> extends d
applicationHandler: (request: Request, ...args: any) => Response | Promise<Response>,
optionHandler?: (c: Context) => unknown
): Hono<E, S, BasePath> {
const pathPrefixLength = mergePath(this._basePath, path).length
const mergedPath = mergePath(this._basePath, path)
const pathPrefixLength = mergedPath === '/' ? 0 : mergedPath.length
const handler: MiddlewareHandler = async (c, next) => {
let executionContext: ExecutionContext | undefined = undefined
try {

View File

@ -1916,7 +1916,6 @@ describe('app.mount()', () => {
describe('Basic', () => {
const anotherApp = (req: Request, params: unknown) => {
const path = getPath(req)
console.log(`path in anotherApp: ${path}`)
if (path === '/') {
return new Response('AnotherApp')
}
@ -1939,7 +1938,6 @@ describe('app.mount()', () => {
}
)
}
console.log(`before 404 ${req.url}`)
return new Response('Not Found from AnotherApp', {
status: 404,
})
@ -2065,6 +2063,42 @@ describe('app.mount()', () => {
})
})
})
describe('Mount on `/`', () => {
const anotherApp = (req: Request, params: unknown) => {
const path = getPath(req)
if (path === '/') {
return new Response('AnotherApp')
}
if (path === '/hello') {
return new Response('Hello from AnotherApp')
}
if (path === '/good/night') {
return new Response('Good Night from AnotherApp')
}
return new Response('Not Found from AnotherApp', {
status: 404,
})
}
const app = new Hono()
app.mount('/', anotherApp)
it('Should return responses from AnotherApp - mount on `/`', async () => {
let res = await app.request('/')
expect(res.status).toBe(200)
expect(await res.text()).toBe('AnotherApp')
res = await app.request('/hello')
expect(res.status).toBe(200)
expect(await res.text()).toBe('Hello from AnotherApp')
res = await app.request('/good/night')
expect(res.status).toBe(200)
expect(await res.text()).toBe('Good Night from AnotherApp')
res = await app.request('/not-found')
expect(res.status).toBe(404)
expect(await res.text()).toBe('Not Found from AnotherApp')
})
})
})
describe('Router Name', () => {