Skip to content
Myr

refactor(context): remove unnecessary initialization add add tests for Context (#2949)

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

* refactor(context): remove unnecessary initialization add add tests * Don't initialize `_var` Co-authored-by: Taku Amano <taku@taaas.jp> ---------

authored by Yusuke Wada · committed by GitHub · parent ddb1d86 · Browse files · 2 files+93 added−6 removed
This commit

State

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

Trailers

  • Co-authored-by: Taku Amano <taku@taaas.jp>

Diff stats

2 files+93 added−6 removed

  • tests:+90 added−3 removed
  • other:+3 added−3 removed

2 files+93 added−6 removed

modified:src/context.test.ts+90 added-3 removed
41 unmodified lines
42 expect(await res.text()).toBe('<h1>Hello! Hono!</h1>')43 expect(res.headers.get('X-Custom')).toBe('Message')44 })4546 it('c.html() with async', async () => {47 const res = await c.html(48 new Promise((resolve) => setTimeout(() => resolve('<h1>Hello! Hono!</h1>'), 0)),49 201,50 {51 'X-Custom': 'Message',52 }53 )54 expect(res.status).toBe(201)55 expect(res.headers.get('Content-Type')).toMatch('text/html')56 expect(await res.text()).toBe('<h1>Hello! Hono!</h1>')57 expect(res.headers.get('X-Custom')).toBe('Message')58 })5960 it('c.redirect()', async () => {61 let res = c.redirect('/destination')62 expect(res.status).toBe(302)63 expect(res.headers.get('Location')).toBe('/destination')
16 unmodified lines
80 const foo = res.headers.get('X-Foo')81 expect(foo).toBe('Bar, Buzz')82 })8384 it('c.set() and c.get()', async () => {85 expect(c.get('foo')).toBe(undefined)86 c.set('foo', 'bar')87 expect(c.get('foo')).toBe('bar')88 expect(c.get('foo2')).toBe(undefined)89 })9091 it('c.notFound()', async () => {92 const res = c.notFound()93 expect(res).instanceOf(Response)94 })9596 it('Should set headers if already this.#headers is created by `c.header()`', async () => {97 c.header('X-Foo', 'Bar')98 c.header('X-Foo', 'Buzz', { append: true })99 const res = c.body('Hi', {
21 unmodified lines
121 res = c.res122 expect(res.headers.get('X-Foo2')).toBe(null)123 })124125 it('c.header() - clear the header when append is true', async () => {126 c.header('X-Foo', 'Bar', { append: true })127 c.header('X-Foo', undefined)128 expect(c.res.headers.get('X-Foo')).toBe(null)129 })130131 it('c.body() - multiple header', async () => {132 const res = c.body('Hi', 200, {133 'X-Foo': ['Bar', 'Buzz'],134 })
106 unmodified lines
241 expect(res.headers.get('foo')).toBe('bar')242 })243})244245describe('event and executionCtx', () => {246 const req = new HonoRequest(new Request('http://localhost/'))247248 it('Should return the event if accessing c.event', () => {249 const respondWith = vi.fn()250 const c = new Context(req, {251 // @ts-expect-error the type is not correct252 executionCtx: {253 respondWith: respondWith,254 },255 })256 expect(() => c.event).not.toThrowError()257 c.event.respondWith(new Response())258 expect(respondWith).toHaveBeenCalled()259 })260261 it('Should throw an error if accessing c.event', () => {262 const c = new Context(req)263 expect(() => c.event).toThrowError()264 })265266 it('Should return the executionCtx if accessing c.executionCtx', () => {267 const pathThroughOnException = vi.fn()268 const waitUntil = vi.fn()269 const c = new Context(req, {270 executionCtx: {271 passThroughOnException: pathThroughOnException,272 waitUntil: waitUntil,273 },274 env: {},275 })276 expect(() => c.executionCtx).not.toThrowError()277 c.executionCtx.passThroughOnException()278 expect(pathThroughOnException).toHaveBeenCalled()279 const asyncFunc = async () => {}280 c.executionCtx.waitUntil(asyncFunc())281 expect(waitUntil).toHaveBeenCalled()282 })283284 it('Should throw an error if accessing c.executionCtx', () => {285 const c = new Context(req)286 expect(() => c.executionCtx).toThrowError()287 })288})289290describe('Context header', () => {291 const req = new HonoRequest(new Request('http://localhost/'))292 let c: Context293 beforeEach(() => {294 c = new Context(req)295 })296297 it('Should return only one content-type value', async () => {298 c.header('Content-Type', 'foo')299 const res = await c.html('foo')300 expect(res.headers.get('Content-Type')).toBe('text/html; charset=UTF-8')301 })302303 it('Should rewrite header values correctly', async () => {304 c.res = await c.html('foo')305 const res = c.text('foo')306 expect(res.headers.get('Content-Type')).toMatch(/^text\/plain/)
9 unmodified lines
316 expect(res.headers.get('foo')).toBe('bar')317 })318319 it('Should set cookie headers when re-assigning Response to `c.res`', () => {241 c.res = new Response(null)320 const cookies = ['foo=bar; Path=/', 'foo2=bar2; Path=/']321 const res = new Response(null)243 res.headers.append('set-cookie', 'foo=bar; Path=/')244 res.headers.append('set-cookie', 'foo2=bar2; Path=/')322 res.headers.append('set-cookie', cookies[0])323 res.headers.append('set-cookie', cookies[1])324 c.res = res325 expect(c.res.headers.getSetCookie().length).toBe(2)326327 // Re-assign328 const newCookies = ['foo3=bar3; Path=/']329 const newResponse = new Response(null)330 newResponse.headers.append('set-cookie', newCookies[0])331 c.res = newResponse332 expect(c.res.headers.getSetCookie().length).toBe(cookies.length)333 expect(c.res.headers.getSetCookie()).toEqual(cookies)334 })335336 it('Should keep previous cookies in response headers', () => {337 c.res.headers.append('set-cookie', 'foo=bar; Path=/')
121 unmodified lines
modified:src/context.ts+3 added-3 removed
235 unmodified lines
236 * })237 * ```238 */239 env: E['Bindings'] = {}240 private _var: E['Variables'] = {}240 private _var: E['Variables'] | undefined241 finalized: boolean = false242 /**243 * `.error` can get the error object from the middleware if the Handler throws an error.244 *
221 unmodified lines
466 }467468 /**469 * `.set()` can set the value specified by the key.470 * 470 *471 * @see {@link https://hono.dev/api/context#set-get}472 * 472 *473 * @example474 * ```ts475 * app.use('*', async (c, next) => {476 * c.set('message', 'Hono is cool!!')
269 unmodified lines