0
0
mirror of https://github.com/honojs/hono.git synced 2024-12-01 11:51:01 +01:00

feat(context): clear the header with c.header(key, undefined) (#1071)

This commit is contained in:
Yusuke Wada 2023-05-05 11:06:14 +09:00 committed by GitHub
parent c50dcf03bb
commit 177c1a44fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 1 deletions

View File

@ -81,6 +81,17 @@ describe('Context', () => {
expect(res.headers.get('content-type')).toMatch(/^text\/html/)
})
it('c.header() - clear the header', async () => {
c.header('X-Foo', 'Bar')
c.header('X-Foo', undefined)
c.header('X-Foo2', 'Bar')
let res = c.body('Hi')
expect(res.headers.get('X-Foo')).toBe(null)
c.header('X-Foo2', undefined)
res = c.res
expect(res.headers.get('X-Foo2')).toBe(null)
})
it('c.body() - multiple header', async () => {
const res = c.body('Hi', 200, {
'X-Foo': ['Bar', 'Buzz'],

View File

@ -145,7 +145,20 @@ export class Context<
this.finalized = true
}
header = (name: string, value: string, options?: { append?: boolean }): void => {
header = (name: string, value: string | undefined, options?: { append?: boolean }): void => {
// Clear the header
if (value === undefined) {
if (this._headers) {
this._headers.delete(name)
} else if (this._preparedHeaders) {
delete this._preparedHeaders[name.toLocaleLowerCase()]
}
if (this.finalized) {
this.res.headers.delete(name)
}
return
}
if (options?.append) {
if (!this._headers) {
this._headers = new Headers(this._preparedHeaders)