fastify 5.0.0 → 5.2.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/.borp.yaml +3 -0
- package/.vscode/settings.json +22 -0
- package/README.md +12 -7
- package/docs/Guides/Database.md +15 -15
- package/docs/Guides/Detecting-When-Clients-Abort.md +28 -28
- package/docs/Guides/Ecosystem.md +14 -15
- package/docs/Guides/Index.md +1 -1
- package/docs/Guides/Migration-Guide-V4.md +11 -11
- package/docs/Guides/Migration-Guide-V5.md +133 -9
- package/docs/Guides/Plugins-Guide.md +1 -1
- package/docs/Guides/Prototype-Poisoning.md +3 -3
- package/docs/Guides/Recommendations.md +9 -9
- package/docs/Guides/Serverless.md +5 -5
- package/docs/Guides/Testing.md +58 -57
- package/docs/Guides/Write-Plugin.md +2 -2
- package/docs/Guides/Write-Type-Provider.md +3 -3
- package/docs/Reference/ContentTypeParser.md +4 -4
- package/docs/Reference/Decorators.md +2 -2
- package/docs/Reference/Errors.md +3 -3
- package/docs/Reference/Hooks.md +7 -7
- package/docs/Reference/LTS.md +8 -0
- package/docs/Reference/Logging.md +5 -4
- package/docs/Reference/Reply.md +55 -58
- package/docs/Reference/Request.md +49 -42
- package/docs/Reference/Routes.md +16 -13
- package/docs/Reference/Server.md +32 -28
- package/docs/Reference/TypeScript.md +9 -9
- package/docs/Reference/Validation-and-Serialization.md +5 -5
- package/examples/typescript-server.ts +1 -1
- package/fastify.d.ts +14 -5
- package/fastify.js +8 -6
- package/lib/contentTypeParser.js +9 -7
- package/lib/context.js +1 -2
- package/lib/error-handler.js +9 -9
- package/lib/errors.js +1 -1
- package/lib/fourOhFour.js +1 -1
- package/lib/hooks.js +4 -1
- package/lib/{logger.js → logger-factory.js} +70 -122
- package/lib/logger-pino.js +68 -0
- package/lib/pluginOverride.js +1 -1
- package/lib/pluginUtils.js +2 -2
- package/lib/reply.js +4 -5
- package/lib/request.js +16 -9
- package/lib/route.js +23 -22
- package/lib/validation.js +2 -2
- package/package.json +13 -15
- package/test/404s.test.js +675 -629
- package/test/500s.test.js +72 -63
- package/test/{allowUnsafeRegex.test.js → allow-unsafe-regex.test.js} +30 -26
- package/test/als.test.js +48 -45
- package/test/async-await.test.js +148 -134
- package/test/async-dispose.test.js +4 -5
- package/test/async_hooks.test.js +30 -28
- package/test/{bodyLimit.test.js → body-limit.test.js} +61 -58
- package/test/buffer.test.js +9 -10
- package/test/build/error-serializer.test.js +3 -4
- package/test/build/version.test.js +2 -3
- package/test/build-certificate.js +1 -1
- package/test/bundler/README.md +5 -5
- package/test/bundler/esbuild/bundler-test.js +10 -9
- package/test/bundler/webpack/bundler-test.js +10 -9
- package/test/case-insensitive.test.js +31 -28
- package/test/chainable.test.js +4 -5
- package/test/check.test.js +8 -10
- package/test/{childLoggerFactory.test.js → child-logger-factory.test.js} +56 -19
- package/test/client-timeout.test.js +5 -5
- package/test/close-pipelining.test.js +6 -8
- package/test/conditional-pino.test.js +47 -0
- package/test/{connectionTimeout.test.js → connection-timeout.test.js} +10 -11
- package/test/constrained-routes.test.js +243 -236
- package/test/content-length.test.js +53 -68
- package/test/content-parser.test.js +186 -158
- package/test/content-type.test.js +8 -9
- package/test/context-config.test.js +44 -54
- package/test/custom-http-server.test.js +16 -20
- package/test/custom-parser.5.test.js +32 -32
- package/test/diagnostics-channel/404.test.js +15 -15
- package/test/diagnostics-channel/async-delay-request.test.js +25 -25
- package/test/diagnostics-channel/async-request.test.js +24 -24
- package/test/diagnostics-channel/error-before-handler.test.js +4 -5
- package/test/diagnostics-channel/error-request.test.js +19 -19
- package/test/diagnostics-channel/error-status.test.js +8 -8
- package/test/diagnostics-channel/init.test.js +6 -7
- package/test/diagnostics-channel/sync-delay-request.test.js +16 -16
- package/test/diagnostics-channel/sync-request-reply.test.js +16 -16
- package/test/diagnostics-channel/sync-request.test.js +19 -19
- package/test/encapsulated-child-logger-factory.test.js +8 -8
- package/test/encapsulated-error-handler.test.js +20 -20
- package/test/esm/errorCodes.test.mjs +5 -5
- package/test/esm/esm.test.mjs +3 -3
- package/test/esm/named-exports.mjs +3 -3
- package/test/esm/other.mjs +2 -2
- package/test/fastify-instance.test.js +33 -34
- package/test/{findRoute.test.js → find-route.test.js} +11 -10
- package/test/fluent-schema.test.js +33 -36
- package/test/handler-context.test.js +11 -11
- package/test/has-route.test.js +12 -15
- package/test/header-overflow.test.js +13 -12
- package/test/hooks.on-ready.test.js +2 -2
- package/test/hooks.test.js +25 -25
- package/test/http-methods/copy.test.js +22 -24
- package/test/http-methods/custom-http-methods.test.js +24 -21
- package/test/http-methods/get.test.js +97 -84
- package/test/http-methods/head.test.js +63 -57
- package/test/http-methods/lock.test.js +21 -20
- package/test/http-methods/mkcalendar.test.js +31 -27
- package/test/http-methods/mkcol.test.js +10 -10
- package/test/http-methods/move.test.js +11 -11
- package/test/http-methods/propfind.test.js +32 -27
- package/test/http-methods/proppatch.test.js +21 -19
- package/test/http-methods/report.test.js +32 -27
- package/test/http-methods/search.test.js +52 -47
- package/test/http-methods/trace.test.js +3 -4
- package/test/http-methods/unlock.test.js +10 -10
- package/test/http2/closing.test.js +50 -58
- package/test/http2/constraint.test.js +47 -50
- package/test/http2/head.test.js +18 -19
- package/test/http2/missing-http2-module.test.js +4 -5
- package/test/http2/plain.test.js +31 -31
- package/test/http2/secure-with-fallback.test.js +61 -61
- package/test/http2/secure.test.js +28 -31
- package/test/http2/unknown-http-method.test.js +13 -14
- package/test/https/custom-https-server.test.js +6 -7
- package/test/https/https.test.js +78 -78
- package/test/imports.test.js +5 -6
- package/test/internals/all.test.js +8 -11
- package/test/internals/{contentTypeParser.test.js → content-type-parser.test.js} +5 -6
- package/test/internals/context.test.js +9 -11
- package/test/internals/decorator.test.js +20 -21
- package/test/internals/errors.test.js +427 -427
- package/test/internals/{handleRequest.test.js → handle-request.test.js} +53 -42
- package/test/internals/{hookRunner.test.js → hook-runner.test.js} +99 -100
- package/test/internals/hooks.test.js +31 -35
- package/test/internals/{initialConfig.test.js → initial-config.test.js} +92 -80
- package/test/internals/logger.test.js +28 -28
- package/test/internals/plugin.test.js +17 -18
- package/test/internals/reply-serialize.test.js +106 -106
- package/test/internals/reply.test.js +620 -585
- package/test/internals/{reqIdGenFactory.test.js → req-id-gen-factory.test.js} +31 -31
- package/test/internals/request-validate.test.js +218 -221
- package/test/internals/request.test.js +225 -107
- package/test/internals/server.test.js +15 -12
- package/test/internals/validation.test.js +35 -36
- package/test/{keepAliveTimeout.test.js → keep-alive-timeout.test.js} +9 -10
- package/test/listen.5.test.js +9 -9
- package/test/{maxRequestsPerSocket.test.js → max-requests-per-socket.test.js} +30 -30
- package/test/middleware.test.js +4 -5
- package/test/noop-set.test.js +5 -5
- package/test/post-empty-body.test.js +18 -11
- package/test/pretty-print.test.js +59 -49
- package/test/proto-poisoning.test.js +42 -37
- package/test/reply-code.test.js +34 -32
- package/test/{reply-earlyHints.test.js → reply-early-hints.test.js} +21 -19
- package/test/request-error.test.js +122 -0
- package/test/request-header-host.test.js +339 -0
- package/test/request-id.test.js +31 -25
- package/test/{requestTimeout.test.js → request-timeout.test.js} +11 -11
- package/test/route.1.test.js +79 -72
- package/test/route.2.test.js +17 -16
- package/test/route.3.test.js +32 -27
- package/test/route.4.test.js +21 -25
- package/test/route.5.test.js +45 -64
- package/test/route.6.test.js +70 -89
- package/test/route.7.test.js +61 -65
- package/test/route.8.test.js +80 -18
- package/test/router-options.test.js +80 -77
- package/test/same-shape.test.js +5 -5
- package/test/schema-examples.test.js +72 -38
- package/test/serialize-response.test.js +9 -10
- package/test/server.test.js +75 -78
- package/test/set-error-handler.test.js +2 -3
- package/test/stream-serializers.test.js +10 -7
- package/test/sync-routes.test.js +18 -18
- package/test/test-reporter.mjs +68 -0
- package/test/trust-proxy.test.js +51 -45
- package/test/type-provider.test.js +8 -6
- package/test/types/content-type-parser.test-d.ts +1 -1
- package/test/types/fastify.test-d.ts +16 -4
- package/test/types/hooks.test-d.ts +2 -1
- package/test/types/instance.test-d.ts +13 -13
- package/test/types/logger.test-d.ts +2 -2
- package/test/types/plugin.test-d.ts +17 -9
- package/test/types/register.test-d.ts +22 -6
- package/test/types/reply.test-d.ts +1 -1
- package/test/types/route.test-d.ts +34 -4
- package/test/types/serverFactory.test-d.ts +1 -1
- package/test/types/type-provider.test-d.ts +1 -1
- package/test/url-rewriting.test.js +35 -38
- package/test/{useSemicolonDelimiter.test.js → use-semicolon-delimiter.test.js} +30 -30
- package/test/validation-error-handling.test.js +259 -285
- package/test/versioned-routes.test.js +126 -113
- package/test/web-api.test.js +48 -37
- package/test/{wrapThenable.test.js → wrap-thenable.test.js} +10 -9
- package/types/hooks.d.ts +2 -1
- package/types/instance.d.ts +9 -2
- package/types/register.d.ts +12 -3
- package/types/reply.d.ts +1 -1
- package/types/request.d.ts +2 -6
- package/types/serverFactory.d.ts +3 -3
- package/types/utils.d.ts +13 -5
- package/test/types/import.js +0 -2
package/test/async-await.test.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
const test = t.test
|
|
3
|
+
const { test } = require('node:test')
|
|
5
4
|
const sget = require('simple-get').concat
|
|
6
5
|
const Fastify = require('..')
|
|
7
6
|
const split = require('split2')
|
|
@@ -44,26 +43,26 @@ const optsWithHostnameAndPort = {
|
|
|
44
43
|
}
|
|
45
44
|
}
|
|
46
45
|
}
|
|
47
|
-
test('async await', t => {
|
|
48
|
-
t.plan(
|
|
46
|
+
test('async await', (t, done) => {
|
|
47
|
+
t.plan(16)
|
|
49
48
|
const fastify = Fastify()
|
|
50
49
|
try {
|
|
51
50
|
fastify.get('/', opts, async function awaitMyFunc (req, reply) {
|
|
52
51
|
await sleep(200)
|
|
53
52
|
return { hello: 'world' }
|
|
54
53
|
})
|
|
55
|
-
t.
|
|
54
|
+
t.assert.ok(true)
|
|
56
55
|
} catch (e) {
|
|
57
|
-
t.fail()
|
|
56
|
+
t.assert.fail()
|
|
58
57
|
}
|
|
59
58
|
|
|
60
59
|
try {
|
|
61
60
|
fastify.get('/no-await', opts, async function (req, reply) {
|
|
62
61
|
return { hello: 'world' }
|
|
63
62
|
})
|
|
64
|
-
t.
|
|
63
|
+
t.assert.ok(true)
|
|
65
64
|
} catch (e) {
|
|
66
|
-
t.fail()
|
|
65
|
+
t.assert.fail()
|
|
67
66
|
}
|
|
68
67
|
|
|
69
68
|
try {
|
|
@@ -71,52 +70,50 @@ test('async await', t => {
|
|
|
71
70
|
await sleep(200)
|
|
72
71
|
return { hello: 'world', hostname: req.hostname, port: req.port }
|
|
73
72
|
})
|
|
74
|
-
t.
|
|
73
|
+
t.assert.ok(true)
|
|
75
74
|
} catch (e) {
|
|
76
|
-
t.fail()
|
|
75
|
+
t.assert.fail()
|
|
77
76
|
}
|
|
78
77
|
|
|
79
|
-
fastify.listen({ port: 0 }, err => {
|
|
80
|
-
t.
|
|
81
|
-
t.
|
|
78
|
+
fastify.listen({ port: 0 }, async err => {
|
|
79
|
+
t.assert.ifError(err)
|
|
80
|
+
t.after(() => { fastify.close() })
|
|
82
81
|
|
|
83
82
|
sget({
|
|
84
83
|
method: 'GET',
|
|
85
84
|
url: 'http://localhost:' + fastify.server.address().port
|
|
86
85
|
}, (err, response, body) => {
|
|
87
|
-
t.
|
|
88
|
-
t.
|
|
89
|
-
t.
|
|
90
|
-
t.
|
|
86
|
+
t.assert.ifError(err)
|
|
87
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
88
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
89
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
91
90
|
})
|
|
92
91
|
|
|
93
92
|
sget({
|
|
94
93
|
method: 'GET',
|
|
95
94
|
url: 'http://localhost:' + fastify.server.address().port + '/no-await'
|
|
96
95
|
}, (err, response, body) => {
|
|
97
|
-
t.
|
|
98
|
-
t.
|
|
99
|
-
t.
|
|
100
|
-
t.
|
|
96
|
+
t.assert.ifError(err)
|
|
97
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
98
|
+
t.assert.strictEqual(response.headers['content-length'], '' + body.length)
|
|
99
|
+
t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })
|
|
101
100
|
})
|
|
102
101
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
t.equal(parseInt(parsedBody.port), fastify.server.address().port)
|
|
114
|
-
})
|
|
102
|
+
sget({
|
|
103
|
+
method: 'GET',
|
|
104
|
+
url: 'http://localhost:' + fastify.server.address().port + '/await/hostname_port'
|
|
105
|
+
}, (err, response, body) => {
|
|
106
|
+
t.assert.ifError(err)
|
|
107
|
+
t.assert.strictEqual(response.statusCode, 200)
|
|
108
|
+
const parsedBody = JSON.parse(body)
|
|
109
|
+
t.assert.strictEqual(parsedBody.hostname, 'localhost')
|
|
110
|
+
t.assert.strictEqual(parseInt(parsedBody.port), fastify.server.address().port)
|
|
111
|
+
done()
|
|
115
112
|
})
|
|
116
113
|
})
|
|
117
114
|
})
|
|
118
115
|
|
|
119
|
-
test('ignore the result of the promise if reply.send is called beforehand (undefined)', t => {
|
|
116
|
+
test('ignore the result of the promise if reply.send is called beforehand (undefined)', (t, done) => {
|
|
120
117
|
t.plan(4)
|
|
121
118
|
|
|
122
119
|
const server = Fastify()
|
|
@@ -126,22 +123,23 @@ test('ignore the result of the promise if reply.send is called beforehand (undef
|
|
|
126
123
|
await reply.send(payload)
|
|
127
124
|
})
|
|
128
125
|
|
|
129
|
-
t.
|
|
126
|
+
t.after(() => { server.close() })
|
|
130
127
|
|
|
131
128
|
server.listen({ port: 0 }, (err) => {
|
|
132
|
-
t.
|
|
129
|
+
t.assert.ifError(err)
|
|
133
130
|
sget({
|
|
134
131
|
method: 'GET',
|
|
135
132
|
url: 'http://localhost:' + server.server.address().port + '/'
|
|
136
133
|
}, (err, res, body) => {
|
|
137
|
-
t.
|
|
138
|
-
t.
|
|
139
|
-
t.
|
|
134
|
+
t.assert.ifError(err)
|
|
135
|
+
t.assert.deepStrictEqual(payload, JSON.parse(body))
|
|
136
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
137
|
+
done()
|
|
140
138
|
})
|
|
141
139
|
})
|
|
142
140
|
})
|
|
143
141
|
|
|
144
|
-
test('ignore the result of the promise if reply.send is called beforehand (object)', t => {
|
|
142
|
+
test('ignore the result of the promise if reply.send is called beforehand (object)', (t, done) => {
|
|
145
143
|
t.plan(4)
|
|
146
144
|
|
|
147
145
|
const server = Fastify()
|
|
@@ -152,28 +150,29 @@ test('ignore the result of the promise if reply.send is called beforehand (objec
|
|
|
152
150
|
return { hello: 'world' }
|
|
153
151
|
})
|
|
154
152
|
|
|
155
|
-
t.
|
|
153
|
+
t.after(() => { server.close() })
|
|
156
154
|
|
|
157
155
|
server.listen({ port: 0 }, (err) => {
|
|
158
|
-
t.
|
|
156
|
+
t.assert.ifError(err)
|
|
159
157
|
sget({
|
|
160
158
|
method: 'GET',
|
|
161
159
|
url: 'http://localhost:' + server.server.address().port + '/'
|
|
162
160
|
}, (err, res, body) => {
|
|
163
|
-
t.
|
|
164
|
-
t.
|
|
165
|
-
t.
|
|
161
|
+
t.assert.ifError(err)
|
|
162
|
+
t.assert.deepStrictEqual(payload, JSON.parse(body))
|
|
163
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
164
|
+
done()
|
|
166
165
|
})
|
|
167
166
|
})
|
|
168
167
|
})
|
|
169
168
|
|
|
170
|
-
test('server logs an error if reply.send is called and a value is returned via async/await', t => {
|
|
169
|
+
test('server logs an error if reply.send is called and a value is returned via async/await', (t, done) => {
|
|
171
170
|
const lines = ['incoming request', 'request completed', 'Reply was already sent, did you forget to "return reply" in "/" (GET)?']
|
|
172
171
|
t.plan(lines.length + 2)
|
|
173
172
|
|
|
174
173
|
const splitStream = split(JSON.parse)
|
|
175
174
|
splitStream.on('data', (line) => {
|
|
176
|
-
t.
|
|
175
|
+
t.assert.strictEqual(line.msg, lines.shift())
|
|
177
176
|
})
|
|
178
177
|
|
|
179
178
|
const logger = pino(splitStream)
|
|
@@ -191,13 +190,14 @@ test('server logs an error if reply.send is called and a value is returned via a
|
|
|
191
190
|
method: 'GET',
|
|
192
191
|
url: '/'
|
|
193
192
|
}, (err, res) => {
|
|
194
|
-
t.
|
|
193
|
+
t.assert.ifError(err)
|
|
195
194
|
const payload = JSON.parse(res.payload)
|
|
196
|
-
t.
|
|
195
|
+
t.assert.deepStrictEqual(payload, { hello: 'world' })
|
|
196
|
+
done()
|
|
197
197
|
})
|
|
198
198
|
})
|
|
199
199
|
|
|
200
|
-
test('ignore the result of the promise if reply.send is called beforehand (undefined)', t => {
|
|
200
|
+
test('ignore the result of the promise if reply.send is called beforehand (undefined)', (t, done) => {
|
|
201
201
|
t.plan(4)
|
|
202
202
|
|
|
203
203
|
const server = Fastify()
|
|
@@ -207,22 +207,23 @@ test('ignore the result of the promise if reply.send is called beforehand (undef
|
|
|
207
207
|
await reply.send(payload)
|
|
208
208
|
})
|
|
209
209
|
|
|
210
|
-
t.
|
|
210
|
+
t.after(() => { server.close() })
|
|
211
211
|
|
|
212
212
|
server.listen({ port: 0 }, (err) => {
|
|
213
|
-
t.
|
|
213
|
+
t.assert.ifError(err)
|
|
214
214
|
sget({
|
|
215
215
|
method: 'GET',
|
|
216
216
|
url: 'http://localhost:' + server.server.address().port + '/'
|
|
217
217
|
}, (err, res, body) => {
|
|
218
|
-
t.
|
|
219
|
-
t.
|
|
220
|
-
t.
|
|
218
|
+
t.assert.ifError(err)
|
|
219
|
+
t.assert.deepStrictEqual(payload, JSON.parse(body))
|
|
220
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
221
|
+
done()
|
|
221
222
|
})
|
|
222
223
|
})
|
|
223
224
|
})
|
|
224
225
|
|
|
225
|
-
test('ignore the result of the promise if reply.send is called beforehand (object)', t => {
|
|
226
|
+
test('ignore the result of the promise if reply.send is called beforehand (object)', (t, done) => {
|
|
226
227
|
t.plan(4)
|
|
227
228
|
|
|
228
229
|
const server = Fastify()
|
|
@@ -233,28 +234,29 @@ test('ignore the result of the promise if reply.send is called beforehand (objec
|
|
|
233
234
|
return { hello: 'world' }
|
|
234
235
|
})
|
|
235
236
|
|
|
236
|
-
t.
|
|
237
|
+
t.after(() => { server.close() })
|
|
237
238
|
|
|
238
239
|
server.listen({ port: 0 }, (err) => {
|
|
239
|
-
t.
|
|
240
|
+
t.assert.ifError(err)
|
|
240
241
|
sget({
|
|
241
242
|
method: 'GET',
|
|
242
243
|
url: 'http://localhost:' + server.server.address().port + '/'
|
|
243
244
|
}, (err, res, body) => {
|
|
244
|
-
t.
|
|
245
|
-
t.
|
|
246
|
-
t.
|
|
245
|
+
t.assert.ifError(err)
|
|
246
|
+
t.assert.deepStrictEqual(payload, JSON.parse(body))
|
|
247
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
248
|
+
done()
|
|
247
249
|
})
|
|
248
250
|
})
|
|
249
251
|
})
|
|
250
252
|
|
|
251
|
-
test('await reply if we will be calling reply.send in the future', t => {
|
|
253
|
+
test('await reply if we will be calling reply.send in the future', (t, done) => {
|
|
252
254
|
const lines = ['incoming request', 'request completed']
|
|
253
255
|
t.plan(lines.length + 2)
|
|
254
256
|
|
|
255
257
|
const splitStream = split(JSON.parse)
|
|
256
258
|
splitStream.on('data', (line) => {
|
|
257
|
-
t.
|
|
259
|
+
t.assert.strictEqual(line.msg, lines.shift())
|
|
258
260
|
})
|
|
259
261
|
|
|
260
262
|
const server = Fastify({
|
|
@@ -276,19 +278,20 @@ test('await reply if we will be calling reply.send in the future', t => {
|
|
|
276
278
|
method: 'GET',
|
|
277
279
|
url: '/'
|
|
278
280
|
}, (err, res) => {
|
|
279
|
-
t.
|
|
281
|
+
t.assert.ifError(err)
|
|
280
282
|
const payload = JSON.parse(res.payload)
|
|
281
|
-
t.
|
|
283
|
+
t.assert.deepStrictEqual(payload, { hello: 'world' })
|
|
284
|
+
done()
|
|
282
285
|
})
|
|
283
286
|
})
|
|
284
287
|
|
|
285
|
-
test('await reply if we will be calling reply.send in the future (error case)', t => {
|
|
288
|
+
test('await reply if we will be calling reply.send in the future (error case)', (t, done) => {
|
|
286
289
|
const lines = ['incoming request', 'kaboom', 'request completed']
|
|
287
290
|
t.plan(lines.length + 2)
|
|
288
291
|
|
|
289
292
|
const splitStream = split(JSON.parse)
|
|
290
293
|
splitStream.on('data', (line) => {
|
|
291
|
-
t.
|
|
294
|
+
t.assert.strictEqual(line.msg, lines.shift())
|
|
292
295
|
})
|
|
293
296
|
|
|
294
297
|
const server = Fastify({
|
|
@@ -309,12 +312,13 @@ test('await reply if we will be calling reply.send in the future (error case)',
|
|
|
309
312
|
method: 'GET',
|
|
310
313
|
url: '/'
|
|
311
314
|
}, (err, res) => {
|
|
312
|
-
t.
|
|
313
|
-
t.
|
|
315
|
+
t.assert.ifError(err)
|
|
316
|
+
t.assert.strictEqual(res.statusCode, 500)
|
|
317
|
+
done()
|
|
314
318
|
})
|
|
315
319
|
})
|
|
316
320
|
|
|
317
|
-
test('support reply decorators with await', t => {
|
|
321
|
+
test('support reply decorators with await', (t, done) => {
|
|
318
322
|
t.plan(2)
|
|
319
323
|
|
|
320
324
|
const fastify = Fastify()
|
|
@@ -336,9 +340,10 @@ test('support reply decorators with await', t => {
|
|
|
336
340
|
method: 'GET',
|
|
337
341
|
url: '/'
|
|
338
342
|
}, (err, res) => {
|
|
339
|
-
t.
|
|
343
|
+
t.assert.ifError(err)
|
|
340
344
|
const payload = JSON.parse(res.payload)
|
|
341
|
-
t.
|
|
345
|
+
t.assert.deepStrictEqual(payload, { hello: 'world' })
|
|
346
|
+
done()
|
|
342
347
|
})
|
|
343
348
|
})
|
|
344
349
|
|
|
@@ -353,9 +358,9 @@ test('inject async await', async t => {
|
|
|
353
358
|
|
|
354
359
|
try {
|
|
355
360
|
const res = await fastify.inject({ method: 'GET', url: '/' })
|
|
356
|
-
t.
|
|
361
|
+
t.assert.deepStrictEqual({ hello: 'world' }, JSON.parse(res.payload))
|
|
357
362
|
} catch (err) {
|
|
358
|
-
t.fail(err)
|
|
363
|
+
t.assert.fail(err)
|
|
359
364
|
}
|
|
360
365
|
})
|
|
361
366
|
|
|
@@ -370,18 +375,18 @@ test('inject async await - when the server equal up', async t => {
|
|
|
370
375
|
|
|
371
376
|
try {
|
|
372
377
|
const res = await fastify.inject({ method: 'GET', url: '/' })
|
|
373
|
-
t.
|
|
378
|
+
t.assert.deepStrictEqual({ hello: 'world' }, JSON.parse(res.payload))
|
|
374
379
|
} catch (err) {
|
|
375
|
-
t.fail(err)
|
|
380
|
+
t.assert.fail(err)
|
|
376
381
|
}
|
|
377
382
|
|
|
378
383
|
await sleep(200)
|
|
379
384
|
|
|
380
385
|
try {
|
|
381
386
|
const res2 = await fastify.inject({ method: 'GET', url: '/' })
|
|
382
|
-
t.
|
|
387
|
+
t.assert.deepStrictEqual({ hello: 'world' }, JSON.parse(res2.payload))
|
|
383
388
|
} catch (err) {
|
|
384
|
-
t.fail(err)
|
|
389
|
+
t.assert.fail(err)
|
|
385
390
|
}
|
|
386
391
|
})
|
|
387
392
|
|
|
@@ -400,13 +405,13 @@ test('async await plugin', async t => {
|
|
|
400
405
|
|
|
401
406
|
try {
|
|
402
407
|
const res = await fastify.inject({ method: 'GET', url: '/' })
|
|
403
|
-
t.
|
|
408
|
+
t.assert.deepStrictEqual({ hello: 'world' }, JSON.parse(res.payload))
|
|
404
409
|
} catch (err) {
|
|
405
|
-
t.fail(err)
|
|
410
|
+
t.assert.fail(err)
|
|
406
411
|
}
|
|
407
412
|
})
|
|
408
413
|
|
|
409
|
-
test('does not call reply.send() twice if 204 response equal already sent', t => {
|
|
414
|
+
test('does not call reply.send() twice if 204 response equal already sent', (t, done) => {
|
|
410
415
|
t.plan(2)
|
|
411
416
|
|
|
412
417
|
const fastify = Fastify()
|
|
@@ -422,12 +427,13 @@ test('does not call reply.send() twice if 204 response equal already sent', t =>
|
|
|
422
427
|
method: 'GET',
|
|
423
428
|
url: '/'
|
|
424
429
|
}, (err, res) => {
|
|
425
|
-
t.
|
|
426
|
-
t.
|
|
430
|
+
t.assert.ifError(err)
|
|
431
|
+
t.assert.strictEqual(res.statusCode, 204)
|
|
432
|
+
done()
|
|
427
433
|
})
|
|
428
434
|
})
|
|
429
435
|
|
|
430
|
-
test('promise was fulfilled with undefined', t => {
|
|
436
|
+
test('promise was fulfilled with undefined', (t, done) => {
|
|
431
437
|
t.plan(4)
|
|
432
438
|
|
|
433
439
|
let fastify = null
|
|
@@ -440,29 +446,30 @@ test('promise was fulfilled with undefined', t => {
|
|
|
440
446
|
}
|
|
441
447
|
})
|
|
442
448
|
} catch (e) {
|
|
443
|
-
t.fail()
|
|
449
|
+
t.assert.fail()
|
|
444
450
|
}
|
|
445
451
|
|
|
446
|
-
t.
|
|
452
|
+
t.after(() => { fastify.close() })
|
|
447
453
|
|
|
448
454
|
fastify.get('/', async (req, reply) => {
|
|
449
455
|
})
|
|
450
456
|
|
|
451
457
|
stream.once('data', line => {
|
|
452
|
-
t.fail('should not log an error')
|
|
458
|
+
t.assert.fail('should not log an error')
|
|
453
459
|
})
|
|
454
460
|
|
|
455
461
|
fastify.listen({ port: 0 }, (err) => {
|
|
456
|
-
t.
|
|
457
|
-
t.
|
|
462
|
+
t.assert.ifError(err)
|
|
463
|
+
t.after(() => { fastify.close() })
|
|
458
464
|
|
|
459
465
|
sget({
|
|
460
466
|
method: 'GET',
|
|
461
467
|
url: 'http://localhost:' + fastify.server.address().port + '/'
|
|
462
468
|
}, (err, res, body) => {
|
|
463
|
-
t.
|
|
464
|
-
t.
|
|
465
|
-
t.
|
|
469
|
+
t.assert.ifError(err)
|
|
470
|
+
t.assert.strictEqual(res.body, undefined)
|
|
471
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
472
|
+
done()
|
|
466
473
|
})
|
|
467
474
|
})
|
|
468
475
|
})
|
|
@@ -480,16 +487,16 @@ test('promise was fulfilled with undefined using inject', async (t) => {
|
|
|
480
487
|
})
|
|
481
488
|
|
|
482
489
|
stream.once('data', line => {
|
|
483
|
-
t.fail('should not log an error')
|
|
490
|
+
t.assert.fail('should not log an error')
|
|
484
491
|
})
|
|
485
492
|
|
|
486
493
|
const res = await fastify.inject('/')
|
|
487
494
|
|
|
488
|
-
t.
|
|
489
|
-
t.
|
|
495
|
+
t.assert.strictEqual(res.body, '')
|
|
496
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
490
497
|
})
|
|
491
498
|
|
|
492
|
-
test('error is not logged because promise was fulfilled with undefined but response was sent before promise resolution', t => {
|
|
499
|
+
test('error is not logged because promise was fulfilled with undefined but response was sent before promise resolution', (t, done) => {
|
|
493
500
|
t.plan(4)
|
|
494
501
|
|
|
495
502
|
let fastify = null
|
|
@@ -503,38 +510,39 @@ test('error is not logged because promise was fulfilled with undefined but respo
|
|
|
503
510
|
}
|
|
504
511
|
})
|
|
505
512
|
} catch (e) {
|
|
506
|
-
t.fail()
|
|
513
|
+
t.assert.fail()
|
|
507
514
|
}
|
|
508
515
|
|
|
509
|
-
t.
|
|
516
|
+
t.after(() => { fastify.close() })
|
|
510
517
|
|
|
511
518
|
fastify.get('/', async (req, reply) => {
|
|
512
519
|
reply.send(payload)
|
|
513
520
|
})
|
|
514
521
|
|
|
515
522
|
stream.once('data', line => {
|
|
516
|
-
t.fail('should not log an error')
|
|
523
|
+
t.assert.fail('should not log an error')
|
|
517
524
|
})
|
|
518
525
|
|
|
519
526
|
fastify.listen({ port: 0 }, (err) => {
|
|
520
|
-
t.
|
|
521
|
-
t.
|
|
527
|
+
t.assert.ifError(err)
|
|
528
|
+
t.after(() => { fastify.close() })
|
|
522
529
|
|
|
523
530
|
sget({
|
|
524
531
|
method: 'GET',
|
|
525
532
|
url: 'http://localhost:' + fastify.server.address().port + '/'
|
|
526
533
|
}, (err, res, body) => {
|
|
527
|
-
t.
|
|
528
|
-
t.
|
|
529
|
-
t.
|
|
534
|
+
t.assert.ifError(err)
|
|
535
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
536
|
+
t.assert.deepStrictEqual(
|
|
530
537
|
payload,
|
|
531
538
|
JSON.parse(body)
|
|
532
539
|
)
|
|
540
|
+
done()
|
|
533
541
|
})
|
|
534
542
|
})
|
|
535
543
|
})
|
|
536
544
|
|
|
537
|
-
test('Thrown Error instance sets HTTP status code', t => {
|
|
545
|
+
test('Thrown Error instance sets HTTP status code', (t, done) => {
|
|
538
546
|
t.plan(3)
|
|
539
547
|
|
|
540
548
|
const fastify = Fastify()
|
|
@@ -550,9 +558,9 @@ test('Thrown Error instance sets HTTP status code', t => {
|
|
|
550
558
|
method: 'GET',
|
|
551
559
|
url: '/'
|
|
552
560
|
}, (error, res) => {
|
|
553
|
-
t.
|
|
554
|
-
t.
|
|
555
|
-
t.
|
|
561
|
+
t.assert.ifError(error)
|
|
562
|
+
t.assert.strictEqual(res.statusCode, 418)
|
|
563
|
+
t.assert.deepStrictEqual(
|
|
556
564
|
{
|
|
557
565
|
error: statusCodes['418'],
|
|
558
566
|
message: err.message,
|
|
@@ -560,10 +568,11 @@ test('Thrown Error instance sets HTTP status code', t => {
|
|
|
560
568
|
},
|
|
561
569
|
JSON.parse(res.payload)
|
|
562
570
|
)
|
|
571
|
+
done()
|
|
563
572
|
})
|
|
564
573
|
})
|
|
565
574
|
|
|
566
|
-
test('customErrorHandler support', t => {
|
|
575
|
+
test('customErrorHandler support', (t, done) => {
|
|
567
576
|
t.plan(4)
|
|
568
577
|
|
|
569
578
|
const fastify = Fastify()
|
|
@@ -575,7 +584,7 @@ test('customErrorHandler support', t => {
|
|
|
575
584
|
})
|
|
576
585
|
|
|
577
586
|
fastify.setErrorHandler(async err => {
|
|
578
|
-
t.
|
|
587
|
+
t.assert.strictEqual(err.message, 'ouch')
|
|
579
588
|
const error = new Error('kaboom')
|
|
580
589
|
error.statusCode = 401
|
|
581
590
|
throw error
|
|
@@ -585,9 +594,9 @@ test('customErrorHandler support', t => {
|
|
|
585
594
|
method: 'GET',
|
|
586
595
|
url: '/'
|
|
587
596
|
}, (err, res) => {
|
|
588
|
-
t.
|
|
589
|
-
t.
|
|
590
|
-
t.
|
|
597
|
+
t.assert.ifError(err)
|
|
598
|
+
t.assert.strictEqual(res.statusCode, 401)
|
|
599
|
+
t.assert.deepStrictEqual(
|
|
591
600
|
{
|
|
592
601
|
error: statusCodes['401'],
|
|
593
602
|
message: 'kaboom',
|
|
@@ -595,10 +604,11 @@ test('customErrorHandler support', t => {
|
|
|
595
604
|
},
|
|
596
605
|
JSON.parse(res.payload)
|
|
597
606
|
)
|
|
607
|
+
done()
|
|
598
608
|
})
|
|
599
609
|
})
|
|
600
610
|
|
|
601
|
-
test('customErrorHandler support without throwing', t => {
|
|
611
|
+
test('customErrorHandler support without throwing', (t, done) => {
|
|
602
612
|
t.plan(4)
|
|
603
613
|
|
|
604
614
|
const fastify = Fastify()
|
|
@@ -610,26 +620,27 @@ test('customErrorHandler support without throwing', t => {
|
|
|
610
620
|
})
|
|
611
621
|
|
|
612
622
|
fastify.setErrorHandler(async (err, req, reply) => {
|
|
613
|
-
t.
|
|
623
|
+
t.assert.strictEqual(err.message, 'ouch')
|
|
614
624
|
await reply.code(401).send('kaboom')
|
|
615
|
-
reply.send = t.fail.bind(t, 'should not be called')
|
|
625
|
+
reply.send = t.assert.fail.bind(t, 'should not be called')
|
|
616
626
|
})
|
|
617
627
|
|
|
618
628
|
fastify.inject({
|
|
619
629
|
method: 'GET',
|
|
620
630
|
url: '/'
|
|
621
631
|
}, (err, res) => {
|
|
622
|
-
t.
|
|
623
|
-
t.
|
|
624
|
-
t.
|
|
632
|
+
t.assert.ifError(err)
|
|
633
|
+
t.assert.strictEqual(res.statusCode, 401)
|
|
634
|
+
t.assert.deepStrictEqual(
|
|
625
635
|
'kaboom',
|
|
626
636
|
res.payload
|
|
627
637
|
)
|
|
638
|
+
done()
|
|
628
639
|
})
|
|
629
640
|
})
|
|
630
641
|
|
|
631
642
|
// See https://github.com/fastify/fastify/issues/2653
|
|
632
|
-
test('customErrorHandler only called if reply not already sent', t => {
|
|
643
|
+
test('customErrorHandler only called if reply not already sent', (t, done) => {
|
|
633
644
|
t.plan(3)
|
|
634
645
|
|
|
635
646
|
const fastify = Fastify()
|
|
@@ -641,23 +652,24 @@ test('customErrorHandler only called if reply not already sent', t => {
|
|
|
641
652
|
throw error
|
|
642
653
|
})
|
|
643
654
|
|
|
644
|
-
fastify.setErrorHandler(t.fail.bind(t, 'should not be called'))
|
|
655
|
+
fastify.setErrorHandler(t.assert.fail.bind(t, 'should not be called'))
|
|
645
656
|
|
|
646
657
|
fastify.inject({
|
|
647
658
|
method: 'GET',
|
|
648
659
|
url: '/'
|
|
649
660
|
}, (err, res) => {
|
|
650
|
-
t.
|
|
651
|
-
t.
|
|
652
|
-
t.
|
|
661
|
+
t.assert.ifError(err)
|
|
662
|
+
t.assert.strictEqual(res.statusCode, 200)
|
|
663
|
+
t.assert.deepStrictEqual(
|
|
653
664
|
'success',
|
|
654
665
|
res.payload
|
|
655
666
|
)
|
|
667
|
+
done()
|
|
656
668
|
})
|
|
657
669
|
})
|
|
658
670
|
|
|
659
671
|
// See https://github.com/fastify/fastify/issues/3209
|
|
660
|
-
test('setNotFoundHandler should accept return value', t => {
|
|
672
|
+
test('setNotFoundHandler should accept return value', (t, done) => {
|
|
661
673
|
t.plan(3)
|
|
662
674
|
|
|
663
675
|
const fastify = Fastify()
|
|
@@ -677,9 +689,9 @@ test('setNotFoundHandler should accept return value', t => {
|
|
|
677
689
|
method: 'GET',
|
|
678
690
|
url: '/elsewhere'
|
|
679
691
|
}, (err, res) => {
|
|
680
|
-
t.
|
|
681
|
-
t.
|
|
682
|
-
t.
|
|
692
|
+
t.assert.ifError(err)
|
|
693
|
+
t.assert.strictEqual(res.statusCode, 404)
|
|
694
|
+
t.assert.deepStrictEqual(
|
|
683
695
|
{
|
|
684
696
|
error: statusCodes['404'],
|
|
685
697
|
message: 'lost',
|
|
@@ -687,11 +699,12 @@ test('setNotFoundHandler should accept return value', t => {
|
|
|
687
699
|
},
|
|
688
700
|
JSON.parse(res.payload)
|
|
689
701
|
)
|
|
702
|
+
done()
|
|
690
703
|
})
|
|
691
704
|
})
|
|
692
705
|
|
|
693
706
|
// See https://github.com/fastify/fastify/issues/3209
|
|
694
|
-
test('customErrorHandler should accept return value', t => {
|
|
707
|
+
test('customErrorHandler should accept return value', (t, done) => {
|
|
695
708
|
t.plan(4)
|
|
696
709
|
|
|
697
710
|
const fastify = Fastify()
|
|
@@ -703,7 +716,7 @@ test('customErrorHandler should accept return value', t => {
|
|
|
703
716
|
})
|
|
704
717
|
|
|
705
718
|
fastify.setErrorHandler((err, req, reply) => {
|
|
706
|
-
t.
|
|
719
|
+
t.assert.strictEqual(err.message, 'ouch')
|
|
707
720
|
reply.code(401)
|
|
708
721
|
return {
|
|
709
722
|
error: statusCodes['401'],
|
|
@@ -716,9 +729,9 @@ test('customErrorHandler should accept return value', t => {
|
|
|
716
729
|
method: 'GET',
|
|
717
730
|
url: '/'
|
|
718
731
|
}, (err, res) => {
|
|
719
|
-
t.
|
|
720
|
-
t.
|
|
721
|
-
t.
|
|
732
|
+
t.assert.ifError(err)
|
|
733
|
+
t.assert.strictEqual(res.statusCode, 401)
|
|
734
|
+
t.assert.deepStrictEqual(
|
|
722
735
|
{
|
|
723
736
|
error: statusCodes['401'],
|
|
724
737
|
message: 'kaboom',
|
|
@@ -726,10 +739,11 @@ test('customErrorHandler should accept return value', t => {
|
|
|
726
739
|
},
|
|
727
740
|
JSON.parse(res.payload)
|
|
728
741
|
)
|
|
742
|
+
done()
|
|
729
743
|
})
|
|
730
744
|
})
|
|
731
745
|
|
|
732
746
|
test('await self', async t => {
|
|
733
747
|
const app = Fastify()
|
|
734
|
-
t.
|
|
748
|
+
t.assert.strictEqual(await app, app)
|
|
735
749
|
})
|