0
0
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:
PatrickJS 2024-10-14 22:50:39 -04:00 committed by GitHub
parent cebf4e87f3
commit fc9cc6da28
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 3 deletions

View File

@ -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')
})
})

View File

@ -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')
}
}