Skip to content
Myr

fix: enable to return Response like object (#2085)

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

* fix: enable to return Response like object This improves behavior, especially in cases where Response is being overwritten on a node-server. * chore: denoify

authored by Taku Amano · committed by GitHub · parent b23cf8d · Browse files · 3 files+220 added−71 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+220 added−71 removed

  • tests:+204 added−55 removed
  • other:+16 added−16 removed

3 files+220 added−71 removed

modified:deno_dist/hono-base.ts+8 added-8 removed
313 unmodified lines
314 } catch (err) {315 return this.handleError(err, c)316 }317318 if (res instanceof Response) return res319320 return res321 .then(322 (resolved: Response | undefined) =>323 resolved || (c.finalized ? c.res : this.notFoundHandler(c))324 )325 .catch((err: Error) => this.handleError(err, c))318 return res instanceof Promise319 ? res320 .then(321 (resolved: Response | undefined) =>322 resolved || (c.finalized ? c.res : this.notFoundHandler(c))323 )324 .catch((err: Error) => this.handleError(err, c))325 : res326 }327328 const composed = compose<Context>(matchResult[0], this.errorHandler, this.notFoundHandler)329
56 unmodified lines
modified:src/hono-base.ts+8 added-8 removed
313 unmodified lines
314 } catch (err) {315 return this.handleError(err, c)316 }317318 if (res instanceof Response) return res319320 return res321 .then(322 (resolved: Response | undefined) =>323 resolved || (c.finalized ? c.res : this.notFoundHandler(c))324 )325 .catch((err: Error) => this.handleError(err, c))318 return res instanceof Promise319 ? res320 .then(321 (resolved: Response | undefined) =>322 resolved || (c.finalized ? c.res : this.notFoundHandler(c))323 )324 .catch((err: Error) => this.handleError(err, c))325 : res326 }327328 const composed = compose<Context>(matchResult[0], this.errorHandler, this.notFoundHandler)329
56 unmodified lines
modified:src/hono.test.ts+204 added-55 removed
18 unmodified lines
19 throw new Error(errorMessage)20}2122describe('GET Request', () => {23 const app = new Hono()23 describe('without middleware', () => {24 // In other words, this is a test for cases that do not use `compose()`2526 const app = new Hono()2725 app.get('/hello', async () => {26 return new Response('hello', {27 status: 200,28 statusText: 'Hono is OK',28 app.get('/hello', async () => {29 return new Response('hello', {30 status: 200,31 statusText: 'Hono is OK',32 })33 })30 })3432 app.get('/hello-with-shortcuts', (c) => {33 c.header('X-Custom', 'This is Hono')34 c.status(201)35 return c.html('<h1>Hono!!!</h1>')36 })35 app.get('/hello-with-shortcuts', (c) => {36 c.header('X-Custom', 'This is Hono')37 c.status(201)38 return c.html('<h1>Hono!!!</h1>')39 })4038 app.get('/hello-env', (c) => {39 return c.json(c.env)40 })41 app.get('/hello-env', (c) => {42 return c.json(c.env)43 })4442 it('GET http://localhost/hello is ok', async () => {43 const res = await app.request('http://localhost/hello')44 expect(res).not.toBeNull()45 expect(res.status).toBe(200)46 expect(res.statusText).toBe('Hono is OK')47 expect(await res.text()).toBe('hello')48 })45 app.get(46 '/proxy-object',47 () =>48 new Proxy(new Response('proxy'), {49 get(target, prop: keyof Response) {50 return target[prop]51 },52 })53 )5450 it('GET httphello is ng', async () => {51 const res = await app.request('httphello')52 expect(res.status).toBe(404)53 })55 app.get(56 '/async-proxy-object',57 async () =>58 new Proxy(new Response('proxy'), {59 get(target, prop: keyof Response) {60 return target[prop]61 },62 })63 )6455 it('GET /hello is ok', async () => {56 const res = await app.request('/hello')57 expect(res).not.toBeNull()58 expect(res.status).toBe(200)59 expect(res.statusText).toBe('Hono is OK')60 expect(await res.text()).toBe('hello')61 })65 it('GET http://localhost/hello is ok', async () => {66 const res = await app.request('http://localhost/hello')67 expect(res).not.toBeNull()68 expect(res.status).toBe(200)69 expect(res.statusText).toBe('Hono is OK')70 expect(await res.text()).toBe('hello')71 })7263 it('GET hello is ok', async () => {64 const res = await app.request('hello')65 expect(res).not.toBeNull()66 expect(res.status).toBe(200)67 expect(res.statusText).toBe('Hono is OK')68 expect(await res.text()).toBe('hello')69 })73 it('GET httphello is ng', async () => {74 const res = await app.request('httphello')75 expect(res.status).toBe(404)76 })7771 it('GET /hello-with-shortcuts is ok', async () => {72 const res = await app.request('http://localhost/hello-with-shortcuts')73 expect(res).not.toBeNull()74 expect(res.status).toBe(201)75 expect(res.headers.get('X-Custom')).toBe('This is Hono')76 expect(res.headers.get('Content-Type')).toMatch(/text\/html/)77 expect(await res.text()).toBe('<h1>Hono!!!</h1>')78 })78 it('GET /hello is ok', async () => {79 const res = await app.request('/hello')80 expect(res).not.toBeNull()81 expect(res.status).toBe(200)82 expect(res.statusText).toBe('Hono is OK')83 expect(await res.text()).toBe('hello')84 })8580 it('GET / is not found', async () => {81 const res = await app.request('http://localhost/')82 expect(res).not.toBeNull()83 expect(res.status).toBe(404)86 it('GET hello is ok', async () => {87 const res = await app.request('hello')88 expect(res).not.toBeNull()89 expect(res.status).toBe(200)90 expect(res.statusText).toBe('Hono is OK')91 expect(await res.text()).toBe('hello')92 })9394 it('GET /hello-with-shortcuts is ok', async () => {95 const res = await app.request('http://localhost/hello-with-shortcuts')96 expect(res).not.toBeNull()97 expect(res.status).toBe(201)98 expect(res.headers.get('X-Custom')).toBe('This is Hono')99 expect(res.headers.get('Content-Type')).toMatch(/text\/html/)100 expect(await res.text()).toBe('<h1>Hono!!!</h1>')101 })102103 it('GET / is not found', async () => {104 const res = await app.request('http://localhost/')105 expect(res).not.toBeNull()106 expect(res.status).toBe(404)107 })108109 it('GET /hello-env is ok', async () => {110 const res = await app.request('/hello-env', undefined, { HELLO: 'world' })111 expect(res.status).toBe(200)112 expect(await res.json()).toEqual({ HELLO: 'world' })113 })114115 it('GET /proxy-object is ok', async () => {116 const res = await app.request('/proxy-object')117 expect(res).not.toBeNull()118 expect(res.status).toBe(200)119 expect(await res.text()).toBe('proxy')120 })121122 it('GET /async-proxy-object is ok', async () => {123 const res = await app.request('/proxy-object')124 expect(res).not.toBeNull()125 expect(res.status).toBe(200)126 expect(await res.text()).toBe('proxy')127 })128 })129130 describe('with middleware', () => {131 // when using `compose()`132133 const app = new Hono()134135 app.use('*', async (ctx, next) => {136 await next()137 })138139 app.get('/hello', async () => {140 return new Response('hello', {141 status: 200,142 statusText: 'Hono is OK',143 })144 })145146 app.get('/hello-with-shortcuts', (c) => {147 c.header('X-Custom', 'This is Hono')148 c.status(201)149 return c.html('<h1>Hono!!!</h1>')150 })151152 app.get('/hello-env', (c) => {153 return c.json(c.env)154 })155156 app.get(157 '/proxy-object',158 () =>159 new Proxy(new Response('proxy'), {160 get(target, prop: keyof Response) {161 return target[prop]162 },163 })164 )165166 app.get(167 '/async-proxy-object',168 async () =>169 new Proxy(new Response('proxy'), {170 get(target, prop: keyof Response) {171 return target[prop]172 },173 })174 )175176 it('GET http://localhost/hello is ok', async () => {177 const res = await app.request('http://localhost/hello')178 expect(res).not.toBeNull()179 expect(res.status).toBe(200)180 expect(res.statusText).toBe('Hono is OK')181 expect(await res.text()).toBe('hello')182 })183184 it('GET httphello is ng', async () => {185 const res = await app.request('httphello')186 expect(res.status).toBe(404)187 })188189 it('GET /hello is ok', async () => {190 const res = await app.request('/hello')191 expect(res).not.toBeNull()192 expect(res.status).toBe(200)193 expect(res.statusText).toBe('Hono is OK')194 expect(await res.text()).toBe('hello')195 })196197 it('GET hello is ok', async () => {198 const res = await app.request('hello')199 expect(res).not.toBeNull()200 expect(res.status).toBe(200)201 expect(res.statusText).toBe('Hono is OK')202 expect(await res.text()).toBe('hello')203 })204205 it('GET /hello-with-shortcuts is ok', async () => {206 const res = await app.request('http://localhost/hello-with-shortcuts')207 expect(res).not.toBeNull()208 expect(res.status).toBe(201)209 expect(res.headers.get('X-Custom')).toBe('This is Hono')210 expect(res.headers.get('Content-Type')).toMatch(/text\/html/)211 expect(await res.text()).toBe('<h1>Hono!!!</h1>')212 })213214 it('GET / is not found', async () => {215 const res = await app.request('http://localhost/')216 expect(res).not.toBeNull()217 expect(res.status).toBe(404)218 })21986 it('GET /hello-env is ok', async () => {87 const res = await app.request('/hello-env', undefined, { HELLO: 'world' })88 expect(res.status).toBe(200)89 expect(await res.json()).toEqual({ HELLO: 'world' })220 it('GET /hello-env is ok', async () => {221 const res = await app.request('/hello-env', undefined, { HELLO: 'world' })222 expect(res.status).toBe(200)223 expect(await res.json()).toEqual({ HELLO: 'world' })224 })225226 it('GET /proxy-object is ok', async () => {227 const res = await app.request('/proxy-object')228 expect(res).not.toBeNull()229 expect(res.status).toBe(200)230 expect(await res.text()).toBe('proxy')231 })232233 it('GET /async-proxy-object is ok', async () => {234 const res = await app.request('/proxy-object')235 expect(res).not.toBeNull()236 expect(res.status).toBe(200)237 expect(await res.text()).toBe('proxy')238 })239 })240})241242describe('Register handlers without a path', () => {
3208 unmodified lines