fastify 5.3.3 → 5.5.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.
- package/.vscode/settings.json +15 -15
- package/LICENSE +1 -1
- package/README.md +2 -0
- package/SECURITY.md +158 -2
- package/build/build-validation.js +20 -1
- package/docs/Guides/Delay-Accepting-Requests.md +8 -5
- package/docs/Guides/Ecosystem.md +20 -5
- package/docs/Guides/Migration-Guide-V5.md +6 -10
- package/docs/Guides/Recommendations.md +1 -1
- package/docs/Reference/ContentTypeParser.md +1 -1
- package/docs/Reference/Errors.md +5 -3
- package/docs/Reference/Hooks.md +16 -20
- package/docs/Reference/Lifecycle.md +2 -2
- package/docs/Reference/Logging.md +3 -3
- package/docs/Reference/Middleware.md +1 -1
- package/docs/Reference/Reply.md +8 -8
- package/docs/Reference/Request.md +2 -2
- package/docs/Reference/Routes.md +7 -6
- package/docs/Reference/Server.md +341 -200
- package/docs/Reference/TypeScript.md +1 -3
- package/docs/Reference/Validation-and-Serialization.md +56 -4
- package/docs/Reference/Warnings.md +2 -1
- package/fastify.d.ts +4 -3
- package/fastify.js +47 -34
- package/lib/configValidator.js +196 -28
- package/lib/contentTypeParser.js +41 -48
- package/lib/error-handler.js +3 -3
- package/lib/errors.js +11 -0
- package/lib/handleRequest.js +13 -17
- package/lib/pluginOverride.js +3 -1
- package/lib/promise.js +23 -0
- package/lib/reply.js +24 -30
- package/lib/request.js +3 -10
- package/lib/route.js +37 -3
- package/lib/server.js +36 -35
- package/lib/symbols.js +1 -0
- package/lib/warnings.js +19 -1
- package/package.json +14 -10
- package/test/404s.test.js +226 -325
- package/test/allow-unsafe-regex.test.js +19 -48
- package/test/als.test.js +28 -40
- package/test/async-await.test.js +84 -128
- package/test/async_hooks.test.js +18 -37
- package/test/body-limit.test.js +90 -63
- package/test/buffer.test.js +22 -0
- package/test/build-certificate.js +1 -1
- package/test/case-insensitive.test.js +44 -65
- package/test/check.test.js +17 -21
- package/test/close-pipelining.test.js +24 -15
- package/test/constrained-routes.test.js +231 -0
- package/test/custom-http-server.test.js +7 -15
- package/test/custom-parser-async.test.js +17 -22
- package/test/custom-parser.0.test.js +267 -348
- package/test/custom-parser.1.test.js +141 -191
- package/test/custom-parser.2.test.js +34 -44
- package/test/custom-parser.3.test.js +56 -104
- package/test/custom-parser.4.test.js +106 -144
- package/test/custom-parser.5.test.js +56 -75
- package/test/custom-querystring-parser.test.js +51 -77
- package/test/decorator-namespace.test._js_ +3 -4
- package/test/decorator.test.js +76 -259
- package/test/delete.test.js +101 -110
- package/test/diagnostics-channel/404.test.js +7 -15
- package/test/diagnostics-channel/async-delay-request.test.js +7 -16
- package/test/diagnostics-channel/async-request.test.js +8 -16
- package/test/diagnostics-channel/error-request.test.js +7 -15
- package/test/diagnostics-channel/sync-delay-request.test.js +7 -16
- package/test/diagnostics-channel/sync-request-reply.test.js +9 -16
- package/test/diagnostics-channel/sync-request.test.js +9 -16
- package/test/fastify-instance.test.js +1 -1
- package/test/header-overflow.test.js +18 -29
- package/test/helper.js +139 -135
- package/test/hooks-async.test.js +259 -235
- package/test/hooks.test.js +951 -996
- package/test/http-methods/copy.test.js +14 -19
- package/test/http-methods/get.test.js +131 -143
- package/test/http-methods/head.test.js +53 -84
- package/test/http-methods/lock.test.js +31 -31
- package/test/http-methods/mkcalendar.test.js +45 -72
- package/test/http-methods/mkcol.test.js +5 -9
- package/test/http-methods/move.test.js +6 -10
- package/test/http-methods/propfind.test.js +34 -44
- package/test/http-methods/proppatch.test.js +23 -29
- package/test/http-methods/report.test.js +44 -69
- package/test/http-methods/search.test.js +67 -82
- package/test/http-methods/unlock.test.js +5 -9
- package/test/http2/closing.test.js +38 -20
- package/test/http2/secure-with-fallback.test.js +31 -28
- package/test/https/custom-https-server.test.js +9 -13
- package/test/https/https.test.js +56 -53
- package/test/input-validation.js +139 -150
- package/test/internals/errors.test.js +50 -1
- package/test/internals/handle-request.test.js +72 -65
- package/test/internals/promise.test.js +63 -0
- package/test/internals/reply.test.js +277 -496
- package/test/issue-4959.test.js +12 -3
- package/test/listen.4.test.js +31 -43
- package/test/nullable-validation.test.js +33 -46
- package/test/output-validation.test.js +24 -26
- package/test/plugin.1.test.js +40 -68
- package/test/plugin.2.test.js +108 -120
- package/test/plugin.3.test.js +50 -72
- package/test/plugin.4.test.js +124 -119
- package/test/promises.test.js +42 -63
- package/test/proto-poisoning.test.js +78 -97
- package/test/register.test.js +8 -18
- package/test/request-error.test.js +57 -146
- package/test/request-id.test.js +30 -49
- package/test/route-hooks.test.js +117 -101
- package/test/route-prefix.test.js +194 -133
- package/test/route-shorthand.test.js +9 -27
- package/test/route.1.test.js +74 -131
- package/test/route.8.test.js +9 -17
- package/test/router-options.test.js +450 -0
- package/test/schema-serialization.test.js +177 -154
- package/test/schema-special-usage.test.js +165 -132
- package/test/schema-validation.test.js +254 -218
- package/test/server.test.js +143 -5
- package/test/set-error-handler.test.js +58 -1
- package/test/skip-reply-send.test.js +64 -69
- package/test/stream.1.test.js +33 -50
- package/test/stream.4.test.js +18 -28
- package/test/stream.5.test.js +11 -19
- package/test/trust-proxy.test.js +32 -58
- package/test/types/errors.test-d.ts +13 -1
- package/test/types/fastify.test-d.ts +3 -0
- package/test/types/request.test-d.ts +1 -0
- package/test/types/type-provider.test-d.ts +55 -0
- package/test/url-rewriting.test.js +45 -62
- package/test/use-semicolon-delimiter.test.js +117 -59
- package/test/versioned-routes.test.js +39 -56
- package/types/errors.d.ts +11 -1
- package/types/hooks.d.ts +1 -1
- package/types/instance.d.ts +1 -1
- package/types/reply.d.ts +2 -2
- package/types/request.d.ts +1 -0
- package/.taprc +0 -7
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const { test } = require('node:test')
|
|
4
|
-
const sget = require('simple-get').concat
|
|
5
4
|
const http = require('node:http')
|
|
6
5
|
const NotFound = require('http-errors').NotFound
|
|
7
6
|
const Request = require('../../lib/request')
|
|
@@ -19,18 +18,17 @@ const {
|
|
|
19
18
|
const fs = require('node:fs')
|
|
20
19
|
const path = require('node:path')
|
|
21
20
|
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
if (err) {
|
|
28
|
-
reject(err)
|
|
29
|
-
} else {
|
|
30
|
-
resolve({ response, body })
|
|
31
|
-
}
|
|
32
|
-
})
|
|
21
|
+
const doGet = async function (url) {
|
|
22
|
+
const result = await fetch(url, {
|
|
23
|
+
method: 'GET',
|
|
24
|
+
redirect: 'manual',
|
|
25
|
+
keepAlive: false
|
|
33
26
|
})
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
response: result,
|
|
30
|
+
body: await result.json().catch(() => undefined)
|
|
31
|
+
}
|
|
34
32
|
}
|
|
35
33
|
|
|
36
34
|
test('Once called, Reply should return an object with methods', t => {
|
|
@@ -279,65 +277,44 @@ test('within an instance', async t => {
|
|
|
279
277
|
done()
|
|
280
278
|
})
|
|
281
279
|
|
|
282
|
-
await fastify.listen({ port: 0 })
|
|
280
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
283
281
|
|
|
284
|
-
await t.test('custom serializer should be used',
|
|
282
|
+
await t.test('custom serializer should be used', async t => {
|
|
285
283
|
t.plan(3)
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
t.assert.ifError(err)
|
|
291
|
-
t.assert.strictEqual(response.headers['content-type'], 'text/plain')
|
|
292
|
-
t.assert.deepStrictEqual(body.toString(), 'hello=world!')
|
|
293
|
-
done()
|
|
294
|
-
})
|
|
284
|
+
const result = await fetch(fastifyServer + '/custom-serializer')
|
|
285
|
+
t.assert.ok(result.ok)
|
|
286
|
+
t.assert.strictEqual(result.headers.get('content-type'), 'text/plain')
|
|
287
|
+
t.assert.deepStrictEqual(await result.text(), 'hello=world!')
|
|
295
288
|
})
|
|
296
289
|
|
|
297
|
-
await t.test('status code and content-type should be correct',
|
|
298
|
-
t.plan(
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
t.assert.ifError(err)
|
|
304
|
-
t.assert.strictEqual(response.statusCode, 200)
|
|
305
|
-
t.assert.strictEqual(response.headers['content-type'], 'text/plain')
|
|
306
|
-
t.assert.deepStrictEqual(body.toString(), 'hello world!')
|
|
307
|
-
done()
|
|
308
|
-
})
|
|
290
|
+
await t.test('status code and content-type should be correct', async t => {
|
|
291
|
+
t.plan(3)
|
|
292
|
+
const result = await fetch(fastifyServer)
|
|
293
|
+
t.assert.ok(result.ok)
|
|
294
|
+
t.assert.strictEqual(result.headers.get('content-type'), 'text/plain')
|
|
295
|
+
t.assert.deepStrictEqual(await result.text(), 'hello world!')
|
|
309
296
|
})
|
|
310
297
|
|
|
311
|
-
await t.test('auto status code should be 200',
|
|
298
|
+
await t.test('auto status code should be 200', async t => {
|
|
312
299
|
t.plan(3)
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
t.assert.ifError(err)
|
|
318
|
-
t.assert.strictEqual(response.statusCode, 200)
|
|
319
|
-
t.assert.deepStrictEqual(body.toString(), 'hello world!')
|
|
320
|
-
done()
|
|
321
|
-
})
|
|
300
|
+
const result = await fetch(fastifyServer + '/auto-status-code')
|
|
301
|
+
t.assert.ok(result.ok)
|
|
302
|
+
t.assert.strictEqual(result.status, 200)
|
|
303
|
+
t.assert.deepStrictEqual(await result.text(), 'hello world!')
|
|
322
304
|
})
|
|
323
305
|
|
|
324
|
-
await t.test('auto type should be text/plain',
|
|
306
|
+
await t.test('auto type should be text/plain', async t => {
|
|
325
307
|
t.plan(3)
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
t.assert.ifError(err)
|
|
331
|
-
t.assert.strictEqual(response.headers['content-type'], 'text/plain')
|
|
332
|
-
t.assert.deepStrictEqual(body.toString(), 'hello world!')
|
|
333
|
-
done()
|
|
334
|
-
})
|
|
308
|
+
const result = await fetch(fastifyServer + '/auto-type')
|
|
309
|
+
t.assert.ok(result.ok)
|
|
310
|
+
t.assert.strictEqual(result.headers.get('content-type'), 'text/plain')
|
|
311
|
+
t.assert.deepStrictEqual(await result.text(), 'hello world!')
|
|
335
312
|
})
|
|
336
313
|
|
|
337
314
|
await t.test('redirect to `/` - 1', (t, done) => {
|
|
338
315
|
t.plan(1)
|
|
339
316
|
|
|
340
|
-
http.get(
|
|
317
|
+
http.get(fastifyServer + '/redirect', function (response) {
|
|
341
318
|
t.assert.strictEqual(response.statusCode, 302)
|
|
342
319
|
done()
|
|
343
320
|
})
|
|
@@ -346,43 +323,33 @@ test('within an instance', async t => {
|
|
|
346
323
|
await t.test('redirect to `/` - 2', (t, done) => {
|
|
347
324
|
t.plan(1)
|
|
348
325
|
|
|
349
|
-
http.get(
|
|
326
|
+
http.get(fastifyServer + '/redirect-code', function (response) {
|
|
350
327
|
t.assert.strictEqual(response.statusCode, 301)
|
|
351
328
|
done()
|
|
352
329
|
})
|
|
353
330
|
})
|
|
354
331
|
|
|
355
|
-
await t.test('redirect to `/` - 3',
|
|
332
|
+
await t.test('redirect to `/` - 3', async t => {
|
|
356
333
|
t.plan(4)
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
t.assert.strictEqual(response.statusCode, 200)
|
|
363
|
-
t.assert.strictEqual(response.headers['content-type'], 'text/plain')
|
|
364
|
-
t.assert.deepStrictEqual(body.toString(), 'hello world!')
|
|
365
|
-
done()
|
|
366
|
-
})
|
|
334
|
+
const result = await fetch(fastifyServer + '/redirect')
|
|
335
|
+
t.assert.ok(result.ok)
|
|
336
|
+
t.assert.strictEqual(result.status, 200)
|
|
337
|
+
t.assert.strictEqual(result.headers.get('content-type'), 'text/plain')
|
|
338
|
+
t.assert.deepStrictEqual(await result.text(), 'hello world!')
|
|
367
339
|
})
|
|
368
340
|
|
|
369
|
-
await t.test('redirect to `/` - 4',
|
|
341
|
+
await t.test('redirect to `/` - 4', async t => {
|
|
370
342
|
t.plan(4)
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
t.assert.strictEqual(response.statusCode, 200)
|
|
377
|
-
t.assert.strictEqual(response.headers['content-type'], 'text/plain')
|
|
378
|
-
t.assert.deepStrictEqual(body.toString(), 'hello world!')
|
|
379
|
-
done()
|
|
380
|
-
})
|
|
343
|
+
const result = await fetch(fastifyServer + '/redirect-code')
|
|
344
|
+
t.assert.ok(result.ok)
|
|
345
|
+
t.assert.strictEqual(result.status, 200)
|
|
346
|
+
t.assert.strictEqual(result.headers.get('content-type'), 'text/plain')
|
|
347
|
+
t.assert.deepStrictEqual(await result.text(), 'hello world!')
|
|
381
348
|
})
|
|
382
349
|
|
|
383
350
|
await t.test('redirect to `/` - 5', (t, done) => {
|
|
384
351
|
t.plan(3)
|
|
385
|
-
const url =
|
|
352
|
+
const url = fastifyServer + '/redirect-onsend'
|
|
386
353
|
http.get(url, (response) => {
|
|
387
354
|
t.assert.strictEqual(response.headers['x-onsend'], 'yes')
|
|
388
355
|
t.assert.strictEqual(response.headers['content-length'], '0')
|
|
@@ -391,38 +358,28 @@ test('within an instance', async t => {
|
|
|
391
358
|
})
|
|
392
359
|
})
|
|
393
360
|
|
|
394
|
-
await t.test('redirect to `/` - 6',
|
|
361
|
+
await t.test('redirect to `/` - 6', async t => {
|
|
395
362
|
t.plan(4)
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
t.assert.strictEqual(response.statusCode, 200)
|
|
402
|
-
t.assert.strictEqual(response.headers['content-type'], 'text/plain')
|
|
403
|
-
t.assert.deepStrictEqual(body.toString(), 'hello world!')
|
|
404
|
-
done()
|
|
405
|
-
})
|
|
363
|
+
const result = await fetch(fastifyServer + '/redirect-code-before-call')
|
|
364
|
+
t.assert.ok(result.ok)
|
|
365
|
+
t.assert.strictEqual(result.status, 200)
|
|
366
|
+
t.assert.strictEqual(result.headers.get('content-type'), 'text/plain')
|
|
367
|
+
t.assert.deepStrictEqual(await result.text(), 'hello world!')
|
|
406
368
|
})
|
|
407
369
|
|
|
408
|
-
await t.test('redirect to `/` - 7',
|
|
370
|
+
await t.test('redirect to `/` - 7', async t => {
|
|
409
371
|
t.plan(4)
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
t.assert.strictEqual(response.statusCode, 200)
|
|
416
|
-
t.assert.strictEqual(response.headers['content-type'], 'text/plain')
|
|
417
|
-
t.assert.deepStrictEqual(body.toString(), 'hello world!')
|
|
418
|
-
done()
|
|
419
|
-
})
|
|
372
|
+
const result = await fetch(fastifyServer + '/redirect-code-before-call-overwrite')
|
|
373
|
+
t.assert.ok(result.ok)
|
|
374
|
+
t.assert.strictEqual(result.status, 200)
|
|
375
|
+
t.assert.strictEqual(result.headers.get('content-type'), 'text/plain')
|
|
376
|
+
t.assert.deepStrictEqual(await result.text(), 'hello world!')
|
|
420
377
|
})
|
|
421
378
|
|
|
422
379
|
await t.test('redirect to `/` - 8', (t, done) => {
|
|
423
380
|
t.plan(1)
|
|
424
381
|
|
|
425
|
-
http.get(
|
|
382
|
+
http.get(fastifyServer + '/redirect-code-before-call', function (response) {
|
|
426
383
|
t.assert.strictEqual(response.statusCode, 307)
|
|
427
384
|
done()
|
|
428
385
|
})
|
|
@@ -431,7 +388,7 @@ test('within an instance', async t => {
|
|
|
431
388
|
await t.test('redirect to `/` - 9', (t, done) => {
|
|
432
389
|
t.plan(1)
|
|
433
390
|
|
|
434
|
-
http.get(
|
|
391
|
+
http.get(fastifyServer + '/redirect-code-before-call-overwrite', function (response) {
|
|
435
392
|
t.assert.strictEqual(response.statusCode, 302)
|
|
436
393
|
done()
|
|
437
394
|
})
|
|
@@ -440,15 +397,15 @@ test('within an instance', async t => {
|
|
|
440
397
|
await t.test('redirect with async function to `/` - 10', (t, done) => {
|
|
441
398
|
t.plan(1)
|
|
442
399
|
|
|
443
|
-
http.get(
|
|
400
|
+
http.get(fastifyServer + '/redirect-async', function (response) {
|
|
444
401
|
t.assert.strictEqual(response.statusCode, 302)
|
|
445
402
|
done()
|
|
446
403
|
})
|
|
447
404
|
})
|
|
448
405
|
})
|
|
449
406
|
|
|
450
|
-
test('buffer without content type should send a application/octet-stream and raw buffer',
|
|
451
|
-
t.plan(
|
|
407
|
+
test('buffer without content type should send a application/octet-stream and raw buffer', async t => {
|
|
408
|
+
t.plan(3)
|
|
452
409
|
|
|
453
410
|
const fastify = Fastify()
|
|
454
411
|
|
|
@@ -456,20 +413,13 @@ test('buffer without content type should send a application/octet-stream and raw
|
|
|
456
413
|
reply.send(Buffer.alloc(1024))
|
|
457
414
|
})
|
|
458
415
|
|
|
459
|
-
fastify.listen({ port: 0 }
|
|
460
|
-
|
|
461
|
-
t.after(() => fastify.close())
|
|
416
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
417
|
+
t.after(() => fastify.close())
|
|
462
418
|
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
t.assert.ifError(err)
|
|
468
|
-
t.assert.strictEqual(response.headers['content-type'], 'application/octet-stream')
|
|
469
|
-
t.assert.deepStrictEqual(body, Buffer.alloc(1024))
|
|
470
|
-
done()
|
|
471
|
-
})
|
|
472
|
-
})
|
|
419
|
+
const result = await fetch(fastifyServer)
|
|
420
|
+
t.assert.ok(result.ok)
|
|
421
|
+
t.assert.strictEqual(result.headers.get('content-type'), 'application/octet-stream')
|
|
422
|
+
t.assert.deepStrictEqual(Buffer.from(await result.arrayBuffer()), Buffer.alloc(1024))
|
|
473
423
|
})
|
|
474
424
|
|
|
475
425
|
test('Uint8Array without content type should send a application/octet-stream and raw buffer', (t, done) => {
|
|
@@ -545,8 +495,8 @@ test('TypedArray with content type should not send application/octet-stream', (t
|
|
|
545
495
|
})
|
|
546
496
|
})
|
|
547
497
|
})
|
|
548
|
-
test('buffer with content type should not send application/octet-stream',
|
|
549
|
-
t.plan(
|
|
498
|
+
test('buffer with content type should not send application/octet-stream', async t => {
|
|
499
|
+
t.plan(3)
|
|
550
500
|
|
|
551
501
|
const fastify = Fastify()
|
|
552
502
|
|
|
@@ -555,24 +505,17 @@ test('buffer with content type should not send application/octet-stream', (t, do
|
|
|
555
505
|
reply.send(Buffer.alloc(1024))
|
|
556
506
|
})
|
|
557
507
|
|
|
558
|
-
fastify.listen({ port: 0 }
|
|
559
|
-
|
|
560
|
-
t.after(() => fastify.close())
|
|
508
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
509
|
+
t.after(() => fastify.close())
|
|
561
510
|
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
t.assert.ifError(err)
|
|
567
|
-
t.assert.strictEqual(response.headers['content-type'], 'text/plain')
|
|
568
|
-
t.assert.deepStrictEqual(body, Buffer.alloc(1024))
|
|
569
|
-
done()
|
|
570
|
-
})
|
|
571
|
-
})
|
|
511
|
+
const result = await fetch(fastifyServer)
|
|
512
|
+
t.assert.ok(result.ok)
|
|
513
|
+
t.assert.strictEqual(result.headers.get('content-type'), 'text/plain')
|
|
514
|
+
t.assert.deepStrictEqual(Buffer.from(await result.arrayBuffer()), Buffer.alloc(1024))
|
|
572
515
|
})
|
|
573
516
|
|
|
574
|
-
test('stream with content type should not send application/octet-stream',
|
|
575
|
-
t.plan(
|
|
517
|
+
test('stream with content type should not send application/octet-stream', async t => {
|
|
518
|
+
t.plan(3)
|
|
576
519
|
|
|
577
520
|
const fastify = Fastify()
|
|
578
521
|
|
|
@@ -584,23 +527,17 @@ test('stream with content type should not send application/octet-stream', (t, do
|
|
|
584
527
|
reply.header('Content-Type', 'text/plain').send(stream)
|
|
585
528
|
})
|
|
586
529
|
|
|
587
|
-
fastify.listen({ port: 0 }
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
t.assert.ifError(err)
|
|
595
|
-
t.assert.strictEqual(response.headers['content-type'], 'text/plain')
|
|
596
|
-
t.assert.deepStrictEqual(body, buf)
|
|
597
|
-
done()
|
|
598
|
-
})
|
|
599
|
-
})
|
|
530
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
531
|
+
t.after(() => fastify.close())
|
|
532
|
+
|
|
533
|
+
const result = await fetch(fastifyServer)
|
|
534
|
+
t.assert.ok(result.ok)
|
|
535
|
+
t.assert.strictEqual(result.headers.get('content-type'), 'text/plain')
|
|
536
|
+
t.assert.deepStrictEqual(Buffer.from(await result.arrayBuffer()), buf)
|
|
600
537
|
})
|
|
601
538
|
|
|
602
|
-
test('stream without content type should not send application/octet-stream',
|
|
603
|
-
t.plan(
|
|
539
|
+
test('stream without content type should not send application/octet-stream', async t => {
|
|
540
|
+
t.plan(3)
|
|
604
541
|
|
|
605
542
|
const fastify = Fastify()
|
|
606
543
|
|
|
@@ -611,23 +548,17 @@ test('stream without content type should not send application/octet-stream', (t,
|
|
|
611
548
|
reply.send(stream)
|
|
612
549
|
})
|
|
613
550
|
|
|
614
|
-
fastify.listen({ port: 0 }
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
t.assert.ifError(err)
|
|
622
|
-
t.assert.strictEqual(response.headers['content-type'], undefined)
|
|
623
|
-
t.assert.deepStrictEqual(body, buf)
|
|
624
|
-
done()
|
|
625
|
-
})
|
|
626
|
-
})
|
|
551
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
552
|
+
t.after(() => fastify.close())
|
|
553
|
+
|
|
554
|
+
const result = await fetch(fastifyServer)
|
|
555
|
+
t.assert.ok(result.ok)
|
|
556
|
+
t.assert.strictEqual(result.headers.get('content-type'), null)
|
|
557
|
+
t.assert.deepStrictEqual(Buffer.from(await result.arrayBuffer()), buf)
|
|
627
558
|
})
|
|
628
559
|
|
|
629
|
-
test('stream using reply.raw.writeHead should return customize headers',
|
|
630
|
-
t.plan(
|
|
560
|
+
test('stream using reply.raw.writeHead should return customize headers', async t => {
|
|
561
|
+
t.plan(5)
|
|
631
562
|
|
|
632
563
|
const fastify = Fastify()
|
|
633
564
|
const fs = require('node:fs')
|
|
@@ -647,24 +578,18 @@ test('stream using reply.raw.writeHead should return customize headers', (t, don
|
|
|
647
578
|
reply.send(stream)
|
|
648
579
|
})
|
|
649
580
|
|
|
650
|
-
fastify.listen({ port: 0 }
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
t.assert.strictEqual(response.headers.location, '/')
|
|
659
|
-
t.assert.strictEqual(response.headers['Content-Type'], undefined)
|
|
660
|
-
t.assert.deepStrictEqual(body, buf)
|
|
661
|
-
done()
|
|
662
|
-
})
|
|
663
|
-
})
|
|
581
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
582
|
+
t.after(() => fastify.close())
|
|
583
|
+
|
|
584
|
+
const result = await fetch(fastifyServer)
|
|
585
|
+
t.assert.ok(result.ok)
|
|
586
|
+
t.assert.strictEqual(result.headers.get('location'), '/')
|
|
587
|
+
t.assert.strictEqual(result.headers.get('content-type'), null)
|
|
588
|
+
t.assert.deepStrictEqual(Buffer.from(await result.arrayBuffer()), buf)
|
|
664
589
|
})
|
|
665
590
|
|
|
666
|
-
test('plain string without content type should send a text/plain',
|
|
667
|
-
t.plan(
|
|
591
|
+
test('plain string without content type should send a text/plain', async t => {
|
|
592
|
+
t.plan(3)
|
|
668
593
|
|
|
669
594
|
const fastify = Fastify()
|
|
670
595
|
|
|
@@ -672,24 +597,17 @@ test('plain string without content type should send a text/plain', (t, done) =>
|
|
|
672
597
|
reply.send('hello world!')
|
|
673
598
|
})
|
|
674
599
|
|
|
675
|
-
fastify.listen({ port: 0 }
|
|
676
|
-
|
|
677
|
-
t.after(() => fastify.close())
|
|
600
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
601
|
+
t.after(() => fastify.close())
|
|
678
602
|
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
t.assert.ifError(err)
|
|
684
|
-
t.assert.strictEqual(response.headers['content-type'], 'text/plain; charset=utf-8')
|
|
685
|
-
t.assert.deepStrictEqual(body.toString(), 'hello world!')
|
|
686
|
-
done()
|
|
687
|
-
})
|
|
688
|
-
})
|
|
603
|
+
const result = await fetch(fastifyServer)
|
|
604
|
+
t.assert.ok(result.ok)
|
|
605
|
+
t.assert.strictEqual(result.headers.get('content-type'), 'text/plain; charset=utf-8')
|
|
606
|
+
t.assert.deepStrictEqual(await result.text(), 'hello world!')
|
|
689
607
|
})
|
|
690
608
|
|
|
691
|
-
test('plain string with content type should be sent unmodified',
|
|
692
|
-
t.plan(
|
|
609
|
+
test('plain string with content type should be sent unmodified', async t => {
|
|
610
|
+
t.plan(3)
|
|
693
611
|
|
|
694
612
|
const fastify = Fastify()
|
|
695
613
|
|
|
@@ -697,24 +615,17 @@ test('plain string with content type should be sent unmodified', (t, done) => {
|
|
|
697
615
|
reply.type('text/css').send('hello world!')
|
|
698
616
|
})
|
|
699
617
|
|
|
700
|
-
fastify.listen({ port: 0 }
|
|
701
|
-
|
|
702
|
-
t.after(() => fastify.close())
|
|
618
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
619
|
+
t.after(() => fastify.close())
|
|
703
620
|
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
t.assert.ifError(err)
|
|
709
|
-
t.assert.strictEqual(response.headers['content-type'], 'text/css')
|
|
710
|
-
t.assert.deepStrictEqual(body.toString(), 'hello world!')
|
|
711
|
-
done()
|
|
712
|
-
})
|
|
713
|
-
})
|
|
621
|
+
const result = await fetch(fastifyServer)
|
|
622
|
+
t.assert.ok(result.ok)
|
|
623
|
+
t.assert.strictEqual(result.headers.get('content-type'), 'text/css')
|
|
624
|
+
t.assert.deepStrictEqual(await result.text(), 'hello world!')
|
|
714
625
|
})
|
|
715
626
|
|
|
716
|
-
test('plain string with content type and custom serializer should be serialized',
|
|
717
|
-
t.plan(
|
|
627
|
+
test('plain string with content type and custom serializer should be serialized', async t => {
|
|
628
|
+
t.plan(3)
|
|
718
629
|
|
|
719
630
|
const fastify = Fastify()
|
|
720
631
|
|
|
@@ -725,24 +636,17 @@ test('plain string with content type and custom serializer should be serialized'
|
|
|
725
636
|
.send('hello world!')
|
|
726
637
|
})
|
|
727
638
|
|
|
728
|
-
fastify.listen({ port: 0 }
|
|
729
|
-
|
|
730
|
-
t.after(() => fastify.close())
|
|
639
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
640
|
+
t.after(() => fastify.close())
|
|
731
641
|
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
t.assert.ifError(err)
|
|
737
|
-
t.assert.strictEqual(response.headers['content-type'], 'text/css')
|
|
738
|
-
t.assert.deepStrictEqual(body.toString(), 'serialized')
|
|
739
|
-
done()
|
|
740
|
-
})
|
|
741
|
-
})
|
|
642
|
+
const result = await fetch(fastifyServer)
|
|
643
|
+
t.assert.ok(result.ok)
|
|
644
|
+
t.assert.strictEqual(result.headers.get('content-type'), 'text/css')
|
|
645
|
+
t.assert.deepStrictEqual(await result.text(), 'serialized')
|
|
742
646
|
})
|
|
743
647
|
|
|
744
|
-
test('plain string with content type application/json should NOT be serialized as json',
|
|
745
|
-
t.plan(
|
|
648
|
+
test('plain string with content type application/json should NOT be serialized as json', async t => {
|
|
649
|
+
t.plan(3)
|
|
746
650
|
|
|
747
651
|
const fastify = Fastify()
|
|
748
652
|
|
|
@@ -750,24 +654,17 @@ test('plain string with content type application/json should NOT be serialized a
|
|
|
750
654
|
reply.type('application/json').send('{"key": "hello world!"}')
|
|
751
655
|
})
|
|
752
656
|
|
|
753
|
-
fastify.listen({ port: 0 }
|
|
754
|
-
|
|
755
|
-
t.after(() => fastify.close())
|
|
657
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
658
|
+
t.after(() => fastify.close())
|
|
756
659
|
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
t.assert.ifError(err)
|
|
762
|
-
t.assert.strictEqual(response.headers['content-type'], 'application/json; charset=utf-8')
|
|
763
|
-
t.assert.deepStrictEqual(body.toString(), '{"key": "hello world!"}')
|
|
764
|
-
done()
|
|
765
|
-
})
|
|
766
|
-
})
|
|
660
|
+
const result = await fetch(fastifyServer)
|
|
661
|
+
t.assert.ok(result.ok)
|
|
662
|
+
t.assert.strictEqual(result.headers.get('content-type'), 'application/json; charset=utf-8')
|
|
663
|
+
t.assert.deepStrictEqual(await result.text(), '{"key": "hello world!"}')
|
|
767
664
|
})
|
|
768
665
|
|
|
769
666
|
test('plain string with custom json content type should NOT be serialized as json', async t => {
|
|
770
|
-
t.plan(
|
|
667
|
+
t.plan(18)
|
|
771
668
|
|
|
772
669
|
const fastify = Fastify()
|
|
773
670
|
|
|
@@ -806,27 +703,18 @@ test('plain string with custom json content type should NOT be serialized as jso
|
|
|
806
703
|
})
|
|
807
704
|
})
|
|
808
705
|
|
|
809
|
-
await fastify.listen({ port: 0 })
|
|
706
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
810
707
|
|
|
811
|
-
await Promise.all(Object.keys(customSamples).map(path => {
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
}, (err, response, body) => {
|
|
817
|
-
if (err) {
|
|
818
|
-
reject(err)
|
|
819
|
-
}
|
|
820
|
-
t.assert.strictEqual(response.headers['content-type'], customSamples[path].mimeType + '; charset=utf-8')
|
|
821
|
-
t.assert.deepStrictEqual(body.toString(), customSamples[path].sample)
|
|
822
|
-
resolve()
|
|
823
|
-
})
|
|
824
|
-
})
|
|
708
|
+
await Promise.all(Object.keys(customSamples).map(async path => {
|
|
709
|
+
const result = await fetch(fastifyServer + '/' + path)
|
|
710
|
+
t.assert.ok(result.ok)
|
|
711
|
+
t.assert.strictEqual(result.headers.get('content-type'), customSamples[path].mimeType + '; charset=utf-8')
|
|
712
|
+
t.assert.deepStrictEqual(await result.text(), customSamples[path].sample)
|
|
825
713
|
}))
|
|
826
714
|
})
|
|
827
715
|
|
|
828
|
-
test('non-string with content type application/json SHOULD be serialized as json',
|
|
829
|
-
t.plan(
|
|
716
|
+
test('non-string with content type application/json SHOULD be serialized as json', async t => {
|
|
717
|
+
t.plan(3)
|
|
830
718
|
|
|
831
719
|
const fastify = Fastify()
|
|
832
720
|
|
|
@@ -834,24 +722,17 @@ test('non-string with content type application/json SHOULD be serialized as json
|
|
|
834
722
|
reply.type('application/json').send({ key: 'hello world!' })
|
|
835
723
|
})
|
|
836
724
|
|
|
837
|
-
fastify.listen({ port: 0 }
|
|
838
|
-
|
|
839
|
-
t.after(() => fastify.close())
|
|
725
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
726
|
+
t.after(() => fastify.close())
|
|
840
727
|
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
t.assert.ifError(err)
|
|
846
|
-
t.assert.strictEqual(response.headers['content-type'], 'application/json; charset=utf-8')
|
|
847
|
-
t.assert.deepStrictEqual(body.toString(), JSON.stringify({ key: 'hello world!' }))
|
|
848
|
-
done()
|
|
849
|
-
})
|
|
850
|
-
})
|
|
728
|
+
const result = await fetch(fastifyServer)
|
|
729
|
+
t.assert.ok(result.ok)
|
|
730
|
+
t.assert.strictEqual(result.headers.get('content-type'), 'application/json; charset=utf-8')
|
|
731
|
+
t.assert.deepStrictEqual(await result.text(), JSON.stringify({ key: 'hello world!' }))
|
|
851
732
|
})
|
|
852
733
|
|
|
853
|
-
test('non-string with custom json\'s content-type SHOULD be serialized as json',
|
|
854
|
-
t.plan(
|
|
734
|
+
test('non-string with custom json\'s content-type SHOULD be serialized as json', async t => {
|
|
735
|
+
t.plan(3)
|
|
855
736
|
|
|
856
737
|
const fastify = Fastify()
|
|
857
738
|
|
|
@@ -859,24 +740,17 @@ test('non-string with custom json\'s content-type SHOULD be serialized as json',
|
|
|
859
740
|
reply.type('application/json; version=2; ').send({ key: 'hello world!' })
|
|
860
741
|
})
|
|
861
742
|
|
|
862
|
-
fastify.listen({ port: 0 }
|
|
863
|
-
|
|
864
|
-
t.after(() => fastify.close())
|
|
743
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
744
|
+
t.after(() => fastify.close())
|
|
865
745
|
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
t.assert.ifError(err)
|
|
871
|
-
t.assert.strictEqual(response.headers['content-type'], 'application/json; version=2; charset=utf-8')
|
|
872
|
-
t.assert.deepStrictEqual(body.toString(), JSON.stringify({ key: 'hello world!' }))
|
|
873
|
-
done()
|
|
874
|
-
})
|
|
875
|
-
})
|
|
746
|
+
const result = await fetch(fastifyServer)
|
|
747
|
+
t.assert.ok(result.ok)
|
|
748
|
+
t.assert.strictEqual(result.headers.get('content-type'), 'application/json; version=2; charset=utf-8')
|
|
749
|
+
t.assert.deepStrictEqual(await result.text(), JSON.stringify({ key: 'hello world!' }))
|
|
876
750
|
})
|
|
877
751
|
|
|
878
752
|
test('non-string with custom json content type SHOULD be serialized as json', async t => {
|
|
879
|
-
t.plan(
|
|
753
|
+
t.plan(15)
|
|
880
754
|
|
|
881
755
|
const fastify = Fastify()
|
|
882
756
|
t.after(() => fastify.close())
|
|
@@ -910,22 +784,13 @@ test('non-string with custom json content type SHOULD be serialized as json', as
|
|
|
910
784
|
})
|
|
911
785
|
})
|
|
912
786
|
|
|
913
|
-
await fastify.listen({ port: 0 })
|
|
787
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
914
788
|
|
|
915
|
-
await Promise.all(Object.keys(customSamples).map(path => {
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
}, (err, response, body) => {
|
|
921
|
-
if (err) {
|
|
922
|
-
reject(err)
|
|
923
|
-
}
|
|
924
|
-
t.assert.strictEqual(response.headers['content-type'], customSamples[path].mimeType + '; charset=utf-8')
|
|
925
|
-
t.assert.deepStrictEqual(body.toString(), JSON.stringify(customSamples[path].sample))
|
|
926
|
-
resolve()
|
|
927
|
-
})
|
|
928
|
-
})
|
|
789
|
+
await Promise.all(Object.keys(customSamples).map(async path => {
|
|
790
|
+
const result = await fetch(fastifyServer + '/' + path)
|
|
791
|
+
t.assert.ok(result.ok)
|
|
792
|
+
t.assert.strictEqual(result.headers.get('content-type'), customSamples[path].mimeType + '; charset=utf-8')
|
|
793
|
+
t.assert.deepStrictEqual(await result.text(), JSON.stringify(customSamples[path].sample))
|
|
929
794
|
}))
|
|
930
795
|
})
|
|
931
796
|
|
|
@@ -963,8 +828,8 @@ test('error object with a content type that is not application/json should work'
|
|
|
963
828
|
}
|
|
964
829
|
})
|
|
965
830
|
|
|
966
|
-
test('undefined payload should be sent as-is',
|
|
967
|
-
t.plan(
|
|
831
|
+
test('undefined payload should be sent as-is', async t => {
|
|
832
|
+
t.plan(5)
|
|
968
833
|
|
|
969
834
|
const fastify = Fastify()
|
|
970
835
|
|
|
@@ -977,25 +842,19 @@ test('undefined payload should be sent as-is', (t, done) => {
|
|
|
977
842
|
reply.code(204).send()
|
|
978
843
|
})
|
|
979
844
|
|
|
980
|
-
fastify.listen({ port: 0 }
|
|
981
|
-
|
|
982
|
-
t.after(() => fastify.close())
|
|
845
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
846
|
+
t.after(() => fastify.close())
|
|
983
847
|
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
t.assert.strictEqual(response.headers['content-length'], undefined)
|
|
991
|
-
t.assert.strictEqual(body.length, 0)
|
|
992
|
-
done()
|
|
993
|
-
})
|
|
994
|
-
})
|
|
848
|
+
const result = await fetch(fastifyServer)
|
|
849
|
+
t.assert.ok(result.ok)
|
|
850
|
+
t.assert.strictEqual(result.headers.get('content-type'), null)
|
|
851
|
+
t.assert.strictEqual(result.headers.get('content-length'), null)
|
|
852
|
+
const body = await result.text()
|
|
853
|
+
t.assert.strictEqual(body.length, 0)
|
|
995
854
|
})
|
|
996
855
|
|
|
997
856
|
test('for HEAD method, no body should be sent but content-length should be', async t => {
|
|
998
|
-
t.plan(
|
|
857
|
+
t.plan(10)
|
|
999
858
|
|
|
1000
859
|
const fastify = Fastify()
|
|
1001
860
|
t.after(() => fastify.close())
|
|
@@ -1024,37 +883,23 @@ test('for HEAD method, no body should be sent but content-length should be', asy
|
|
|
1024
883
|
reply.code(200).send(null)
|
|
1025
884
|
})
|
|
1026
885
|
|
|
1027
|
-
await fastify.listen({ port: 0 })
|
|
886
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
1028
887
|
|
|
1029
|
-
const promise1 =
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
}
|
|
1037
|
-
t.assert.strictEqual(response.headers['content-type'], contentType)
|
|
1038
|
-
t.assert.strictEqual(response.headers['content-length'], bodySize.toString())
|
|
1039
|
-
t.assert.strictEqual(body.length, 0)
|
|
1040
|
-
resolve()
|
|
1041
|
-
})
|
|
1042
|
-
})
|
|
888
|
+
const promise1 = (async () => {
|
|
889
|
+
const result = await fetch(fastifyServer, { method: 'HEAD' })
|
|
890
|
+
t.assert.ok(result.ok)
|
|
891
|
+
t.assert.strictEqual(result.headers.get('content-type'), contentType)
|
|
892
|
+
t.assert.strictEqual(result.headers.get('content-length'), bodySize.toString())
|
|
893
|
+
t.assert.strictEqual((await result.text()).length, 0)
|
|
894
|
+
})()
|
|
1043
895
|
|
|
1044
|
-
const promise2 =
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
}
|
|
1052
|
-
t.assert.strictEqual(response.headers['content-type'], contentType)
|
|
1053
|
-
t.assert.strictEqual(response.headers['content-length'], bodySize.toString())
|
|
1054
|
-
t.assert.strictEqual(body.length, 0)
|
|
1055
|
-
resolve()
|
|
1056
|
-
})
|
|
1057
|
-
})
|
|
896
|
+
const promise2 = (async () => {
|
|
897
|
+
const result = await fetch(fastifyServer + '/with/null', { method: 'HEAD' })
|
|
898
|
+
t.assert.ok(result.ok)
|
|
899
|
+
t.assert.strictEqual(result.headers.get('content-type'), contentType)
|
|
900
|
+
t.assert.strictEqual(result.headers.get('content-length'), bodySize.toString())
|
|
901
|
+
t.assert.strictEqual((await result.text()).length, 0)
|
|
902
|
+
})()
|
|
1058
903
|
|
|
1059
904
|
await Promise.all([promise1, promise2])
|
|
1060
905
|
})
|
|
@@ -1081,51 +926,35 @@ test('reply.send(new NotFound()) should not invoke the 404 handler', async t =>
|
|
|
1081
926
|
done()
|
|
1082
927
|
}, { prefix: '/prefixed' })
|
|
1083
928
|
|
|
1084
|
-
await fastify.listen({ port: 0 })
|
|
929
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
1085
930
|
|
|
1086
|
-
const promise1 =
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
t.assert.strictEqual(response.statusCode, 404)
|
|
1095
|
-
t.assert.strictEqual(response.headers['content-type'], 'application/json; charset=utf-8')
|
|
1096
|
-
t.assert.deepStrictEqual(JSON.parse(body.toString()), {
|
|
1097
|
-
statusCode: 404,
|
|
1098
|
-
error: 'Not Found',
|
|
1099
|
-
message: 'Not Found'
|
|
1100
|
-
})
|
|
1101
|
-
resolve()
|
|
931
|
+
const promise1 = (async () => {
|
|
932
|
+
const result = await fetch(`${fastifyServer}/not-found`)
|
|
933
|
+
t.assert.strictEqual(result.status, 404)
|
|
934
|
+
t.assert.strictEqual(result.headers.get('content-type'), 'application/json; charset=utf-8')
|
|
935
|
+
t.assert.deepStrictEqual(JSON.parse(await result.text()), {
|
|
936
|
+
statusCode: 404,
|
|
937
|
+
error: 'Not Found',
|
|
938
|
+
message: 'Not Found'
|
|
1102
939
|
})
|
|
1103
|
-
})
|
|
940
|
+
})()
|
|
1104
941
|
|
|
1105
|
-
const promise2 =
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
t.assert.strictEqual(response.statusCode, 404)
|
|
1114
|
-
t.assert.strictEqual(response.headers['content-type'], 'application/json; charset=utf-8')
|
|
1115
|
-
t.assert.deepStrictEqual(JSON.parse(body.toString()), {
|
|
1116
|
-
statusCode: 404,
|
|
1117
|
-
error: 'Not Found',
|
|
1118
|
-
message: 'Not Found'
|
|
1119
|
-
})
|
|
1120
|
-
resolve()
|
|
942
|
+
const promise2 = (async () => {
|
|
943
|
+
const result = await fetch(`${fastifyServer}/prefixed/not-found`)
|
|
944
|
+
t.assert.strictEqual(result.status, 404)
|
|
945
|
+
t.assert.strictEqual(result.headers.get('content-type'), 'application/json; charset=utf-8')
|
|
946
|
+
t.assert.deepStrictEqual(JSON.parse(await result.text()), {
|
|
947
|
+
statusCode: 404,
|
|
948
|
+
error: 'Not Found',
|
|
949
|
+
message: 'Not Found'
|
|
1121
950
|
})
|
|
1122
|
-
})
|
|
951
|
+
})()
|
|
1123
952
|
|
|
1124
953
|
await Promise.all([promise1, promise2])
|
|
1125
954
|
})
|
|
1126
955
|
|
|
1127
|
-
test('reply can set multiple instances of same header',
|
|
1128
|
-
t.plan(
|
|
956
|
+
test('reply can set multiple instances of same header', async t => {
|
|
957
|
+
t.plan(3)
|
|
1129
958
|
|
|
1130
959
|
const fastify = require('../../')()
|
|
1131
960
|
|
|
@@ -1136,24 +965,17 @@ test('reply can set multiple instances of same header', (t, done) => {
|
|
|
1136
965
|
.send({})
|
|
1137
966
|
})
|
|
1138
967
|
|
|
1139
|
-
fastify.listen({ port: 0 }
|
|
1140
|
-
|
|
1141
|
-
t.after(() => fastify.close())
|
|
968
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
969
|
+
t.after(() => fastify.close())
|
|
1142
970
|
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
t.assert.ifError(err)
|
|
1148
|
-
t.assert.ok(response.headers['set-cookie'])
|
|
1149
|
-
t.assert.deepStrictEqual(response.headers['set-cookie'], ['one', 'two'])
|
|
1150
|
-
done()
|
|
1151
|
-
})
|
|
1152
|
-
})
|
|
971
|
+
const result = await fetch(`${fastifyServer}/headers`)
|
|
972
|
+
t.assert.ok(result.ok)
|
|
973
|
+
t.assert.ok(result.headers.get('set-cookie'))
|
|
974
|
+
t.assert.deepStrictEqual(result.headers.getSetCookie(), ['one', 'two'])
|
|
1153
975
|
})
|
|
1154
976
|
|
|
1155
|
-
test('reply.hasHeader returns correct values',
|
|
1156
|
-
t.plan(
|
|
977
|
+
test('reply.hasHeader returns correct values', async t => {
|
|
978
|
+
t.plan(2)
|
|
1157
979
|
|
|
1158
980
|
const fastify = require('../../')()
|
|
1159
981
|
|
|
@@ -1164,20 +986,14 @@ test('reply.hasHeader returns correct values', (t, done) => {
|
|
|
1164
986
|
reply.send()
|
|
1165
987
|
})
|
|
1166
988
|
|
|
1167
|
-
fastify.listen({ port: 0 }
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
method: 'GET',
|
|
1172
|
-
url: 'http://127.0.0.1:' + fastify.server.address().port + '/headers'
|
|
1173
|
-
}, () => {
|
|
1174
|
-
done()
|
|
1175
|
-
})
|
|
1176
|
-
})
|
|
989
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
990
|
+
t.after(() => fastify.close())
|
|
991
|
+
|
|
992
|
+
await fetch(`${fastifyServer}/headers`)
|
|
1177
993
|
})
|
|
1178
994
|
|
|
1179
|
-
test('reply.getHeader returns correct values',
|
|
1180
|
-
t.plan(
|
|
995
|
+
test('reply.getHeader returns correct values', async t => {
|
|
996
|
+
t.plan(4)
|
|
1181
997
|
|
|
1182
998
|
const fastify = require('../../')()
|
|
1183
999
|
|
|
@@ -1198,16 +1014,10 @@ test('reply.getHeader returns correct values', (t, done) => {
|
|
|
1198
1014
|
reply.send()
|
|
1199
1015
|
})
|
|
1200
1016
|
|
|
1201
|
-
fastify.listen({ port: 0 }
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
method: 'GET',
|
|
1206
|
-
url: 'http://127.0.0.1:' + fastify.server.address().port + '/headers'
|
|
1207
|
-
}, () => {
|
|
1208
|
-
done()
|
|
1209
|
-
})
|
|
1210
|
-
})
|
|
1017
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
1018
|
+
t.after(() => fastify.close())
|
|
1019
|
+
|
|
1020
|
+
await fetch(`${fastifyServer}/headers`)
|
|
1211
1021
|
})
|
|
1212
1022
|
|
|
1213
1023
|
test('reply.getHeader returns raw header if there is not in the reply headers', (t) => {
|
|
@@ -1254,8 +1064,8 @@ test('reply.getHeaders returns correct values', (t, done) => {
|
|
|
1254
1064
|
})
|
|
1255
1065
|
})
|
|
1256
1066
|
|
|
1257
|
-
test('reply.removeHeader can remove the value',
|
|
1258
|
-
t.plan(
|
|
1067
|
+
test('reply.removeHeader can remove the value', async t => {
|
|
1068
|
+
t.plan(3)
|
|
1259
1069
|
|
|
1260
1070
|
const fastify = require('../../')()
|
|
1261
1071
|
|
|
@@ -1271,20 +1081,13 @@ test('reply.removeHeader can remove the value', (t, done) => {
|
|
|
1271
1081
|
reply.send()
|
|
1272
1082
|
})
|
|
1273
1083
|
|
|
1274
|
-
fastify.listen({ port: 0 }
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
sget({
|
|
1278
|
-
method: 'GET',
|
|
1279
|
-
url: 'http://127.0.0.1:' + fastify.server.address().port + '/headers'
|
|
1280
|
-
}, () => {
|
|
1281
|
-
done()
|
|
1282
|
-
})
|
|
1283
|
-
})
|
|
1084
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
1085
|
+
|
|
1086
|
+
await fetch(`${fastifyServer}/headers`)
|
|
1284
1087
|
})
|
|
1285
1088
|
|
|
1286
|
-
test('reply.header can reset the value',
|
|
1287
|
-
t.plan(
|
|
1089
|
+
test('reply.header can reset the value', async t => {
|
|
1090
|
+
t.plan(1)
|
|
1288
1091
|
|
|
1289
1092
|
const fastify = require('../../')()
|
|
1290
1093
|
|
|
@@ -1298,21 +1101,14 @@ test('reply.header can reset the value', (t, done) => {
|
|
|
1298
1101
|
reply.send()
|
|
1299
1102
|
})
|
|
1300
1103
|
|
|
1301
|
-
fastify.listen({ port: 0 }
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
sget({
|
|
1305
|
-
method: 'GET',
|
|
1306
|
-
url: 'http://127.0.0.1:' + fastify.server.address().port + '/headers'
|
|
1307
|
-
}, () => {
|
|
1308
|
-
done()
|
|
1309
|
-
})
|
|
1310
|
-
})
|
|
1104
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
1105
|
+
|
|
1106
|
+
await fetch(`${fastifyServer}/headers`)
|
|
1311
1107
|
})
|
|
1312
1108
|
|
|
1313
1109
|
// https://github.com/fastify/fastify/issues/3030
|
|
1314
|
-
test('reply.hasHeader computes raw and fastify headers',
|
|
1315
|
-
t.plan(
|
|
1110
|
+
test('reply.hasHeader computes raw and fastify headers', async t => {
|
|
1111
|
+
t.plan(2)
|
|
1316
1112
|
|
|
1317
1113
|
const fastify = require('../../')()
|
|
1318
1114
|
|
|
@@ -1327,16 +1123,10 @@ test('reply.hasHeader computes raw and fastify headers', (t, done) => {
|
|
|
1327
1123
|
reply.send()
|
|
1328
1124
|
})
|
|
1329
1125
|
|
|
1330
|
-
fastify.listen({ port: 0 }
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
method: 'GET',
|
|
1335
|
-
url: 'http://127.0.0.1:' + fastify.server.address().port + '/headers'
|
|
1336
|
-
}, () => {
|
|
1337
|
-
done()
|
|
1338
|
-
})
|
|
1339
|
-
})
|
|
1126
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
1127
|
+
t.after(() => fastify.close())
|
|
1128
|
+
|
|
1129
|
+
await fetch(`${fastifyServer}/headers`)
|
|
1340
1130
|
})
|
|
1341
1131
|
|
|
1342
1132
|
test('Reply should handle JSON content type with a charset', async t => {
|
|
@@ -1485,7 +1275,7 @@ test('.statusCode is getter and setter', (t, done) => {
|
|
|
1485
1275
|
})
|
|
1486
1276
|
|
|
1487
1277
|
test('reply.header setting multiple cookies as multiple Set-Cookie headers', async t => {
|
|
1488
|
-
t.plan(
|
|
1278
|
+
t.plan(5)
|
|
1489
1279
|
|
|
1490
1280
|
const fastify = require('../../')()
|
|
1491
1281
|
t.after(() => fastify.close())
|
|
@@ -1499,21 +1289,12 @@ test('reply.header setting multiple cookies as multiple Set-Cookie headers', asy
|
|
|
1499
1289
|
.send({})
|
|
1500
1290
|
})
|
|
1501
1291
|
|
|
1502
|
-
await fastify.listen({ port: 0 })
|
|
1292
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
1503
1293
|
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
}, (err, response, body) => {
|
|
1509
|
-
if (err) {
|
|
1510
|
-
reject(err)
|
|
1511
|
-
}
|
|
1512
|
-
t.assert.ok(response.headers['set-cookie'])
|
|
1513
|
-
t.assert.deepStrictEqual(response.headers['set-cookie'], ['one', 'two', 'three', 'four', 'five', 'six'])
|
|
1514
|
-
resolve()
|
|
1515
|
-
})
|
|
1516
|
-
})
|
|
1294
|
+
const result = await fetch(`${fastifyServer}/headers`)
|
|
1295
|
+
t.assert.ok(result.ok)
|
|
1296
|
+
t.assert.ok(result.headers.get('set-cookie'))
|
|
1297
|
+
t.assert.deepStrictEqual(result.headers.getSetCookie(), ['one', 'two', 'three', 'four', 'five', 'six'])
|
|
1517
1298
|
|
|
1518
1299
|
const response = await fastify.inject('/headers')
|
|
1519
1300
|
t.assert.ok(response.headers['set-cookie'])
|
|
@@ -1937,12 +1718,12 @@ test('redirect to an invalid URL should not crash the server', async t => {
|
|
|
1937
1718
|
}
|
|
1938
1719
|
})
|
|
1939
1720
|
|
|
1940
|
-
await fastify.listen({ port: 0 })
|
|
1721
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
1941
1722
|
|
|
1942
1723
|
{
|
|
1943
|
-
const { response, body } = await doGet(
|
|
1944
|
-
t.assert.strictEqual(response.
|
|
1945
|
-
t.assert.deepStrictEqual(
|
|
1724
|
+
const { response, body } = await doGet(`${fastifyServer}/redirect?useCase=1`)
|
|
1725
|
+
t.assert.strictEqual(response.status, 500)
|
|
1726
|
+
t.assert.deepStrictEqual(body, {
|
|
1946
1727
|
statusCode: 500,
|
|
1947
1728
|
code: 'ERR_INVALID_CHAR',
|
|
1948
1729
|
error: 'Internal Server Error',
|
|
@@ -1950,15 +1731,15 @@ test('redirect to an invalid URL should not crash the server', async t => {
|
|
|
1950
1731
|
})
|
|
1951
1732
|
}
|
|
1952
1733
|
{
|
|
1953
|
-
const { response } = await doGet(
|
|
1954
|
-
t.assert.strictEqual(response.
|
|
1955
|
-
t.assert.strictEqual(response.headers.location, '/?key=a%E2%80%99b')
|
|
1734
|
+
const { response } = await doGet(`${fastifyServer}/redirect?useCase=2`)
|
|
1735
|
+
t.assert.strictEqual(response.status, 302)
|
|
1736
|
+
t.assert.strictEqual(response.headers.get('location'), '/?key=a%E2%80%99b')
|
|
1956
1737
|
}
|
|
1957
1738
|
|
|
1958
1739
|
{
|
|
1959
|
-
const { response } = await doGet(
|
|
1960
|
-
t.assert.strictEqual(response.
|
|
1961
|
-
t.assert.strictEqual(response.headers.location, '/?key=ab')
|
|
1740
|
+
const { response } = await doGet(`${fastifyServer}/redirect?useCase=3`)
|
|
1741
|
+
t.assert.strictEqual(response.status, 302)
|
|
1742
|
+
t.assert.strictEqual(response.headers.get('location'), '/?key=ab')
|
|
1962
1743
|
}
|
|
1963
1744
|
|
|
1964
1745
|
await fastify.close()
|
|
@@ -1983,11 +1764,11 @@ test('invalid response headers should not crash the server', async t => {
|
|
|
1983
1764
|
}
|
|
1984
1765
|
})
|
|
1985
1766
|
|
|
1986
|
-
await fastify.listen({ port: 0 })
|
|
1767
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
1987
1768
|
|
|
1988
|
-
const { response, body } = await doGet(
|
|
1989
|
-
t.assert.strictEqual(response.
|
|
1990
|
-
t.assert.deepStrictEqual(
|
|
1769
|
+
const { response, body } = await doGet(`${fastifyServer}/bad-headers`)
|
|
1770
|
+
t.assert.strictEqual(response.status, 500)
|
|
1771
|
+
t.assert.deepStrictEqual(body, {
|
|
1991
1772
|
statusCode: 500,
|
|
1992
1773
|
code: 'ERR_INVALID_CHAR',
|
|
1993
1774
|
error: 'Internal Server Error',
|
|
@@ -2012,11 +1793,11 @@ test('invalid response headers when sending back an error', async t => {
|
|
|
2012
1793
|
}
|
|
2013
1794
|
})
|
|
2014
1795
|
|
|
2015
|
-
await fastify.listen({ port: 0 })
|
|
1796
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
2016
1797
|
|
|
2017
|
-
const { response, body } = await doGet(
|
|
2018
|
-
t.assert.strictEqual(response.
|
|
2019
|
-
t.assert.deepStrictEqual(
|
|
1798
|
+
const { response, body } = await doGet(`${fastifyServer}/bad-headers`)
|
|
1799
|
+
t.assert.strictEqual(response.status, 500)
|
|
1800
|
+
t.assert.deepStrictEqual(body, {
|
|
2020
1801
|
statusCode: 500,
|
|
2021
1802
|
code: 'ERR_INVALID_CHAR',
|
|
2022
1803
|
error: 'Internal Server Error',
|
|
@@ -2046,11 +1827,11 @@ test('invalid response headers and custom error handler', async t => {
|
|
|
2046
1827
|
reply.status(500).send({ ops: true })
|
|
2047
1828
|
})
|
|
2048
1829
|
|
|
2049
|
-
await fastify.listen({ port: 0 })
|
|
1830
|
+
const fastifyServer = await fastify.listen({ port: 0 })
|
|
2050
1831
|
|
|
2051
|
-
const { response, body } = await doGet(
|
|
2052
|
-
t.assert.strictEqual(response.
|
|
2053
|
-
t.assert.deepStrictEqual(
|
|
1832
|
+
const { response, body } = await doGet(`${fastifyServer}/bad-headers`)
|
|
1833
|
+
t.assert.strictEqual(response.status, 500)
|
|
1834
|
+
t.assert.deepStrictEqual(body, {
|
|
2054
1835
|
statusCode: 500,
|
|
2055
1836
|
code: 'ERR_INVALID_CHAR',
|
|
2056
1837
|
error: 'Internal Server Error',
|