fix(context): Inherit current status if not specified (#2218)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* fix(context): Inherit current status if not specified * chore: denoify
authored by Taku Amano · committed by GitHub · parent 48c6ce9 · Browse files · 4 files·+65 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+65 added−2 removed
- tests:+63 added−0 removed
- other:+2 added−2 removed
Provenance
- Commit
06cb43a- Tree
f093c80- Author
Taku Amano <taku@taaas.jp>- Committer
GitHub <noreply@github.com>- Parent
48c6ce9
4 files changed+65 added−2 removed
modified:deno_dist/context.ts+1 added-1 removed
357 unmodified lines358 if (arg && typeof arg !== 'number') {359 const headers = setHeaders(new Headers(arg.headers), this.#preparedHeaders)360 return new Response(data, {361 headers,362- status: arg.status,362+ status: arg.status ?? this.#status,363 })364 }365 366 const status = typeof arg === 'number' ? arg : this.#status180 unmodified linesmodified:src/middleware/jsx-renderer/index.test.tsx+49 added
364 unmodified lines365 const res = await app.request('/')366 expect(res.status).toBe(200)367 expect(await res.text()).toBe('<!DOCTYPE html><div>Hi</div>')368 })369+370+ describe('keep context status', async () => {371+ it('Should keep context status', async () => {372+ const app = new Hono()373+ app.use(374+ '*',375+ jsxRenderer(({ children }) => {376+ return (377+ <html>378+ <body>{children}</body>379+ </html>380+ )381+ })382+ )383+ app.get('/', (c) => {384+ c.status(201)385+ return c.render(<h1>Hello</h1>, { title: 'Title' })386+ })387+ const res = await app.request('/')388+ expect(res).not.toBeNull()389+ expect(res.status).toBe(201)390+ expect(await res.text()).toBe('<!DOCTYPE html><html><body><h1>Hello</h1></body></html>')391+ })392+393+ it('Should keep context status with stream option', async () => {394+ const app = new Hono()395+ app.use(396+ '*',397+ jsxRenderer(398+ ({ children }) => {399+ return (400+ <html>401+ <body>{children}</body>402+ </html>403+ )404+ },405+ { stream: true }406+ )407+ )408+ app.get('/', (c) => {409+ c.status(201)410+ return c.render(<h1>Hello</h1>, { title: 'Title' })411+ })412+ const res = await app.request('/')413+ expect(res).not.toBeNull()414+ expect(res.status).toBe(201)415+ expect(await res.text()).toBe('<!DOCTYPE html><html><body><h1>Hello</h1></body></html>')416+ })417+ })418 })modified:src/context.test.ts+14 added
126 unmodified lines127 expect(c.res.headers.get('X-Custom4')).toBe('Message4')128 expect(c.res.status).toBe(201)129 })130 131+ it('Inherit current status if not specified', async () => {132+ c.status(201)133+ const res = c.newResponse('this is body', {134+ headers: {135+ 'x-custom3': 'Message3',136+ 'x-custom2': 'Message2-Override',137+ },138+ })139+ expect(res.headers.get('x-Custom2')).toBe('Message2-Override')140+ expect(res.headers.get('x-Custom3')).toBe('Message3')141+ expect(res.status).toBe(201)142+ expect(await res.text()).toBe('this is body')143+ })144+145 it('Should append the previous headers to new Response', () => {146 c.res.headers.set('x-Custom1', 'Message1')147 const res2 = new Response('foo2', {148 headers: {173 unmodified linesmodified:src/context.ts+1 added-1 removed
357 unmodified lines358 if (arg && typeof arg !== 'number') {359 const headers = setHeaders(new Headers(arg.headers), this.#preparedHeaders)360 return new Response(data, {361 headers,362- status: arg.status,362+ status: arg.status ?? this.#status,363 })364 }365 366 const status = typeof arg === 'number' ? arg : this.#status180 unmodified lines