Skip to content
Myr

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 file

Take it

feat(adapters): add @hono/bun as a workspace package (#5447)Yusuke Wadabefore adoptionfc2343d

lines 1–85why, one step back
1import { describe, expect, it } from 'bun:test'
2import { Context } from 'hono'3import type { AddressType } from 'hono/conninfo'4import { getConnInfo } from './conninfo'56const createRandomBunServer = ({7 address = Math.random().toString(),8 port = Math.floor(Math.random() * (65535 + 1)),9 family = 'IPv6',10}: {11 address?: string12 port?: number13 family?: AddressType | string14} = {}) => {15 return {16 address,17 port,18 server: {19 requestIP() {20 return {21 address,22 family,23 port,24 }25 },26 },27 }28}29describe('getConnInfo', () => {30 it('Should info is valid', () => {31 const { port, server, address } = createRandomBunServer()32 const c = new Context(new Request('http://localhost/'), { env: server })33 const info = getConnInfo(c)3435 expect(info.remote.port).toBe(port)36 expect(info.remote.address).toBe(address)37 expect(info.remote.addressType).toBe('IPv6')38 expect(info.remote.transport).toBeUndefined()39 })40 it('Should getConnInfo works when env is { server: server }', () => {41 const { port, server, address } = createRandomBunServer()42 const c = new Context(new Request('http://localhost/'), { env: { server } })4344 const info = getConnInfo(c)4546 expect(info.remote.port).toBe(port)47 expect(info.remote.address).toBe(address)48 expect(info.remote.addressType).toBe('IPv6')49 expect(info.remote.transport).toBeUndefined()50 })51 it('should return undefined when addressType is invalid string', () => {52 const { server } = createRandomBunServer({ family: 'invalid' })53 const c = new Context(new Request('http://localhost/'), { env: { server } })5455 const info = getConnInfo(c)5657 expect(info.remote.addressType).toBeUndefined()58 })59 it('Should throw error when user did not give server', () => {60 const c = new Context(new Request('http://localhost/'), { env: {} })6162 expect(() => getConnInfo(c)).toThrowError(TypeError)63 })64 it('Should throw error when requestIP is not function', () => {65 const c = new Context(new Request('http://localhost/'), {66 env: {67 requestIP: 0,68 },69 })70 expect(() => getConnInfo(c)).toThrowError(TypeError)71 })72 it('Should return empty remote when requestIP returns null', () => {73 const c = new Context(new Request('http://localhost/'), {74 env: {75 requestIP() {76 return null77 },78 },79 })8081 const info = getConnInfo(c)8283 expect(info.remote).toEqual({})84 })85})