fix: Fixed context headers (#69)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent ff5a83b · Browse files · 2 files·+25 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+25 added−6 removed
- tests:+19 added−0 removed
- other:+6 added−6 removed
Provenance
- Commit
3017a4a- Tree
da7dd67- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
ff5a83b
2 files changed+25 added−6 removed
modified:src/context.ts+6 added-6 removed
36 unmodified lines37383940414241424344454646474849505151525354554 unmodified lines606162636464656667685 unmodified lines7475767778787980818225 unmodified lines36 unmodified lines this._statusText = text }
newResponse(data: any, init: ResponseInit = {}): Response { init.status = this._status || init.status init.statusText = this._statusText || init.statusText init.status = init.status || this._status init.statusText = init.statusText || this._statusText
const Encoder = new TextEncoder() const length = data ? data.bytelength || Encoder.encode(data).byteLength : 0 init.headers = { ...init.headers, ...this._headers, ...{ 'Content-Length': String(length) } } init.headers = { ...this._headers, ...init.headers, ...{ 'Content-Length': String(length) } }
return new Response(data, init) }
text(text: string, status: number = 200, headers: Headers = {}): Response { text(text: string, status: number = this._status, headers: Headers = {}): Response { if (typeof text !== 'string') { throw new TypeError('text method arg must be a string!') }
4 unmodified lines headers: headers, }) }
json(object: object, status: number = 200, headers: Headers = {}): Response { json(object: object, status: number = this._status, headers: Headers = {}): Response { if (typeof object !== 'object') { throw new TypeError('json method arg must be a object!') }
5 unmodified lines headers: headers, }) }
html(html: string, status: number = 200, headers: Headers = {}): Response { html(html: string, status: number = this._status, headers: Headers = {}): Response { if (typeof html !== 'string') { throw new TypeError('html method arg must be a string!') } headers['Content-Type'] = 'text/html; charset=UTF-8'25 unmodified linesmodified:test/context.test.ts+19 added
69 unmodified lines70717273747576777879808182838485868788899091929369 unmodified lines expect(res.headers.get('Content-Length')).toBe('7') res = c.text('炎') expect(res.headers.get('Content-Length')).toBe('3') })
it('Headers', async () => { c.header('X-Custom1', 'Message1') c.header('X-Custom2', 'Message2') c.status(200) c.statusText('OK') const res = c.body('this is body', { status: 201, headers: { 'X-Custom3': 'Message3', 'X-Custom2': 'Message2-Override', }, }) expect(res.headers.get('X-Custom1')).toBe('Message1') 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') })})