Skip to content

Commit

Permalink
feat: fix UTF-8 issue, refactor tests, bump hono to 3.12.8
Browse files Browse the repository at this point in the history
  • Loading branch information
ariskemper committed Jan 30, 2024
1 parent de167f9 commit 6b63231
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 40 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"@types/supertest": "^2.0.12",
"@whatwg-node/fetch": "^0.9.14",
"eslint": "^8.55.0",
"hono": "3.12.7",
"hono": "^3.12.8",
"jest": "^29.6.1",
"np": "^7.7.0",
"publint": "^0.1.16",
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 @@ -53,7 +53,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
}
65 changes: 32 additions & 33 deletions test/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,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 @@ -51,7 +51,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 @@ -70,14 +70,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']).toEqual('text/plain;charset=UTF-8')
expect(res.headers['content-type']).toEqual('text/plain')
})

// it fails with hono 3.12.7
it.skip('Should return 200 response - GET /ponyfill', async () => {
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 +188,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 +234,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 +393,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 +428,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,18 +454,18 @@ 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!')
})
})

describe('HTTP2', () => {
const app = new Hono()
app.get('/', (c) => c.text('Hello! Node!'))
app.get('/headers', (c) => {
app.get('/headers', (c) => {
// call newRequestFromIncoming
c.req.header('Accept')
return c.text('Hello! Node!')
return c.text('Hello! Node!')
})
app.get('/url', (c) => c.text(c.req.url))

Expand All @@ -479,15 +478,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,7 +495,7 @@ 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')
})
})
Expand Down Expand Up @@ -530,8 +529,8 @@ describe('Hono compression default gzip', () => {
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 () => {
Expand All @@ -540,7 +539,7 @@ describe('Hono compression default gzip', () => {
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/)
expect(res.headers['content-encoding']).toMatch('gzip')
})

it('should return 500 Custom Error!', async () => {
Expand All @@ -549,7 +548,7 @@ describe('Hono compression default gzip', () => {
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/)
expect(res.headers['content-encoding']).toMatch('gzip')
})
})

Expand Down Expand Up @@ -582,8 +581,8 @@ describe('Hono compression deflate', () => {
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/)
expect(res.headers['content-type']).toMatch('text/plain')
expect(res.headers['content-encoding']).toMatch('deflate')
})

it('should return 404 Custom NotFound', async () => {
Expand All @@ -592,7 +591,7 @@ describe('Hono compression deflate', () => {
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/)
expect(res.headers['content-encoding']).toMatch('deflate')
})

it('should return 500 Custom Error!', async () => {
Expand All @@ -601,7 +600,7 @@ describe('Hono compression deflate', () => {
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/)
expect(res.headers['content-encoding']).toMatch('deflate')
})
})

Expand All @@ -621,6 +620,6 @@ 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')
})
})
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.12.7:
version "3.12.7"
resolved "https://registry.yarnpkg.com/hono/-/hono-3.12.7.tgz#76be7d8a2f43ef29bba47e663b1adfba264a3281"
integrity sha512-jfyIoE8D5I1PGj0tXAGqpQ2miAWBBuHFjJsWjh0hbBVludQ8QO4lF6KTbPvjMi4venp8Q6DyMoJ51CwP6L5LNA==
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 6b63231

Please sign in to comment.