|
| 1 | +import './setup' |
| 2 | +import { resolve } from 'node:path' |
| 3 | +import type { IncomingMessage, ServerResponse } from 'node:http' |
| 4 | +import { afterEach, describe, expect, test } from 'vitest' |
| 5 | +import type { Listener } from '../src' |
| 6 | +import { listen } from '../src' |
| 7 | + |
| 8 | +// console.log = fn() |
| 9 | + |
| 10 | +function handle(request: IncomingMessage, response: ServerResponse) { |
| 11 | + response.end(request.url) |
| 12 | +} |
| 13 | + |
| 14 | +describe('earlist', () => { |
| 15 | + let listener: Listener | undefined |
| 16 | + |
| 17 | + afterEach(async () => { |
| 18 | + if (listener) { |
| 19 | + await listener.close() |
| 20 | + listener = undefined |
| 21 | + } |
| 22 | + }) |
| 23 | + |
| 24 | + test('listen (no args)', async () => { |
| 25 | + listener = await listen(handle) |
| 26 | + expect(listener.url.startsWith('http://')).toBe(true) |
| 27 | + }) |
| 28 | + |
| 29 | + test('listen (http)', async () => { |
| 30 | + listener = await listen(handle, { |
| 31 | + isTest: false, |
| 32 | + autoClose: false, |
| 33 | + baseURL: '/foo/bar', |
| 34 | + }) |
| 35 | + expect(listener.url.startsWith('http://')).toBe(true) |
| 36 | + expect(listener.url.endsWith('/foo/bar')).toBe(true) |
| 37 | + |
| 38 | + // expect(console.log).toHaveBeenCalledWith(expect.stringMatching('\n > Local: http://localhost:3000/foo/bar')) |
| 39 | + }) |
| 40 | + |
| 41 | + test('listen (https - selfsigned)', async () => { |
| 42 | + listener = await listen(handle, { https: true }) |
| 43 | + expect(listener.url.startsWith('https://')).toBe(true) |
| 44 | + }) |
| 45 | + |
| 46 | + test('listen (https - custom)', async () => { |
| 47 | + listener = await listen(handle, { |
| 48 | + https: { |
| 49 | + |
| 50 | + key: resolve(__dirname, 'fixture/cert/key.pem'), |
| 51 | + |
| 52 | + cert: resolve(__dirname, 'fixture/cert/cert.pem'), |
| 53 | + }, |
| 54 | + }) |
| 55 | + expect(listener.url.startsWith('https://')).toBe(true) |
| 56 | + }) |
| 57 | + |
| 58 | + test('double close', async () => { |
| 59 | + listener = await listen(handle, { isTest: false }) |
| 60 | + await listener.close() |
| 61 | + await listener.close() |
| 62 | + }) |
| 63 | + |
| 64 | + test('autoClose', async () => { |
| 65 | + /* not passing close */ await listen(handle) |
| 66 | + // @ts-expect-error is fine |
| 67 | + process.emit('exit') |
| 68 | + }) |
| 69 | + |
| 70 | + test('pass hostname to scotty-beam-me-up', async () => { |
| 71 | + listener = await listen(handle, { hostname: '127.0.0.1' }) |
| 72 | + expect(listener.url.startsWith('http://localhost')).toBe(true) |
| 73 | + }) |
| 74 | + |
| 75 | + test('pass port to scotty-beam-me-up', async () => { |
| 76 | + listener = await listen(handle, { port: 40_000 }) |
| 77 | + expect(listener.url.endsWith(':40000/')).toBe(true) |
| 78 | + }) |
| 79 | + |
| 80 | + test('pass extended options to scotty-beam-me-up', async () => { |
| 81 | + listener = await listen(handle, { |
| 82 | + port: { port: 50_000, portRange: [50_000, 59_999] }, |
| 83 | + }) |
| 84 | + expect(listener.url).toMatch(/:5\d{4}\/$/) |
| 85 | + }) |
| 86 | + |
| 87 | + test('should listen to the next port in range (3000 -> 31000)', async () => { |
| 88 | + listener = await listen(handle, { |
| 89 | + port: { port: 3000 }, |
| 90 | + }) |
| 91 | + expect(listener.url).toMatch(/:3000\/$/) |
| 92 | + const listener2 = await listen(handle, { |
| 93 | + port: { port: 3000 }, |
| 94 | + }) |
| 95 | + expect(listener2.url).toMatch(/:3001\/$/) |
| 96 | + await listener2.close() |
| 97 | + }) |
| 98 | +}) |
0 commit comments