index.test.ts
This file
Path
src/middleware/mount/index.test.ts
334 lines 路 9.9 KiB 路 TypeScript
Last changed by
feat(mount): introduce Mount Middleware and deprecate `app.mount` (#5221)Yusuke Wadabefore adoptionae595de
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335import { mount } from '.'/* eslint-disable @typescript-eslint/ban-ts-comment */import type { ExecutionContext } from '../../context'import { Hono } from '../../hono'import { getPath } from '../../utils/url'
describe('mount()', () => { describe('Basic', () => { 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 === '/header') { const message = req.headers.get('x-message') return new Response(message) } if (path === '/with-query') { const queryStrings = new URL(req.url).searchParams.toString() return new Response(queryStrings) } if (path == '/with-params') { return new Response( JSON.stringify({ params, }), { headers: { 'Content-Type': 'application.json', }, } ) } if (path === '/undefined') { return undefined as unknown as Response } return new Response('Not Found from AnotherApp', { status: 404, }) }
const app = new Hono() app.use('*', async (c, next) => { await next() c.header('x-message', 'Foo') }) app.get('/', (c) => c.text('Hono')) app.notFound((c) => { return c.text('Not Found from App', 404) })
app.all( '/another-app/*', mount(anotherApp, () => { return 'params' }) ) app.all( '/another-app-with-array-option/*', mount(anotherApp, () => { return ['param1', 'param2'] }) ) app.all('/another-app2/sub-slash/*', mount(anotherApp))
const api = new Hono().basePath('/api') api.all('/another-app/*', mount(anotherApp))
it('Should return responses from Hono app', async () => { const res = await app.request('/') expect(res.status).toBe(200) expect(res.headers.get('x-message')).toBe('Foo') expect(await res.text()).toBe('Hono') })
it('Should return responses from AnotherApp', async () => { let res = await app.request('/another-app') expect(res.status).toBe(200) expect(res.headers.get('x-message')).toBe('Foo') expect(await res.text()).toBe('AnotherApp')
res = await app.request('/another-app/hello') expect(res.status).toBe(200) expect(res.headers.get('x-message')).toBe('Foo') expect(await res.text()).toBe('Hello from AnotherApp')
const req = new Request('http://localhost/another-app/header', { headers: { 'x-message': 'Message Foo!', }, }) res = await app.request(req) expect(res.status).toBe(200) expect(res.headers.get('x-message')).toBe('Foo') expect(await res.text()).toBe('Message Foo!')
res = await app.request('/another-app/not-found') expect(res.status).toBe(404) expect(res.headers.get('x-message')).toBe('Foo') expect(await res.text()).toBe('Not Found from AnotherApp')
res = await app.request('/another-app/with-query?foo=bar&baz=qux') expect(res.status).toBe(200) expect(await res.text()).toBe('foo=bar&baz=qux')
res = await app.request('/another-app/with-params') expect(res.status).toBe(200) expect(await res.json()).toEqual({ params: ['params'], })
res = await app.request('/another-app/undefined') expect(res.status).toBe(404) expect(await res.text()).toBe('Not Found from App') })
it('Should return response from Another app with an array option', async () => { const res = await app.request('/another-app-with-array-option/with-params') expect(res.status).toBe(200) expect(await res.json()).toEqual({ params: ['param1', 'param2'], }) })
it('Should return responses from AnotherApp - sub + slash', async () => { const res = await app.request('/another-app2/sub-slash') expect(res.status).toBe(200) expect(await res.text()).toBe('AnotherApp') })
it('Should return responses from AnotherApp - with `basePath()`', async () => { const res = await api.request('/api/another-app') expect(res.status).toBe(200) expect(await res.text()).toBe('AnotherApp') }) })
describe('With encoded paths', () => { const anotherApp = (req: Request) => new Response(getPath(req))
it('Should strip a decoded non-ASCII mount prefix', async () => { const app = new Hono() app.all('/api/茅/*', mount(anotherApp))
const res = await app.request('/api/%C3%A9/hello')
expect(res.status).toBe(200) expect(await res.text()).toBe('/hello') })
it('Should preserve an encoded slash as a literal path segment after stripping the prefix', async () => { const app = new Hono() app.all('/api/v1/*', mount(anotherApp))
const res = await app.request('/api/v1/admin%2Fsecret')
expect(res.status).toBe(200) expect(await res.text()).toBe('/admin%2Fsecret') })
it('Should preserve encoded percent characters after stripping the prefix', async () => { const app = new Hono() app.all('/api/*', mount(anotherApp))
const res = await app.request('/api/foo%252Fbar')
expect(res.status).toBe(200) expect(await res.text()).toBe('/foo%252Fbar') }) })
describe('With fetch', () => { const anotherApp = async (req: Request, env: {}, executionContext: ExecutionContext) => { const path = getPath(req) if (path === '/') { return new Response( JSON.stringify({ env, executionContext, }), { headers: { 'Content-Type': 'application/json', }, } ) } return new Response('Not Found from AnotherApp', { status: 404, }) }
const app = new Hono() app.all('/another-app/*', mount(anotherApp))
it('Should handle Env and ExecuteContext', async () => { const request = new Request('http://localhost/another-app') const res = await app.fetch( request, { TOKEN: 'foo', }, { // Force mocking!
// @ts-ignore waitUntil: 'waitUntil',
// @ts-ignore passThroughOnException: 'passThroughOnException', } ) expect(res.status).toBe(200) expect(await res.json()).toEqual({ env: { TOKEN: 'foo', }, executionContext: { waitUntil: 'waitUntil', passThroughOnException: 'passThroughOnException', }, }) }) })
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.all('/*', 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('With replaceRequest option', () => { const anotherApp = (req: Request) => { const path = getPath(req) if (path === '/app') { return new Response(getPath(req)) } return new Response(null, { status: 404 }) }
const app = new Hono() app.all( '/app/*', mount(anotherApp, { replaceRequest: (req) => req, }) )
it('Should return 200 response with the correct path', async () => { const res = await app.request('/app') expect(res.status).toBe(200) expect(await res.text()).toBe('/app') }) })
describe('With replaceRequest: false', () => { const anotherApp = (req: Request) => { const path = getPath(req) if (path === '/app') { return new Response(getPath(req)) } return new Response(null, { status: 404 }) }
const app = new Hono() app.all('/app/*', mount(anotherApp, { replaceRequest: false }))
it('Should return 200 response with the correct path', async () => { const res = await app.request('/app') expect(res.status).toBe(200) expect(await res.text()).toBe('/app') }) })
describe('Used as `app.use()` middleware', () => { const anotherApp = (req: Request) => { const path = getPath(req) if (path === '/hello') { return new Response('Hello from AnotherApp') } // Fall through to the Hono app return undefined as unknown as Response }
const app = new Hono() app.use('/another-app/*', mount(anotherApp)) app.get('/another-app/fallback', (c) => c.text('Fallback from App'))
it('Should return responses from AnotherApp', async () => { const res = await app.request('/another-app/hello') expect(res.status).toBe(200) expect(await res.text()).toBe('Hello from AnotherApp') })
it('Should fall through to the Hono app if AnotherApp returns nothing', async () => { const res = await app.request('/another-app/fallback') expect(res.status).toBe(200) expect(await res.text()).toBe('Fallback from App') }) })})