Skip to content

Commit

Permalink
fix(context): Inherit current status if not specified (honojs#2218)
Browse files Browse the repository at this point in the history
* fix(context): Inherit current status if not specified

* chore: denoify
  • Loading branch information
usualoma authored Feb 15, 2024
1 parent 48c6ce9 commit 06cb43a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
2 changes: 1 addition & 1 deletion deno_dist/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}

Expand Down
14 changes: 14 additions & 0 deletions src/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down
2 changes: 1 addition & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}

Expand Down
49 changes: 49 additions & 0 deletions src/middleware/jsx-renderer/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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>')
})
})
})

0 comments on commit 06cb43a

Please sign in to comment.