Skip to content

Commit

Permalink
add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
cometkim committed Apr 30, 2024
1 parent 8b4dc49 commit 6ba2fe4
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions src/middleware/serve-static/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import { createMiddleware } from '../../helper'
import { Hono } from '../../hono'
import { serveStatic as baseServeStatic } from '.'

describe('Serve Static Middleware', () => {
const app = new Hono()

const serveStatic = createMiddleware(async (c, next) => {
const mw = baseServeStatic({
getContent: async (path) => {
if (path.endsWith('not-found.txt')) {
return null
}
return `Hello in ${path}`
},
pathResolve: (path) => {
return `./${path}`
},
})
return await mw(c, next)
const serveStatic = baseServeStatic({
getContent: async (path) => {
if (path.endsWith('not-found.txt')) {
return null
}
return `Hello in ${path}`
},
pathResolve: (path) => {
return `./${path}`
},
})

app.get('/static/*', serveStatic)
Expand Down Expand Up @@ -56,4 +52,24 @@ describe('Serve Static Middleware', () => {
expect(res.status).toBe(404)
expect(await res.text()).toBe('404 Not Found')
})

it('Should return response object content as-is', async () => {
const body = new ReadableStream()
const response = new Response(body)
const app = new Hono().use(
'*',
baseServeStatic({
getContent: async () => {
return response
},
})
)

const res = await app.fetch({
method: 'GET',
url: 'http://localhost',
} as Request)
expect(res.status).toBe(200)
expect(res.body).toBe(body)
})
})

0 comments on commit 6ba2fe4

Please sign in to comment.