forked from fastify/fastify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisten.2.test.js
110 lines (100 loc) · 3.12 KB
/
listen.2.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
'use strict'
const { test, before } = require('node:test')
const Fastify = require('..')
const helper = require('./helper')
let localhostForURL
before(async function () {
[, localhostForURL] = await helper.getLoopbackHost()
})
test('register after listen using Promise.resolve()', async t => {
t.plan(1)
const fastify = Fastify()
const handler = (req, res) => res.send({})
await Promise.resolve()
.then(() => {
fastify.get('/', handler)
fastify.register((f2, options, done) => {
f2.get('/plugin', handler)
done()
})
return fastify.ready()
})
.catch((err) => {
t.assert.fail(err.message)
})
.then(() => t.assert.ok('resolved'))
})
test('double listen errors', (t, done) => {
t.plan(3)
const fastify = Fastify()
t.after(() => fastify.close())
fastify.listen({ port: 0 }, (err) => {
t.assert.ifError(err)
fastify.listen({ port: fastify.server.address().port }, (err, address) => {
t.assert.strictEqual(address, null)
t.assert.ok(err)
done()
})
})
})
test('double listen errors callback with (err, address)', (t, done) => {
t.plan(4)
const fastify = Fastify()
t.after(() => fastify.close())
fastify.listen({ port: 0 }, (err1, address1) => {
t.assert.strictEqual(address1, `http://${localhostForURL}:${fastify.server.address().port}`)
t.assert.ifError(err1)
fastify.listen({ port: fastify.server.address().port }, (err2, address2) => {
t.assert.strictEqual(address2, null)
t.assert.ok(err2)
done()
})
})
})
test('nonlocalhost double listen errors callback with (err, address)', (t, done) => {
t.plan(4)
const fastify = Fastify()
t.after(() => fastify.close())
fastify.listen({ host: '::1', port: 0 }, (err, address) => {
t.assert.strictEqual(address, `http://${'[::1]'}:${fastify.server.address().port}`)
t.assert.ifError(err)
fastify.listen({ host: '::1', port: fastify.server.address().port }, (err2, address2) => {
t.assert.strictEqual(address2, null)
t.assert.ok(err2)
done()
})
})
})
test('listen twice on the same port', (t, done) => {
t.plan(4)
const fastify = Fastify()
t.after(() => fastify.close())
fastify.listen({ port: 0 }, (err1, address1) => {
t.assert.strictEqual(address1, `http://${localhostForURL}:${fastify.server.address().port}`)
t.assert.ifError(err1)
const s2 = Fastify()
t.after(() => fastify.close())
s2.listen({ port: fastify.server.address().port }, (err2, address2) => {
t.assert.strictEqual(address2, null)
t.assert.ok(err2)
done()
})
})
})
test('listen twice on the same port callback with (err, address)', (t, done) => {
t.plan(4)
const fastify = Fastify()
t.after(() => fastify.close())
fastify.listen({ port: 0 }, (err1, address1) => {
const _port = fastify.server.address().port
t.assert.strictEqual(address1, `http://${localhostForURL}:${_port}`)
t.assert.ifError(err1)
const s2 = Fastify()
t.after(() => fastify.close())
s2.listen({ port: _port }, (err2, address2) => {
t.assert.strictEqual(address2, null)
t.assert.ok(err2)
done()
})
})
})