fastify 3.24.1 → 3.25.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/README.md +5 -4
  2. package/docs/{Benchmarking.md → Guides/Benchmarking.md} +14 -5
  3. package/docs/Guides/Ecosystem.md +508 -0
  4. package/docs/{Fluent-Schema.md → Guides/Fluent-Schema.md} +16 -7
  5. package/docs/{Getting-Started.md → Guides/Getting-Started.md} +162 -60
  6. package/docs/Guides/Index.md +30 -4
  7. package/docs/{Migration-Guide-V3.md → Guides/Migration-Guide-V3.md} +43 -37
  8. package/docs/{Plugins-Guide.md → Guides/Plugins-Guide.md} +196 -82
  9. package/docs/{Recommendations.md → Guides/Recommendations.md} +17 -10
  10. package/docs/{Serverless.md → Guides/Serverless.md} +84 -49
  11. package/docs/Guides/Style-Guide.md +246 -0
  12. package/docs/{Testing.md → Guides/Testing.md} +25 -11
  13. package/docs/Guides/Write-Plugin.md +102 -0
  14. package/docs/{ContentTypeParser.md → Reference/ContentTypeParser.md} +68 -30
  15. package/docs/{Decorators.md → Reference/Decorators.md} +52 -47
  16. package/docs/{Encapsulation.md → Reference/Encapsulation.md} +3 -3
  17. package/docs/{Errors.md → Reference/Errors.md} +77 -47
  18. package/docs/{HTTP2.md → Reference/HTTP2.md} +13 -13
  19. package/docs/{Hooks.md → Reference/Hooks.md} +148 -69
  20. package/docs/Reference/Index.md +71 -0
  21. package/docs/{LTS.md → Reference/LTS.md} +31 -32
  22. package/docs/{Lifecycle.md → Reference/Lifecycle.md} +15 -7
  23. package/docs/{Logging.md → Reference/Logging.md} +47 -29
  24. package/docs/Reference/Middleware.md +78 -0
  25. package/docs/{Plugins.md → Reference/Plugins.md} +91 -34
  26. package/docs/{Reply.md → Reference/Reply.md} +195 -97
  27. package/docs/{Request.md → Reference/Request.md} +32 -16
  28. package/docs/{Routes.md → Reference/Routes.md} +243 -113
  29. package/docs/{Server.md → Reference/Server.md} +516 -267
  30. package/docs/{TypeScript.md → Reference/TypeScript.md} +447 -187
  31. package/docs/{Validation-and-Serialization.md → Reference/Validation-and-Serialization.md} +178 -86
  32. package/docs/index.md +24 -0
  33. package/fastify.js +1 -1
  34. package/lib/decorate.js +6 -3
  35. package/lib/route.js +1 -1
  36. package/package.json +7 -2
  37. package/test/constrained-routes.test.js +220 -0
  38. package/test/custom-parser.test.js +11 -2
  39. package/test/decorator.test.js +24 -0
  40. package/test/handler-context.test.js +11 -4
  41. package/test/http2/closing.test.js +14 -5
  42. package/test/listen.test.js +36 -22
  43. package/test/request-error.test.js +2 -8
  44. package/test/router-options.test.js +10 -1
  45. package/test/stream.test.js +14 -3
  46. package/test/trust-proxy.test.js +15 -7
  47. package/test/types/instance.test-d.ts +46 -1
  48. package/types/instance.d.ts +6 -1
  49. package/docs/Ecosystem.md +0 -213
  50. package/docs/Middleware.md +0 -53
  51. package/docs/Style-Guide.md +0 -185
  52. package/docs/Write-Plugin.md +0 -58
@@ -182,3 +182,223 @@ test('Should allow registering custom constrained routes', t => {
182
182
  t.equal(res.statusCode, 404)
183
183
  })
184
184
  })
