1 | 'use strict'
|
2 |
|
3 | const os = require('os')
|
4 | const path = require('path')
|
5 | const fs = require('fs')
|
6 | const test = require('tap').test
|
7 | const Fastify = require('..')
|
8 |
|
9 | test('listen accepts a callback', t => {
|
10 | t.plan(2)
|
11 | const fastify = Fastify()
|
12 | t.tearDown(fastify.close.bind(fastify))
|
13 | fastify.listen((err) => {
|
14 | t.is(fastify.server.address().address, '127.0.0.1')
|
15 | t.error(err)
|
16 | })
|
17 | })
|
18 |
|
19 | test('listen accepts a port and a callback', t => {
|
20 | t.plan(2)
|
21 | const fastify = Fastify()
|
22 | t.tearDown(fastify.close.bind(fastify))
|
23 | fastify.listen(0, (err) => {
|
24 | t.is(fastify.server.address().address, '127.0.0.1')
|
25 | t.error(err)
|
26 | })
|
27 | })
|
28 |
|
29 | test('listen accepts a port and a callback with (err, address)', t => {
|
30 | t.plan(2)
|
31 | const fastify = Fastify()
|
32 | t.tearDown(fastify.close.bind(fastify))
|
33 | fastify.listen(0, (err, address) => {
|
34 | t.is(address, 'http://127.0.0.1:' + fastify.server.address().port)
|
35 | t.error(err)
|
36 | })
|
37 | })
|
38 |
|
39 | test('listen accepts a port, address, and callback', t => {
|
40 | t.plan(1)
|
41 | const fastify = Fastify()
|
42 | t.tearDown(fastify.close.bind(fastify))
|
43 | fastify.listen(0, '127.0.0.1', (err) => {
|
44 | t.error(err)
|
45 | })
|
46 | })
|
47 |
|
48 | test('listen accepts options and a callback', t => {
|
49 | t.plan(1)
|
50 | const fastify = Fastify()
|
51 | t.tearDown(fastify.close.bind(fastify))
|
52 | fastify.listen({
|
53 | port: 0,
|
54 | host: 'localhost',
|
55 | backlog: 511,
|
56 | exclusive: false,
|
57 | readableAll: false,
|
58 | writableAll: false,
|
59 | ipv6Only: false
|
60 | }, (err) => {
|
61 | t.error(err)
|
62 | })
|
63 | })
|
64 |
|
65 | test('listen accepts a port, address and a callback with (err, address)', t => {
|
66 | t.plan(2)
|
67 | const fastify = Fastify()
|
68 | t.tearDown(fastify.close.bind(fastify))
|
69 | fastify.listen(0, '127.0.0.1', (err, address) => {
|
70 | t.is(address, 'http://127.0.0.1:' + fastify.server.address().port)
|
71 | t.error(err)
|
72 | })
|
73 | })
|
74 |
|
75 | test('listen accepts a port, address, backlog and callback', t => {
|
76 | t.plan(1)
|
77 | const fastify = Fastify()
|
78 | t.tearDown(fastify.close.bind(fastify))
|
79 | fastify.listen(0, '127.0.0.1', 511, (err) => {
|
80 | t.error(err)
|
81 | })
|
82 | })
|
83 |
|
84 | test('listen accepts a port, address, backlog and callback with (err, address)', t => {
|
85 | t.plan(2)
|
86 | const fastify = Fastify()
|
87 | t.tearDown(fastify.close.bind(fastify))
|
88 | fastify.listen(0, '127.0.0.1', 511, (err, address) => {
|
89 | t.is(address, 'http://127.0.0.1:' + fastify.server.address().port)
|
90 | t.error(err)
|
91 | })
|
92 | })
|
93 |
|
94 | test('listen after Promise.resolve()', t => {
|
95 | t.plan(2)
|
96 | const f = Fastify()
|
97 | t.tearDown(f.close.bind(f))
|
98 | Promise.resolve()
|
99 | .then(() => {
|
100 | f.listen(0, (err, address) => {
|
101 | f.server.unref()
|
102 | t.is(address, 'http://127.0.0.1:' + f.server.address().port)
|
103 | t.error(err)
|
104 | })
|
105 | })
|
106 | })
|
107 |
|
108 | test('register after listen using Promise.resolve()', t => {
|
109 | t.plan(1)
|
110 | const f = Fastify()
|
111 |
|
112 | const handler = (req, res) => res.send({})
|
113 | Promise.resolve()
|
114 | .then(() => {
|
115 | f.get('/', handler)
|
116 | f.register((f2, options, done) => {
|
117 | f2.get('/plugin', handler)
|
118 | done()
|
119 | })
|
120 | return f.ready()
|
121 | })
|
122 | .catch(t.error)
|
123 | .then(() => t.pass('resolved'))
|
124 | })
|
125 |
|
126 | test('double listen errors', t => {
|
127 | t.plan(3)
|
128 | const fastify = Fastify()
|
129 | t.tearDown(fastify.close.bind(fastify))
|
130 | fastify.listen(0, (err) => {
|
131 | t.error(err)
|
132 | fastify.listen(fastify.server.address().port, (err, address) => {
|
133 | t.is(address, null)
|
134 | t.ok(err)
|
135 | })
|
136 | })
|
137 | })
|
138 |
|
139 | test('double listen errors callback with (err, address)', t => {
|
140 | t.plan(4)
|
141 | const fastify = Fastify()
|
142 | t.tearDown(fastify.close.bind(fastify))
|
143 | fastify.listen(0, (err1, address1) => {
|
144 | t.is(address1, 'http://127.0.0.1:' + fastify.server.address().port)
|
145 | t.error(err1)
|
146 | fastify.listen(fastify.server.address().port, (err2, address2) => {
|
147 | t.is(address2, null)
|
148 | t.ok(err2)
|
149 | })
|
150 | })
|
151 | })
|
152 |
|
153 | test('listen twice on the same port', t => {
|
154 | t.plan(4)
|
155 | const fastify = Fastify()
|
156 | t.tearDown(fastify.close.bind(fastify))
|
157 | fastify.listen(0, (err1, address1) => {
|
158 | t.is(address1, 'http://127.0.0.1:' + fastify.server.address().port)
|
159 | t.error(err1)
|
160 | const s2 = Fastify()
|
161 | t.tearDown(s2.close.bind(s2))
|
162 | s2.listen(fastify.server.address().port, (err2, address2) => {
|
163 | t.is(address2, null)
|
164 | t.ok(err2)
|
165 | })
|
166 | })
|
167 | })
|
168 |
|
169 | test('listen twice on the same port callback with (err, address)', t => {
|
170 | t.plan(4)
|
171 | const fastify = Fastify()
|
172 | t.tearDown(fastify.close.bind(fastify))
|
173 | fastify.listen(0, (err1, address1) => {
|
174 | const _port = fastify.server.address().port
|
175 | t.is(address1, 'http://127.0.0.1:' + _port)
|
176 | t.error(err1)
|
177 | const s2 = Fastify()
|
178 | t.tearDown(s2.close.bind(s2))
|
179 | s2.listen(_port, (err2, address2) => {
|
180 | t.is(address2, null)
|
181 | t.ok(err2)
|
182 | })
|
183 | })
|
184 | })
|
185 |
|
186 |
|
187 | if (os.platform() !== 'win32') {
|
188 | test('listen on socket', t => {
|
189 | t.plan(3)
|
190 | const fastify = Fastify()
|
191 | t.tearDown(fastify.close.bind(fastify))
|
192 |
|
193 | const sockFile = path.join(os.tmpdir(), 'server.sock')
|
194 | try {
|
195 | fs.unlinkSync(sockFile)
|
196 | } catch (e) { }
|
197 |
|
198 | fastify.listen(sockFile, (err, address) => {
|
199 | t.error(err)
|
200 | t.equal(sockFile, fastify.server.address())
|
201 | t.equal(address, sockFile)
|
202 | })
|
203 | })
|
204 | }
|
205 |
|
206 | test('listen without callback (port zero)', t => {
|
207 | t.plan(1)
|
208 | const fastify = Fastify()
|
209 | t.tearDown(fastify.close.bind(fastify))
|
210 | fastify.listen(0)
|
211 | .then(() => {
|
212 | t.is(fastify.server.address().address, '127.0.0.1')
|
213 | })
|
214 | })
|
215 |
|
216 | test('listen without callback (port not given)', t => {
|
217 | t.plan(1)
|
218 | const fastify = Fastify()
|
219 | t.tearDown(fastify.close.bind(fastify))
|
220 | fastify.listen()
|
221 | .then(() => {
|
222 | t.is(fastify.server.address().address, '127.0.0.1')
|
223 | })
|
224 | })
|
225 |
|
226 | test('listen null without callback with (address)', t => {
|
227 | t.plan(1)
|
228 | const fastify = Fastify()
|
229 | t.tearDown(fastify.close.bind(fastify))
|
230 | fastify.listen(null)
|
231 | .then(address => {
|
232 | t.is(address, 'http://127.0.0.1:' + fastify.server.address().port)
|
233 | })
|
234 | })
|
235 |
|
236 | test('listen without port without callback with (address)', t => {
|
237 | t.plan(1)
|
238 | const fastify = Fastify()
|
239 | t.tearDown(fastify.close.bind(fastify))
|
240 | fastify.listen()
|
241 | .then(address => {
|
242 | t.is(address, 'http://127.0.0.1:' + fastify.server.address().port)
|
243 | })
|
244 | })
|
245 |
|
246 | test('listen with undefined without callback with (address)', t => {
|
247 | t.plan(1)
|
248 | const fastify = Fastify()
|
249 | t.tearDown(fastify.close.bind(fastify))
|
250 | fastify.listen(undefined)
|
251 | .then(address => {
|
252 | t.is(address, 'http://127.0.0.1:' + fastify.server.address().port)
|
253 | })
|
254 | })
|
255 |
|
256 | test('listen without callback with (address)', t => {
|
257 | t.plan(1)
|
258 | const fastify = Fastify()
|
259 | t.tearDown(fastify.close.bind(fastify))
|
260 | fastify.listen(0)
|
261 | .then(address => {
|
262 | t.is(address, 'http://127.0.0.1:' + fastify.server.address().port)
|
263 | })
|
264 | })
|
265 |
|
266 | test('double listen without callback rejects', t => {
|
267 | t.plan(1)
|
268 | const fastify = Fastify()
|
269 | t.tearDown(fastify.close.bind(fastify))
|
270 | fastify.listen(0)
|
271 | .then(() => {
|
272 | fastify.listen(0)
|
273 | .catch(err => {
|
274 | t.ok(err)
|
275 | })
|
276 | })
|
277 | .catch(err => t.error(err))
|
278 | })
|
279 |
|
280 | test('double listen without callback with (address)', t => {
|
281 | t.plan(2)
|
282 | const fastify = Fastify()
|
283 | t.tearDown(fastify.close.bind(fastify))
|
284 | fastify.listen(0)
|
285 | .then(address => {
|
286 | t.is(address, 'http://127.0.0.1:' + fastify.server.address().port)
|
287 | fastify.listen(0)
|
288 | .catch(err => {
|
289 | t.ok(err)
|
290 | })
|
291 | })
|
292 | .catch(err => t.error(err))
|
293 | })
|
294 |
|
295 | test('listen twice on the same port without callback rejects', t => {
|
296 | t.plan(1)
|
297 | const fastify = Fastify()
|
298 | t.tearDown(fastify.close.bind(fastify))
|
299 |
|
300 | fastify.listen(0)
|
301 | .then(() => {
|
302 | const s2 = Fastify()
|
303 | t.tearDown(s2.close.bind(s2))
|
304 | s2.listen(fastify.server.address().port)
|
305 | .catch(err => {
|
306 | t.ok(err)
|
307 | })
|
308 | })
|
309 | .catch(err => t.error(err))
|
310 | })
|
311 |
|
312 | test('listen twice on the same port without callback rejects with (address)', t => {
|
313 | t.plan(2)
|
314 | const fastify = Fastify()
|
315 | t.tearDown(fastify.close.bind(fastify))
|
316 | fastify.listen(0)
|
317 | .then(address => {
|
318 | const s2 = Fastify()
|
319 | t.tearDown(s2.close.bind(s2))
|
320 | t.is(address, 'http://127.0.0.1:' + fastify.server.address().port)
|
321 | s2.listen(fastify.server.address().port)
|
322 | .catch(err => {
|
323 | t.ok(err)
|
324 | })
|
325 | })
|
326 | .catch(err => t.error(err))
|
327 | })
|
328 |
|
329 | test('listen on invalid port without callback rejects', t => {
|
330 | const fastify = Fastify()
|
331 | t.tearDown(fastify.close.bind(fastify))
|
332 | return fastify.listen(-1)
|
333 | .catch(err => {
|
334 | t.ok(err)
|
335 | return true
|
336 | })
|
337 | })
|
338 |
|
339 | test('listen logs the port as info', t => {
|
340 | t.plan(1)
|
341 | const fastify = Fastify()
|
342 | t.tearDown(fastify.close.bind(fastify))
|
343 |
|
344 | const msgs = []
|
345 | fastify.log.info = function (msg) {
|
346 | msgs.push(msg)
|
347 | }
|
348 |
|
349 | fastify.listen(0)
|
350 | .then(() => {
|
351 | t.ok(/http:\/\//.test(msgs[0]))
|
352 | })
|
353 | })
|