-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.test.ts
67 lines (57 loc) 路 1.81 KB
/
index.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import './setup'
import { resolve } from 'path'
import type { IncomingMessage, ServerResponse } from 'http'
import { describe, afterEach, test, expect } from 'vitest'
import { listen, Listener } from '../src'
// eslint-disable-next-line no-console
// console.log = fn()
function handle (req: IncomingMessage, res: ServerResponse) {
res.end(req.url)
}
describe('listhen', () => {
let listener: Listener | null
afterEach(async () => {
if (listener) {
await listener.close()
listener = null
}
})
test('listen (no args)', async () => {
listener = await listen(handle)
expect(listener.url.startsWith('http://')).toBe(true)
})
test('listen (http)', async () => {
listener = await listen(handle, {
isTest: false,
autoClose: false,
baseURL: '/foo/bar'
})
expect(listener.url.startsWith('http://')).toBe(true)
expect(listener.url.endsWith('/foo/bar')).toBe(true)
// eslint-disable-next-line no-console
// expect(console.log).toHaveBeenCalledWith(expect.stringMatching('\n > Local: http://localhost:3000/foo/bar'))
})
test('listen (https - selfsigned)', async () => {
listener = await listen(handle, { https: true })
expect(listener.url.startsWith('https://')).toBe(true)
})
test('listen (https - custom)', async () => {
listener = await listen(handle, {
https: {
key: resolve(__dirname, 'fixture/cert/key.pem'),
cert: resolve(__dirname, 'fixture/cert/cert.pem')
}
})
expect(listener.url.startsWith('https://')).toBe(true)
})
test('double close', async () => {
listener = await listen(handle, { isTest: false })
await listener.close()
await listener.close()
})
test('autoClose', async () => {
/* not passing close */ await listen(handle)
// @ts-ignore
process.emit('exit')
})
})