185
+
186
+ test('Should allow registering an unconstrained route after a constrained route', t => {
187
+ t.plan(6)
188
+ const fastify = Fastify()
189
+
190
+ fastify.route({
191
+ method: 'GET',
192
+ url: '/',
193
+ constraints: { host: 'fastify.io' },
194
+ handler: (req, reply) => {
195
+ reply.send({ hello: 'from fastify.io' })
196
+ }
197
+ })
198
+
199
+ fastify.route({
200
+ method: 'GET',
201
+ url: '/',
202
+ handler: (req, reply) => {
203
+ reply.send({ hello: 'from any other domain' })
204
+ }
205
+ })
206
+
207
+ fastify.inject({
208
+ method: 'GET',
209
+ url: '/',
210
+ headers: {
211
+ host: 'fastify.io'
212
+ }
213
+ }, (err, res) => {
214
+ t.error(err)
215
+ t.same(JSON.parse(res.payload), { hello: 'from fastify.io' })
216
+ t.equal(res.statusCode, 200)
217
+ })
218
+
219
+ fastify.inject({
220
+ method: 'GET',
221
+ url: '/',
222
+ headers: {
223
+ host: 'example.com'
224
+ }
225
+ }, (err, res) => {
226
+ t.error(err)
227
+ t.same(JSON.parse(res.payload), { hello: 'from any other domain' })
228
+ t.equal(res.statusCode, 200)
229
+ })
230
+ })
231
+
232
+ test('Should allow registering constrained routes in a prefixed plugin', t => {
233
+ t.plan(3)
234
+
235
+ const fastify = Fastify()
236
+
237
+ fastify.register(async (scope, opts) => {
238
+ scope.route({
239
+ method: 'GET',
240
+ constraints: { host: 'fastify.io' },
241
+ path: '/route',
242
+ handler: (req, reply) => {
243
+ reply.send({ ok: true })
244
+ }
245
+ })
246
+ }, { prefix: '/prefix' })
247
+
248
+ fastify.inject({
249
+ method: 'GET',
250
+ url: '/prefix/route',
251
+ headers: {
252
+ host: 'fastify.io'
253
+ }
254
+ }, (err, res) => {
255
+ t.error(err)
256
+ t.same(JSON.parse(res.payload), { ok: true })
257
+ t.equal(res.statusCode, 200)
258
+ })
259
+ })
260
+
261
+ test('Should allow registering a constrained GET route after a constrained HEAD route', t => {
262
+ t.plan(3)
263
+ const fastify = Fastify()
264
+
265
+ fastify.route({
266
+ method: 'HEAD',
267
+ url: '/',
268
+ constraints: { host: 'fastify.io' },
269
+ handler: (req, reply) => {
270
+ reply.header('content-type', 'text/plain')
271
+ reply.send('custom HEAD response')
272
+ }
273
+ })
274
+
275
+ fastify.route({
276
+ method: 'GET',
277
+ url: '/',
278
+ constraints: { host: 'fastify.io' },
279
+ handler: (req, reply) => {
280
+ reply.send({ hello: 'from any other domain' })
281
+ }
282
+ })
283
+
284
+ fastify.inject({
285
+ method: 'HEAD',
286
+ url: '/',
287
+ headers: {
288
+ host: 'fastify.io'
289
+ }
290
+ }, (err, res) => {
291
+ t.error(err)
292
+ t.same(res.payload, 'custom HEAD response')
293
+ t.equal(res.statusCode, 200)
294
+ })
295
+ })
296
+
297
+ test('Should allow registering a constrained GET route after an unconstrained HEAD route', t => {
298
+ t.plan(3)
299
+ const fastify = Fastify()
300
+
301
+ fastify.route({
302
+ method: 'HEAD',
303
+ url: '/',
304
+ handler: (req, reply) => {
305
+ reply.header('content-type', 'text/plain')
306
+ reply.send('custom HEAD response')
307
+ }
308
+ })
309
+
310
+ fastify.route({
311
+ method: 'GET',
312
+ url: '/',
313
+ constraints: { host: 'fastify.io' },
314
+ handler: (req, reply) => {
315
+ reply.send({ hello: 'from any other domain' })
316
+ }
317
+ })
318
+
319
+ fastify.inject({
320
+ method: 'HEAD',
321
+ url: '/',
322
+ headers: {
323
+ host: 'fastify.io'
324
+ }
325
+ }, (err, res) => {
326
+ t.error(err)
327
+ t.same(res.payload, 'custom HEAD response')
328
+ t.equal(res.statusCode, 200)
329
+ })
330
+ })
331
+
332
+ test('Will not try to re-createprefixed HEAD route if it already exists and exposeHeadRoutes is true for constrained routes', async (t) => {
333
+ t.plan(1)
334
+
335
+ const fastify = Fastify({ exposeHeadRoutes: true })
336
+
337
+ fastify.register((scope, opts, next) => {
338
+ scope.route({
339
+ method: 'HEAD',
340
+ path: '/route',
341
+ constraints: { host: 'fastify.io' },
342
+ handler: (req, reply) => {
343
+ reply.header('content-type', 'text/plain')
344
+ reply.send('custom HEAD response')
345
+ }
346
+ })
347
+ scope.route({
348
+ method: 'GET',
349
+ path: '/route',
350
+ constraints: { host: 'fastify.io' },
351
+ handler: (req, reply) => {
352
+ reply.send({ ok: true })
353
+ }
354
+ })
355
+
356
+ next()
357
+ }, { prefix: '/prefix' })
358
+
359
+ await fastify.ready()
360
+
361
+ t.ok(true)
362
+ })
363
+
364
+ test('allows separate constrained and unconstrained HEAD routes', async (t) => {
365
+ t.plan(1)
366
+
367
+ const fastify = Fastify({ exposeHeadRoutes: true })
368
+
369
+ fastify.register((scope, opts, next) => {
370
+ scope.route({
371
+ method: 'HEAD',
372
+ path: '/route',
373
+ handler: (req, reply) => {
374
+ reply.header('content-type', 'text/plain')
375
+ reply.send('unconstrained HEAD response')
376
+ }
377
+ })
378
+
379
+ scope.route({
380
+ method: 'HEAD',
381
+ path: '/route',
382
+ constraints: { host: 'fastify.io' },
383
+ handler: (req, reply) => {
384
+ reply.header('content-type', 'text/plain')
385
+ reply.send('constrained HEAD response')
386
+ }
387
+ })
388
+
389
+ scope.route({
390
+ method: 'GET',
391
+ path: '/route',
392
+ constraints: { host: 'fastify.io' },
393
+ handler: (req, reply) => {
394
+ reply.send({ ok: true })
395
+ }
396
+ })
397
+
398
+ next()
399
+ }, { prefix: '/prefix' })
400
+
401
+ await fastify.ready()
402
+
403
+ t.ok(true)
404
+ })
@@ -25,6 +25,15 @@ function plainTextParser (request, callback) {
25
25
  }
