index.test.ts
This file
Path
runtime-tests/node/index.test.ts
301 lines · 8.0 KiB · TypeScript
Last changed by
test: support `alg` option for JWT middleware (#4624)Yusuke Wadabefore adoptiona48ef18
History of this fileTake it
The walk stopped after 200 commits: lines older than 82bdc4d point at it.
4.9.0Yusuke Wadabefore adoption82bdc4d
line 1why, one step back1import { createAdaptorServer, serve } from '@hono/node-server'chore: replace supertest with undici (#4365)Jonathan Hainesbefore adoption67f2c8d
lines 2–3why, one step back2import * as undici from 'undici'3import { once } from 'node:events'4.9.0Yusuke Wadabefore adoption82bdc4d
lines 4–26why, one step back4import type { Server } from 'node:http'5import type { AddressInfo } from 'node:net'6import { Hono } from '../../src'7import { Context } from '../../src/context'8import { env, getRuntimeKey } from '../../src/helper/adapter'9import { stream, streamSSE } from '../../src/helper/streaming'10import { basicAuth } from '../../src/middleware/basic-auth'11import { compress } from '../../src/middleware/compress'12import { jwt } from '../../src/middleware/jwt'1314// Test only minimal patterns.15// See <https://github.com/honojs/node-server> for more tests and information.1617describe('Basic', () => {18 const app = new Hono()1920 app.get('/', (c) => {21 return c.text('Hello! Node.js!')22 })23 app.get('/runtime-name', (c) => {24 return c.text(getRuntimeKey())25 })26chore: replace supertest with undici (#4365)Jonathan Hainesbefore adoption67f2c8d
line 27why, one step back27 const agent = createAgent(app)4.9.0Yusuke Wadabefore adoption82bdc4d
lines 28–29why, one step back2829 it('Should return 200 response', async () => {chore: replace supertest with undici (#4365)Jonathan Hainesbefore adoption67f2c8d
line 30why, one step back30 const res = await agent.get('/')4.9.0Yusuke Wadabefore adoption82bdc4d
line 31why, one step back31 expect(res.status).toBe(200)chore: replace supertest with undici (#4365)Jonathan Hainesbefore adoption67f2c8d
line 32why, one step back32 await expect(res.text()).resolves.toBe('Hello! Node.js!')4.9.0Yusuke Wadabefore adoption82bdc4d
lines 33–35why, one step back33 })3435 it('Should return correct runtime name', async () => {chore: replace supertest with undici (#4365)Jonathan Hainesbefore adoption67f2c8d
line 36why, one step back36 const res = await agent.get('/runtime-name')4.9.0Yusuke Wadabefore adoption82bdc4d
line 37why, one step back37 expect(res.status).toBe(200)chore: replace supertest with undici (#4365)Jonathan Hainesbefore adoption67f2c8d
line 38why, one step back38 await expect(res.text()).resolves.toBe('node')4.9.0Yusuke Wadabefore adoption82bdc4d
lines 39–64why, one step back39 })40})4142describe('Environment Variables', () => {43 it('Should return the environment variable', async () => {44 const c = new Context(new Request('http://localhost/'))45 const { NAME } = env<{ NAME: string }>(c)46 expect(NAME).toBe('Node')47 })48})4950describe('Basic Auth Middleware', () => {51 const app = new Hono()5253 const username = 'hono-user-a'54 const password = 'hono-password-a'55 app.use(56 '/auth/*',57 basicAuth({58 username,59 password,60 })61 )6263 app.get('/auth/*', () => new Response('auth'))64chore: replace supertest with undici (#4365)Jonathan Hainesbefore adoption67f2c8d
line 65why, one step back65 const agent = createAgent(app)4.9.0Yusuke Wadabefore adoption82bdc4d
lines 66–67why, one step back6667 it('Should not authorize, return 401 Response', async () => {chore: replace supertest with undici (#4365)Jonathan Hainesbefore adoption67f2c8d
line 68why, one step back68 const res = await agent.get('/auth/a')4.9.0Yusuke Wadabefore adoption82bdc4d
line 69why, one step back69 expect(res.status).toBe(401)chore: replace supertest with undici (#4365)Jonathan Hainesbefore adoption67f2c8d
line 70why, one step back70 await expect(res.text()).resolves.toBe('Unauthorized')4.9.0Yusuke Wadabefore adoption82bdc4d
lines 71–74why, one step back71 })7273 it('Should authorize, return 200 Response', async () => {74 const credential = 'aG9uby11c2VyLWE6aG9uby1wYXNzd29yZC1h'chore: replace supertest with undici (#4365)Jonathan Hainesbefore adoption67f2c8d
line 75why, one step back75 const res = await agent.get('/auth/a', { headers: { Authorization: `Basic ${credential}` } })4.9.0Yusuke Wadabefore adoption82bdc4d
line 76why, one step back76 expect(res.status).toBe(200)chore: replace supertest with undici (#4365)Jonathan Hainesbefore adoption67f2c8d
line 77why, one step back77 await expect(res.text()).resolves.toBe('auth')4.9.0Yusuke Wadabefore adoption82bdc4d
lines 78–83why, one step back78 })79})8081describe('JWT Auth Middleware', () => {82 const app = new Hono()83test: support `alg` option for JWT middleware (#4624)Yusuke Wadabefore adoptiona48ef18
line 84why, one step back84 app.use('/jwt/*', jwt({ secret: 'a-secret', alg: 'HS256' }))4.9.0Yusuke Wadabefore adoption82bdc4d
lines 85–86why, one step back85 app.get('/jwt/a', (c) => c.text('auth'))86chore: replace supertest with undici (#4365)Jonathan Hainesbefore adoption67f2c8d
line 87why, one step back87 const agent = createAgent(app)4.9.0Yusuke Wadabefore adoption82bdc4d
lines 88–89why, one step back8889 it('Should not authorize, return 401 Response', async () => {chore: replace supertest with undici (#4365)Jonathan Hainesbefore adoption67f2c8d
line 90why, one step back90 const res = await agent.get('/jwt/a')4.9.0Yusuke Wadabefore adoption82bdc4d
line 91why, one step back91 expect(res.status).toBe(401)chore: replace supertest with undici (#4365)Jonathan Hainesbefore adoption67f2c8d
line 92why, one step back92 await expect(res.text()).resolves.toBe('Unauthorized')4.9.0Yusuke Wadabefore adoption82bdc4d
lines 93–97why, one step back93 })9495 it('Should authorize, return 200 Response', async () => {96 const credential =97 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiaGVsbG8gd29ybGQifQ.B54pAqIiLbu170tGQ1rY06Twv__0qSHTA0ioQPIOvFE'chore: replace supertest with undici (#4365)Jonathan Hainesbefore adoption67f2c8d
line 98why, one step back98 const res = await agent.get('/jwt/a', { headers: { Authorization: `Bearer ${credential}` } })4.9.0Yusuke Wadabefore adoption82bdc4d
line 99why, one step back99 expect(res.status).toBe(200)chore: replace supertest with undici (#4365)Jonathan Hainesbefore adoption67f2c8d
line 100why, one step back100 await expect(res.text()).resolves.toBe('auth')4.9.0Yusuke Wadabefore adoption82bdc4d
lines 101–127why, one step back101 })102})103104describe('stream', () => {105 const app = new Hono()106107 let aborted = false108109 app.get('/stream', (c) => {110 return stream(c, async (stream) => {111 stream.onAbort(() => {112 aborted = true113 })114 return new Promise<void>((resolve) => {115 stream.onAbort(resolve)116 })117 })118 })119 app.get('/streamHello', (c) => {120 return stream(c, async (stream) => {121 stream.onAbort(() => {122 aborted = true123 })124 await stream.write('Hello')125 })126 })127chore: replace supertest with undici (#4365)Jonathan Hainesbefore adoption67f2c8d
line 128why, one step back128 const agent = createAgent(app)4.9.0Yusuke Wadabefore adoption82bdc4d
lines 129–134why, one step back129130 beforeEach(() => {131 aborted = false132 })133134 it('Should call onAbort', async () => {chore: replace supertest with undici (#4365)Jonathan Hainesbefore adoption67f2c8d
lines 135–138why, one step back135 const controller = new AbortController()136 const res = expect(agent.get('/stream', { signal: controller.signal })).rejects.toThrow(137 'This operation was aborted'138 )4.9.0Yusuke Wadabefore adoption82bdc4d
lines 139–141why, one step back139140 expect(aborted).toBe(false)141 await new Promise((resolve) => setTimeout(resolve, 10))chore: replace supertest with undici (#4365)Jonathan Hainesbefore adoption67f2c8d
lines 142–143why, one step back142 controller.abort()143 await res4.9.0Yusuke Wadabefore adoption82bdc4d
lines 144–151why, one step back144 while (!aborted) {145 await new Promise((resolve) => setTimeout(resolve))146 }147 expect(aborted).toBe(true)148 })149150 it('Should not be called onAbort if already closed', async () => {151 expect(aborted).toBe(false)chore: replace supertest with undici (#4365)Jonathan Hainesbefore adoption67f2c8d
line 152why, one step back152 const res = await agent.get('/streamHello')4.9.0Yusuke Wadabefore adoption82bdc4d
line 153why, one step back153 expect(res.status).toBe(200)chore: replace supertest with undici (#4365)Jonathan Hainesbefore adoption67f2c8d
line 154why, one step back154 await expect(res.text()).resolves.toBe('Hello')4.9.0Yusuke Wadabefore adoption82bdc4d
lines 155–182why, one step back155 expect(aborted).toBe(false)156 })157})158159describe('streamSSE', () => {160 const app = new Hono()161162 let aborted = false163164 app.get('/stream', (c) => {165 return streamSSE(c, async (stream) => {166 stream.onAbort(() => {167 aborted = true168 })169 return new Promise<void>((resolve) => {170 stream.onAbort(resolve)171 })172 })173 })174 app.get('/streamHello', (c) => {175 return streamSSE(c, async (stream) => {176 stream.onAbort(() => {177 aborted = true178 })179 await stream.write('Hello')180 })181 })182chore: replace supertest with undici (#4365)Jonathan Hainesbefore adoption67f2c8d
line 183why, one step back183 const agent = createAgent(app)4.9.0Yusuke Wadabefore adoption82bdc4d
lines 184–189why, one step back184185 beforeEach(() => {186 aborted = false187 })188189 it('Should call onAbort', async () => {chore: replace supertest with undici (#4365)Jonathan Hainesbefore adoption67f2c8d
lines 190–193why, one step back190 const controller = new AbortController()191 const res = expect(agent.get('/stream', { signal: controller.signal })).rejects.toThrow(192 'This operation was aborted'193 )4.9.0Yusuke Wadabefore adoption82bdc4d
lines 194–196why, one step back194195 expect(aborted).toBe(false)196 await new Promise((resolve) => setTimeout(resolve, 10))chore: replace supertest with undici (#4365)Jonathan Hainesbefore adoption67f2c8d
lines 197–198why, one step back197 controller.abort()198 await res4.9.0Yusuke Wadabefore adoption82bdc4d
lines 199–206why, one step back199 while (!aborted) {200 await new Promise((resolve) => setTimeout(resolve))201 }202 expect(aborted).toBe(true)203 })204205 it('Should not be called onAbort if already closed', async () => {206 expect(aborted).toBe(false)chore: replace supertest with undici (#4365)Jonathan Hainesbefore adoption67f2c8d
line 207why, one step back207 const res = await agent.get('/streamHello')4.9.0Yusuke Wadabefore adoption82bdc4d
line 208why, one step back208 expect(res.status).toBe(200)chore: replace supertest with undici (#4365)Jonathan Hainesbefore adoption67f2c8d
line 209why, one step back209 await expect(res.text()).resolves.toBe('Hello')4.9.0Yusuke Wadabefore adoption82bdc4d
lines 210–241why, one step back210 expect(aborted).toBe(false)211 })212})213214describe('compress', async () => {215 const cssContent = Array.from({ length: 60 }, () => 'body { color: red; }').join('\n')216 const [externalServer, serverInfo] = await new Promise<[Server, AddressInfo]>((resolve) => {217 const externalApp = new Hono()218 externalApp.get('/style.css', (c) =>219 c.text(cssContent, {220 headers: {221 'Content-Type': 'text/css',222 },223 })224 )225 const server = serve(226 {227 fetch: externalApp.fetch,228 port: 0,229 hostname: '0.0.0.0',230 },231 (serverInfo) => {232 resolve([server as Server, serverInfo])233 }234 )235 })236237 const app = new Hono()238 app.use(compress())239 app.get('/fetch/:file', (c) => {240 return fetch(`http://${serverInfo.address}:${serverInfo.port}/${c.req.param('file')}`)241 })chore: replace supertest with undici (#4365)Jonathan Hainesbefore adoption67f2c8d
line 242why, one step back242 const agent = createAgent(app)4.9.0Yusuke Wadabefore adoption82bdc4d
lines 243–248why, one step back243244 afterAll(() => {245 externalServer.close()246 })247248 it('Should be compressed a fetch response', async () => {chore: replace supertest with undici (#4365)Jonathan Hainesbefore adoption67f2c8d
line 249why, one step back249 const res = await agent.get('/fetch/style.css')4.9.0Yusuke Wadabefore adoption82bdc4d
line 250why, one step back250 expect(res.status).toBe(200)chore: replace supertest with undici (#4365)Jonathan Hainesbefore adoption67f2c8d
lines 251–252why, one step back251 expect(res.headers.get('content-encoding')).toBe('gzip')252 await expect(res.text()).resolves.toBe(cssContent)4.9.0Yusuke Wadabefore adoption82bdc4d
lines 253–264why, one step back253 })254})255256describe('Buffers', () => {257 const app = new Hono()258 .get('/', async (c) => {259 return c.body(Buffer.from('hello'))260 })261 .get('/uint8array', async (c) => {262 return c.body(Uint8Array.from('hello'.split(''), (c) => c.charCodeAt(0)))263 })264chore: replace supertest with undici (#4365)Jonathan Hainesbefore adoption67f2c8d
line 265why, one step back265 const agent = createAgent(app)4.9.0Yusuke Wadabefore adoption82bdc4d
lines 266–267why, one step back266267 it('should allow returning buffers', async () => {chore: replace supertest with undici (#4365)Jonathan Hainesbefore adoption67f2c8d
line 268why, one step back268 const res = await agent.get('/')4.9.0Yusuke Wadabefore adoption82bdc4d
line 269why, one step back269 expect(res.status).toBe(200)chore: replace supertest with undici (#4365)Jonathan Hainesbefore adoption67f2c8d
line 270why, one step back270 await expect(res.text()).resolves.toBe('hello')4.9.0Yusuke Wadabefore adoption82bdc4d
lines 271–273why, one step back271 })272273 it('should allow returning uint8array as well', async () => {chore: replace supertest with undici (#4365)Jonathan Hainesbefore adoption67f2c8d
line 274why, one step back274 const res = await agent.get('/uint8array')4.9.0Yusuke Wadabefore adoption82bdc4d
line 275why, one step back275 expect(res.status).toBe(200)chore: replace supertest with undici (#4365)Jonathan Hainesbefore adoption67f2c8d
line 276why, one step back276 await expect(res.text()).resolves.toBe('hello')4.9.0Yusuke Wadabefore adoption82bdc4d
lines 277–278why, one step back277 })278})chore: replace supertest with undici (#4365)Jonathan Hainesbefore adoption67f2c8d
lines 279–301why, one step back279280function createAgent(app: Hono) {281 const server = createAdaptorServer(app)282 const listening = once(server.listen(), 'listening')283284 return {285 async get(path: string, init?: undici.RequestInit) {286 await listening287 const url = new URL(path, getOrigin())288 return undici.fetch(url, init)289 },290 }291292 function getOrigin(): string {293 let address = server.address()294295 if (typeof address === 'object') {296 address = address?.port ? `http://localhost:${address.port}` : 'http://localhost'297 }298299 return address300 }301}