fix(app): `app.mount()` supports `/` (#1119)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* fix(app): `app.mount()` supports `/` * chore: denoify
authored by Yusuke Wada · committed by GitHub · parent 0ffd795 · Browse files · 3 files·+42 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+42 added−4 removed
- tests:+36 added−2 removed
- other:+6 added−2 removed
Provenance
- Commit
127fa30- Tree
0d036bf- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
0ffd795
3 files changed+42 added−4 removed
modified:deno_dist/hono-base.ts+3 added-1 removed
203 unmodified lines204 // eslint-disable-next-line @typescript-eslint/no-explicit-any205 applicationHandler: (request: Request, ...args: any) => Response | Promise<Response>,206 optionHandler?: (c: Context) => unknown207 ): Hono<E, S, BasePath> {208- const pathPrefixLength = mergePath(this._basePath, path).length208+ const mergedPath = mergePath(this._basePath, path)209+ const pathPrefixLength = mergedPath === '/' ? 0 : mergedPath.length210+211 const handler: MiddlewareHandler = async (c, next) => {212 let executionContext: ExecutionContext | undefined = undefined213 try {214 executionContext = c.executionCtx150 unmodified linesmodified:src/hono-base.ts+3 added-1 removed
203 unmodified lines204 // eslint-disable-next-line @typescript-eslint/no-explicit-any205 applicationHandler: (request: Request, ...args: any) => Response | Promise<Response>,206 optionHandler?: (c: Context) => unknown207 ): Hono<E, S, BasePath> {208- const pathPrefixLength = mergePath(this._basePath, path).length208+ const mergedPath = mergePath(this._basePath, path)209+ const pathPrefixLength = mergedPath === '/' ? 0 : mergedPath.length210+211 const handler: MiddlewareHandler = async (c, next) => {212 let executionContext: ExecutionContext | undefined = undefined213 try {214 executionContext = c.executionCtx150 unmodified linesmodified:src/hono.test.ts+36 added-2 removed
1914 unmodified lines1915 describe('app.mount()', () => {1916 describe('Basic', () => {1917 const anotherApp = (req: Request, params: unknown) => {1918 const path = getPath(req)1919- console.log(`path in anotherApp: ${path}`)1919 if (path === '/') {1920 return new Response('AnotherApp')1921 }1922 if (path === '/hello') {14 unmodified lines1937 },1938 }1939 )1940 }1942- console.log(`before 404 ${req.url}`)1941 return new Response('Not Found from AnotherApp', {1942 status: 404,1943 })1944 }117 unmodified lines2062 },2063 })2064 })2065 })2066+2067+ describe('Mount on `/`', () => {2068+ const anotherApp = (req: Request, params: unknown) => {2069+ const path = getPath(req)2070+ if (path === '/') {2071+ return new Response('AnotherApp')2072+ }2073+ if (path === '/hello') {2074+ return new Response('Hello from AnotherApp')2075+ }2076+ if (path === '/good/night') {2077+ return new Response('Good Night from AnotherApp')2078+ }2079+ return new Response('Not Found from AnotherApp', {2080+ status: 404,2081+ })2082+ }2083+2084+ const app = new Hono()2085+ app.mount('/', anotherApp)2086+2087+ it('Should return responses from AnotherApp - mount on `/`', async () => {2088+ let res = await app.request('/')2089+ expect(res.status).toBe(200)2090+ expect(await res.text()).toBe('AnotherApp')2091+ res = await app.request('/hello')2092+ expect(res.status).toBe(200)2093+ expect(await res.text()).toBe('Hello from AnotherApp')2094+ res = await app.request('/good/night')2095+ expect(res.status).toBe(200)2096+ expect(await res.text()).toBe('Good Night from AnotherApp')2097+ res = await app.request('/not-found')2098+ expect(res.status).toBe(404)2099+ expect(await res.text()).toBe('Not Found from AnotherApp')2100+ })2101+ })2102 })2103 2104 describe('Router Name', () => {2105 it('Should return the correct router name', async () => {10 unmodified lines