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

fix(app): set / for path as default (#1330)

* fix(app): set `/` for `path` as default

* denoify
This commit is contained in:
Yusuke Wada 2023-08-15 00:33:16 +09:00 committed by GitHub
parent 5e653dfafd
commit acbd495bbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 6 deletions

View File

@ -66,8 +66,8 @@ class Hono<E extends Env = Env, S = {}, BasePath extends string = '/'> extends d
*/
router!: Router<H>
readonly getPath: (request: Request) => string
private _basePath: string = ''
private path: string = '*'
private _basePath: string = '/'
private path: string = '/'
routes: RouterRoute[] = []

View File

@ -67,7 +67,7 @@ export interface HandlerInterface<
E extends Env = Env,
M extends string = any,
S = {},
BasePath extends string = ''
BasePath extends string = '/'
> {
//// app.get(...handlers[])

View File

@ -66,8 +66,8 @@ class Hono<E extends Env = Env, S = {}, BasePath extends string = '/'> extends d
*/
router!: Router<H>
readonly getPath: (request: Request) => string
private _basePath: string = ''
private path: string = '*'
private _basePath: string = '/'
private path: string = '/'
routes: RouterRoute[] = []

View File

@ -1269,6 +1269,44 @@ describe('Hono with `app.route`', () => {
expect(res.status).toBe(200)
expect(await res.text()).toBe('bar')
})
describe('With app.get(...handler)', () => {
const app = new Hono()
const about = new Hono()
about.get((c) => c.text('me'))
const subApp = new Hono()
subApp.route('/about', about)
app.route('/', subApp)
it('Should return 200 response - /about', async () => {
const res = await app.request('/about')
expect(res.status).toBe(200)
expect(await res.text()).toBe('me')
})
test('Should return 404 response /about/foo', async () => {
const res = await app.request('/about/foo')
expect(res.status).toBe(404)
})
})
describe('With app.get(...handler) and app.basePath()', () => {
const app = new Hono()
const about = new Hono().basePath('/about')
about.get((c) => c.text('me'))
app.route('/', about)
it('Should return 200 response - /about', async () => {
const res = await app.request('/about')
expect(res.status).toBe(200)
expect(await res.text()).toBe('me')
})
test('Should return 404 response /about/foo', async () => {
const res = await app.request('/about/foo')
expect(res.status).toBe(404)
})
})
})
describe('Chaining', () => {

View File

@ -67,7 +67,7 @@ export interface HandlerInterface<
E extends Env = Env,
M extends string = any,
S = {},
BasePath extends string = ''
BasePath extends string = '/'
> {
//// app.get(...handlers[])