Skip to content

Commit

Permalink
add mime and serve-static tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuapp committed Jan 26, 2024
1 parent 1bc8231 commit 3570302
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/adapter/cloudflare-workers/serve-static.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const store: Record<string, string> = {
'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',
Expand Down Expand Up @@ -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) => {
Expand Down
11 changes: 11 additions & 0 deletions src/utils/mime.test.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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')
Expand Down

0 comments on commit 3570302

Please sign in to comment.