@fastify/reply-from 9.0.0 → 9.0.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.
Files changed (94) hide show
  1. package/.github/workflows/ci.yml +5 -0
  2. package/.taprc +1 -1
  3. package/README.md +50 -0
  4. package/index.js +3 -0
  5. package/lib/errors.js +1 -0
  6. package/lib/request.js +11 -1
  7. package/package.json +4 -4
  8. package/test/{core-with-path-in-base.js → core-with-path-in-base.test.js} +4 -1
  9. package/test/{disable-request-logging.js → disable-request-logging.test.js} +1 -3
  10. package/test/http-timeout.test.js +82 -0
  11. package/test/{http2-target-multi-crash.js → http2-target-multi-crash.test.js} +3 -1
  12. package/test/{http2-timeout.js → http2-timeout.test.js} +32 -1
  13. package/test/undici-connect-timeout.test.js +58 -0
  14. package/test/http-timeout.js +0 -57
  15. /package/test/{async-route-handler.js → async-route-handler.test.js} +0 -0
  16. /package/test/{base-get.js → base-get.test.js} +0 -0
  17. /package/test/{base-path.js → base-path.test.js} +0 -0
  18. /package/test/{base-querystring.js → base-querystring.test.js} +0 -0
  19. /package/test/{build-url.js → build-url.test.js} +0 -0
  20. /package/test/{custom-undici-instance.js → custom-undici-instance.test.js} +0 -0
  21. /package/test/{fastify-multipart-incompatibility.js → fastify-multipart-incompatibility.test.js} +0 -0
  22. /package/test/{full-get.js → full-get-test.test.js} +0 -0
  23. /package/test/{full-https-get.js → full-https-get.test.js} +0 -0
  24. /package/test/{full-post-extended-content-type.js → full-post-extended-content-type.test.js} +0 -0
  25. /package/test/{full-post-http2.js → full-post-http2.test.js} +0 -0
  26. /package/test/{full-post-stream-core.js → full-post-stream-core.test.js} +0 -0
  27. /package/test/{full-post-stream.js → full-post-stream.test.js} +0 -0
  28. /package/test/{full-post.js → full-post.test.js} +0 -0
  29. /package/test/{full-querystring-rewrite-option-complex.js → full-querystring-rewrite-option-complex.test.js} +0 -0
  30. /package/test/{full-querystring-rewrite-option-function.js → full-querystring-rewrite-option-function.test.js} +0 -0
  31. /package/test/{full-querystring-rewrite-option.js → full-querystring-rewrite-option.test.js} +0 -0
  32. /package/test/{full-querystring-rewrite-string.js → full-querystring-rewrite-string.test.js} +0 -0
  33. /package/test/{full-querystring-rewrite.js → full-querystring-rewrite.test.js} +0 -0
  34. /package/test/{full-querystring.js → full-querystring.test.js} +0 -0
  35. /package/test/{full-rewrite-body-content-type.js → full-rewrite-body-content-type.test.js} +0 -0
  36. /package/test/{full-rewrite-body-http.js → full-rewrite-body-http.test.js} +0 -0
  37. /package/test/{full-rewrite-body-to-empty-string.js → full-rewrite-body-to-empty-string.test.js} +0 -0
  38. /package/test/{full-rewrite-body-to-null.js → full-rewrite-body-to-null.test.js} +0 -0
  39. /package/test/{full-rewrite-body.js → full-rewrite-body.test.js} +0 -0
  40. /package/test/{get-upstream-cache.js → get-upstream-cache.test.js} +0 -0
  41. /package/test/{get-upstream-http.js → get-upstream-http.test.js} +0 -0
  42. /package/test/{get-upstream-type.js → get-upstream-type.test.js} +0 -0
  43. /package/test/{get-upstream-undici.js → get-upstream-undici.test.js} +0 -0
  44. /package/test/{get-with-body.js → get-with-body.test.js} +0 -0
  45. /package/test/{head-with-body.js → head-with-body.test.js} +0 -0
  46. /package/test/{host-header.js → host-header.test.js} +0 -0
  47. /package/test/{http-agents.js → http-agents.test.js} +0 -0
  48. /package/test/{http-global-agent.js → http-global-agent.test.js} +0 -0
  49. /package/test/{http-http2.js → http-http2.test.js} +0 -0
  50. /package/test/{http-invalid-target.js → http-invalid-target.test.js} +0 -0
  51. /package/test/{http-retry.js → http-retry.test.js} +0 -0
  52. /package/test/{http2-http2.js → http2-http2.test.js} +0 -0
  53. /package/test/{http2-https.js → http2-https.test.js} +0 -0
  54. /package/test/{http2-invalid-base.js → http2-invalid-base.test.js} +0 -0
  55. /package/test/{http2-invalid-target.js → http2-invalid-target.test.js} +0 -0
  56. /package/test/{http2-target-crash.js → http2-target-crash.test.js} +0 -0
  57. /package/test/{http2-unix-socket.js → http2-unix-socket.test.js} +0 -0
  58. /package/test/{https-agents.js → https-agents.test.js} +0 -0
  59. /package/test/{https-global-agent.js → https-global-agent.test.js} +0 -0
  60. /package/test/{modifyCoreObjects-false.js → modifyCoreObjects-false.test.js} +0 -0
  61. /package/test/{no-body-opts-with-get.js → no-body-opts-with-get.test.js} +0 -0
  62. /package/test/{no-body-opts-with-head.js → no-body-opts-with-head.test.js} +0 -0
  63. /package/test/{no-stream-body-option.js → no-stream-body-option.test.js} +0 -0
  64. /package/test/{on-error.js → on-error.test.js} +0 -0
  65. /package/test/{onResponse.js → onResponse.test.js} +0 -0
  66. /package/test/{padded-body.js → padded-body.test.js} +0 -0
  67. /package/test/{post-formbody.js → post-formbody.test.js} +0 -0
  68. /package/test/{post-plain-text.js → post-plain-text.test.js} +0 -0
  69. /package/test/{post-with-custom-encoded-contenttype.js → post-with-custom-encoded-contenttype.test.js} +0 -0
  70. /package/test/{post-with-octet-stream.js → post-with-octet-stream.test.js} +0 -0
  71. /package/test/{retry-on-503.js → retry-on-503.test.js} +0 -0
  72. /package/test/{rewrite-headers-type.js → rewrite-headers-type.test.js} +0 -0
  73. /package/test/{rewrite-headers.js → rewrite-headers.test.js} +0 -0
  74. /package/test/{rewrite-request-headers-type.js → rewrite-request-headers-type.test.js} +0 -0
  75. /package/test/{rewrite-request-headers.js → rewrite-request-headers.test.js} +0 -0
  76. /package/test/{transform-body.js → transform-body.test.js} +0 -0
  77. /package/test/{undici-agent.js → undici-agent.test.js} +0 -0
  78. /package/test/{undici-body.js → undici-body.test.js} +0 -0
  79. /package/test/{undici-chaining.js → undici-chaining.test.js} +0 -0
  80. /package/test/{undici-global-agent.js → undici-global-agent.test.js} +0 -0
  81. /package/test/{undici-options.js → undici-options.test.js} +0 -0
  82. /package/test/{undici-retry.js → undici-retry.test.js} +0 -0
  83. /package/test/{undici-timeout-body-partial.js → undici-timeout-body-partial.test.js} +0 -0
  84. /package/test/{undici-timeout-body.js → undici-timeout-body.test.js} +0 -0
  85. /package/test/{undici-timeout.js → undici-timeout.test.js} +0 -0
  86. /package/test/{undici-with-path-in-base.js → undici-with-path-in-base.test.js} +0 -0
  87. /package/test/{undici.js → undici.test.js} +0 -0
  88. /package/test/{unexpected-error.js → unexpected-error.test.js} +0 -0
  89. /package/test/{unix-http-undici-from.js → unix-http-undici-from.test.js} +0 -0
  90. /package/test/{unix-http-undici.js → unix-http-undici.test.js} +0 -0
  91. /package/test/{unix-http.js → unix-http.test.js} +0 -0
  92. /package/test/{unix-https-undici.js → unix-https-undici.test.js} +0 -0
  93. /package/test/{unix-https.js → unix-https.test.js} +0 -0
  94. /package/test/{utils-filter-pseudo-headers.js → utils-filter-pseudo-headers.test.js} +0 -0
