Skip to content

Commit

Permalink
tests: test: add serveStatic onNotFound tests for cloudflare workers
Browse files Browse the repository at this point in the history
  • Loading branch information
Th1nkK1D committed Dec 18, 2023
1 parent ab0e8d7 commit 53ebb66
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/adapter/cloudflare-workers/serve-static.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ Object.assign(global, {

describe('ServeStatic Middleware', () => {
const app = new Hono()
app.use('/static/*', serveStatic({ root: './assets' }))
const onNotFound = vi.fn(() => {})
app.use('/static/*', serveStatic({ root: './assets', onNotFound }))
app.use('/static-no-root/*', serveStatic())
app.use(
'/dot-static/*',
Expand All @@ -41,23 +42,28 @@ describe('ServeStatic Middleware', () => {
})
)

beforeEach(() => onNotFound.mockClear())

it('Should return plain.txt', async () => {
const res = await app.request('http://localhost/static/plain.txt')
expect(res.status).toBe(200)
expect(await res.text()).toBe('This is plain.txt')
expect(res.headers.get('Content-Type')).toBe('text/plain; charset=utf-8')
expect(onNotFound).not.toHaveBeenCalled()
})

it('Should return hono.html', async () => {
const res = await app.request('http://localhost/static/hono.html')
expect(res.status).toBe(200)
expect(await res.text()).toBe('<h1>Hono!</h1>')
expect(res.headers.get('Content-Type')).toBe('text/html; charset=utf-8')
expect(onNotFound).not.toHaveBeenCalled()
})

it('Should return 404 response', async () => {
const res = await app.request('http://localhost/static/not-found.html')
expect(res.status).toBe(404)
expect(onNotFound).toHaveBeenCalledWith('assets/static/not-found.html')
})

it('Should return plan.txt', async () => {
Expand Down

0 comments on commit 53ebb66

Please sign in to comment.