Skip to content

Commit

Permalink
chore: add test to handle whether port 80 is in use
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed Oct 8, 2024
1 parent 553492b commit b0c494c
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion test/reverse-proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,31 @@ import { afterEach, beforeEach, describe, expect, it, mock } from 'bun:test'
import { setupReverseProxy, startHttpRedirectServer, startProxies, startProxy, startServer } from '../src/start'
import type { ReverseProxyOption } from '../src/types'

const mockLog = {
debug: mock(),
error: mock(),
info: mock(),
}

mock.module('@stacksjs/cli', () => ({
log: mockLog,
bold: mock((str) => str),
dim: mock((str) => str),
green: mock((str) => str),
}))

describe('@stacksjs/reverse-proxy', () => {
beforeEach(() => {
// Reset all mocks before each test
mock.restore()

// Re-mock @stacksjs/cli after restoring all mocks
mock.module('@stacksjs/cli', () => ({
log: mockLog,
bold: mock((str) => str),
dim: mock((str) => str),
green: mock((str) => str),
}))
})

afterEach(() => {
Expand Down Expand Up @@ -70,9 +91,38 @@ describe('@stacksjs/reverse-proxy', () => {

setupReverseProxy({ hostname: 'localhost', port: 3000, from: 'localhost:3000', to: 'example.com' })

expect(mockLog.debug).toHaveBeenCalledWith('setupReverseProxy', expect.any(Object))
expect(mockCreateServer).toHaveBeenCalled()
expect(mockHttpServer.listen).toHaveBeenCalledWith(80, '0.0.0.0', expect.any(Function))
})

it('handles port 80 already in use', () => {
const mockExit = mock((code: number) => {})
process.exit = mockExit as any

const mockTestServer = {
once: mock((event: string, callback: (err?: Error) => void) => {
if (event === 'error') {
const error = new Error('EADDRINUSE') as NodeJS.ErrnoException
error.code = 'EADDRINUSE'
callback(error)
}
}),
listen: mock(),
}
mock.module('node:net', () => ({
createServer: mock(() => mockTestServer),
}))

setupReverseProxy({ hostname: 'localhost', port: 3000, from: 'localhost:3000', to: 'example.com' })

expect(mockLog.debug).toHaveBeenCalledWith('setupReverseProxy', expect.any(Object))
expect(mockTestServer.once).toHaveBeenCalledWith('error', expect.any(Function))
expect(mockTestServer.listen).toHaveBeenCalledWith(80, '0.0.0.0')
expect(mockLog.error).toHaveBeenCalled()
expect(mockLog.info).toHaveBeenCalled()
expect(mockExit).toHaveBeenCalledWith(1)
})
})

describe('startHttpRedirectServer', () => {
Expand Down Expand Up @@ -101,7 +151,7 @@ describe('@stacksjs/reverse-proxy', () => {
}))

const option: ReverseProxyOption = { from: 'localhost:4000', to: 'example.com' }
await startProxy(option)
startProxy(option)

expect(mockStartServer).toHaveBeenCalledWith(option)
})
Expand Down

0 comments on commit b0c494c

Please sign in to comment.