26
26
  }
27
27
 
28
+ function getUrl (app) {
29
+ const { address, port } = app.server.address()
30
+ if (address === '::1') {
31
+ return `http://[${address}]:${port}`
32
+ } else {
33
+ return `http://${address}:${port}`
34
+ }
35
+ }
36
+
28
37
  process.removeAllListeners('warning')
29
38
 
30
39
  test('contentTypeParser method should exist', t => {
@@ -61,7 +70,7 @@ test('contentTypeParser should add a custom parser', t => {
61
70
 
62
71
  sget({
63
72
  method: 'POST',
64
- url: 'http://localhost:' + fastify.server.address().port,
73
+ url: getUrl(fastify),
65
74
  body: '{"hello":"world"}',
66
75
  headers: {
67
76
  'Content-Type': 'application/jsoff'
@@ -78,7 +87,7 @@ test('contentTypeParser should add a custom parser', t => {
78
87
 
79
88
  sget({
80
89
  method: 'OPTIONS',
81
- url: 'http://localhost:' + fastify.server.address().port,
90
+ url: getUrl(fastify),
82
91
  body: '{"hello":"world"}',
83
92
  headers: {
84
93
  'Content-Type': 'application/jsoff'
@@ -1024,3 +1024,27 @@ test('decorateRequest/decorateReply is not set to a value', t => {
1024
1024
  })
1025
1025
  })
1026
1026
  })
1027
+
1028
+ test('decorateRequest with dependencies', (t) => {
1029
+ t.plan(2)
1030
+ const app = Fastify()
1031
+
1032
+ const decorator1 = {
1033
+ config: {},
1034
+ app: {}
1035
+ }
1036
+ const decorator2 = {
1037
+ stuff: {}
1038
+ }
1039
+
1040
+ app.decorate('decorator1', decorator1)
1041
+ app.decorateRequest('decorator1', decorator1)
1042
+
1043
+ if (
1044
+ app.hasDecorator('decorator1') &&
1045
+ app.hasRequestDecorator('decorator1')
1046
+ ) {
1047
+ t.doesNotThrow(() => app.decorateRequest('decorator2', decorator2, ['decorator1']))
1048
+ t.ok(app.hasRequestDecorator('decorator2'))
1049
+ }
1050
+ })
@@ -4,6 +4,15 @@ const http = require('http')
4
4
  const test = require('tap').test
5
5
  const fastify = require('../')
6
6
 
7
+ function getUrl (app) {
8
+ const { address, port } = app.server.address()
9
+ if (address === '::1') {
10
+ return `http://[${address}]:${port}`
11
+ } else {
12
+ return `http://${address}:${port}`
13
+ }
14
+ }
15
+
7
16
  test('handlers receive correct `this` context', (t) => {
8
17
  t.plan(4)
9
18
 
@@ -29,8 +38,7 @@ test('handlers receive correct `this` context', (t) => {
29
38
  t.ok(instance.foo)
30
39
  t.equal(instance.foo, 'foo')
31
40
 
32
- const address = `http://127.0.0.1:${instance.server.address().port}/`
33
- http.get(address, () => {}).on('error', t.threw)
41
+ http.get(getUrl(instance), () => {}).on('error', t.threw)
34
42
  })
35
43
  })
36
44
 
@@ -50,7 +58,6 @@ test('handlers have access to the internal context', (t) => {
50
58
  instance.listen(0, (err) => {
51
59
  instance.server.unref()
52
60
  if (err) t.threw(err)
53
- const address = `http://127.0.0.1:${instance.server.address().port}/`
54
- http.get(address, () => {}).on('error', t.threw)
61
+ http.get(getUrl(instance), () => {}).on('error', t.threw)
55
62
  })
56
63
  })
@@ -11,6 +11,15 @@ const { once } = require('events')
11
11
  const { buildCertificate } = require('../build-certificate')
12
12
  t.before(buildCertificate)
13
13
 
14
+ function getUrl (app) {
15
+ const { address, port } = app.server.address()
16
+ if (address === '::1') {
17
+ return `http://[${address}]:${port}`
18
+ } else {
19
+ return `http://${address}:${port}`
20
+ }
21
+ }
22
+
14
23
  t.test('http/2 request while fastify closing', t => {
15
24
  let fastify
16
25
  try {
@@ -30,7 +39,7 @@ t.test('http/2 request while fastify closing', t => {
30
39
 
31
40
  // Skipped because there is likely a bug on Node 8.
32
41
  t.test('return 200', { skip: semver.lt(process.versions.node, '10.15.0') }, t => {
33
- const url = `http://127.0.0.1:${fastify.server.address().port}`
42
+ const url = getUrl(fastify)
34
43
  const session = http2.connect(url, function () {
35
44
  this.request({
36
45
  ':method': 'GET',
@@ -78,7 +87,7 @@ t.test('http/2 request while fastify closing - return503OnClosing: false', t =>
78
87
 
79
88
  // Skipped because there is likely a bug on Node 8.
80
89
  t.test('return 200', { skip: semver.lt(process.versions.node, '10.15.0') }, t => {
81
- const url = `http://127.0.0.1:${fastify.server.address().port}`
90
+ const url = getUrl(fastify)
82
91
  const session = http2.connect(url, function () {
83
92
  this.request({
84
93
  ':method': 'GET',
@@ -115,7 +124,7 @@ t.test('http/2 closes successfully with async await', { skip: semver.lt(process.
115
124
 
116
125
  await fastify.listen(0)
117
126
 
118
- const url = `http://127.0.0.1:${fastify.server.address().port}`
127
+ const url = getUrl(fastify)
119
128
  const session = await connect(url)
120
129
  // An error might or might not happen, as it's OS dependent.
121
130
  session.on('error', () => {})
@@ -135,7 +144,7 @@ t.test('https/2 closes successfully with async await', { skip: semver.lt(process
135
144
 
136
145
  await fastify.listen(0)
137
146
 
138
- const url = `http://127.0.0.1:${fastify.server.address().port}`
147
+ const url = getUrl(fastify)
139
148
  const session = await connect(url)
140
149
  // An error might or might not happen, as it's OS dependent.
141
150
  session.on('error', () => {})
@@ -159,7 +168,7 @@ t.test('http/2 server side session emits a timeout event', { skip: semver.lt(pro
159
168
 
160
169
  await fastify.listen(0)
161
170
 
162
- const url = `http://127.0.0.1:${fastify.server.address().port}`
171
+ const url = getUrl(fastify)
163
172
  const session = await connect(url)
164
173
  const req = session.request({
165
174
  ':method': 'GET',
@@ -3,15 +3,29 @@
3
3
  const os = require('os')
4
4
  const path = require('path')
5
5
  const fs = require('fs')
6
- const test = require('tap').test
6
+ const { test, before } = require('tap')
7
7
  const Fastify = require('..')
8
+ const dns = require('dns').promises
9
+
10
+ let localhost
11
+ let localhostForURL
12
+
13
+ before(async function () {
14
+ const lookup = await dns.lookup('localhost')
15
+ localhost = lookup.address
16
+ if (lookup.family === 6) {
17
+ localhostForURL = `[${lookup.address}]`
18
+ } else {
19
+ localhostForURL = localhost
20
+ }
21
+ })
8
22
 
9
23
  test('listen accepts a callback', t => {
10
24
  t.plan(2)
11
25
  const fastify = Fastify()
12
26
  t.teardown(fastify.close.bind(fastify))
13
27
  fastify.listen((err) => {
14
- t.equal(fastify.server.address().address, '127.0.0.1')
28
+ t.equal(fastify.server.address().address, localhost)
15
29
  t.error(err)
16
30
  })
17
31
  })
@@ -21,7 +35,7 @@ test('listen accepts a port and a callback', t => {
21
35
  const fastify = Fastify()
22
36
  t.teardown(fastify.close.bind(fastify))
23
37
  fastify.listen(0, (err) => {
24
- t.equal(fastify.server.address().address, '127.0.0.1')
38
+ t.equal(fastify.server.address().address, localhost)
25
39
  t.error(err)
26
40
  })
27
41
  })
@@ -31,7 +45,7 @@ test('listen accepts a port and a callback with (err, address)', t => {
31
45
  const fastify = Fastify()
32
46
  t.teardown(fastify.close.bind(fastify))
33
47
  fastify.listen(0, (err, address) => {
34
- t.equal(address, 'http://127.0.0.1:' + fastify.server.address().port)
48
+ t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
35
49
  t.error(err)
36
50
  })
37
51
  })
@@ -40,7 +54,7 @@ test('listen accepts a port, address, and callback', t => {
40
54
  t.plan(1)
41
55
  const fastify = Fastify()
42
56
  t.teardown(fastify.close.bind(fastify))
43
- fastify.listen(0, '127.0.0.1', (err) => {
57
+ fastify.listen(0, localhost, (err) => {
44
58
  t.error(err)
45
59
  })
46
60
  })
@@ -78,8 +92,8 @@ test('listen accepts a port, address and a callback with (err, address)', t => {
78
92
  t.plan(2)
79
93
  const fastify = Fastify()
80
94
  t.teardown(fastify.close.bind(fastify))
81
- fastify.listen(0, '127.0.0.1', (err, address) => {
82
- t.equal(address, 'http://127.0.0.1:' + fastify.server.address().port)
95
+ fastify.listen(0, localhost, (err, address) => {
96
+ t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
83
97
  t.error(err)
84
98
  })
85
99
  })
@@ -88,7 +102,7 @@ test('listen accepts a port, address, backlog and callback', t => {
88
102
  t.plan(1)
89
103
  const fastify = Fastify()
90
104
  t.teardown(fastify.close.bind(fastify))
91
- fastify.listen(0, '127.0.0.1', 511, (err) => {
105
+ fastify.listen(0, localhost, 511, (err) => {
92
106
  t.error(err)
93
107
  })
94
108
  })
@@ -97,8 +111,8 @@ test('listen accepts a port, address, backlog and callback with (err, address)',
97
111
  t.plan(2)
98
112
  const fastify = Fastify()
99
113
  t.teardown(fastify.close.bind(fastify))
100
- fastify.listen(0, '127.0.0.1', 511, (err, address) => {
101
- t.equal(address, 'http://127.0.0.1:' + fastify.server.address().port)
114
+ fastify.listen(0, localhost, 511, (err, address) => {
115
+ t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
102
116
  t.error(err)
103
117
  })
104
118
  })
@@ -111,7 +125,7 @@ test('listen after Promise.resolve()', t => {
111
125
  .then(() => {
112
126
  f.listen(0, (err, address) => {
113
127
  f.server.unref()
114
- t.equal(address, 'http://127.0.0.1:' + f.server.address().port)
128
+ t.equal(address, `http://${localhostForURL}:${f.server.address().port}`)
115
129
  t.error(err)
116
130
  })
117
131
  })
@@ -153,7 +167,7 @@ test('double listen errors callback with (err, address)', t => {
153
167
  const fastify = Fastify()
154
168
  t.teardown(fastify.close.bind(fastify))
155
169
  fastify.listen(0, (err1, address1) => {
156
- t.equal(address1, 'http://127.0.0.1:' + fastify.server.address().port)
170
+ t.equal(address1, `http://${localhostForURL}:${fastify.server.address().port}`)
157
171
  t.error(err1)
158
172
  fastify.listen(fastify.server.address().port, (err2, address2) => {
159
173
  t.equal(address2, null)
@@ -167,7 +181,7 @@ test('listen twice on the same port', t => {
167
181
  const fastify = Fastify()
168
182
  t.teardown(fastify.close.bind(fastify))
169
183
  fastify.listen(0, (err1, address1) => {
170
- t.equal(address1, 'http://127.0.0.1:' + fastify.server.address().port)
184
+ t.equal(address1, `http://${localhostForURL}:${fastify.server.address().port}`)
171
185
  t.error(err1)
172
186
  const s2 = Fastify()
173
187
  t.teardown(s2.close.bind(s2))
@@ -184,7 +198,7 @@ test('listen twice on the same port callback with (err, address)', t => {
184
198
  t.teardown(fastify.close.bind(fastify))
185
199
  fastify.listen(0, (err1, address1) => {
186
200
  const _port = fastify.server.address().port
187
- t.equal(address1, 'http://127.0.0.1:' + _port)
201
+ t.equal(address1, `http://${localhostForURL}:${_port}`)
188
202
  t.error(err1)
189
203
  const s2 = Fastify()
190
204
  t.teardown(s2.close.bind(s2))
@@ -221,7 +235,7 @@ test('listen without callback (port zero)', t => {
221
235
  t.teardown(fastify.close.bind(fastify))
222
236
  fastify.listen(0)
223
237
  .then(() => {
224
- t.equal(fastify.server.address().address, '127.0.0.1')
238
+ t.equal(fastify.server.address().address, localhost)
225
239
  })
226
240
  })
227
241
 
@@ -231,7 +245,7 @@ test('listen without callback (port not given)', t => {
231
245
  t.teardown(fastify.close.bind(fastify))
232
246
  fastify.listen()
233
247
  .then(() => {
234
- t.equal(fastify.server.address().address, '127.0.0.1')
248
+ t.equal(fastify.server.address().address, localhost)
235
249
  })
236
250
  })
237
251
 
@@ -241,7 +255,7 @@ test('listen null without callback with (address)', t => {
241
255
  t.teardown(fastify.close.bind(fastify))
242
256
  fastify.listen(null)
243
257
  .then(address => {
244
- t.equal(address, 'http://127.0.0.1:' + fastify.server.address().port)
258
+ t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
245
259
  })
246
260
  })
247
261
 
@@ -251,7 +265,7 @@ test('listen without port without callback with (address)', t => {
251
265
  t.teardown(fastify.close.bind(fastify))
252
266
  fastify.listen()
253
267
  .then(address => {
254
- t.equal(address, 'http://127.0.0.1:' + fastify.server.address().port)
268
+ t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
255
269
  })
256
270
  })
257
271
 
@@ -261,7 +275,7 @@ test('listen with undefined without callback with (address)', t => {
261
275
  t.teardown(fastify.close.bind(fastify))
262
276
  fastify.listen(undefined)
263
277
  .then(address => {
264
- t.equal(address, 'http://127.0.0.1:' + fastify.server.address().port)
278
+ t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
265
279
  })
266
280
  })
267
281
 
@@ -271,7 +285,7 @@ test('listen without callback with (address)', t => {
271
285
  t.teardown(fastify.close.bind(fastify))
272
286
  fastify.listen(0)
273
287
  .then(address => {
274
- t.equal(address, 'http://127.0.0.1:' + fastify.server.address().port)
288
+ t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
275
289
  })
276
290
  })
277
291
 
@@ -295,7 +309,7 @@ test('double listen without callback with (address)', t => {
295
309
  t.teardown(fastify.close.bind(fastify))
296
310
  fastify.listen(0)
297
311
  .then(address => {
298
- t.equal(address, 'http://127.0.0.1:' + fastify.server.address().port)
312
+ t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
299
313
  fastify.listen(0)
300
314
  .catch(err => {
301
315
  t.ok(err)
@@ -329,7 +343,7 @@ test('listen twice on the same port without callback rejects with (address)', t
329
343
  .then(address => {
330
344
  const s2 = Fastify()
331
345
  t.teardown(s2.close.bind(s2))
332
- t.equal(address, 'http://127.0.0.1:' + fastify.server.address().port)
346
+ t.equal(address, `http://${localhostForURL}:${fastify.server.address().port}`)
333
347
  s2.listen(fastify.server.address().port)
334
348
  .catch(err => {
335
349
  t.ok(err)
@@ -157,10 +157,7 @@ test('default clientError handler destroys sockets in writable state', t => {
157
157
 
158
158
  const fastify = Fastify({
159
159
  bodyLimit: 1,
160
- keepAliveTimeout: 100,
161
- logger: {
162
- level: 'trace'
163
- }
160
+ keepAliveTimeout: 100
164
161
  })
165
162
 
166
163
  fastify.server.emit('clientError', new Error(), {
@@ -181,10 +178,7 @@ test('default clientError handler destroys http sockets in non-writable state',
181
178
 
182
179
  const fastify = Fastify({
183
180
  bodyLimit: 1,
184
- keepAliveTimeout: 100,
185
- logger: {
186
- level: 'trace'
187
- }
181
+ keepAliveTimeout: 100
188
182
  })
189
183
 
190
184
  fastify.server.emit('clientError', new Error(), {
@@ -5,6 +5,15 @@ const sget = require('simple-get')
5
5
  const Fastify = require('../')
6
6
  const { FST_ERR_BAD_URL } = require('../lib/errors')
7
7
 
8
+ function getUrl (app) {
9
+ const { address, port } = app.server.address()
10
+ if (address === '::1') {
11
+ return `http://[${address}]:${port}`
12
+ } else {
13
+ return `http://${address}:${port}`
14
+ }
15
+ }
16
+
8
17
  test('Should honor ignoreTrailingSlash option', t => {
9
18
  t.plan(4)
10
19
  const fastify = Fastify({
@@ -19,7 +28,7 @@ test('Should honor ignoreTrailingSlash option', t => {
19
28
  fastify.server.unref()
20
29
  if (err) t.threw(err)
21
30
 
22
- const baseUrl = 'http://127.0.0.1:' + fastify.server.address().port
31
+ const baseUrl = getUrl(fastify)
23
32
 
24
33
  sget.concat(baseUrl + '/test', (err, res, data) => {
25
34
  if (err) t.threw(err)
@@ -16,6 +16,15 @@ const Readable = require('stream').Readable
16
16
  const split = require('split2')
17
17
  const { kDisableRequestLogging, kReplySent } = require('../lib/symbols.js')
18
18
 
19
+ function getUrl (app) {
20
+ const { address, port } = app.server.address()
21
+ if (address === '::1') {
22
+ return `http://[${address}]:${port}`
23
+ } else {
24
+ return `http://${address}:${port}`
25
+ }
26
+ }
27
+
19
28
  test('should respond with a stream', t => {
20
29
  t.plan(8)
21
30
  const fastify = Fastify()
@@ -573,7 +582,7 @@ test('return a 404 if the stream emits a 404 error', t => {
573
582
  })
574
583
  })
575
584
 
576
- test('should support send module 200 and 404', t => {
585
+ test('should support send module 200 and 404', { only: true }, t => {
577
586
  t.plan(8)
578
587
  const fastify = Fastify()
579
588
 
@@ -591,7 +600,9 @@ test('should support send module 200 and 404', t => {
591
600
  t.error(err)
592
601
  fastify.server.unref()
593
602
 
594
- sget(`http://localhost:${fastify.server.address().port}`, function (err, response, data) {
603
+ const url = getUrl(fastify)
604
+
605
+ sget(url, function (err, response, data) {
595
606
  t.error(err)
596
607
  t.equal(response.headers['content-type'], 'application/octet-stream')
597
608
  t.equal(response.statusCode, 200)
@@ -602,7 +613,7 @@ test('should support send module 200 and 404', t => {
602
613
  })
603
614
  })
604
615
 
605
- sget(`http://localhost:${fastify.server.address().port}/error`, function (err, response) {
616
+ sget(url + '/error', function (err, response) {
606
617
  t.error(err)
607
618
  t.equal(response.statusCode, 404)
608
619
  })