fastify 4.27.0 → 5.0.0-alpha.2
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/.markdownlint-cli2.yaml +1 -1
- package/.tap/processinfo/09002e93-10ad-430c-bc86-c0576928b0ed.json +241 -0
- package/.tap/processinfo/ee66c5ab-635d-48b5-8be6-3dc3ceea5bfc.json +268 -0
- package/.tap/test-results/test/404s.test.js.tap +623 -0
- package/.tap/test-results/test/500s.test.js.tap +64 -0
- package/.tap/test-results/test/allowUnsafeRegex.test.js.tap +36 -0
- package/.tap/test-results/test/als.test.js.tap +15 -0
- package/.tap/test-results/test/async-await.test.js.tap +184 -0
- package/.tap/test-results/test/async-dispose.test.js.tap +8 -0
- package/.tap/test-results/test/async_hooks.test.js.tap +10 -0
- package/.tap/test-results/test/bodyLimit.test.js.tap +48 -0
- package/.tap/test-results/test/buffer.test.js.tap +20 -0
- package/.tap/test-results/test/build/error-serializer.test.js.tap +12 -0
- package/.tap/test-results/test/build/version.test.js.tap +7 -0
- package/.tap/test-results/test/case-insensitive.test.js.tap +36 -0
- package/.tap/test-results/test/chainable.test.js.tap +17 -0
- package/.tap/test-results/test/check.test.js.tap +10 -0
- package/.tap/test-results/test/childLoggerFactory.test.js.tap +23 -0
- package/.tap/test-results/test/client-timeout.test.js.tap +7 -0
- package/.tap/test-results/test/close-pipelining.test.js.tap +15 -0
- package/.tap/test-results/test/close.test.js.tap +172 -0
- package/.tap/test-results/test/connectionTimeout.test.js.tap +12 -0
- package/.tap/test-results/test/constrained-routes.test.js.tap +173 -0
- package/.tap/test-results/test/content-length.test.js.tap +46 -0
- package/.tap/test-results/test/content-parser.test.js.tap +266 -0
- package/.tap/test-results/test/content-type.test.js.tap +14 -0
- package/.tap/test-results/test/context-config.test.js.tap +41 -0
- package/.tap/test-results/test/copy.test.js.tap +14 -0
- package/.tap/test-results/test/custom-http-server.test.js.tap +30 -0
- package/.tap/test-results/test/custom-parser-async.test.js.tap +21 -0
- package/.tap/test-results/test/custom-parser.0.test.js.tap +199 -0
- package/.tap/test-results/test/custom-parser.1.test.js.tap +90 -0
- package/.tap/test-results/test/custom-parser.2.test.js.tap +22 -0
- package/.tap/test-results/test/custom-parser.3.test.js.tap +53 -0
- package/.tap/test-results/test/custom-parser.4.test.js.tap +45 -0
- package/.tap/test-results/test/custom-parser.5.test.js.tap +41 -0
- package/.tap/test-results/test/custom-querystring-parser.test.js.tap +46 -0
- package/.tap/test-results/test/decorator.test.js.tap +465 -0
- package/.tap/test-results/test/delete.test.js.tap +110 -0
- package/.tap/test-results/test/diagnostics-channel/404.test.js.tap +15 -0
- package/.tap/test-results/test/diagnostics-channel/async-delay-request.test.js.tap +25 -0
- package/.tap/test-results/test/diagnostics-channel/async-request.test.js.tap +24 -0
- package/.tap/test-results/test/diagnostics-channel/error-before-handler.test.js.tap +9 -0
- package/.tap/test-results/test/diagnostics-channel/error-request.test.js.tap +20 -0
- package/.tap/test-results/test/diagnostics-channel/error-status.test.js.tap +10 -0
- package/.tap/test-results/test/diagnostics-channel/init.test.js.tap +14 -0
- package/.tap/test-results/test/diagnostics-channel/sync-delay-request.test.js.tap +16 -0
- package/.tap/test-results/test/diagnostics-channel/sync-request-reply.test.js.tap +16 -0
- package/.tap/test-results/test/diagnostics-channel/sync-request.test.js.tap +19 -0
- package/.tap/test-results/test/encapsulated-child-logger-factory.test.js.tap +18 -0
- package/.tap/test-results/test/encapsulated-error-handler.test.js.tap +243 -0
- package/.tap/test-results/test/esm/errorCodes.test.mjs.tap +9 -0
- package/.tap/test-results/test/esm/esm.test.mjs.tap +8 -0
- package/.tap/test-results/test/esm/index.test.js.tap +8 -0
- package/.tap/test-results/test/fastify-instance.test.js.tap +114 -0
- package/.tap/test-results/test/findRoute.test.js.tap +37 -0
- package/.tap/test-results/test/fluent-schema.test.js.tap +36 -0
- package/.tap/test-results/test/genReqId.test.js.tap +106 -0
- package/.tap/test-results/test/get.test.js.tap +151 -0
- package/.tap/test-results/test/handler-context.test.js.tap +19 -0
- package/.tap/test-results/test/has-route.test.js.tap +30 -0
- package/.tap/test-results/test/head.test.js.tap +130 -0
- package/.tap/test-results/test/header-overflow.test.js.tap +16 -0
- package/.tap/test-results/test/hooks-async.test.js.tap +286 -0
- package/.tap/test-results/test/hooks.on-listen.test.js.tap +311 -0
- package/.tap/test-results/test/hooks.on-ready.test.js.tap +151 -0
- package/.tap/test-results/test/hooks.test.js.tap +966 -0
- package/.tap/test-results/test/http2/closing.test.js.tap +35 -0
- package/.tap/test-results/test/http2/constraint.test.js.tap +32 -0
- package/.tap/test-results/test/http2/head.test.js.tap +9 -0
- package/.tap/test-results/test/http2/missing-http2-module.test.js.tap +8 -0
- package/.tap/test-results/test/http2/plain.test.js.tap +22 -0
- package/.tap/test-results/test/http2/secure-with-fallback.test.js.tap +40 -0
- package/.tap/test-results/test/http2/secure.test.js.tap +27 -0
- package/.tap/test-results/test/http2/unknown-http-method.test.js.tap +9 -0
- package/.tap/test-results/test/https/custom-https-server.test.js.tap +10 -0
- package/.tap/test-results/test/https/https.test.js.tap +45 -0
- package/.tap/test-results/test/imports.test.js.tap +14 -0
- package/.tap/test-results/test/inject.test.js.tap +165 -0
- package/.tap/test-results/test/internals/all.test.js.tap +42 -0
- package/.tap/test-results/test/internals/contentTypeParser.test.js.tap +14 -0
- package/.tap/test-results/test/internals/context.test.js.tap +14 -0
- package/.tap/test-results/test/internals/decorator.test.js.tap +51 -0
- package/.tap/test-results/test/internals/errors.test.js.tap +1212 -0
- package/.tap/test-results/test/internals/handleRequest.test.js.tap +69 -0
- package/.tap/test-results/test/internals/hookRunner.test.js.tap +143 -0
- package/.tap/test-results/test/internals/hooks.test.js.tap +45 -0
- package/.tap/test-results/test/internals/initialConfig.test.js.tap +125 -0
- package/.tap/test-results/test/internals/logger.test.js.tap +71 -0
- package/.tap/test-results/test/internals/plugin.test.js.tap +48 -0
- package/.tap/test-results/test/internals/reply-serialize.test.js.tap +166 -0
- package/.tap/test-results/test/internals/reply.test.js.tap +688 -0
- package/.tap/test-results/test/internals/reqIdGenFactory.test.js.tap +74 -0
- package/.tap/test-results/test/internals/request-validate.test.js.tap +384 -0
- package/.tap/test-results/test/internals/request.test.js.tap +163 -0
- package/.tap/test-results/test/internals/server.test.js.tap +30 -0
- package/.tap/test-results/test/internals/validation.test.js.tap +121 -0
- package/.tap/test-results/test/keepAliveTimeout.test.js.tap +12 -0
- package/.tap/test-results/test/listen.1.test.js.tap +31 -0
- package/.tap/test-results/test/listen.2.test.js.tap +46 -0
- package/.tap/test-results/test/listen.3.test.js.tap +25 -0
- package/.tap/test-results/test/listen.4.test.js.tap +51 -0
- package/.tap/test-results/test/lock.test.js.tap +29 -0
- package/.tap/test-results/test/logger/instantiation.test.js.tap +92 -0
- package/.tap/test-results/test/logger/logging.test.js.tap +117 -0
- package/.tap/test-results/test/logger/options.test.js.tap +165 -0
- package/.tap/test-results/test/logger/request.test.js.tap +82 -0
- package/.tap/test-results/test/logger/response.test.js.tap +38 -0
- package/.tap/test-results/test/maxRequestsPerSocket.test.js.tap +44 -0
- package/.tap/test-results/test/method-missing.test.js.tap +8 -0
- package/.tap/test-results/test/middleware.test.js.tap +17 -0
- package/.tap/test-results/test/mkcalendar.test.js.tap +43 -0
- package/.tap/test-results/test/mkcol.test.js.tap +14 -0
- package/.tap/test-results/test/move.test.js.tap +15 -0
- package/.tap/test-results/test/noop-set.test.js.tap +8 -0
- package/.tap/test-results/test/nullable-validation.test.js.tap +36 -0
- package/.tap/test-results/test/options.error-handler.test.js.tap +186 -0
- package/.tap/test-results/test/options.test.js.tap +174 -0
- package/.tap/test-results/test/output-validation.test.js.tap +66 -0
- package/.tap/test-results/test/patch.error-handler.test.js.tap +206 -0
- package/.tap/test-results/test/patch.test.js.tap +182 -0
- package/.tap/test-results/test/plugin.1.test.js.tap +78 -0
- package/.tap/test-results/test/plugin.2.test.js.tap +102 -0
- package/.tap/test-results/test/plugin.3.test.js.tap +58 -0
- package/.tap/test-results/test/plugin.4.test.js.tap +164 -0
- package/.tap/test-results/test/post-empty-body.test.js.tap +8 -0
- package/.tap/test-results/test/pretty-print.test.js.tap +82 -0
- package/.tap/test-results/test/promises.test.js.tap +46 -0
- package/.tap/test-results/test/propfind.test.js.tap +43 -0
- package/.tap/test-results/test/proppatch.test.js.tap +29 -0
- package/.tap/test-results/test/proto-poisoning.test.js.tap +47 -0
- package/.tap/test-results/test/put.error-handler.test.js.tap +206 -0
- package/.tap/test-results/test/put.test.js.tap +182 -0
- package/.tap/test-results/test/register.test.js.tap +61 -0
- package/.tap/test-results/test/reply-code.test.js.tap +40 -0
- package/.tap/test-results/test/reply-earlyHints.test.js.tap +22 -0
- package/.tap/test-results/test/reply-error.test.js.tap +643 -0
- package/.tap/test-results/test/reply-trailers.test.js.tap +176 -0
- package/.tap/test-results/test/report.test.js.tap +43 -0
- package/.tap/test-results/test/request-error.test.js.tap +98 -0
- package/.tap/test-results/test/request-id.test.js.tap +38 -0
- package/.tap/test-results/test/request.deprecated.test.js.tap +13 -0
- package/.tap/test-results/test/requestTimeout.test.js.tap +21 -0
- package/.tap/test-results/test/route-hooks.test.js.tap +498 -0
- package/.tap/test-results/test/route-prefix.test.js.tap +195 -0
- package/.tap/test-results/test/route-shorthand.test.js.tap +190 -0
- package/.tap/test-results/test/route.1.test.js.tap +93 -0
- package/.tap/test-results/test/route.2.test.js.tap +28 -0
- package/.tap/test-results/test/route.3.test.js.tap +39 -0
- package/.tap/test-results/test/route.4.test.js.tap +32 -0
- package/.tap/test-results/test/route.5.test.js.tap +54 -0
- package/.tap/test-results/test/route.6.test.js.tap +81 -0
- package/.tap/test-results/test/route.7.test.js.tap +93 -0
- package/.tap/test-results/test/route.8.test.js.tap +38 -0
- package/.tap/test-results/test/router-options.test.js.tap +104 -0
- package/.tap/test-results/test/same-shape.test.js.tap +22 -0
- package/.tap/test-results/test/schema-examples.test.js.tap +85 -0
- package/.tap/test-results/test/schema-feature.test.js.tap +445 -0
- package/.tap/test-results/test/schema-serialization.test.js.tap +194 -0
- package/.tap/test-results/test/schema-special-usage.test.js.tap +186 -0
- package/.tap/test-results/test/schema-validation.test.js.tap +199 -0
- package/.tap/test-results/test/search.test.js.tap +77 -0
- package/.tap/test-results/test/serialize-response.test.js.tap +26 -0
- package/.tap/test-results/test/server.test.js.tap +65 -0
- package/.tap/test-results/test/set-error-handler.test.js.tap +7 -0
- package/.tap/test-results/test/skip-reply-send.test.js.tap +272 -0
- package/.tap/test-results/test/stream.1.test.js.tap +36 -0
- package/.tap/test-results/test/stream.2.test.js.tap +20 -0
- package/.tap/test-results/test/stream.3.test.js.tap +34 -0
- package/.tap/test-results/test/stream.4.test.js.tap +40 -0
- package/.tap/test-results/test/stream.5.test.js.tap +37 -0
- package/.tap/test-results/test/sync-routes.test.js.tap +19 -0
- package/.tap/test-results/test/throw.test.js.tap +116 -0
- package/.tap/test-results/test/trace.test.js.tap +7 -0
- package/.tap/test-results/test/trust-proxy.test.js.tap +109 -0
- package/.tap/test-results/test/type-provider.test.js.tap +12 -0
- package/.tap/test-results/test/unlock.test.js.tap +14 -0
- package/.tap/test-results/test/upgrade.test.js.tap +8 -0
- package/.tap/test-results/test/url-rewriting.test.js.tap +39 -0
- package/.tap/test-results/test/useSemicolonDelimiter.test.js.tap +33 -0
- package/.tap/test-results/test/validation-error-handling.test.js.tap +180 -0
- package/.tap/test-results/test/versioned-routes.test.js.tap +151 -0
- package/.tap/test-results/test/web-api.test.js.tap +51 -0
- package/.tap/test-results/test/wrapThenable.test.js.tap +11 -0
- package/.taprc +4 -8
- package/README.md +3 -6
- package/build/build-error-serializer.js +4 -1
- package/build/build-validation.js +5 -4
- package/docs/Guides/Database.md +1 -1
- package/docs/Guides/Delay-Accepting-Requests.md +3 -3
- package/docs/Guides/Ecosystem.md +2 -0
- package/docs/Guides/Migration-Guide-V5.md +20 -0
- package/docs/Guides/Write-Type-Provider.md +4 -2
- package/docs/Reference/ContentTypeParser.md +30 -1
- package/docs/Reference/Decorators.md +42 -16
- package/docs/Reference/Errors.md +10 -2
- package/docs/Reference/Hooks.md +48 -14
- package/docs/Reference/Logging.md +5 -5
- package/docs/Reference/Reply.md +29 -24
- package/docs/Reference/Request.md +5 -1
- package/docs/Reference/Routes.md +24 -28
- package/docs/Reference/Server.md +14 -53
- package/docs/Reference/Type-Providers.md +21 -26
- package/docs/Reference/TypeScript.md +46 -29
- package/docs/Reference/Warnings.md +2 -8
- package/eslint.config.js +27 -0
- package/examples/typescript-server.ts +14 -14
- package/fastify.d.ts +15 -14
- package/fastify.js +41 -15
- package/lib/configValidator.js +94 -76
- package/lib/contentTypeParser.js +54 -88
- package/lib/decorate.js +3 -7
- package/lib/error-serializer.js +77 -19
- package/lib/errors.js +31 -6
- package/lib/handleRequest.js +70 -39
- package/lib/httpMethods.js +34 -18
- package/lib/logger.js +24 -6
- package/lib/pluginUtils.js +5 -5
- package/lib/reply.js +16 -13
- package/lib/request.js +37 -19
- package/lib/route.js +7 -31
- package/lib/server.js +62 -123
- package/lib/warnings.js +28 -27
- package/lib/wrapThenable.js +46 -22
- package/package.json +38 -58
- package/test/404s.test.js +8 -12
- package/test/async-await.test.js +46 -2
- package/test/build/error-serializer.test.js +4 -2
- package/test/check.test.js +225 -0
- package/test/close-pipelining.test.js +2 -34
- package/test/close.test.js +1 -41
- package/test/content-parser.test.js +69 -117
- package/test/custom-parser.1.test.js +40 -1
- package/test/decorator-namespace.test._js_ +31 -0
- package/test/decorator.test.js +92 -43
- package/test/delete.test.js +21 -1
- package/test/diagnostics-channel/404.test.js +57 -0
- package/test/diagnostics-channel/async-delay-request.test.js +74 -0
- package/test/diagnostics-channel/async-request.test.js +72 -0
- package/test/diagnostics-channel/error-before-handler.test.js +36 -0
- package/test/diagnostics-channel/error-request.test.js +61 -0
- package/test/diagnostics-channel/error-status.test.js +39 -0
- package/test/{diagnostics-channel.test.js → diagnostics-channel/init.test.js} +6 -16
- package/test/diagnostics-channel/sync-delay-request.test.js +58 -0
- package/test/diagnostics-channel/sync-request-reply.test.js +58 -0
- package/test/diagnostics-channel/sync-request.test.js +61 -0
- package/test/encapsulated-error-handler.test.js +201 -14
- package/test/esm/index.test.js +2 -12
- package/test/findRoute.test.js +16 -0
- package/test/genReqId.test.js +9 -0
- package/test/get.test.js +28 -0
- package/test/has-route.test.js +18 -2
- package/test/helper.js +1 -5
- package/test/hooks.test.js +0 -4
- package/test/http2/constraint.test.js +22 -1
- package/test/http2/plain.test.js +21 -6
- package/test/http2/secure.test.js +12 -1
- package/test/https/https.test.js +57 -0
- package/test/inject.test.js +1 -2
- package/test/internals/decorator.test.js +0 -2
- package/test/internals/errors.test.js +57 -17
- package/test/internals/handleRequest.test.js +5 -1
- package/test/internals/initialConfig.test.js +5 -5
- package/test/internals/logger.test.js +31 -2
- package/test/internals/reply.test.js +38 -80
- package/test/internals/request.test.js +13 -11
- package/test/listen.1.test.js +5 -15
- package/test/listen.5.test.js +88 -0
- package/test/logger/instantiation.test.js +8 -8
- package/test/logger/logging.test.js +4 -4
- package/test/logger/options.test.js +102 -21
- package/test/logger/response.test.js +6 -6
- package/test/maxRequestsPerSocket.test.js +2 -5
- package/test/method-missing.test.js +24 -0
- package/test/plugin.1.test.js +2 -4
- package/test/plugin.2.test.js +0 -2
- package/test/plugin.3.test.js +0 -2
- package/test/plugin.4.test.js +92 -56
- package/test/register.test.js +2 -4
- package/test/reply-earlyHints.test.js +98 -0
- package/test/reply-error.test.js +0 -2
- package/test/route-hooks.test.js +0 -1
- package/test/route-shorthand.test.js +60 -0
- package/test/schema-special-usage.test.js +1 -1
- package/test/server.test.js +17 -2
- package/test/stream.2.test.js +1 -1
- package/test/stream.4.test.js +0 -42
- package/test/stream.5.test.js +2 -2
- package/test/trust-proxy.test.js +33 -27
- package/test/types/errors.test-d.ts +0 -2
- package/test/types/fastify.test-d.ts +14 -12
- package/test/types/hooks.test-d.ts +1 -0
- package/test/types/import.ts +1 -0
- package/test/types/instance.test-d.ts +10 -51
- package/test/types/logger.test-d.ts +43 -6
- package/test/types/plugin.test-d.ts +5 -2
- package/test/types/register.test-d.ts +2 -2
- package/test/types/reply.test-d.ts +13 -12
- package/test/types/request.test-d.ts +19 -8
- package/test/types/route.test-d.ts +30 -2
- package/test/types/schema.test-d.ts +2 -2
- package/test/types/serverFactory.test-d.ts +1 -1
- package/test/types/type-provider.test-d.ts +60 -13
- package/test/types/using.test-d.ts +4 -1
- package/test/url-rewriting.test.js +3 -2
- package/test/useSemicolonDelimiter.test.js +3 -6
- package/test/versioned-routes.test.js +1 -1
- package/test/web-api.test.js +0 -6
- package/types/content-type-parser.d.ts +3 -3
- package/types/context.d.ts +0 -1
- package/types/errors.d.ts +1 -0
- package/types/hooks.d.ts +6 -6
- package/types/instance.d.ts +28 -41
- package/types/logger.d.ts +3 -3
- package/types/plugin.d.ts +3 -3
- package/types/reply.d.ts +13 -14
- package/types/request.d.ts +5 -3
- package/types/route.d.ts +29 -29
- package/types/schema.d.ts +3 -3
- package/types/serverFactory.d.ts +2 -2
- package/types/type-provider.d.ts +22 -12
- package/types/utils.d.ts +18 -18
- package/.c8rc.json +0 -8
- package/.eslintrc +0 -4
- package/test/default-route.test.js +0 -88
- package/test/listen.deprecated.test.js +0 -229
- package/test/unsupported-httpversion.test.js +0 -31
- package/types/.eslintrc.json +0 -48
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const t = require('tap')
|
|
4
|
+
const diagnostics = require('dc-polyfill')
|
|
5
|
+
const test = t.test
|
|
6
|
+
const sget = require('simple-get').concat
|
|
7
|
+
const Fastify = require('../..')
|
|
8
|
+
const { getServerUrl } = require('../helper')
|
|
9
|
+
const Request = require('../../lib/request')
|
|
10
|
+
const Reply = require('../../lib/reply')
|
|
11
|
+
|
|
12
|
+
test('diagnostics channel sync events fire in expected order', t => {
|
|
13
|
+
t.plan(10)
|
|
14
|
+
let callOrder = 0
|
|
15
|
+
let firstEncounteredMessage
|
|
16
|
+
|
|
17
|
+
diagnostics.subscribe('tracing:fastify.request.handler:start', (msg) => {
|
|
18
|
+
t.equal(callOrder++, 0)
|
|
19
|
+
firstEncounteredMessage = msg
|
|
20
|
+
t.ok(msg.request instanceof Request)
|
|
21
|
+
t.ok(msg.reply instanceof Reply)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
diagnostics.subscribe('tracing:fastify.request.handler:end', (msg) => {
|
|
25
|
+
t.ok(msg.request instanceof Request)
|
|
26
|
+
t.ok(msg.reply instanceof Reply)
|
|
27
|
+
t.equal(callOrder++, 1)
|
|
28
|
+
t.equal(msg, firstEncounteredMessage)
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
diagnostics.subscribe('tracing:fastify.request.handler:error', (msg) => {
|
|
32
|
+
t.fail('should not trigger error channel')
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
const fastify = Fastify()
|
|
36
|
+
fastify.route({
|
|
37
|
+
method: 'GET',
|
|
38
|
+
url: '/',
|
|
39
|
+
handler: function (req, reply) {
|
|
40
|
+
reply.send({ hello: 'world' })
|
|
41
|
+
}
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
fastify.listen({ port: 0 }, function (err) {
|
|
45
|
+
if (err) t.error(err)
|
|
46
|
+
|
|
47
|
+
t.teardown(() => { fastify.close() })
|
|
48
|
+
|
|
49
|
+
sget({
|
|
50
|
+
method: 'GET',
|
|
51
|
+
url: getServerUrl(fastify) + '/'
|
|
52
|
+
}, (err, response, body) => {
|
|
53
|
+
t.error(err)
|
|
54
|
+
t.equal(response.statusCode, 200)
|
|
55
|
+
t.same(JSON.parse(body), { hello: 'world' })
|
|
56
|
+
})
|
|
57
|
+
})
|
|
58
|
+
})
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const t = require('tap')
|
|
4
|
+
const diagnostics = require('dc-polyfill')
|
|
5
|
+
const test = t.test
|
|
6
|
+
const sget = require('simple-get').concat
|
|
7
|
+
const Fastify = require('../..')
|
|
8
|
+
const { getServerUrl } = require('../helper')
|
|
9
|
+
const Request = require('../../lib/request')
|
|
10
|
+
const Reply = require('../../lib/reply')
|
|
11
|
+
|
|
12
|
+
test('diagnostics channel sync events fire in expected order', t => {
|
|
13
|
+
t.plan(13)
|
|
14
|
+
let callOrder = 0
|
|
15
|
+
let firstEncounteredMessage
|
|
16
|
+
|
|
17
|
+
diagnostics.subscribe('tracing:fastify.request.handler:start', (msg) => {
|
|
18
|
+
t.equal(callOrder++, 0)
|
|
19
|
+
firstEncounteredMessage = msg
|
|
20
|
+
t.ok(msg.request instanceof Request)
|
|
21
|
+
t.ok(msg.reply instanceof Reply)
|
|
22
|
+
t.ok(msg.route)
|
|
23
|
+
t.equal(msg.route.url, '/:id')
|
|
24
|
+
t.equal(msg.route.method, 'GET')
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
diagnostics.subscribe('tracing:fastify.request.handler:end', (msg) => {
|
|
28
|
+
t.ok(msg.request instanceof Request)
|
|
29
|
+
t.ok(msg.reply instanceof Reply)
|
|
30
|
+
t.equal(callOrder++, 1)
|
|
31
|
+
t.equal(msg, firstEncounteredMessage)
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
diagnostics.subscribe('tracing:fastify.request.handler:error', (msg) => {
|
|
35
|
+
t.fail('should not trigger error channel')
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
const fastify = Fastify()
|
|
39
|
+
fastify.route({
|
|
40
|
+
method: 'GET',
|
|
41
|
+
url: '/:id',
|
|
42
|
+
handler: function (req, reply) {
|
|
43
|
+
return { hello: 'world' }
|
|
44
|
+
}
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
fastify.listen({ port: 0 }, function (err) {
|
|
48
|
+
if (err) t.error(err)
|
|
49
|
+
|
|
50
|
+
t.teardown(() => { fastify.close() })
|
|
51
|
+
|
|
52
|
+
sget({
|
|
53
|
+
method: 'GET',
|
|
54
|
+
url: getServerUrl(fastify) + '/7'
|
|
55
|
+
}, (err, response, body) => {
|
|
56
|
+
t.error(err)
|
|
57
|
+
t.equal(response.statusCode, 200)
|
|
58
|
+
t.same(JSON.parse(body), { hello: 'world' })
|
|
59
|
+
})
|
|
60
|
+
})
|
|
61
|
+
})
|
|
@@ -3,25 +3,66 @@
|
|
|
3
3
|
const { test } = require('tap')
|
|
4
4
|
const Fastify = require('..')
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
// Because of how error handlers wrap things, following the control flow can be tricky
|
|
7
|
+
// In this test file numbered comments indicate the order statements are expected to execute
|
|
8
|
+
|
|
9
|
+
test('encapsulates an asynchronous error handler', async t => {
|
|
7
10
|
t.plan(3)
|
|
8
11
|
|
|
9
12
|
const fastify = Fastify()
|
|
10
13
|
fastify.register(async function (fastify) {
|
|
11
14
|
fastify.setErrorHandler(async function a (err) {
|
|
12
|
-
|
|
13
|
-
|
|
15
|
+
// 3. the inner error handler catches the error, and throws a new error
|
|
16
|
+
t.equal(err.message, 'from_endpoint')
|
|
17
|
+
throw new Error('from_inner')
|
|
18
|
+
})
|
|
19
|
+
fastify.get('/encapsulated', async () => {
|
|
20
|
+
// 2. the endpoint throws an error
|
|
21
|
+
throw new Error('from_endpoint')
|
|
14
22
|
})
|
|
15
|
-
fastify.get('/encapsulated', async () => { throw new Error('kaboom') })
|
|
16
23
|
})
|
|
17
24
|
|
|
18
25
|
fastify.setErrorHandler(async function b (err) {
|
|
19
|
-
|
|
20
|
-
|
|
26
|
+
// 4. the outer error handler catches the error thrown by the inner error handler
|
|
27
|
+
t.equal(err.message, 'from_inner')
|
|
28
|
+
// 5. the outer error handler throws a new error
|
|
29
|
+
throw new Error('from_outer')
|
|
21
30
|
})
|
|
22
31
|
|
|
32
|
+
// 1. the endpoint is called
|
|
23
33
|
const res = await fastify.inject('/encapsulated')
|
|
24
|
-
|
|
34
|
+
// 6. the default error handler returns the error from the outer error handler
|
|
35
|
+
t.equal(res.json().message, 'from_outer')
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
// See discussion in https://github.com/fastify/fastify/pull/5222#discussion_r1432573655
|
|
39
|
+
test('encapsulates a synchronous error handler', async t => {
|
|
40
|
+
t.plan(3)
|
|
41
|
+
|
|
42
|
+
const fastify = Fastify()
|
|
43
|
+
fastify.register(async function (fastify) {
|
|
44
|
+
fastify.setErrorHandler(function a (err) {
|
|
45
|
+
// 3. the inner error handler catches the error, and throws a new error
|
|
46
|
+
t.equal(err.message, 'from_endpoint')
|
|
47
|
+
throw new Error('from_inner')
|
|
48
|
+
})
|
|
49
|
+
fastify.get('/encapsulated', async () => {
|
|
50
|
+
// 2. the endpoint throws an error
|
|
51
|
+
throw new Error('from_endpoint')
|
|
52
|
+
})
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
fastify.setErrorHandler(async function b (err) {
|
|
56
|
+
// 4. the outer error handler catches the error thrown by the inner error handler
|
|
57
|
+
t.equal(err.message, 'from_inner')
|
|
58
|
+
// 5. the outer error handler throws a new error
|
|
59
|
+
throw new Error('from_outer')
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
// 1. the endpoint is called
|
|
63
|
+
const res = await fastify.inject('/encapsulated')
|
|
64
|
+
// 6. the default error handler returns the error from the outer error handler
|
|
65
|
+
t.equal(res.json().message, 'from_outer')
|
|
25
66
|
})
|
|
26
67
|
|
|
27
68
|
test('onError hook nested', async t => {
|
|
@@ -30,21 +71,167 @@ test('onError hook nested', async t => {
|
|
|
30
71
|
const fastify = Fastify()
|
|
31
72
|
fastify.register(async function (fastify) {
|
|
32
73
|
fastify.setErrorHandler(async function a (err) {
|
|
33
|
-
|
|
34
|
-
|
|
74
|
+
// 4. the inner error handler catches the error, and throws a new error
|
|
75
|
+
t.equal(err.message, 'from_endpoint')
|
|
76
|
+
throw new Error('from_inner')
|
|
77
|
+
})
|
|
78
|
+
fastify.get('/encapsulated', async () => {
|
|
79
|
+
// 2. the endpoint throws an error
|
|
80
|
+
throw new Error('from_endpoint')
|
|
35
81
|
})
|
|
36
|
-
fastify.get('/encapsulated', async () => { throw new Error('kaboom') })
|
|
37
82
|
})
|
|
38
83
|
|
|
39
84
|
fastify.setErrorHandler(async function b (err) {
|
|
40
|
-
|
|
41
|
-
|
|
85
|
+
// 5. the outer error handler catches the error thrown by the inner error handler
|
|
86
|
+
t.equal(err.message, 'from_inner')
|
|
87
|
+
// 6. the outer error handler throws a new error
|
|
88
|
+
throw new Error('from_outer')
|
|
42
89
|
})
|
|
43
90
|
|
|
44
91
|
fastify.addHook('onError', async function (request, reply, err) {
|
|
45
|
-
|
|
92
|
+
// 3. the hook receives the error
|
|
93
|
+
t.equal(err.message, 'from_endpoint')
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
// 1. the endpoint is called
|
|
97
|
+
const res = await fastify.inject('/encapsulated')
|
|
98
|
+
// 7. the default error handler returns the error from the outer error handler
|
|
99
|
+
t.equal(res.json().message, 'from_outer')
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
// See https://github.com/fastify/fastify/issues/5220
|
|
103
|
+
test('encapuslates an error handler, for errors thrown in hooks', async t => {
|
|
104
|
+
t.plan(3)
|
|
105
|
+
|
|
106
|
+
const fastify = Fastify()
|
|
107
|
+
fastify.register(async function (fastify) {
|
|
108
|
+
fastify.setErrorHandler(function a (err) {
|
|
109
|
+
// 3. the inner error handler catches the error, and throws a new error
|
|
110
|
+
t.equal(err.message, 'from_hook')
|
|
111
|
+
throw new Error('from_inner')
|
|
112
|
+
})
|
|
113
|
+
fastify.addHook('onRequest', async () => {
|
|
114
|
+
// 2. the hook throws an error
|
|
115
|
+
throw new Error('from_hook')
|
|
116
|
+
})
|
|
117
|
+
fastify.get('/encapsulated', async () => {})
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
fastify.setErrorHandler(function b (err) {
|
|
121
|
+
// 4. the outer error handler catches the error thrown by the inner error handler
|
|
122
|
+
t.equal(err.message, 'from_inner')
|
|
123
|
+
// 5. the outer error handler throws a new error
|
|
124
|
+
throw new Error('from_outer')
|
|
46
125
|
})
|
|
47
126
|
|
|
127
|
+
// 1. the endpoint is called
|
|
128
|
+
const res = await fastify.inject('/encapsulated')
|
|
129
|
+
// 6. the default error handler returns the error from the outer error handler
|
|
130
|
+
t.equal(res.json().message, 'from_outer')
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
// See https://github.com/fastify/fastify/issues/5220
|
|
134
|
+
test('encapuslates many synchronous error handlers that rethrow errors', async t => {
|
|
135
|
+
const DEPTH = 100
|
|
136
|
+
t.plan(DEPTH + 2)
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* This creates a very nested set of error handlers, that looks like:
|
|
140
|
+
* plugin
|
|
141
|
+
* - error handler
|
|
142
|
+
* - plugin
|
|
143
|
+
* - error handler
|
|
144
|
+
* - plugin
|
|
145
|
+
* ... {to DEPTH levels}
|
|
146
|
+
* - plugin
|
|
147
|
+
* - error handler
|
|
148
|
+
* - GET /encapsulated
|
|
149
|
+
*/
|
|
150
|
+
const createNestedRoutes = (fastify, depth) => {
|
|
151
|
+
if (depth < 0) {
|
|
152
|
+
throw new Error('Expected depth >= 0')
|
|
153
|
+
} else if (depth === 0) {
|
|
154
|
+
fastify.setErrorHandler(function a (err) {
|
|
155
|
+
// 3. innermost error handler catches the error, and throws a new error
|
|
156
|
+
t.equal(err.message, 'from_route')
|
|
157
|
+
throw new Error(`from_handler_${depth}`)
|
|
158
|
+
})
|
|
159
|
+
fastify.get('/encapsulated', async () => {
|
|
160
|
+
// 2. the endpoint throws an error
|
|
161
|
+
throw new Error('from_route')
|
|
162
|
+
})
|
|
163
|
+
} else {
|
|
164
|
+
fastify.setErrorHandler(function d (err) {
|
|
165
|
+
// 4 to {DEPTH+4}. error handlers each catch errors, and then throws a new error
|
|
166
|
+
t.equal(err.message, `from_handler_${depth - 1}`)
|
|
167
|
+
throw new Error(`from_handler_${depth}`)
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
fastify.register(async function (fastify) {
|
|
171
|
+
createNestedRoutes(fastify, depth - 1)
|
|
172
|
+
})
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const fastify = Fastify()
|
|
177
|
+
createNestedRoutes(fastify, DEPTH)
|
|
178
|
+
|
|
179
|
+
// 1. the endpoint is called
|
|
180
|
+
const res = await fastify.inject('/encapsulated')
|
|
181
|
+
// {DEPTH+5}. the default error handler returns the error from the outermost error handler
|
|
182
|
+
t.equal(res.json().message, `from_handler_${DEPTH}`)
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
// See https://github.com/fastify/fastify/issues/5220
|
|
186
|
+
// This was not failing previously, but we want to make sure the behavior continues to work in the same way across async and sync handlers
|
|
187
|
+
// Plus, the current setup is somewhat fragile to tweaks to wrapThenable as that's what retries (by calling res.send(err) again)
|
|
188
|
+
test('encapuslates many asynchronous error handlers that rethrow errors', async t => {
|
|
189
|
+
const DEPTH = 100
|
|
190
|
+
t.plan(DEPTH + 2)
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* This creates a very nested set of error handlers, that looks like:
|
|
194
|
+
* plugin
|
|
195
|
+
* - error handler
|
|
196
|
+
* - plugin
|
|
197
|
+
* - error handler
|
|
198
|
+
* - plugin
|
|
199
|
+
* ... {to DEPTH levels}
|
|
200
|
+
* - plugin
|
|
201
|
+
* - error handler
|
|
202
|
+
* - GET /encapsulated
|
|
203
|
+
*/
|
|
204
|
+
const createNestedRoutes = (fastify, depth) => {
|
|
205
|
+
if (depth < 0) {
|
|
206
|
+
throw new Error('Expected depth >= 0')
|
|
207
|
+
} else if (depth === 0) {
|
|
208
|
+
fastify.setErrorHandler(async function a (err) {
|
|
209
|
+
// 3. innermost error handler catches the error, and throws a new error
|
|
210
|
+
t.equal(err.message, 'from_route')
|
|
211
|
+
throw new Error(`from_handler_${depth}`)
|
|
212
|
+
})
|
|
213
|
+
fastify.get('/encapsulated', async () => {
|
|
214
|
+
// 2. the endpoint throws an error
|
|
215
|
+
throw new Error('from_route')
|
|
216
|
+
})
|
|
217
|
+
} else {
|
|
218
|
+
fastify.setErrorHandler(async function m (err) {
|
|
219
|
+
// 4 to {DEPTH+4}. error handlers each catch errors, and then throws a new error
|
|
220
|
+
t.equal(err.message, `from_handler_${depth - 1}`)
|
|
221
|
+
throw new Error(`from_handler_${depth}`)
|
|
222
|
+
})
|
|
223
|
+
|
|
224
|
+
fastify.register(async function (fastify) {
|
|
225
|
+
createNestedRoutes(fastify, depth - 1)
|
|
226
|
+
})
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const fastify = Fastify()
|
|
231
|
+
createNestedRoutes(fastify, DEPTH)
|
|
232
|
+
|
|
233
|
+
// 1. the endpoint is called
|
|
48
234
|
const res = await fastify.inject('/encapsulated')
|
|
49
|
-
|
|
235
|
+
// {DEPTH+5}. the default error handler returns the error from the outermost error handler
|
|
236
|
+
t.equal(res.json().message, `from_handler_${DEPTH}`)
|
|
50
237
|
})
|
package/test/esm/index.test.js
CHANGED
|
@@ -1,18 +1,8 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
if (semver.lt(process.versions.node, '14.13.0')) {
|
|
7
|
-
t.skip('Skip named exports because Node version < 14.13.0')
|
|
8
|
-
} else {
|
|
9
|
-
// Node v8 throw a `SyntaxError: Unexpected token import`
|
|
10
|
-
// even if this branch is never touch in the code,
|
|
11
|
-
// by using `eval` we can avoid this issue.
|
|
12
|
-
// eslint-disable-next-line
|
|
13
|
-
new Function('module', 'return import(module)')('./named-exports.mjs').catch((err) => {
|
|
3
|
+
import('./named-exports.mjs')
|
|
4
|
+
.catch(err => {
|
|
14
5
|
process.nextTick(() => {
|
|
15
6
|
throw err
|
|
16
7
|
})
|
|
17
8
|
})
|
|
18
|
-
}
|
package/test/findRoute.test.js
CHANGED
|
@@ -49,6 +49,22 @@ test('findRoute should return an immutable route to avoid leaking and runtime ro
|
|
|
49
49
|
t.same(route.params, { artistId: ':artistId' })
|
|
50
50
|
})
|
|
51
51
|
|
|
52
|
+
test('findRoute should return null when when url is not passed', t => {
|
|
53
|
+
t.plan(1)
|
|
54
|
+
const fastify = Fastify()
|
|
55
|
+
|
|
56
|
+
fastify.get('/artists/:artistId', {
|
|
57
|
+
schema: {
|
|
58
|
+
params: { artistId: { type: 'integer' } }
|
|
59
|
+
},
|
|
60
|
+
handler: (req, reply) => reply.send(typeof req.params.artistId)
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
t.equal(fastify.findRoute({
|
|
64
|
+
method: 'POST'
|
|
65
|
+
}), null)
|
|
66
|
+
})
|
|
67
|
+
|
|
52
68
|
test('findRoute should return null when route cannot be found due to a different path', t => {
|
|
53
69
|
t.plan(1)
|
|
54
70
|
const fastify = Fastify()
|
package/test/genReqId.test.js
CHANGED
|
@@ -74,6 +74,15 @@ test('Custom genReqId function gets raw request as argument', t => {
|
|
|
74
74
|
})
|
|
75
75
|
})
|
|
76
76
|
|
|
77
|
+
test('Should handle properly requestIdHeader option', t => {
|
|
78
|
+
t.plan(4)
|
|
79
|
+
|
|
80
|
+
t.equal(Fastify({ requestIdHeader: '' }).initialConfig.requestIdHeader, false)
|
|
81
|
+
t.equal(Fastify({ requestIdHeader: false }).initialConfig.requestIdHeader, false)
|
|
82
|
+
t.equal(Fastify({ requestIdHeader: true }).initialConfig.requestIdHeader, 'request-id')
|
|
83
|
+
t.equal(Fastify({ requestIdHeader: 'x-request-id' }).initialConfig.requestIdHeader, 'x-request-id')
|
|
84
|
+
})
|
|
85
|
+
|
|
77
86
|
test('Should accept option to set genReqId with setGenReqId option', t => {
|
|
78
87
|
t.plan(9)
|
|
79
88
|
|
package/test/get.test.js
CHANGED
|
@@ -203,6 +203,18 @@ test('send a falsy boolean', t => {
|
|
|
203
203
|
}
|
|
204
204
|
})
|
|
205
205
|
|
|
206
|
+
test('shorthand - get, set port', t => {
|
|
207
|
+
t.plan(1)
|
|
208
|
+
try {
|
|
209
|
+
fastify.get('/port', headersSchema, function (req, reply) {
|
|
210
|
+
reply.code(200).send({ port: req.port })
|
|
211
|
+
})
|
|
212
|
+
t.pass()
|
|
213
|
+
} catch (e) {
|
|
214
|
+
t.fail()
|
|
215
|
+
}
|
|
216
|
+
})
|
|
217
|
+
|
|
206
218
|
fastify.listen({ port: 0 }, err => {
|
|
207
219
|
t.error(err)
|
|
208
220
|
t.teardown(() => { fastify.close() })
|
|
@@ -380,4 +392,20 @@ fastify.listen({ port: 0 }, err => {
|
|
|
380
392
|
t.same(body.toString(), 'null')
|
|
381
393
|
})
|
|
382
394
|
})
|
|
395
|
+
|
|
396
|
+
test('shorthand - request get headers - test fall back port', t => {
|
|
397
|
+
t.plan(3)
|
|
398
|
+
sget({
|
|
399
|
+
method: 'GET',
|
|
400
|
+
headers: {
|
|
401
|
+
host: 'example.com'
|
|
402
|
+
},
|
|
403
|
+
json: true,
|
|
404
|
+
url: 'http://localhost:' + fastify.server.address().port + '/port'
|
|
405
|
+
}, (err, response, body) => {
|
|
406
|
+
t.error(err)
|
|
407
|
+
t.equal(response.statusCode, 200)
|
|
408
|
+
t.equal(body.port, null)
|
|
409
|
+
})
|
|
410
|
+
})
|
|
383
411
|
})
|
package/test/has-route.test.js
CHANGED
|
@@ -5,7 +5,7 @@ const test = t.test
|
|
|
5
5
|
const Fastify = require('../fastify')
|
|
6
6
|
|
|
7
7
|
test('hasRoute', t => {
|
|
8
|
-
t.plan(
|
|
8
|
+
t.plan(5)
|
|
9
9
|
const test = t.test
|
|
10
10
|
const fastify = Fastify()
|
|
11
11
|
|
|
@@ -71,7 +71,23 @@ test('hasRoute', t => {
|
|
|
71
71
|
|
|
72
72
|
t.equal(fastify.hasRoute({
|
|
73
73
|
method: 'GET',
|
|
74
|
-
url: '/example
|
|
74
|
+
url: '/example/:file(^\\d+).png'
|
|
75
|
+
}), true)
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
test('hasRoute - finds a route even if method is not uppercased', t => {
|
|
79
|
+
t.plan(1)
|
|
80
|
+
fastify.route({
|
|
81
|
+
method: 'GET',
|
|
82
|
+
url: '/equal',
|
|
83
|
+
handler: function (req, reply) {
|
|
84
|
+
reply.send({ hello: 'world' })
|
|
85
|
+
}
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
t.equal(fastify.hasRoute({
|
|
89
|
+
method: 'get',
|
|
90
|
+
url: '/equal'
|
|
75
91
|
}), true)
|
|
76
92
|
})
|
|
77
93
|
})
|
package/test/helper.js
CHANGED
|
@@ -216,11 +216,7 @@ module.exports.payloadMethod = function (method, t, isSetErrorHandler = false) {
|
|
|
216
216
|
|
|
217
217
|
}, (err, response, body) => {
|
|
218
218
|
t.error(err)
|
|
219
|
-
|
|
220
|
-
t.equal(response.statusCode, 200)
|
|
221
|
-
} else {
|
|
222
|
-
t.equal(response.statusCode, 415)
|
|
223
|
-
}
|
|
219
|
+
t.equal(response.statusCode, 415)
|
|
224
220
|
})
|
|
225
221
|
})
|
|
226
222
|
|
package/test/hooks.test.js
CHANGED
|
@@ -3216,8 +3216,6 @@ test('reply.send should throw if undefined error is thrown', t => {
|
|
|
3216
3216
|
})
|
|
3217
3217
|
|
|
3218
3218
|
test('reply.send should throw if undefined error is thrown at preParsing hook', t => {
|
|
3219
|
-
/* eslint prefer-promise-reject-errors: ["error", {"allowEmptyReject": true}] */
|
|
3220
|
-
|
|
3221
3219
|
t.plan(3)
|
|
3222
3220
|
const fastify = Fastify()
|
|
3223
3221
|
|
|
@@ -3245,8 +3243,6 @@ test('reply.send should throw if undefined error is thrown at preParsing hook',
|
|
|
3245
3243
|
})
|
|
3246
3244
|
|
|
3247
3245
|
test('reply.send should throw if undefined error is thrown at onSend hook', t => {
|
|
3248
|
-
/* eslint prefer-promise-reject-errors: ["error", {"allowEmptyReject": true}] */
|
|
3249
|
-
|
|
3250
3246
|
t.plan(3)
|
|
3251
3247
|
const fastify = Fastify()
|
|
3252
3248
|
|
|
@@ -12,7 +12,7 @@ const { buildCertificate } = require('../build-certificate')
|
|
|
12
12
|
t.before(buildCertificate)
|
|
13
13
|
|
|
14
14
|
test('A route supports host constraints under http2 protocol and secure connection', (t) => {
|
|
15
|
-
t.plan(
|
|
15
|
+
t.plan(6)
|
|
16
16
|
|
|
17
17
|
let fastify
|
|
18
18
|
try {
|
|
@@ -45,6 +45,14 @@ test('A route supports host constraints under http2 protocol and secure connecti
|
|
|
45
45
|
reply.code(200).send(beta)
|
|
46
46
|
}
|
|
47
47
|
})
|
|
48
|
+
fastify.route({
|
|
49
|
+
method: 'GET',
|
|
50
|
+
url: '/hostname_port',
|
|
51
|
+
constraints: { host: constrain },
|
|
52
|
+
handler: function (req, reply) {
|
|
53
|
+
reply.code(200).send({ ...beta, hostname: req.hostname })
|
|
54
|
+
}
|
|
55
|
+
})
|
|
48
56
|
|
|
49
57
|
fastify.listen({ port: 0 }, err => {
|
|
50
58
|
t.error(err)
|
|
@@ -87,5 +95,18 @@ test('A route supports host constraints under http2 protocol and secure connecti
|
|
|
87
95
|
|
|
88
96
|
t.equal(res.headers[':status'], 404)
|
|
89
97
|
})
|
|
98
|
+
t.test('https get request - constrain - verify hostname and port from request', async (t) => {
|
|
99
|
+
t.plan(1)
|
|
100
|
+
|
|
101
|
+
const url = `https://localhost:${fastify.server.address().port}/hostname_port`
|
|
102
|
+
const res = await h2url.concat({
|
|
103
|
+
url,
|
|
104
|
+
headers: {
|
|
105
|
+
':authority': constrain
|
|
106
|
+
}
|
|
107
|
+
})
|
|
108
|
+
const body = JSON.parse(res.body)
|
|
109
|
+
t.equal(body.hostname, constrain)
|
|
110
|
+
})
|
|
90
111
|
})
|
|
91
112
|
})
|
package/test/http2/plain.test.js
CHANGED
|
@@ -20,8 +20,12 @@ fastify.get('/', function (req, reply) {
|
|
|
20
20
|
reply.code(200).send(msg)
|
|
21
21
|
})
|
|
22
22
|
|
|
23
|
-
fastify.get('/
|
|
24
|
-
reply.code(200).send(req.
|
|
23
|
+
fastify.get('/host', function (req, reply) {
|
|
24
|
+
reply.code(200).send(req.host)
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
fastify.get('/hostname_port', function (req, reply) {
|
|
28
|
+
reply.code(200).send({ hostname: req.hostname, port: req.port })
|
|
25
29
|
})
|
|
26
30
|
|
|
27
31
|
fastify.listen({ port: 0 }, err => {
|
|
@@ -40,14 +44,25 @@ fastify.listen({ port: 0 }, err => {
|
|
|
40
44
|
t.same(JSON.parse(res.body), msg)
|
|
41
45
|
})
|
|
42
46
|
|
|
43
|
-
test('http
|
|
47
|
+
test('http host', async (t) => {
|
|
44
48
|
t.plan(1)
|
|
45
49
|
|
|
46
|
-
const
|
|
50
|
+
const host = `localhost:${fastify.server.address().port}`
|
|
51
|
+
|
|
52
|
+
const url = `http://${host}/host`
|
|
53
|
+
const res = await h2url.concat({ url })
|
|
54
|
+
|
|
55
|
+
t.equal(res.body, host)
|
|
56
|
+
})
|
|
57
|
+
test('http hostname and port', async (t) => {
|
|
58
|
+
t.plan(2)
|
|
59
|
+
|
|
60
|
+
const host = `localhost:${fastify.server.address().port}`
|
|
47
61
|
|
|
48
|
-
const url = `http://${
|
|
62
|
+
const url = `http://${host}/hostname_port`
|
|
49
63
|
const res = await h2url.concat({ url })
|
|
50
64
|
|
|
51
|
-
t.equal(res.body,
|
|
65
|
+
t.equal(JSON.parse(res.body).hostname, host.split(':')[0])
|
|
66
|
+
t.equal(JSON.parse(res.body).port, parseInt(host.split(':')[1]))
|
|
52
67
|
})
|
|
53
68
|
})
|
|
@@ -10,7 +10,7 @@ const { buildCertificate } = require('../build-certificate')
|
|
|
10
10
|
t.before(buildCertificate)
|
|
11
11
|
|
|
12
12
|
test('secure', (t) => {
|
|
13
|
-
t.plan(
|
|
13
|
+
t.plan(5)
|
|
14
14
|
|
|
15
15
|
let fastify
|
|
16
16
|
try {
|
|
@@ -32,6 +32,9 @@ test('secure', (t) => {
|
|
|
32
32
|
fastify.get('/proto', function (req, reply) {
|
|
33
33
|
reply.code(200).send({ proto: req.protocol })
|
|
34
34
|
})
|
|
35
|
+
fastify.get('/hostname_port', function (req, reply) {
|
|
36
|
+
reply.code(200).send({ hostname: req.hostname, port: req.port })
|
|
37
|
+
})
|
|
35
38
|
|
|
36
39
|
fastify.listen({ port: 0 }, err => {
|
|
37
40
|
t.error(err)
|
|
@@ -55,5 +58,13 @@ test('secure', (t) => {
|
|
|
55
58
|
t.same(JSON.parse((await h2url.concat({ url })).body), { proto: 'https' })
|
|
56
59
|
t.same(JSON.parse((await h2url.concat({ url, headers: { 'X-Forwarded-Proto': 'lorem' } })).body), { proto: 'https' })
|
|
57
60
|
})
|
|
61
|
+
t.test('https get request - test hostname and port', async (t) => {
|
|
62
|
+
t.plan(2)
|
|
63
|
+
|
|
64
|
+
const url = `https://localhost:${fastify.server.address().port}/hostname_port`
|
|
65
|
+
const parsedbody = JSON.parse((await h2url.concat({ url })).body)
|
|
66
|
+
t.equal(parsedbody.hostname, 'localhost')
|
|
67
|
+
t.equal(parsedbody.port, fastify.server.address().port)
|
|
68
|
+
})
|
|
58
69
|
})
|
|
59
70
|
})
|