Skip to content
Myr

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 file

Take it

feat(mount): introduce Mount Middleware and deprecate `app.mount` (#5221)Yusuke Wadabefore adoptionae595de

lines 1–334why, one step back
1import { mount } from '.'
2/* eslint-disable @typescript-eslint/ban-ts-comment */3import type { ExecutionContext } from '../../context'4import { Hono } from '../../hono'5import { getPath } from '../../utils/url'67describe('mount()', () => {8 describe('Basic', () => {9 const anotherApp = (req: Request, ...params: unknown[]) => {10 const path = getPath(req)11 if (path === '/') {12 return new Response('AnotherApp')13 }14 if (path === '/hello') {15 return new Response('Hello from AnotherApp')16 }17 if (path === '/header') {18 const message = req.headers.get('x-message')19 return new Response(message)20 }21 if (path === '/with-query') {22 const queryStrings = new URL(req.url).searchParams.toString()23 return new Response(queryStrings)24 }25 if (path == '/with-params') {26 return new Response(27 JSON.stringify({28 params,29 }),30 {31 headers: {32 'Content-Type': 'application.json',33 },34 }35 )36 }37 if (path === '/undefined') {38 return undefined as unknown as Response39 }40 return new Response('Not Found from AnotherApp', {41 status: 404,42 })43 }4445 const app = new Hono()46 app.use('*', async (c, next) => {47 await next()48 c.header('x-message', 'Foo')49 })50 app.get('/', (c) => c.text('Hono'))51 app.notFound((c) => {52 return c.text('Not Found from App', 404)53 })5455 app.all(56 '/another-app/*',57 mount(anotherApp, () => {58 return 'params'59 })60 )61 app.all(62 '/another-app-with-array-option/*',63 mount(anotherApp, () => {64 return ['param1', 'param2']65 })66 )67 app.all('/another-app2/sub-slash/*', mount(anotherApp))6869 const api = new Hono().basePath('/api')70 api.all('/another-app/*', mount(anotherApp))7172 it('Should return responses from Hono app', async () => {73 const res = await app.request('/')74 expect(res.status).toBe(200)75 expect(res.headers.get('x-message')).toBe('Foo')76 expect(await res.text()).toBe('Hono')77 })7879 it('Should return responses from AnotherApp', async () => {80 let res = await app.request('/another-app')81 expect(res.status).toBe(200)82 expect(res.headers.get('x-message')).toBe('Foo')83 expect(await res.text()).toBe('AnotherApp')8485 res = await app.request('/another-app/hello')86 expect(res.status).toBe(200)87 expect(res.headers.get('x-message')).toBe('Foo')88 expect(await res.text()).toBe('Hello from AnotherApp')8990 const req = new Request('http://localhost/another-app/header', {91 headers: {92 'x-message': 'Message Foo!',93 },94 })95 res = await app.request(req)96 expect(res.status).toBe(200)97 expect(res.headers.get('x-message')).toBe('Foo')98 expect(await res.text()).toBe('Message Foo!')99100 res = await app.request('/another-app/not-found')101 expect(res.status).toBe(404)102 expect(res.headers.get('x-message')).toBe('Foo')103 expect(await res.text()).toBe('Not Found from AnotherApp')104105 res = await app.request('/another-app/with-query?foo=bar&baz=qux')106 expect(res.status).toBe(200)107 expect(await res.text()).toBe('foo=bar&baz=qux')108109 res = await app.request('/another-app/with-params')110 expect(res.status).toBe(200)111 expect(await res.json()).toEqual({112 params: ['params'],113 })114115 res = await app.request('/another-app/undefined')116 expect(res.status).toBe(404)117 expect(await res.text()).toBe('Not Found from App')118 })119120 it('Should return response from Another app with an array option', async () => {121 const res = await app.request('/another-app-with-array-option/with-params')122 expect(res.status).toBe(200)123 expect(await res.json()).toEqual({124 params: ['param1', 'param2'],125 })126 })127128 it('Should return responses from AnotherApp - sub + slash', async () => {129 const res = await app.request('/another-app2/sub-slash')130 expect(res.status).toBe(200)131 expect(await res.text()).toBe('AnotherApp')132 })133134 it('Should return responses from AnotherApp - with `basePath()`', async () => {135 const res = await api.request('/api/another-app')136 expect(res.status).toBe(200)137 expect(await res.text()).toBe('AnotherApp')138 })139 })140141 describe('With encoded paths', () => {142 const anotherApp = (req: Request) => new Response(getPath(req))143144 it('Should strip a decoded non-ASCII mount prefix', async () => {145 const app = new Hono()146 app.all('/api/é/*', mount(anotherApp))147148 const res = await app.request('/api/%C3%A9/hello')149150 expect(res.status).toBe(200)151 expect(await res.text()).toBe('/hello')152 })153154 it('Should preserve an encoded slash as a literal path segment after stripping the prefix', async () => {155 const app = new Hono()156 app.all('/api/v1/*', mount(anotherApp))157158 const res = await app.request('/api/v1/admin%2Fsecret')159160 expect(res.status).toBe(200)161 expect(await res.text()).toBe('/admin%2Fsecret')162 })163164 it('Should preserve encoded percent characters after stripping the prefix', async () => {165 const app = new Hono()166 app.all('/api/*', mount(anotherApp))167168 const res = await app.request('/api/foo%252Fbar')169170 expect(res.status).toBe(200)171 expect(await res.text()).toBe('/foo%252Fbar')172 })173 })174175 describe('With fetch', () => {176 const anotherApp = async (req: Request, env: {}, executionContext: ExecutionContext) => {177 const path = getPath(req)178 if (path === '/') {179 return new Response(180 JSON.stringify({181 env,182 executionContext,183 }),184 {185 headers: {186 'Content-Type': 'application/json',187 },188 }189 )190 }191 return new Response('Not Found from AnotherApp', {192 status: 404,193 })194 }195196 const app = new Hono()197 app.all('/another-app/*', mount(anotherApp))198199 it('Should handle Env and ExecuteContext', async () => {200 const request = new Request('http://localhost/another-app')201 const res = await app.fetch(202 request,203 {204 TOKEN: 'foo',205 },206 {207 // Force mocking!208209 // @ts-ignore210 waitUntil: 'waitUntil',211212 // @ts-ignore213 passThroughOnException: 'passThroughOnException',214 }215 )216 expect(res.status).toBe(200)217 expect(await res.json()).toEqual({218 env: {219 TOKEN: 'foo',220 },221 executionContext: {222 waitUntil: 'waitUntil',223 passThroughOnException: 'passThroughOnException',224 },225 })226 })227 })228229 describe('Mount on `/`', () => {230 const anotherApp = (req: Request, _params: unknown) => {231 const path = getPath(req)232 if (path === '/') {233 return new Response('AnotherApp')234 }235 if (path === '/hello') {236 return new Response('Hello from AnotherApp')237 }238 if (path === '/good/night') {239 return new Response('Good Night from AnotherApp')240 }241 return new Response('Not Found from AnotherApp', {242 status: 404,243 })244 }245246 const app = new Hono()247 app.all('/*', mount(anotherApp))248249 it('Should return responses from AnotherApp - mount on `/`', async () => {250 let res = await app.request('/')251 expect(res.status).toBe(200)252 expect(await res.text()).toBe('AnotherApp')253 res = await app.request('/hello')254 expect(res.status).toBe(200)255 expect(await res.text()).toBe('Hello from AnotherApp')256 res = await app.request('/good/night')257 expect(res.status).toBe(200)258 expect(await res.text()).toBe('Good Night from AnotherApp')259 res = await app.request('/not-found')260 expect(res.status).toBe(404)261 expect(await res.text()).toBe('Not Found from AnotherApp')262 })263 })264265 describe('With replaceRequest option', () => {266 const anotherApp = (req: Request) => {267 const path = getPath(req)268 if (path === '/app') {269 return new Response(getPath(req))270 }271 return new Response(null, { status: 404 })272 }273274 const app = new Hono()275 app.all(276 '/app/*',277 mount(anotherApp, {278 replaceRequest: (req) => req,279 })280 )281282 it('Should return 200 response with the correct path', async () => {283 const res = await app.request('/app')284 expect(res.status).toBe(200)285 expect(await res.text()).toBe('/app')286 })287 })288289 describe('With replaceRequest: false', () => {290 const anotherApp = (req: Request) => {291 const path = getPath(req)292 if (path === '/app') {293 return new Response(getPath(req))294 }295 return new Response(null, { status: 404 })296 }297298 const app = new Hono()299 app.all('/app/*', mount(anotherApp, { replaceRequest: false }))300301 it('Should return 200 response with the correct path', async () => {302 const res = await app.request('/app')303 expect(res.status).toBe(200)304 expect(await res.text()).toBe('/app')305 })306 })307308 describe('Used as `app.use()` middleware', () => {309 const anotherApp = (req: Request) => {310 const path = getPath(req)311 if (path === '/hello') {312 return new Response('Hello from AnotherApp')313 }314 // Fall through to the Hono app315 return undefined as unknown as Response316 }317318 const app = new Hono()319 app.use('/another-app/*', mount(anotherApp))320 app.get('/another-app/fallback', (c) => c.text('Fallback from App'))321322 it('Should return responses from AnotherApp', async () => {323 const res = await app.request('/another-app/hello')324 expect(res.status).toBe(200)325 expect(await res.text()).toBe('Hello from AnotherApp')326 })327328 it('Should fall through to the Hono app if AnotherApp returns nothing', async () => {329 const res = await app.request('/another-app/fallback')330 expect(res.status).toBe(200)331 expect(await res.text()).toBe('Fallback from App')332 })333 })334})