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:
parent
48c6ce9b6f
commit
06cb43aa8e
@ -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,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -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', {
|
||||
|
@ -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,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -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>')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user