@@ -2,6 +2,11 @@ name: CI
2
2
 
3
3
  on:
4
4
  push:
5
+ branches:
6
+ - main
7
+ - master
8
+ - next
9
+ - 'v*'
5
10
  paths-ignore:
6
11
  - 'docs/**'
7
12
  - '*.md'
package/.taprc CHANGED
@@ -8,4 +8,4 @@ statements: 96
8
8
  branches: 96
9
9
 
10
10
  files:
11
- - test/**/*.js
11
+ - test/**/*.test.js
package/README.md CHANGED
@@ -312,6 +312,56 @@ Helpful for a gradual rollout of new services.
312
312
  Parameters are the Fastify request and the base string from the plugin options.
313
313
  It must return the upstream destination.
314
314
 
315
+ Only http1! As http2 uses one connection for the whole session only the base upstream is used. If you want to
316
+ have different upstreams based on the request you can add multiple Fastify.register's with different
317
+ ContraintStrategies.
318
+
319
+ e.g.:
320
+
321
+ Route grpc-web/http1 and grpc/http2 to different routes with a ContentType-ConstraintStrategy:
322
+ ```
323
+ const contentTypeMatchContraintStrategy = {
324
+ // strategy name for referencing in the route handler `constraints` options
325
+ name: 'contentType',
326
+ // storage factory for storing routes in the find-my-way route tree
327
+ storage: function () {
328
+ let handlers = {}
329
+ return {
330
+ get: (type: any) => { return handlers[type] || null },
331
+ set: (type: any, store: any) => { handlers[type] = store }
332
+ }
333
+ },
334
+ // function to get the value of the constraint from each incoming request
335
+ deriveConstraint: (req: any, ctx: any) => {
336
+ return req.headers['content-type']
337
+ },
338
+ // optional flag marking if handlers without constraints can match requests that have a value for this constraint
339
+ mustMatchWhenDerived: true
340
+ }
341
+
342
+ server.addConstraintStrategy(contentTypeMatchContraintStrategy);
343
+ ```
344
+
345
+ and then 2 different upstreams with different register's:
346
+ ```
347
+ // grpc-web / http1
348
+ server.register(fastifyHttpProxy, {
349
+ // Although most browsers send with http2, nodejs cannot handle this http2 request
350
+ // therefore we have to transport to the grpc-web-proxy via http1
351
+ http2: false,
352
+ upstream: 'http://grpc-web-proxy',
353
+ constraints: { "contentType": "application/grpc-web+proto" }
354
+ });
355
+
356
+ // grpc / http2
357
+ server.register(fastifyHttpProxy, {
358
+ http2: true,
359
+ upstream: 'http://grpc.server',
360
+ constraints: { "contentType": "application/grpc+proto" }
361
+ });
362
+ ```
363
+
364
+
315
365
  #### `queryString` or `queryString(search, reqUrl)`
