diff --git a/src/adapter/cloudflare-workers/serve-static.test.ts b/src/adapter/cloudflare-workers/serve-static.test.ts index 2ebff9c7e..79fce4fbf 100644 --- a/src/adapter/cloudflare-workers/serve-static.test.ts +++ b/src/adapter/cloudflare-workers/serve-static.test.ts @@ -11,6 +11,9 @@ const store: Record = { 'static-no-root/plain.abcdef.txt': 'That is plain.txt', 'assets/static/options/foo.abcdef.txt': 'With options', 'assets/.static/plain.abcdef.txt': 'In the dot', + 'assets/static/video/morning-routine.abcdef.m3u8': 'Good morning', + 'assets/static/video/morning-routine1.abcdef.ts': 'Good', + 'assets/static/video/introduction.abcdef.mp4': 'Let me introduce myself', } const manifest = JSON.stringify({ 'assets/static/plain.txt': 'assets/static/plain.abcdef.txt', @@ -123,6 +126,37 @@ describe('With `file` options', () => { }) }) +describe('With `mime` options', () => { + const mime = { + m3u8: 'application/vnd.apple.mpegurl', + ts: 'video/mp2t', + } + const manifest = { + 'assets/static/video/morning-routine.m3u8': 'assets/static/video/morning-routine.abcdef.m3u8', + 'assets/static/video/morning-routine1.ts': 'assets/static/video/morning-routine1.abcdef.ts', + 'assets/static/video/introduction.mp4': 'assets/static/video/introduction.abcdef.mp4', + } + + const app = new Hono() + app.use('/static/*', serveStatic({ root: './assets', mime, manifest })) + + it('Should return content-type of m3u8', async () => { + const res = await app.request('http://localhost/static/video/morning-routine.m3u8') + expect(res.status).toBe(200) + expect(res.headers.get('Content-Type')).toBe('application/vnd.apple.mpegurl') + }) + it('Should return content-type of ts', async () => { + const res = await app.request('http://localhost/static/video/morning-routine1.ts') + expect(res.status).toBe(200) + expect(res.headers.get('Content-Type')).toBe('video/mp2t') + }) + it('Should return content-type of default', async () => { + const res = await app.request('http://localhost/static/video/introduction.mp4') + expect(res.status).toBe(200) + expect(res.headers.get('Content-Type')).not.toBe('video/mp4') + }) +}) + describe('With middleware', () => { const app = new Hono() const md1 = async (c: Context, next: Next) => { diff --git a/src/utils/mime.test.ts b/src/utils/mime.test.ts index e360802c2..5d9cdc9bd 100644 --- a/src/utils/mime.test.ts +++ b/src/utils/mime.test.ts @@ -1,5 +1,10 @@ import { getMimeType, getExtension } from './mime' +const mime = { + m3u8: 'application/vnd.apple.mpegurl', + ts: 'video/mp2t', +} + describe('mime', () => { it('getMimeType', () => { expect(getMimeType('hello.txt')).toBe('text/plain; charset=utf-8') @@ -11,6 +16,12 @@ describe('mime', () => { expect(getMimeType('indexjs.abcd')).toBeUndefined() }) + it('getMimeType with custom mime', () => { + expect(getMimeType('morning-routine.m3u8', mime)).toBe('application/vnd.apple.mpegurl') + expect(getMimeType('morning-routine1.ts', mime)).toBe('video/mp2t') + expect(getMimeType('readme.txt', mime)).toBeUndefined() + }) + it('getExtension', () => { expect(getExtension('audio/aac')).toBe('aac') expect(getExtension('application/x-abiword')).toBe('abw')