fix(hono-base): custom not found with middleware like compress (#2220)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* fix(hono-base): custom not found with middleware like compress * denoify
authored by Yusuke Wada · committed by GitHub · parent 06cb43a · Browse files · 4 files·+59 added−8 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+59 added−8 removed
- tests:+53 added−0 removed
- other:+6 added−8 removed
Provenance
- Commit
89f8e07- Tree
245b383- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
06cb43a
4 files changed+59 added−8 removed
modified:deno_dist/hono-base.ts+3 added-4 removed
330 unmodified lines331 // Do not `compose` if it has only one handler332 if (matchResult[0].length === 1) {333 let res: ReturnType<H>334 try {335- res = matchResult[0][0][0][0](c, async () => {})336- if (!res) {337- return this.notFoundHandler(c)338- }335+ res = matchResult[0][0][0][0](c, async () => {336+ c.res = await this.notFoundHandler(c)337+ })338 } catch (err) {339 return this.handleError(err, c)340 }341 82 unmodified linesmodified:src/middleware/compress/index.test.ts+33 added
7 unmodified lines8910111213141516171839 unmodified lines58596061626364656667686970717273747576777879808182838485868788899091927 unmodified lines app.get('/hello', async (ctx) => { ctx.header('Content-Length', '5') return ctx.text('hello') }) app.notFound((c) => { return c.text('Custom NotFound', 404) })
it('gzip', async () => { const req = new Request('http://localhost/hello', { method: 'GET',39 unmodified lines expect(res.status).toBe(200) expect(res.headers.get('Content-Encoding')).toBeNull() expect(res.headers.get('Content-Length')).toBe('5') })
it('Should handle Custom 404 Not Found', async () => { const req = new Request('http://localhost/not-found', { method: 'GET', headers: new Headers({ 'Accept-Encoding': 'gzip' }), }) const res = await app.request(req) expect(res).not.toBeNull() expect(res.status).toBe(404) expect(res.headers.get('Content-Encoding')).toEqual('gzip')
// decompress response body const decompressionStream = new DecompressionStream('gzip') const decompressedStream = res.body!.pipeThrough(decompressionStream)
const textDecoder = new TextDecoder() const reader = decompressedStream.getReader() let text = ''
for (;;) { const { done, value } = await reader.read() if (done) { break } text += textDecoder.decode(value, { stream: true }) }
text += textDecoder.decode() expect(text).toBe('Custom NotFound') })})modified:src/hono-base.ts+3 added-4 removed
330 unmodified lines331 // Do not `compose` if it has only one handler332 if (matchResult[0].length === 1) {333 let res: ReturnType<H>334 try {335- res = matchResult[0][0][0][0](c, async () => {})336- if (!res) {337- return this.notFoundHandler(c)338- }335+ res = matchResult[0][0][0][0](c, async () => {336+ c.res = await this.notFoundHandler(c)337+ })338 } catch (err) {339 return this.handleError(err, c)340 }341 82 unmodified linesmodified:src/hono.test.ts+20 added
1256 unmodified lines1257 expect(res.status).toBe(404)1258 expect(await res.text()).toBe('404 Not Found')1259 })1260 })1261+1262+ describe('Custom 404 Not Found with a middleware like Compress Middleware', () => {1263+ const app = new Hono()1264+1265+ // Custom Middleware which creates a new Response object after `next()`.1266+ app.use('*', async (c, next) => {1267+ await next()1268+ c.res = new Response(await c.res.text(), c.res)1269+ })1270+1271+ app.notFound((c) => {1272+ return c.text('Custom NotFound', 404)1273+ })1274+1275+ it('Custom 404 Not Found', async () => {1276+ const res = await app.request('http://localhost/')1277+ expect(res.status).toBe(404)1278+ expect(await res.text()).toBe('Custom NotFound')1279+ })1280+ })1281 })1282 1283 describe('Redirect', () => {1284 const app = new Hono()2103 unmodified lines