conninfo.test.ts
This file
Path
adapters/bun/src/conninfo.test.ts
85 lines 路 2.4 KiB 路 TypeScript
Last changed by
feat(adapters): add @hono/bun as a workspace package (#5447)Yusuke Wadabefore adoptionfc2343d
History of this fileTake it
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586import { describe, expect, it } from 'bun:test'import { Context } from 'hono'import type { AddressType } from 'hono/conninfo'import { getConnInfo } from './conninfo'
const createRandomBunServer = ({ address = Math.random().toString(), port = Math.floor(Math.random() * (65535 + 1)), family = 'IPv6',}: { address?: string port?: number family?: AddressType | string} = {}) => { return { address, port, server: { requestIP() { return { address, family, port, } }, }, }}describe('getConnInfo', () => { it('Should info is valid', () => { const { port, server, address } = createRandomBunServer() const c = new Context(new Request('http://localhost/'), { env: server }) const info = getConnInfo(c)
expect(info.remote.port).toBe(port) expect(info.remote.address).toBe(address) expect(info.remote.addressType).toBe('IPv6') expect(info.remote.transport).toBeUndefined() }) it('Should getConnInfo works when env is { server: server }', () => { const { port, server, address } = createRandomBunServer() const c = new Context(new Request('http://localhost/'), { env: { server } })
const info = getConnInfo(c)
expect(info.remote.port).toBe(port) expect(info.remote.address).toBe(address) expect(info.remote.addressType).toBe('IPv6') expect(info.remote.transport).toBeUndefined() }) it('should return undefined when addressType is invalid string', () => { const { server } = createRandomBunServer({ family: 'invalid' }) const c = new Context(new Request('http://localhost/'), { env: { server } })
const info = getConnInfo(c)
expect(info.remote.addressType).toBeUndefined() }) it('Should throw error when user did not give server', () => { const c = new Context(new Request('http://localhost/'), { env: {} })
expect(() => getConnInfo(c)).toThrowError(TypeError) }) it('Should throw error when requestIP is not function', () => { const c = new Context(new Request('http://localhost/'), { env: { requestIP: 0, }, }) expect(() => getConnInfo(c)).toThrowError(TypeError) }) it('Should return empty remote when requestIP returns null', () => { const c = new Context(new Request('http://localhost/'), { env: { requestIP() { return null }, }, })
const info = getConnInfo(c)
expect(info.remote).toEqual({}) })})