Skip to content

Commit

Permalink
feat: bump hono to v3.12.8, add customer err tests for compress middl… (
Browse files Browse the repository at this point in the history
#130)

* feat: bump hono to v3.12.7, add customer err tests for compress middleware

* feat: fix UTF-8 issue, refactor tests, bump hono to 3.12.8

* fix: added prettier dev dep and formating script, code format

* updated yarn.lock with prettier

* refactor utils

* Revert "refactor utils"

This reverts commit 0885eab.

* fix: add caret range for prettier
  • Loading branch information
ariskemper authored Feb 1, 2024
1 parent 5490018 commit 3a9ec60
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 32 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"@types/supertest": "^2.0.12",
"@whatwg-node/fetch": "^0.9.14",
"eslint": "^8.55.0",
"hono": "^3.11.7",
"hono": "^3.12.8",
"jest": "^29.6.1",
"np": "^7.7.0",
"prettier": "^3.2.4",
Expand Down
2 changes: 1 addition & 1 deletion src/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class Response {
}

if (typeof body === 'string' || body instanceof ReadableStream) {
let headers = (init?.headers || { 'content-type': 'text/plain;charset=UTF-8' }) as
let headers = (init?.headers || { 'content-type': 'text/plain; charset=UTF-8' }) as
| Record<string, string>
| Headers
| OutgoingHttpHeaders
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const buildOutgoingHttpHeaders = (headers: Headers): OutgoingHttpHeaders
if (cookies.length > 0) {
res['set-cookie'] = cookies
}
res['content-type'] ??= 'text/plain;charset=UTF-8'
res['content-type'] ??= 'text/plain; charset=UTF-8'

return res
}
132 changes: 107 additions & 25 deletions test/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('Basic', () => {
it('Should return 200 response - GET /', async () => {
const res = await request(server).get('/')
expect(res.status).toBe(200)
expect(res.headers['content-type']).toMatch(/text\/plain/)
expect(res.headers['content-type']).toMatch('text/plain')
expect(res.text).toBe('Hello! Node!')
})

Expand All @@ -52,7 +52,7 @@ describe('Basic', () => {
it('Should return 200 response - GET /user-agent', async () => {
const res = await request(server).get('/user-agent').set('user-agent', 'Hono')
expect(res.status).toBe(200)
expect(res.headers['content-type']).toMatch(/text\/plain/)
expect(res.headers['content-type']).toMatch('text/plain')
expect(res.text).toBe('Hono')
})

Expand All @@ -71,13 +71,13 @@ describe('Basic', () => {
it('Should return 500 response - GET /invalid', async () => {
const res = await request(server).get('/invalid')
expect(res.status).toBe(500)
expect(res.headers['content-type']).toBe('text/plain')
expect(res.headers['content-type']).toEqual('text/plain')
})

it('Should return 200 response - GET /ponyfill', async () => {
const res = await request(server).get('/ponyfill')
expect(res.status).toBe(200)
expect(res.headers['content-type']).toMatch(/text\/plain/)
expect(res.headers['content-type']).toMatch('text/plain')
expect(res.text).toBe('Pony')
})
})
Expand Down Expand Up @@ -189,28 +189,28 @@ describe('Response body', () => {
it('Should return JSON body', async () => {
const res = await request(server).get('/json')
expect(res.status).toBe(200)
expect(res.headers['content-type']).toMatch(/application\/json/)
expect(res.headers['content-type']).toMatch('application/json')
expect(JSON.parse(res.text)).toEqual({ foo: 'bar' })
})

it('Should return JSON body from /json-async', async () => {
const res = await request(server).get('/json-async')
expect(res.status).toBe(200)
expect(res.headers['content-type']).toMatch(/application\/json/)
expect(res.headers['content-type']).toMatch('application/json')
expect(JSON.parse(res.text)).toEqual({ foo: 'async' })
})

it('Should return HTML', async () => {
const res = await request(server).get('/html')
expect(res.status).toBe(200)
expect(res.headers['content-type']).toMatch(/text\/html/)
expect(res.headers['content-type']).toMatch('text/html')
expect(res.text).toBe('<h1>Hello!</h1>')
})

it('Should return HTML from /html-async', async () => {
const res = await request(server).get('/html-async')
expect(res.status).toBe(200)
expect(res.headers['content-type']).toMatch(/text\/html/)
expect(res.headers['content-type']).toMatch('text/html')
expect(res.text).toBe('<h1>Hello!</h1>')
})
})
Expand All @@ -235,14 +235,14 @@ describe('Response body', () => {
it('Should return JSON body from /json-blob', async () => {
const res = await request(server).get('/json-blob')
expect(res.status).toBe(200)
expect(res.headers['content-type']).toMatch(/application\/json/)
expect(res.headers['content-type']).toMatch('application/json')
expect(JSON.parse(res.text)).toEqual({ foo: 'blob' })
})

it('Should return JSON body from /json-buffer', async () => {
const res = await request(server).get('/json-buffer')
expect(res.status).toBe(200)
expect(res.headers['content-type']).toMatch(/application\/json/)
expect(res.headers['content-type']).toMatch('application/json')
expect(JSON.parse(res.text)).toEqual({ foo: 'buffer' })
})
})
Expand Down Expand Up @@ -394,24 +394,24 @@ describe('Stream and non-stream response', () => {
const res = await request(server).get('/json')
expect(res.status).toBe(200)
expect(res.headers['content-length']).toMatch('13')
expect(res.headers['content-type']).toMatch(/application\/json/)
expect(res.headers['content-type']).toMatch('application/json')
expect(JSON.parse(res.text)).toEqual({ foo: 'bar' })
})

it('Should return text body', async () => {
const res = await request(server).get('/text')
expect(res.status).toBe(200)
expect(res.headers['content-length']).toMatch('6')
expect(res.headers['content-type']).toMatch(/text\/plain/)
expect(res.headers['content-type']).toMatch('text/plain')
expect(res.text).toBe('Hello!')
})

it('Should return JSON body - stream', async () => {
const res = await request(server).get('/json-stream')
expect(res.status).toBe(200)
expect(res.headers['content-length']).toBeUndefined()
expect(res.headers['content-type']).toMatch(/application\/json/)
expect(res.headers['transfer-encoding']).toMatch(/chunked/)
expect(res.headers['content-type']).toMatch('application/json')
expect(res.headers['transfer-encoding']).toMatch('chunked')
expect(JSON.parse(res.text)).toEqual({ foo: 'bar' })
})

Expand All @@ -429,8 +429,8 @@ describe('Stream and non-stream response', () => {
})
expect(res.status).toBe(200)
expect(res.headers['content-length']).toBeUndefined()
expect(res.headers['content-type']).toMatch(/text\/event-stream/)
expect(res.headers['transfer-encoding']).toMatch(/chunked/)
expect(res.headers['content-type']).toMatch('text/event-stream')
expect(res.headers['transfer-encoding']).toMatch('chunked')
})

it('Should return error - stream without app crashing', async () => {
Expand All @@ -455,7 +455,7 @@ describe('SSL', () => {
it('Should return 200 response - GET /', async () => {
const res = await request(server).get('/').trustLocalhost()
expect(res.status).toBe(200)
expect(res.headers['content-type']).toMatch(/text\/plain/)
expect(res.headers['content-type']).toMatch('text/plain')
expect(res.text).toBe('Hello! Node!')
})
})
Expand All @@ -479,15 +479,15 @@ describe('HTTP2', () => {
// @ts-expect-error: @types/supertest is not updated yet
const res = await request(server, { http2: true }).get('/').trustLocalhost()
expect(res.status).toBe(200)
expect(res.headers['content-type']).toMatch(/text\/plain/)
expect(res.headers['content-type']).toMatch('text/plain')
expect(res.text).toBe('Hello! Node!')
})

it('Should return 200 response - GET /headers', async () => {
// @ts-expect-error: @types/supertest is not updated yet
const res = await request(server, { http2: true }).get('/headers').trustLocalhost()
expect(res.status).toBe(200)
expect(res.headers['content-type']).toMatch(/text\/plain/)
expect(res.headers['content-type']).toMatch('text/plain')
expect(res.text).toBe('Hello! Node!')
})

Expand All @@ -496,15 +496,27 @@ describe('HTTP2', () => {
// @ts-expect-error: @types/supertest is not updated yet
const res = await request(server, { http2: true }).get('/url').trustLocalhost()
expect(res.status).toBe(200)
expect(res.headers['content-type']).toMatch(/text\/plain/)
expect(res.headers['content-type']).toMatch('text/plain')
expect(new URL(res.text).hostname).toBe('127.0.0.1')
})
})

describe('Hono compression', () => {
describe('Hono compression default gzip', () => {
const app = new Hono()
app.use('*', compress())

app.notFound((c) => {
return c.text('Custom NotFound', 404)
})

app.onError((_, c) => {
return c.text('Custom Error!', 500)
})

app.get('/error', () => {
throw new Error()
})

app.get('/one', async (c) => {
let body = 'one'

Expand All @@ -514,12 +526,82 @@ describe('Hono compression', () => {
return c.text(body)
})

it('Should return 200 response - GET /one', async () => {
it('should return 200 response - GET /one', async () => {
const server = createAdaptorServer(app)
const res = await request(server).get('/one')
expect(res.status).toBe(200)
expect(res.headers['content-type']).toMatch(/text\/plain/)
expect(res.headers['content-encoding']).toMatch(/gzip/)
expect(res.headers['content-type']).toMatch('text/plain')
expect(res.headers['content-encoding']).toMatch('gzip')
})

it('should return 404 Custom NotFound', async () => {
const server = createAdaptorServer(app)
const res = await request(server).get('/err')
expect(res.status).toBe(404)
expect(res.text).toEqual('Custom NotFound')
expect(res.headers['content-type']).toEqual('text/plain; charset=UTF-8')
expect(res.headers['content-encoding']).toMatch('gzip')
})

it('should return 500 Custom Error!', async () => {
const server = createAdaptorServer(app)
const res = await request(server).get('/error')
expect(res.status).toBe(500)
expect(res.text).toEqual('Custom Error!')
expect(res.headers['content-type']).toEqual('text/plain; charset=UTF-8')
expect(res.headers['content-encoding']).toMatch('gzip')
})
})

describe('Hono compression deflate', () => {
const app = new Hono()
app.use('*', compress({ encoding: 'deflate' }))

app.notFound((c) => {
return c.text('Custom NotFound', 404)
})

app.onError((_, c) => {
return c.text('Custom Error!', 500)
})

app.get('/error', () => {
throw new Error()
})

app.get('/one', async (c) => {
let body = 'one'

for (let index = 0; index < 1000 * 1000; index++) {
body += ' one'
}
return c.text(body)
})

it('should return 200 response - GET /one', async () => {
const server = createAdaptorServer(app)
const res = await request(server).get('/one')
expect(res.status).toBe(200)
expect(res.headers['content-type']).toMatch('text/plain')
expect(res.headers['content-encoding']).toMatch('deflate')
})

it('should return 404 Custom NotFound', async () => {
const server = createAdaptorServer(app)
const res = await request(server).get('/err')
expect(res.status).toBe(404)
expect(res.text).toEqual('Custom NotFound')
expect(res.headers['content-type']).toEqual('text/plain; charset=UTF-8')
expect(res.headers['content-encoding']).toMatch('deflate')
})

it('should return 500 Custom Error!', async () => {
const server = createAdaptorServer(app)
const res = await request(server).get('/error')
expect(res.status).toBe(500)
expect(res.text).toEqual('Custom Error!')
expect(res.headers['content-type']).toEqual('text/plain; charset=UTF-8')
expect(res.headers['content-encoding']).toMatch('deflate')
})
})

Expand All @@ -539,7 +621,7 @@ describe('set child response to c.res', () => {
const server = createAdaptorServer(app)
const res = await request(server).get('/json')
expect(res.status).toBe(200)
expect(res.headers['content-type']).toMatch(/application\/json/)
expect(res.headers['content-type']).toMatch('application/json')
})
})

Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2924,10 +2924,10 @@ hexoid@^1.0.0:
resolved "https://registry.yarnpkg.com/hexoid/-/hexoid-1.0.0.tgz#ad10c6573fb907de23d9ec63a711267d9dc9bc18"
integrity sha512-QFLV0taWQOZtvIRIAdBChesmogZrtuXvVWsFHZTk2SU+anspqZ2vMnoLg7IE1+Uk16N19APic1BuF8bC8c2m5g==

hono@^3.11.7:
version "3.11.7"
resolved "https://registry.yarnpkg.com/hono/-/hono-3.11.7.tgz#e44f8aa3a18f19775304328bde6f559f7a02447f"
integrity sha512-TcfAq7IdipF+9coxnuzYlSSBXbm9mTyWjjagLCv/2ampboNcKJdi+XCK5G48mHQtpI5+9Rj3J4FfcGgw9vzIww==
hono@^3.12.8:
version "3.12.8"
resolved "https://registry.yarnpkg.com/hono/-/hono-3.12.8.tgz#7c137aa6ac7bcd2aec3f55b9596d71e97081963a"
integrity sha512-vnOEIRdqsp4uHE/dkOBr9EYmTsR86sD/FyG2xhfAQzR9udDRglN1nuO7SGc/7U3HfSorc6PSCNGN6upnVtCmfg==

hosted-git-info@^2.1.4:
version "2.8.9"
Expand Down

0 comments on commit 3a9ec60

Please sign in to comment.