Skip to content
Myr

serve-static.test.ts

This file

Path

adapters/cloudflare-workers/src/serve-static.test.ts

221 lines · 7.8 KiB · TypeScript

Last changed by

feat(adapters): add the seven adapters as workspace packages (#5463)Yusuke Wadabefore adoptionb877827

History of this file

Take it

feat(adapters): add the seven adapters as workspace packages (#5463)Yusuke Wadabefore adoptionb877827

lines 1–221why, one step back
1import type { Context } from 'hono'
2import { Hono } from 'hono'3import type { Next } from 'hono/types'4import { serveStatic } from './serve-static'56// Mock7const store: Record<string, string> = {8 'assets/static/plain.abcdef.txt': 'This is plain.txt',9 'assets/static/hono.abcdef.html': '<h1>Hono!</h1>',10 'assets/static/top/index.abcdef.html': '<h1>Top</h1>',11 'static-no-root/plain.abcdef.txt': 'That is plain.txt',12 'assets/static/options/foo.abcdef.txt': 'With options',13 'assets/.static/plain.abcdef.txt': 'In the dot',14 'assets/static/video/morning-routine.abcdef.m3u8': 'Good morning',15 'assets/static/video/morning-routine1.abcdef.ts': 'Good',16 'assets/static/video/introduction.abcdef.mp4': 'Let me introduce myself',17 'assets/static/download': 'download',18}19const manifest = JSON.stringify({20 'assets/static/plain.txt': 'assets/static/plain.abcdef.txt',21 'assets/static/hono.html': 'assets/static/hono.abcdef.html',22 'assets/static/top/index.html': 'assets/static/top/index.abcdef.html',23 'static-no-root/plain.txt': 'static-no-root/plain.abcdef.txt',24 'assets/.static/plain.txt': 'assets/.static/plain.abcdef.txt',25 'assets/static/download': 'assets/static/download',26})2728Object.assign(global, { __STATIC_CONTENT_MANIFEST: manifest })29Object.assign(global, {30 __STATIC_CONTENT: {31 get: (path: string) => {32 return store[path]33 },34 },35})3637describe('ServeStatic Middleware', () => {38 const app = new Hono()39 const onNotFound = vi.fn(() => {})40 app.use('/static/*', serveStatic({ root: './assets', onNotFound, manifest }))41 app.use('/static-no-root/*', serveStatic({ manifest }))42 app.use(43 '/dot-static/*',44 serveStatic({45 root: './assets',46 rewriteRequestPath: (path) => path.replace(/^\/dot-static/, '/.static'),47 manifest,48 })49 )5051 beforeEach(() => onNotFound.mockClear())5253 it('Should return plain.txt', async () => {54 const res = await app.request('http://localhost/static/plain.txt')55 expect(res.status).toBe(200)56 expect(await res.text()).toBe('This is plain.txt')57 expect(res.headers.get('Content-Type')).toBe('text/plain; charset=utf-8')58 expect(onNotFound).not.toHaveBeenCalled()59 })6061 it('Should return hono.html', async () => {62 const res = await app.request('http://localhost/static/hono.html')63 expect(res.status).toBe(200)64 expect(await res.text()).toBe('<h1>Hono!</h1>')65 expect(res.headers.get('Content-Type')).toBe('text/html; charset=utf-8')66 expect(onNotFound).not.toHaveBeenCalled()67 })6869 it('Should return 404 response', async () => {70 const res = await app.request('http://localhost/static/not-found.html')71 expect(res.status).toBe(404)72 expect(onNotFound).toHaveBeenCalledWith('assets/static/not-found.html', expect.anything())73 })7475 it('Should return plan.txt', async () => {76 const res = await app.request('http://localhost/static-no-root/plain.txt')77 expect(res.status).toBe(200)78 expect(await res.text()).toBe('That is plain.txt')79 expect(res.headers.get('Content-Type')).toBe('text/plain; charset=utf-8')80 })8182 // Serve static on Cloudflare Workers cannot determine whether the target path is a directory or not83 it.skip('Should return index.html', async () => {84 const res = await app.request('http://localhost/static/top')85 expect(res.status).toBe(200)86 expect(await res.text()).toBe('<h1>Top</h1>')87 expect(res.headers.get('Content-Type')).toBe('text/html; charset=utf-8')88 })8990 it('Should return plain.txt with a rewriteRequestPath option', async () => {91 const res = await app.request('http://localhost/dot-static/plain.txt')92 expect(res.status).toBe(200)93 expect(await res.text()).toBe('In the dot')94 expect(res.headers.get('Content-Type')).toBe('text/plain; charset=utf-8')95 })96})9798describe('With options', () => {99 const manifest = {100 'assets/static/options/foo.txt': 'assets/static/options/foo.abcdef.txt',101 }102103 const app = new Hono()104 app.use('/static/*', serveStatic({ root: './assets', manifest: manifest }))105106 it('Should return foo.txt', async () => {107 const res = await app.request('http://localhost/static/options/foo.txt')108 expect(res.status).toBe(200)109 expect(await res.text()).toBe('With options')110 expect(res.headers.get('Content-Type')).toBe('text/plain; charset=utf-8')111 })112})113114describe('With `file` options', () => {115 const app = new Hono()116 app.get('/foo/*', serveStatic({ path: './assets/static/hono.html', manifest }))117 app.get('/bar/*', serveStatic({ path: './static/hono.html', root: './assets', manifest }))118119 it('Should return hono.html', async () => {120 const res = await app.request('http://localhost/foo/fallback')121 expect(res.status).toBe(200)122 expect(await res.text()).toBe('<h1>Hono!</h1>')123 })124125 it('Should return hono.html - with `root` option', async () => {126 const res = await app.request('http://localhost/bar/fallback')127 expect(res.status).toBe(200)128 expect(await res.text()).toBe('<h1>Hono!</h1>')129 })130})131132describe('With `mimes` options', () => {133 const mimes = {134 m3u8: 'application/vnd.apple.mpegurl',135 ts: 'video/mp2t',136 }137 const manifest = {138 'assets/static/video/morning-routine.m3u8': 'assets/static/video/morning-routine.abcdef.m3u8',139 'assets/static/video/morning-routine1.ts': 'assets/static/video/morning-routine1.abcdef.ts',140 'assets/static/video/introduction.mp4': 'assets/static/video/introduction.abcdef.mp4',141 }142143 const app = new Hono()144 app.use('/static/*', serveStatic({ root: './assets', mimes, manifest }))145146 it('Should return content-type of m3u8', async () => {147 const res = await app.request('http://localhost/static/video/morning-routine.m3u8')148 expect(res.status).toBe(200)149 expect(res.headers.get('Content-Type')).toBe('application/vnd.apple.mpegurl')150 })151 it('Should return content-type of ts', async () => {152 const res = await app.request('http://localhost/static/video/morning-routine1.ts')153 expect(res.status).toBe(200)154 expect(res.headers.get('Content-Type')).toBe('video/mp2t')155 })156 it('Should return content-type of default on Hono', async () => {157 const res = await app.request('http://localhost/static/video/introduction.mp4')158 expect(res.status).toBe(200)159 expect(res.headers.get('Content-Type')).toBe('video/mp4')160 })161})162163describe('With middleware', () => {164 const app = new Hono()165 const md1 = async (c: Context, next: Next) => {166 await next()167 c.res.headers.append('x-foo', 'bar')168 }169 const md2 = async (c: Context, next: Next) => {170 await next()171 c.res.headers.append('x-foo2', 'bar2')172 }173174 app.use('/static/*', md1)175 app.use('/static/*', md2)176 app.use('/static/*', serveStatic({ root: './assets', manifest }))177 app.get('/static/foo', (c) => {178 return c.text('bar')179 })180181 it('Should return plain.txt with correct headers', async () => {182 const res = await app.request('http://localhost/static/plain.txt')183 expect(res.status).toBe(200)184 expect(await res.text()).toBe('This is plain.txt')185 expect(res.headers.get('Content-Type')).toBe('text/plain; charset=utf-8')186 expect(res.headers.get('x-foo')).toBe('bar')187 expect(res.headers.get('x-foo2')).toBe('bar2')188 })189190 it('Should return 200 Response', async () => {191 const res = await app.request('http://localhost/static/foo')192 expect(res.status).toBe(200)193 expect(await res.text()).toBe('bar')194 })195196 it('Should handle a file without an extension', async () => {197 const res = await app.request('http://localhost/static/download')198 expect(res.status).toBe(200)199 })200})201202describe('Types of middleware', () => {203 it('Should pass env type from generics of serveStatic', async () => {204 type Env = {205 Bindings: {206 HOGE: string207 }208 }209 const app = new Hono<Env>()210 app.use(211 '/static/*',212 serveStatic<Env>({213 root: './assets',214 onNotFound: (_, c) => {215 expectTypeOf(c.env).toEqualTypeOf<Env['Bindings']>()216 },217 manifest,218 })219 )220 })221})