0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-25 05:07:03 +01:00

fix(context): Inherit current status if not specified (#2218)

* fix(context): Inherit current status if not specified

* chore: denoify
This commit is contained in:
Taku Amano 2024-02-15 19:29:05 +09:00 committed by GitHub
parent 48c6ce9b6f
commit 06cb43aa8e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 65 additions and 2 deletions

View File

@ -359,7 +359,7 @@ export class Context<
const headers = setHeaders(new Headers(arg.headers), this.#preparedHeaders)
return new Response(data, {
headers,
status: arg.status,
status: arg.status ?? this.#status,
})
}

View File

@ -128,6 +128,20 @@ describe('Context', () => {
expect(c.res.status).toBe(201)
})
it('Inherit current status if not specified', async () => {
c.status(201)
const res = c.newResponse('this is body', {
headers: {
'x-custom3': 'Message3',
'x-custom2': 'Message2-Override',
},
})
expect(res.headers.get('x-Custom2')).toBe('Message2-Override')
expect(res.headers.get('x-Custom3')).toBe('Message3')
expect(res.status).toBe(201)
expect(await res.text()).toBe('this is body')
})
it('Should append the previous headers to new Response', () => {
c.res.headers.set('x-Custom1', 'Message1')
const res2 = new Response('foo2', {

View File

@ -359,7 +359,7 @@ export class Context<
const headers = setHeaders(new Headers(arg.headers), this.#preparedHeaders)
return new Response(data, {
headers,
status: arg.status,
status: arg.status ?? this.#status,
})
}

View File

@ -366,4 +366,53 @@ d.replaceWith(c.content)
expect(res.status).toBe(200)
expect(await res.text()).toBe('<!DOCTYPE html><div>Hi</div>')
})
describe('keep context status', async () => {
it('Should keep context status', async () => {
const app = new Hono()
app.use(
'*',
jsxRenderer(({ children }) => {
return (
<html>
<body>{children}</body>
</html>
)
})
)
app.get('/', (c) => {
c.status(201)
return c.render(<h1>Hello</h1>, { title: 'Title' })
})
const res = await app.request('/')
expect(res).not.toBeNull()
expect(res.status).toBe(201)
expect(await res.text()).toBe('<!DOCTYPE html><html><body><h1>Hello</h1></body></html>')
})
it('Should keep context status with stream option', async () => {
const app = new Hono()
app.use(
'*',
jsxRenderer(
({ children }) => {
return (
<html>
<body>{children}</body>
</html>
)
},
{ stream: true }
)
)
app.get('/', (c) => {
c.status(201)
return c.render(<h1>Hello</h1>, { title: 'Title' })
})
const res = await app.request('/')
expect(res).not.toBeNull()
expect(res.status).toBe(201)
expect(await res.text()).toBe('<!DOCTYPE html><html><body><h1>Hello</h1></body></html>')
})
})
})