Skip to content

Commit

Permalink
test: add serveStatic onNotFound tests for bun and deno
Browse files Browse the repository at this point in the history
  • Loading branch information
Th1nkK1D committed Dec 15, 2023
1 parent 330b72c commit bcff806
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
10 changes: 8 additions & 2 deletions runtime_tests/bun/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, expect, it } from 'bun:test'
import { describe, expect, it, mock, beforeEach } from 'bun:test'
import { serveStatic } from '../../src/adapter/bun'
import { Context } from '../../src/context'
import { env, getRuntimeKey } from '../../src/helper/adapter'
Expand Down Expand Up @@ -76,10 +76,11 @@ describe('Basic Auth Middleware', () => {

describe('Serve Static Middleware', () => {
const app = new Hono()
const onNotFound = mock(() => {})
app.all('/favicon.ico', serveStatic({ path: './runtime_tests/bun/favicon.ico' }))
app.all(
'/favicon-notfound.ico',
serveStatic({ path: './runtime_tests/bun/favicon-notfound.ico' })
serveStatic({ path: './runtime_tests/bun/favicon-notfound.ico', onNotFound })
)
app.use('/favicon-notfound.ico', async (c, next) => {
await next()
Expand All @@ -89,6 +90,7 @@ describe('Serve Static Middleware', () => {
'/static/*',
serveStatic({
root: './runtime_tests/bun/',
onNotFound,
})
)
app.get(
Expand All @@ -99,6 +101,8 @@ describe('Serve Static Middleware', () => {
})
)

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

it('Should return static file correctly', async () => {
const res = await app.request(new Request('http://localhost/favicon.ico'))
await res.arrayBuffer()
Expand All @@ -110,12 +114,14 @@ describe('Serve Static Middleware', () => {
const res = await app.request(new Request('http://localhost/favicon-notfound.ico'))
expect(res.status).toBe(404)
expect(res.headers.get('X-Custom')).toBe('Bun')
expect(onNotFound).toHaveBeenCalledWith('./runtime_tests/bun/favicon-notfound.ico')
})

it('Should return 200 response - /static/plain.txt', async () => {
const res = await app.request(new Request('http://localhost/static/plain.txt'))
expect(res.status).toBe(200)
expect(await res.text()).toBe('Bun!')
expect(onNotFound).not.toHaveBeenCalled()
})

it('Should return 200 response - /dot-static/plain.txt', async () => {
Expand Down
1 change: 1 addition & 0 deletions runtime_tests/deno/deps.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { assert, assertEquals, assertMatch } from 'https://deno.land/std@0.147.0/testing/asserts.ts'
export { assertSpyCall, assertSpyCalls, spy } from 'https://deno.land/std@0.147.0/testing/mock.ts'
10 changes: 8 additions & 2 deletions runtime_tests/deno/middleware.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/** @jsxFrag Fragment */
import { basicAuth, jsx, Fragment, serveStatic, jwt } from '../../deno_dist/middleware.ts'
import { Hono } from '../../deno_dist/mod.ts'
import { assertEquals, assertMatch } from './deps.ts'
import { assertEquals, assertMatch, assertSpyCall, assertSpyCalls, spy } from './deps.ts'

// Test just only minimal patterns.
// Because others are already tested well in Cloudflare Workers environment.
Expand Down Expand Up @@ -66,10 +66,11 @@ Deno.test('JSX middleware', async () => {

Deno.test('Serve Static middleware', async () => {
const app = new Hono()
const onNotFound = spy(() => {})
app.all('/favicon.ico', serveStatic({ path: './runtime_tests/deno/favicon.ico' }))
app.all(
'/favicon-notfound.ico',
serveStatic({ path: './runtime_tests/deno/favicon-notfound.ico' })
serveStatic({ path: './runtime_tests/deno/favicon-notfound.ico', onNotFound })
)
app.use('/favicon-notfound.ico', async (c, next) => {
await next()
Expand All @@ -80,6 +81,7 @@ Deno.test('Serve Static middleware', async () => {
'/static/*',
serveStatic({
root: './runtime_tests/deno',
onNotFound,
})
)

Expand All @@ -100,6 +102,9 @@ Deno.test('Serve Static middleware', async () => {
assertEquals(res.status, 404)
assertMatch(res.headers.get('Content-Type') || '', /^text\/plain/)
assertEquals(res.headers.get('X-Custom'), 'Deno')
assertSpyCall(onNotFound, 0, {
args: ['./runtime_tests/deno/favicon-notfound.ico'],
})

res = await app.request('http://localhost/static/plain.txt')
assertEquals(res.status, 200)
Expand All @@ -108,6 +113,7 @@ Deno.test('Serve Static middleware', async () => {
res = await app.request('http://localhost/dot-static/plain.txt')
assertEquals(res.status, 200)
assertEquals(await res.text(), 'Deno!!')
assertSpyCalls(onNotFound, 1)
})

Deno.test('JWT Authentication middleware', async () => {
Expand Down

0 comments on commit bcff806

Please sign in to comment.