diff --git a/src/middleware/powered-by/index.test.ts b/src/middleware/powered-by/index.test.ts index 89a650a1..d0a1ada3 100644 --- a/src/middleware/powered-by/index.test.ts +++ b/src/middleware/powered-by/index.test.ts @@ -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') + }) }) diff --git a/src/middleware/powered-by/index.ts b/src/middleware/powered-by/index.ts index 605065a0..66004cb2 100644 --- a/src/middleware/powered-by/index.ts +++ b/src/middleware/powered-by/index.ts @@ -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') } }