forked from fastify/fastify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisten.3.test.js
87 lines (76 loc) · 2.25 KB
/
listen.3.test.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
'use strict'
const os = require('node:os')
const path = require('node:path')
const fs = require('node:fs')
const { test, before } = require('tap')
const Fastify = require('..')
const helper = require('./helper')
let localhostForURL
before(async function () {
[, localhostForURL] = await helper.getLoopbackHost()
})
// https://nodejs.org/api/net.html#net_ipc_support
if (os.platform() !== 'win32') {
test('listen on socket', t => {
t.plan(3)
const fastify = Fastify()
t.teardown(fastify.close.bind(fastify))
const sockFile = path.join(os.tmpdir(), `${(Math.random().toString(16) + '0000000').slice(2, 10)}-server.sock`)
try {
fs.unlinkSync(sockFile)
} catch (e) { }
fastify.listen({ path: sockFile }, (err, address) => {
t.error(err)
t.strictSame(fastify.addresses(), [sockFile])
t.equal(address, sockFile)
})
})
} else {
test('listen on socket', t => {
t.plan(3)
const fastify = Fastify()
t.teardown(fastify.close.bind(fastify))
const sockFile = `\\\\.\\pipe\\${(Math.random().toString(16) + '0000000').slice(2, 10)}-server-sock`
fastify.listen({ path: sockFile }, (err, address) => {
t.error(err)
t.strictSame(fastify.addresses(), [sockFile])
t.equal(address, sockFile)
})
})
}
test('listen without callback with (address)', t => {
t.plan(1)
const fastify = Fastify()
t.teardown(fastify.close.bind(fastify))
fastify.listen({ port: 0 })
.then(address => {
t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
})
})
test('double listen without callback rejects', t => {
t.plan(1)
const fastify = Fastify()
t.teardown(fastify.close.bind(fastify))
fastify.listen({ port: 0 })
.then(() => {
fastify.listen({ port: 0 })
.catch(err => {
t.ok(err)
})
})
.catch(err => t.error(err))
})
test('double listen without callback with (address)', t => {
t.plan(2)
const fastify = Fastify()
t.teardown(fastify.close.bind(fastify))
fastify.listen({ port: 0 })
.then(address => {
t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
fastify.listen({ port: 0 })
.catch(err => {
t.ok(err)
})
})
.catch(err => t.error(err))
})