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

fix(context): set the values of already set (#834)

This commit is contained in:
Yusuke Wada 2023-01-23 08:35:14 +09:00 committed by GitHub
parent a60e46ae53
commit 033f23189c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 0 deletions

View File

@ -65,6 +65,11 @@ export class Context<
}
set res(_res: Response) {
if (this._res) {
this._res.headers.forEach((v, k) => {
_res.headers.set(k, v)
})
}
this._res = _res
this.finalized = true
}

View File

@ -121,6 +121,14 @@ describe('Context', () => {
expect(c.res.status).toBe(201)
})
it('Should append the previous headers to new Response', () => {
c.res.headers.set('x-Custom1', 'Message1')
const res2 = new Response('foo2')
res2.headers.set('x-Custom2', 'Message2')
c.res = res2
expect(c.res.headers.get('x-Custom1')).toBe('Message1')
})
it('Should return 200 response', async () => {
const res = c.text('Text')
expect(res.status).toBe(200)

View File

@ -65,6 +65,11 @@ export class Context<
}
set res(_res: Response) {
if (this._res) {
this._res.headers.forEach((v, k) => {
_res.headers.set(k, v)
})
}
this._res = _res
this.finalized = true
}

View File

@ -31,6 +31,8 @@ describe('CORS by Middleware', () => {
})
)
app.use('/api5/*', cors())
app.get('/api/abc', (c) => {
return c.json({ success: true })
})
@ -43,6 +45,9 @@ describe('CORS by Middleware', () => {
app.get('/api4/abc', (c) => {
return c.json({ success: true })
})
app.get('/api5/abc', () => {
return new Response(JSON.stringify({ success: true }))
})
it('GET default', async () => {
const res = await app.request('http://localhost/api/abc')
@ -130,4 +135,11 @@ describe('CORS by Middleware', () => {
res = await app.request(req)
expect(res.headers.get('Access-Control-Allow-Origin')).toBe('http://example.com')
})
it('With raw Response object', async () => {
const res = await app.request('http://localhost/api5/abc')
expect(res.headers.get('Access-Control-Allow-Origin')).toBe('*')
expect(res.headers.get('Vary')).toBeNull()
})
})