316
366
 
317
367
  Replaces the original querystring of the request with what is specified.
package/index.js CHANGED
@@ -17,6 +17,7 @@ const {
17
17
  ServiceUnavailableError,
18
18
  GatewayTimeoutError,
19
19
  ConnectionResetError,
20
+ ConnectTimeoutError,
20
21
  UndiciSocketError,
21
22
  InternalServerError
22
23
  } = require('./lib/errors')
@@ -157,6 +158,8 @@ const fastifyReplyFrom = fp(function from (fastify, opts, next) {
157
158
  onError(this, { error: new ConnectionResetError() })
158
159
  } else if (err.code === 'UND_ERR_SOCKET') {
159
160
  onError(this, { error: new UndiciSocketError() })
161
+ } else if (err.code === 'UND_ERR_CONNECT_TIMEOUT') {
162
+ onError(this, { error: new ConnectTimeoutError() })
160
163
  } else {
161
164
  onError(this, { error: new InternalServerError(err.message) })
162
165
  }
package/lib/errors.js CHANGED
@@ -9,5 +9,6 @@ module.exports.Http2SessionTimeoutError = createError('FST_REPLY_FROM_HTTP2_SESS
9
9
  module.exports.ServiceUnavailableError = createError('FST_REPLY_FROM_SERVICE_UNAVAILABLE', 'Service Unavailable', 503)
10
10
  module.exports.GatewayTimeoutError = createError('FST_REPLY_FROM_GATEWAY_TIMEOUT', 'Gateway Timeout', 504)
11
11
  module.exports.ConnectionResetError = createError('ECONNRESET', 'Connection Reset', 500)
12
+ module.exports.ConnectTimeoutError = createError('UND_ERR_CONNECT_TIMEOUT', 'Connect Timeout Error', 500)
12
13
  module.exports.UndiciSocketError = createError('UND_ERR_SOCKET', 'Undici Socket Error', 500)
13
14
  module.exports.InternalServerError = createError('FST_REPLY_FROM_INTERNAL_SERVER_ERROR', '%s', 500)
package/lib/request.js CHANGED
@@ -112,6 +112,10 @@ function buildRequest (opts) {
112
112
  })
113
113
  req.on('error', done)
114
114
  req.on('response', res => {
115
+ // remove timeout for sse connections
116
+ if (res.headers['content-type'] === 'text/event-stream') {
117
+ req.setTimeout(0)
118
+ }
115
119
  done(null, { statusCode: res.statusCode, headers: res.headers, stream: res })
116
120
  })
117
121
  req.once('timeout', () => {
@@ -210,6 +214,12 @@ function buildRequest (opts) {
210
214
  if (err) done(err)
211
215
  })
212
216
  req.on('response', headers => {
217
+ // remove timeout for sse connections
218
+ if (headers['content-type'] === 'text/event-stream') {
219
+ req.setTimeout(0)
220
+ http2Client.setTimeout(0)
221
+ }
222
+
213
223
  const statusCode = headers[':status']
214
224
  done(null, { statusCode, headers, stream: req })
215
225
  })
@@ -250,7 +260,7 @@ function getHttp2Opts (opts) {
250
260
  http2Opts.sessionOptions = http2Opts.sessionOptions || {}
251
261
 
252
262
  if (!http2Opts.sessionTimeout) {
253
- http2Opts.sessionTimeout = opts.sessionTimeout || 6000
263
+ http2Opts.sessionTimeout = opts.sessionTimeout || 60000
254
264
  }
255
265
  if (!http2Opts.requestTimeout) {
256
266
  http2Opts.requestTimeout = 10000
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastify/reply-from",
3
- "version": "9.0.0",
3
+ "version": "9.0.2",
4
4
  "description": "forward your HTTP request to another server, for fastify",
5
5
  "main": "index.js",
6
6
  "types": "types/index.d.ts",
@@ -32,7 +32,7 @@
32
32
  "@fastify/multipart": "^7.4.0",
33
33
  "@fastify/pre-commit": "^2.0.2",
34
34
  "@sinonjs/fake-timers": "^10.0.0",
35
- "@types/node": "^18.0.0",
35
+ "@types/node": "^20.1.4",
36
36
  "@types/tap": "^15.0.7",
37
37
  "fastify": "^4.0.2",
38
38
  "form-data": "^4.0.0",
@@ -46,7 +46,7 @@
46
46
  "split2": "^4.1.0",
47
47
  "standard": "^17.0.0",
48
48
  "tap": "^16.2.0",
49
- "tsd": "^0.25.0"
49
+ "tsd": "^0.28.0"
50
50
  },
51
51
  "dependencies": {
52
52
  "@fastify/error": "^3.0.0",
@@ -54,7 +54,7 @@
54
54
  "fast-querystring": "^1.0.0",
55
55
  "fastify-plugin": "^4.0.0",
56
56
  "pump": "^3.0.0",
57
- "tiny-lru": "^10.0.0",
57
+ "tiny-lru": "^11.0.0",
58
58
  "undici": "^5.19.1"
59
59
  },
60
60
  "pre-commit": [
@@ -39,7 +39,10 @@ target.listen({ port: 0 }, (err) => {
39
39
  instance.listen({ port: 0 }, (err) => {
40
40
  t.error(err)
41
41
 
42
- get(`http://localhost:${instance.server.address().port}`, (err, res, data) => {
42
+ get({
43
+ url: `http://localhost:${instance.server.address().port}`,
44
+ agent: false
45
+ }, (err, res, data) => {
43
46
  t.error(err)
44
47
  t.equal(res.headers['content-type'], 'text/plain')
45
48
  t.equal(res.headers['x-my-header'], 'hello!')
@@ -19,12 +19,12 @@ const target = http.createServer((req, res) => {
19
19
  })
20
20
 
21
21
  t.test('use a custom instance of \'undici\'', async t => {
22
+ t.plan(3)
22
23
  t.teardown(target.close.bind(target))
23
24
 
24
25
  await new Promise((resolve, reject) => target.listen({ port: 0 }, err => err ? reject(err) : resolve()))
25
26
 
26
27
  t.test('disableRequestLogging is set to true', t => {
27
- t.plan(10)
28
28
  const logStream = split(JSON.parse)
29
29
  const instance = Fastify({
30
30
  logger: {
@@ -69,7 +69,6 @@ t.test('use a custom instance of \'undici\'', async t => {
69
69
  })
70
70
 
71
71
  t.test('disableRequestLogging is set to false', t => {
72
- t.plan(8)
73
72
  const logStream = split(JSON.parse)
74
73
  const instance = Fastify({
75
74
  logger: {
@@ -114,7 +113,6 @@ t.test('use a custom instance of \'undici\'', async t => {
114
113
  })
115
114
 
116
115
  t.test('disableRequestLogging is not defined', t => {
117
- t.plan(8)
118
116
  const logStream = split(JSON.parse)
119
117
  const instance = Fastify({
120
118
  logger: {
@@ -0,0 +1,82 @@
1
+ 'use strict'
2
+
3
+ const { test } = require('tap')
4
+ const Fastify = require('fastify')
5
+ const From = require('..')
6
+ const got = require('got')
7
+ const FakeTimers = require('@sinonjs/fake-timers')
8
+
9
+ const clock = FakeTimers.createClock()
10
+
11
+ test('http request timeout', async (t) => {
12
+ t.autoend(false)
13
+
14
+ const target = Fastify()
15
+ t.teardown(target.close.bind(target))
16
+
17
+ target.get('/', (request, reply) => {
18
+ t.pass('request arrives')
19
+
20
+ clock.setTimeout(() => {
21
+ reply.status(200).send('hello world')
22
+ t.end()
23
+ }, 200)
24
+ })
25
+
26
+ await target.listen({ port: 0 })
27
+
28
+ const instance = Fastify()
29
+ t.teardown(instance.close.bind(instance))
30
+
31
+ instance.register(From, { http: { requestOptions: { timeout: 100 } } })
32
+
33
+ instance.get('/', (request, reply) => {
34
+ reply.from(`http://localhost:${target.server.address().port}/`)
35
+ })
36
+
37
+ await instance.listen({ port: 0 })
38
+
39
+ try {
40
+ await got.get(`http://localhost:${instance.server.address().port}/`, { retry: 0 })
41
+ } catch (err) {
42
+ t.equal(err.response.statusCode, 504)
43
+ t.match(err.response.headers['content-type'], /application\/json/)
44
+ t.same(JSON.parse(err.response.body), {
45
+ statusCode: 504,
46
+ code: 'FST_REPLY_FROM_GATEWAY_TIMEOUT',
47
+ error: 'Gateway Timeout',
48
+ message: 'Gateway Timeout'
49
+ })
50
+ clock.tick(200)
51
+ return
52
+ }
53
+
54
+ t.fail()
55
+ })
56
+
57
+ test('http sse removes timeout test', async (t) => {
58
+ const target = Fastify()
59
+ t.teardown(target.close.bind(target))
60
+
61
+ target.get('/', (request, reply) => {
62
+ t.pass('request arrives')
63
+
64
+ reply.header('content-type', 'text/event-stream').status(200).send('hello world')
65
+ })
66
+
67
+ await target.listen({ port: 0 })
68
+
69
+ const instance = Fastify()
70
+ t.teardown(instance.close.bind(instance))
71
+
72
+ instance.register(From, { http: { requestOptions: { timeout: 100 } } })
73
+
74
+ instance.get('/', (request, reply) => {
75
+ reply.from(`http://localhost:${target.server.address().port}/`)
76
+ })
77
+
78
+ await instance.listen({ port: 0 })
79
+
80
+ const { statusCode } = await got.get(`http://localhost:${instance.server.address().port}/`, { retry: 0 })
81
+ t.equal(statusCode, 200)
82
+ })
@@ -16,7 +16,9 @@ test('http -> http2 crash multiple times', async (t) => {
16
16
 
17
17
  instance.register(From, {
18
18
  base: 'http://localhost:3128',
19
- http2: true,
19
+ http2: {
20
+ sessionTimeout: 6000
21
+ },
20
22
  sessionTimeout: 200
21
23
  })
22
24
 
@@ -20,7 +20,7 @@ test('http2 request timeout', async (t) => {
20
20
 
21
21
  instance.register(From, {
22
22
  base: `http://localhost:${target.server.address().port}`,
23
- http2: { requestTimeout: 100 }
23
+ http2: { requestTimeout: 100, sessionTimeout: 6000 }
24
24
  })
25
25
 
26
26
  instance.get('/', (request, reply) => {
@@ -92,3 +92,34 @@ test('http2 session timeout', async (t) => {
92
92
 
93
93
  t.fail()
94
94
  })
95
+
96
+ test('http2 sse removes request and session timeout test', async (t) => {
97
+ const target = Fastify({ http2: true, sessionTimeout: 0 })
98
+
99
+ target.get('/', (request, reply) => {
100
+ t.pass('request arrives')
101
+
102
+ reply.status(200).header('content-type', 'text/event-stream').send('hello world')
103
+ })
104
+
105
+ await target.listen({ port: 0 })
106
+
107
+ const instance = Fastify()
108
+
109
+ instance.register(From, {
110
+ base: `http://localhost:${target.server.address().port}`,
111
+ http2: { sessionTimeout: 100 }
112
+ })
113
+
114
+ instance.get('/', (request, reply) => {
115
+ reply.from(`http://localhost:${target.server.address().port}/`)
116
+ })
117
+
118
+ await instance.listen({ port: 0 })
119
+
120
+ t.teardown(instance.close.bind(instance))
121
+ t.teardown(target.close.bind(target))
122
+
123
+ const { statusCode } = await got.get(`http://localhost:${instance.server.address().port}/`, { retry: 0 })
124
+ t.equal(statusCode, 200)
125
+ })
@@ -0,0 +1,58 @@
1
+ 'use strict'
2
+
3
+ const t = require('tap')
4
+ const http = require('http')
5
+ const net = require('net')
6
+ const Fastify = require('fastify')
7
+ const From = require('..')
8
+ const got = require('got')
9
+
10
+ t.autoend(false)
11
+
12
+ // never connect
13
+ net.connect = function (options) {
14
+ return new net.Socket(options)
15
+ }
16
+
17
+ const target = http.createServer((req, res) => {
18
+ t.fail('target never called')
19
+ })
20
+
21
+ async function main () {
22
+ t.plan(2)
23
+ await target.listen({ port: 0 })
24
+
25
+ const instance = Fastify()
26
+ t.teardown(instance.close.bind(instance))
27
+ t.teardown(target.close.bind(target))
28
+
29
+ instance.register(From, {
30
+ base: `http://localhost:${target.address().port}`,
31
+ undici: {
32
+ connectTimeout: 50
33
+ }
34
+ })
35
+
36
+ instance.get('/', (request, reply) => {
37
+ reply.from()
38
+ })
39
+
40
+ await instance.listen({ port: 0 })
41
+
42
+ try {
43
+ await got.get(`http://localhost:${instance.server.address().port}/`, { retry: 0 })
44
+ } catch (err) {
45
+ t.equal(err.response.statusCode, 500)
46
+ t.same(JSON.parse(err.response.body), {
47
+ statusCode: 500,
48
+ code: 'UND_ERR_CONNECT_TIMEOUT',
49
+ error: 'Internal Server Error',
50
+ message: 'Connect Timeout Error'
51
+ })
52
+ return
53
+ }
54
+
55
+ t.fail()
56
+ }
57
+
58
+ main()
@@ -1,57 +0,0 @@
1
- 'use strict'
2
-
3
- const t = require('tap')
4
- const Fastify = require('fastify')
5
- const From = require('..')
6
- const got = require('got')
7
- const FakeTimers = require('@sinonjs/fake-timers')
8
-
9
- const clock = FakeTimers.createClock()
10
-
11
- t.autoend(false)
12
-
13
- const target = Fastify()
14
- t.teardown(target.close.bind(target))
15
-
16
- target.get('/', (request, reply) => {
17
- t.pass('request arrives')
18
-
19
- clock.setTimeout(() => {
20
- reply.status(200).send('hello world')
21
- t.end()
22
- }, 200)
23
- })
24
-
25
- async function main () {
26
- await target.listen({ port: 0 })
27
-
28
- const instance = Fastify()
29
- t.teardown(instance.close.bind(instance))
30
-
31
- instance.register(From, { http: { requestOptions: { timeout: 100 } } })
32
-
33
- instance.get('/', (request, reply) => {
34
- reply.from(`http://localhost:${target.server.address().port}/`)
35
- })
36
-
37
- await instance.listen({ port: 0 })
38
-
39
- try {
40
- await got.get(`http://localhost:${instance.server.address().port}/`, { retry: 0 })
41
- } catch (err) {
42
- t.equal(err.response.statusCode, 504)
43
- t.match(err.response.headers['content-type'], /application\/json/)
44
- t.same(JSON.parse(err.response.body), {
45
- statusCode: 504,
46
- code: 'FST_REPLY_FROM_GATEWAY_TIMEOUT',
47
- error: 'Gateway Timeout',
48
- message: 'Gateway Timeout'
49
- })
50
- clock.tick(200)
51
- return
52
- }
53
-
54
- t.fail()
55
- }
56
-
57
- main()
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes