mirror of
https://github.com/honojs/hono.git
synced 2024-11-21 18:18:57 +01:00
feat(powered-by): optional server name (#3492)
* refactor(powered-by): optional server name * style(powered-by): format * test(powered-by): test new config * Update index.test.ts
This commit is contained in:
parent
cebf4e87f3
commit
fc9cc6da28
@ -11,6 +11,9 @@ describe('Powered by Middleware', () => {
|
||||
app.use('/poweredBy2/*', poweredBy())
|
||||
app.get('/poweredBy2', (c) => c.text('root'))
|
||||
|
||||
app.use('/poweredBy3/*', poweredBy({ serverName: 'Foo' }))
|
||||
app.get('/poweredBy3', (c) => c.text('root'))
|
||||
|
||||
it('Should return with X-Powered-By header', async () => {
|
||||
const res = await app.request('http://localhost/poweredBy')
|
||||
expect(res).not.toBeNull()
|
||||
@ -24,4 +27,11 @@ describe('Powered by Middleware', () => {
|
||||
expect(res.status).toBe(200)
|
||||
expect(res.headers.get('X-Powered-By')).toBe('Hono')
|
||||
})
|
||||
|
||||
it('Should return custom serverName', async () => {
|
||||
const res = await app.request('http://localhost/poweredBy3')
|
||||
expect(res).not.toBeNull()
|
||||
expect(res.status).toBe(200)
|
||||
expect(res.headers.get('X-Powered-By')).toBe('Foo')
|
||||
})
|
||||
})
|
||||
|
@ -2,12 +2,15 @@
|
||||
* @module
|
||||
* Powered By Middleware for Hono.
|
||||
*/
|
||||
|
||||
import type { MiddlewareHandler } from '../../types'
|
||||
|
||||
export const poweredBy = (): MiddlewareHandler => {
|
||||
type PoweredByOptions = {
|
||||
serverName?: string
|
||||
}
|
||||
|
||||
export const poweredBy = (options?: PoweredByOptions): MiddlewareHandler => {
|
||||
return async function poweredBy(c, next) {
|
||||
await next()
|
||||
c.res.headers.set('X-Powered-By', 'Hono')
|
||||
c.res.headers.set('X-Powered-By', options?.serverName ?? 'Hono')
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user