Skip to content
Myr

fix(context): Set `content-type` with `c.text()` correctly (#1761)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(context): Set `content-type` with `c.text()` correctly * denoify

authored by Yusuke Wada · committed by GitHub · parent d34343c · Browse files · 3 files+30 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+30 added−10 removed

  • tests:+28 added−0 removed
  • other:+2 added−10 removed

3 files+30 added−10 removed

modified:deno_dist/context.ts+1 added-5 removed
310 unmodified lines
311 return new Response(text)312 }313 this._pH = {}314 }315 // If Content-Type is not set, we don't have to set `text/plain`.316 // Fewer the header values, it will be faster.317 if (this._pH['content-type']) {318 this._pH['content-type'] = TEXT_PLAIN315 this._pH['content-type'] = TEXT_PLAIN319 }316 return typeof arg === 'number'317 ? this.newResponse(text, arg, headers)318 : this.newResponse(text, arg)319 }
165 unmodified lines
modified:src/context.ts+1 added-5 removed
310 unmodified lines
311 return new Response(text)312 }313 this._pH = {}314 }315 // If Content-Type is not set, we don't have to set `text/plain`.316 // Fewer the header values, it will be faster.317 if (this._pH['content-type']) {318 this._pH['content-type'] = TEXT_PLAIN315 this._pH['content-type'] = TEXT_PLAIN319 }316 return typeof arg === 'number'317 ? this.newResponse(text, arg, headers)318 : this.newResponse(text, arg)319 }
165 unmodified lines
modified:src/hono.test.ts+28 added
736 unmodified lines
737 expect(await res.text()).toBe('/search')738 })739})740741describe('Header', () => {742 const app = new Hono()743744 app.get('/text', (c) => {745 return c.text('Hello')746 })747748 app.get('/text-with-custom-header', (c) => {749 c.header('X-Custom', 'Message')750 return c.text('Hello')751 })752753 it('Should return correct headers - /text', async () => {754 const res = await app.request('/text')755 expect(res.status).toBe(200)756 expect(res.headers.get('content-type')).toMatch(/^text\/plain/)757 expect(await res.text()).toBe('Hello')758 })759760 it('Should return correct headers - /text-with-custom-header', async () => {761 const res = await app.request('/text-with-custom-header')762 expect(res.status).toBe(200)763 expect(res.headers.get('x-custom')).toBe('Message')764 expect(res.headers.get('content-type')).toMatch(/^text\/plain/)765 expect(await res.text()).toBe('Hello')766 })767})768769describe('Middleware', () => {770 describe('Basic', () => {771 const app = new Hono()772
2006 unmodified lines