@fastify/reply-from 12.0.2 → 12.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.
Files changed (107) hide show
  1. package/.github/workflows/ci.yml +6 -0
  2. package/CLAUDE.md +66 -0
  3. package/LICENSE +4 -1
  4. package/README.md +24 -0
  5. package/index.js +12 -1
  6. package/lib/request.js +16 -6
  7. package/lib/utils.js +1 -0
  8. package/package.json +6 -8
  9. package/test/async-route-handler.test.js +29 -32
  10. package/test/balanced-pool.test.js +48 -0
  11. package/test/base-get.test.js +27 -30
  12. package/test/base-path.test.js +27 -24
  13. package/test/base-querystring.test.js +27 -30
  14. package/test/build-url.test.js +17 -15
  15. package/test/core-with-path-in-base.test.js +30 -33
  16. package/test/custom-undici-instance.test.js +28 -36
  17. package/test/disable-request-logging.test.js +38 -52
  18. package/test/fastify-multipart-incompatibility.test.js +43 -49
  19. package/test/fix-GHSA-v2v2-hph8-q5xp.test.js +3 -2
  20. package/test/full-delete-http2.test.js +12 -11
  21. package/test/full-get-test.test.js +27 -30
  22. package/test/full-https-get.test.js +30 -30
  23. package/test/full-post-extended-content-type.test.js +40 -44
  24. package/test/full-post-http2.test.js +18 -13
  25. package/test/full-post-stream-core.test.js +39 -43
  26. package/test/full-post-stream.test.js +39 -43
  27. package/test/full-post.test.js +36 -38
  28. package/test/full-querystring-rewrite-option-complex.test.js +27 -31
  29. package/test/full-querystring-rewrite-option-function-request.test.js +30 -36
  30. package/test/full-querystring-rewrite-option-function.test.js +30 -33
  31. package/test/full-querystring-rewrite-option.test.js +28 -31
  32. package/test/full-querystring-rewrite-string.test.js +27 -30
  33. package/test/full-querystring-rewrite.test.js +27 -30
  34. package/test/full-querystring.test.js +27 -30
  35. package/test/full-rewrite-body-content-type.test.js +37 -41
  36. package/test/full-rewrite-body-http.test.js +39 -43
  37. package/test/full-rewrite-body-to-empty-string.test.js +37 -41
  38. package/test/full-rewrite-body-to-null.test.js +37 -41
  39. package/test/full-rewrite-body.test.js +39 -43
  40. package/test/get-upstream-cache.test.js +12 -11
  41. package/test/get-upstream-http.test.js +35 -41
  42. package/test/get-upstream-type.test.js +24 -27
  43. package/test/get-upstream-undici.test.js +23 -26
  44. package/test/host-header.test.js +22 -18
  45. package/test/http-agents.test.js +27 -30
  46. package/test/http-global-agent.test.js +20 -33
  47. package/test/http-http2.test.js +13 -18
  48. package/test/http-invalid-target.test.js +13 -17
  49. package/test/http-retry.test.js +22 -29
  50. package/test/http-timeout.test.js +87 -27
  51. package/test/http2-canceled-streams-cleanup.test.js +117 -0
  52. package/test/http2-http2.test.js +8 -8
  53. package/test/http2-https.test.js +29 -27
  54. package/test/http2-invalid-base.test.js +2 -2
  55. package/test/http2-invalid-target.test.js +13 -17
  56. package/test/http2-target-crash.test.js +16 -20
  57. package/test/http2-target-multi-crash.test.js +22 -27
  58. package/test/http2-timeout-disabled.test.js +20 -15
  59. package/test/http2-timeout.test.js +94 -43
  60. package/test/http2-unix-socket.test.js +2 -2
  61. package/test/https-agents.test.js +31 -30
  62. package/test/https-global-agent.test.js +22 -31
  63. package/test/method.test.js +37 -42
  64. package/test/modifyCoreObjects-false.test.js +27 -30
  65. package/test/no-body-opts-with-get.test.js +25 -31
  66. package/test/no-body-opts-with-head.test.js +28 -32
  67. package/test/no-stream-body-option.test.js +34 -36
  68. package/test/on-error.test.js +21 -23
  69. package/test/on-invalid-upstream-response.test.js +30 -37
  70. package/test/onResponse.test.js +25 -32
  71. package/test/padded-body.test.js +38 -43
  72. package/test/post-formbody.test.js +35 -40
  73. package/test/post-plain-text.test.js +35 -39
  74. package/test/post-with-custom-encoded-contenttype.test.js +37 -39
  75. package/test/post-with-octet-stream.test.js +18 -88
  76. package/test/retry-on-503.test.js +27 -26
  77. package/test/retry-with-a-custom-handler.test.js +33 -40
  78. package/test/rewrite-headers-type.test.js +24 -29
  79. package/test/rewrite-headers.test.js +31 -36
  80. package/test/rewrite-request-headers-type.test.js +25 -28
  81. package/test/rewrite-request-headers.test.js +26 -30
  82. package/test/transform-body.test.js +37 -43
  83. package/test/undici-agent.test.js +27 -31
  84. package/test/undici-body.test.js +37 -42
  85. package/test/undici-chaining.test.js +35 -42
  86. package/test/undici-connect-timeout.test.js +13 -13
  87. package/test/undici-custom-dispatcher.test.js +11 -11
  88. package/test/undici-global-agent.test.js +14 -33
  89. package/test/undici-no-destroy.test.js +2 -2
  90. package/test/undici-options.test.js +21 -24
  91. package/test/undici-proxy-agent.test.js +4 -3
  92. package/test/undici-retry.test.js +24 -31
  93. package/test/undici-timeout-body-partial.test.js +14 -16
  94. package/test/undici-timeout-body.test.js +17 -19
  95. package/test/undici-timeout.test.js +86 -23
  96. package/test/undici-with-path-in-base.test.js +28 -31
  97. package/test/undici.test.js +28 -31
  98. package/test/unexpected-error.test.js +13 -18
  99. package/test/unix-http-undici-from.test.js +22 -31
  100. package/test/unix-http-undici.test.js +32 -40
  101. package/test/unix-http.test.js +31 -39
  102. package/test/unix-https-undici.test.js +30 -35
  103. package/test/unix-https.test.js +35 -40
  104. package/types/index.d.ts +13 -5
  105. package/types/index.test-d.ts +6 -6
  106. package/test/get-with-body.test.js +0 -55
  107. package/test/head-with-body.test.js +0 -57
@@ -1,10 +1,10 @@
1
1
  'use strict'
2
2
 
3
- const t = require('tap')
3
+ const t = require('node:test')
4
4
  const Fastify = require('fastify')
5
+ const { request } = require('undici')
5
6
  const From = require('..')
6
7
  const http = require('node:http')
7
- const get = require('simple-get').concat
8
8
 
9
9
  const instance = Fastify()
10
10
  instance.register(From, {
@@ -12,48 +12,43 @@ instance.register(From, {
12
12
  })
13
13
  instance.register(require('@fastify/formbody'))
14
14
 
15
- t.plan(9)
16
- t.teardown(instance.close.bind(instance))
17
-
18
- const target = http.createServer((req, res) => {
19
- t.pass('request proxied')
20
- t.equal(req.method, 'POST')
21
- t.equal(req.headers['content-type'], 'application/x-www-form-urlencoded')
22
- let data = ''
23
- req.setEncoding('utf8')
24
- req.on('data', (d) => {
25
- data += d
26
- })
27
- req.on('end', () => {
28
- const str = data.toString()
29
- t.same(JSON.parse(data), { some: 'info', another: 'detail' })
30
- res.statusCode = 200
31
- res.setHeader('content-type', 'application/x-www-form-urlencoded')
32
- res.end(str)
15
+ t.test('post-formbody', async (t) => {
16
+ t.plan(6)
17
+ t.after(() => instance.close())
18
+
19
+ const target = http.createServer((req, res) => {
20
+ t.assert.ok('request proxied')
21
+ t.assert.strictEqual(req.method, 'POST')
22
+ t.assert.strictEqual(req.headers['content-type'], 'application/x-www-form-urlencoded')
23
+ let data = ''
24
+ req.setEncoding('utf8')
25
+ req.on('data', (d) => {
26
+ data += d
27
+ })
28
+ req.on('end', () => {
29
+ const str = data.toString()
30
+ t.assert.deepStrictEqual(JSON.parse(data), { some: 'info', another: 'detail' })
31
+ res.statusCode = 200
32
+ res.setHeader('content-type', 'application/x-www-form-urlencoded')
33
+ res.end(str)
34
+ })
33
35
  })
34
- })
35
36
 
36
- instance.post('/', (_request, reply) => {
37
- reply.from(`http://localhost:${target.address().port}`)
38
- })
39
-
40
- t.teardown(target.close.bind(target))
37
+ instance.post('/', (_request, reply) => {
38
+ reply.from(`http://localhost:${target.address().port}`)
39
+ })
41
40
 
42
- instance.listen({ port: 0 }, (err) => {
43
- t.error(err)
41
+ t.after(() => target.close())
44
42
 
45
- target.listen({ port: 0 }, (err) => {
46
- t.error(err)
43
+ await new Promise(resolve => instance.listen({ port: 0 }, resolve))
44
+ await new Promise(resolve => target.listen({ port: 0 }, resolve))
47
45
 
48
- get({
49
- url: `http://localhost:${instance.server.address().port}`,
50
- method: 'POST',
51
- headers: { 'content-type': 'application/x-www-form-urlencoded' },
52
- body: 'some=info&another=detail'
53
- }, (err, res, data) => {
54
- t.error(err)
55
- t.equal(res.headers['content-type'], 'application/x-www-form-urlencoded')
56
- t.same(JSON.parse(data), { some: 'info', another: 'detail' })
57
- })
46
+ const result = await request(`http://localhost:${instance.server.address().port}`, {
47
+ method: 'POST',
48
+ headers: { 'content-type': 'application/x-www-form-urlencoded' },
49
+ body: 'some=info&another=detail'
58
50
  })
51
+
52
+ t.assert.strictEqual(result.headers['content-type'], 'application/x-www-form-urlencoded')
53
+ t.assert.deepStrictEqual(await result.body.json(), { some: 'info', another: 'detail' })
59
54
  })
@@ -1,56 +1,52 @@
1
1
  'use strict'
2
2
 
3
- const t = require('tap')
3
+ const t = require('node:test')
4
4
  const Fastify = require('fastify')
5
+ const { request } = require('undici')
5
6
  const From = require('..')
6
7
  const http = require('node:http')
7
- const get = require('simple-get').concat
8
8
 
9
9
  const instance = Fastify()
10
10
  instance.register(From)
11
11
 
12
- t.plan(9)
13
- t.teardown(instance.close.bind(instance))
14
-
15
- const target = http.createServer((req, res) => {
16
- t.pass('request proxied')
17
- t.equal(req.method, 'POST')
18
- t.equal(req.headers['content-type'], 'text/plain')
19
- let data = ''
20
- req.setEncoding('utf8')
21
- req.on('data', (d) => {
22
- data += d
23
- })
24
- req.on('end', () => {
25
- const str = data.toString()
26
- t.same(str, 'this is plain text')
27
- res.statusCode = 200
28
- res.setHeader('content-type', 'text/plain')
29
- res.end(str)
12
+ t.test('post plain text', async (t) => {
13
+ t.plan(6)
14
+ t.after(() => instance.close())
15
+
16
+ const target = http.createServer((req, res) => {
17
+ t.assert.ok('request proxied')
18
+ t.assert.strictEqual(req.method, 'POST')
19
+ t.assert.strictEqual(req.headers['content-type'], 'text/plain')
20
+ let data = ''
21
+ req.setEncoding('utf8')
22
+ req.on('data', (d) => {
23
+ data += d
24
+ })
25
+ req.on('end', () => {
26
+ const str = data.toString()
27
+ t.assert.deepStrictEqual(str, 'this is plain text')
28
+ res.statusCode = 200
29
+ res.setHeader('content-type', 'text/plain')
30
+ res.end(str)
31
+ })
30
32
  })
31
- })
32
33
 
33
- instance.post('/', (_request, reply) => {
34
- reply.from(`http://localhost:${target.address().port}`)
35
- })
34
+ instance.post('/', (_request, reply) => {
35
+ reply.from(`http://localhost:${target.address().port}`)
36
+ })
36
37
 
37
- t.teardown(target.close.bind(target))
38
+ t.after(() => target.close())
38
39
 
39
- instance.listen({ port: 0 }, (err) => {
40
- t.error(err)
40
+ await new Promise(resolve => instance.listen({ port: 0 }, resolve))
41
41
 
42
- target.listen({ port: 0 }, (err) => {
43
- t.error(err)
42
+ await new Promise(resolve => target.listen({ port: 0 }, resolve))
44
43
 
45
- get({
46
- url: `http://localhost:${instance.server.address().port}`,
47
- method: 'POST',
48
- headers: { 'content-type': 'text/plain' },
49
- body: 'this is plain text'
50
- }, (err, res, data) => {
51
- t.error(err)
52
- t.equal(res.headers['content-type'], 'text/plain')
53
- t.same(data.toString(), 'this is plain text')
54
- })
44
+ const result = await request(`http://localhost:${instance.server.address().port}`, {
45
+ method: 'POST',
46
+ headers: { 'content-type': 'text/plain' },
47
+ body: 'this is plain text'
55
48
  })
49
+
50
+ t.assert.strictEqual(result.headers['content-type'], 'text/plain')
51
+ t.assert.deepStrictEqual(await result.body.text(), 'this is plain text')
56
52
  })
@@ -1,10 +1,10 @@
1
1
  'use strict'
2
2
 
3
- const t = require('tap')
3
+ const t = require('node:test')
4
4
  const Fastify = require('fastify')
5
+ const { request } = require('undici')
5
6
  const From = require('..')
6
7
  const http = require('node:http')
7
- const get = require('simple-get').concat
8
8
  const { parse } = require('node:querystring')
9
9
 
10
10
  const instance = Fastify()
@@ -18,48 +18,46 @@ instance.addContentTypeParser(
18
18
  (_req, body, done) => done(null, parse(body.toString()))
19
19
  )
20
20
 
21
- t.plan(9)
22
- t.teardown(instance.close.bind(instance))
23
-
24
- const target = http.createServer((req, res) => {
25
- t.pass('request proxied')
26
- t.equal(req.method, 'POST')
27
- t.equal(req.headers['content-type'], 'application/x-www-form-urlencoded')
28
- let data = ''
29
- req.setEncoding('utf8')
30
- req.on('data', (d) => {
31
- data += d
32
- })
33
- req.on('end', () => {
34
- const str = data.toString()
35
- t.same(JSON.parse(data), { some: 'info', another: 'detail' })
36
- res.statusCode = 200
37
- res.setHeader('content-type', 'application/x-www-form-urlencoded')
38
- res.end(str)
21
+ t.test('post with custom encoded content-type', async (t) => {
22
+ t.plan(6)
23
+ t.after(() => instance.close())
24
+
25
+ const target = http.createServer((req, res) => {
26
+ t.assert.ok('request proxied')
27
+ t.assert.strictEqual(req.method, 'POST')
28
+ t.assert.strictEqual(req.headers['content-type'], 'application/x-www-form-urlencoded')
29
+ let data = ''
30
+ req.setEncoding('utf8')
31
+ req.on('data', (d) => {
32
+ data += d
33
+ })
34
+ req.on('end', () => {
35
+ const str = data.toString()
36
+ t.assert.deepStrictEqual(JSON.parse(data), { some: 'info', another: 'detail' })
37
+ res.statusCode = 200
38
+ res.setHeader('content-type', 'application/x-www-form-urlencoded')
39
+ res.end(str)
40
+ })
39
41
  })
40
- })
41
42
 
42
- instance.post('/', (_request, reply) => {
43
- reply.from(`http://localhost:${target.address().port}`)
44
- })
43
+ instance.post('/', (_request, reply) => {
44
+ reply.from(`http://localhost:${target.address().port}`)
45
+ })
45
46
 
46
- t.teardown(target.close.bind(target))
47
+ t.after(() => target.close())
47
48
 
48
- instance.listen({ port: 0 }, (err) => {
49
- t.error(err)
49
+ await new Promise(resolve => instance.listen({ port: 0 }, resolve))
50
50
 
51
- target.listen({ port: 0 }, (err) => {
52
- t.error(err)
51
+ await new Promise(resolve => target.listen({ port: 0 }, resolve))
53
52
 
54
- get({
55
- url: `http://localhost:${instance.server.address().port}`,
56
- method: 'POST',
57
- headers: { 'content-type': 'application/x-www-form-urlencoded' },
58
- body: 'some=info&another=detail'
59
- }, (err, res, data) => {
60
- t.error(err)
61
- t.equal(res.headers['content-type'], 'application/x-www-form-urlencoded')
62
- t.same(JSON.parse(data), { some: 'info', another: 'detail' })
63
- })
53
+ const result = await request(`http://localhost:${instance.server.address().port}`, {
54
+ method: 'POST',
55
+ headers: {
56
+ 'content-type': 'application/x-www-form-urlencoded'
57
+ },
58
+ body: 'some=info&another=detail'
64
59
  })
60
+
61
+ t.assert.strictEqual(result.headers['content-type'], 'application/x-www-form-urlencoded')
62
+ t.assert.deepStrictEqual(await result.body.json(), { some: 'info', another: 'detail' })
65
63
  })
@@ -1,13 +1,13 @@
1
1
  'use strict'
2
2
 
3
- const test = require('tap').test
3
+ const { test } = require('node:test')
4
4
  const Fastify = require('fastify')
5
+ const { request } = require('undici')
5
6
  const From = require('../index')
6
7
  const http = require('node:http')
7
- const get = require('simple-get').concat
8
8
  const { parse } = require('node:querystring')
9
9
 
10
- test('with explicitly set content-type application/octet-stream', t => {
10
+ test('with explicitly set content-type application/octet-stream', async t => {
11
11
  const instance = Fastify()
12
12
  instance.register(From, {
13
13
  contentTypesToEncode: ['application/octet-stream']
@@ -19,13 +19,13 @@ test('with explicitly set content-type application/octet-stream', t => {
19
19
  (_req, body, done) => done(null, parse(body.toString()))
20
20
  )
21
21
 
22
- t.plan(9)
23
- t.teardown(instance.close.bind(instance))
22
+ t.plan(6)
23
+ t.after(() => instance.close())
24
24
 
25
25
  const target = http.createServer((req, res) => {
26
- t.pass('request proxied')
27
- t.equal(req.method, 'POST')
28
- t.equal(req.headers['content-type'], 'application/octet-stream')
26
+ t.assert.ok('request proxied')
27
+ t.assert.strictEqual(req.method, 'POST')
28
+ t.assert.strictEqual(req.headers['content-type'], 'application/octet-stream')
29
29
  let data = ''
30
30
  req.setEncoding('utf8')
31
31
  req.on('data', (d) => {
@@ -33,7 +33,7 @@ test('with explicitly set content-type application/octet-stream', t => {
33
33
  })
34
34
  req.on('end', () => {
35
35
  const str = data.toString()
36
- t.same(JSON.parse(data), { some: 'info', another: 'detail' })
36
+ t.assert.deepStrictEqual(JSON.parse(data), { some: 'info', another: 'detail' })
37
37
  res.statusCode = 200
38
38
  res.setHeader('content-type', 'application/octet-stream')
39
39
  res.end(str)
@@ -44,88 +44,18 @@ test('with explicitly set content-type application/octet-stream', t => {
44
44
  reply.from(`http://localhost:${target.address().port}`)
45
45
  })
46
46
 
47
- t.teardown(target.close.bind(target))
47
+ t.after(() => target.close())
48
48
 
49
- instance.listen({ port: 0 }, (err) => {
50
- t.error(err)
49
+ await new Promise(resolve => instance.listen({ port: 0 }, resolve))
51
50
 
52
- target.listen({ port: 0 }, (err) => {
53
- t.error(err)
51
+ await new Promise(resolve => target.listen({ port: 0 }, resolve))
54
52
 
55
- get({
56
- url: `http://localhost:${instance.server.address().port}`,
57
- method: 'POST',
58
- headers: { 'content-type': 'application/octet-stream' },
59
- body: 'some=info&another=detail'
60
- }, (err, res, data) => {
61
- t.error(err)
62
- t.equal(res.headers['content-type'], 'application/octet-stream')
63
- t.same(JSON.parse(data), { some: 'info', another: 'detail' })
64
- })
65
- })
66
- })
67
- })
68
-
69
- test('with implicit content-type application/octet-stream', t => {
70
- const instance = Fastify()
71
- instance.register(From, {
72
- contentTypesToEncode: ['application/octet-stream']
73
- })
74
-
75
- instance.addContentTypeParser(
76
- 'application/octet-stream',
77
- { parseAs: 'buffer', bodyLimit: 1000 },
78
- (_req, body, done) => done(null, parse(body.toString()))
79
- )
80
-
81
- instance.addContentTypeParser(
82
- '*',
83
- { parseAs: 'buffer', bodyLimit: 1000 },
84
- (_req, body, done) => done(null, parse(body.toString()))
85
- )
86
-
87
- t.plan(9)
88
- t.teardown(instance.close.bind(instance))
89
-
90
- const target = http.createServer((req, res) => {
91
- t.pass('request proxied')
92
- t.equal(req.method, 'POST')
93
- t.equal(req.headers['content-type'], 'application/octet-stream')
94
- let data = ''
95
- req.setEncoding('utf8')
96
- req.on('data', (d) => {
97
- data += d
98
- })
99
- req.on('end', () => {
100
- const str = data.toString()
101
- t.same(JSON.parse(data), { some: 'info', another: 'detail' })
102
- res.statusCode = 200
103
- res.setHeader('content-type', 'application/octet-stream')
104
- res.end(str)
105
- })
106
- })
107
-
108
- instance.post('/', (_request, reply) => {
109
- reply.from(`http://localhost:${target.address().port}`)
53
+ const result = await request(`http://localhost:${instance.server.address().port}`, {
54
+ method: 'POST',
55
+ headers: { 'content-type': 'application/octet-stream' },
56
+ body: 'some=info&another=detail'
110
57
  })
111
58
 
112
- t.teardown(target.close.bind(target))
113
-
114
- instance.listen({ port: 0 }, (err) => {
115
- t.error(err)
116
-
117
- target.listen({ port: 0 }, (err) => {
118
- t.error(err)
119
-
120
- get({
121
- url: `http://localhost:${instance.server.address().port}`,
122
- method: 'POST',
123
- body: 'some=info&another=detail'
124
- }, (err, res, data) => {
125
- t.error(err)
126
- t.equal(res.headers['content-type'], 'application/octet-stream')
127
- t.same(JSON.parse(data), { some: 'info', another: 'detail' })
128
- })
129
- })
130
- })
59
+ t.assert.strictEqual(result.headers['content-type'], 'application/octet-stream')
60
+ t.assert.deepStrictEqual(await result.body.json(), { some: 'info', another: 'detail' })
131
61
  })
@@ -1,10 +1,10 @@
1
1
  'use strict'
2
2
 
3
- const { test } = require('tap')
3
+ const { test } = require('node:test')
4
4
  const Fastify = require('fastify')
5
+ const { request } = require('undici')
5
6
  const From = require('..')
6
7
  const http = require('node:http')
7
- const got = require('got')
8
8
 
9
9
  function createTargetServer (withRetryAfterHeader, stopAfter = 1) {
10
10
  let requestCount = 0
@@ -26,8 +26,8 @@ function createTargetServer (withRetryAfterHeader, stopAfter = 1) {
26
26
  test('Should retry on 503 HTTP error', async function (t) {
27
27
  t.plan(3)
28
28
  const target = createTargetServer()
29
- await target.listen({ port: 0 })
30
- t.teardown(target.close.bind(target))
29
+ await new Promise(resolve => target.listen({ port: 0 }, resolve))
30
+ t.after(() => target.close())
31
31
 
32
32
  const instance = Fastify()
33
33
 
@@ -39,20 +39,20 @@ test('Should retry on 503 HTTP error', async function (t) {
39
39
  reply.from()
40
40
  })
41
41
 
42
- t.teardown(instance.close.bind(instance))
42
+ t.after(() => instance.close())
43
43
  await instance.listen({ port: 0 })
44
44
 
45
- const res = await got.get(`http://localhost:${instance.server.address().port}`, { retry: 0 })
46
- t.equal(res.headers['content-type'], 'text/plain')
47
- t.equal(res.statusCode, 205)
48
- t.equal(res.body.toString(), 'Hello World 2!')
45
+ const res = await request(`http://localhost:${instance.server.address().port}`)
46
+ t.assert.strictEqual(res.headers['content-type'], 'text/plain')
47
+ t.assert.strictEqual(res.statusCode, 205)
48
+ t.assert.strictEqual(await res.body.text(), 'Hello World 2!')
49
49
  })
50
50
 
51
51
  test('Should retry on 503 HTTP error with Retry-After response header', async function (t) {
52
52
  t.plan(3)
53
53
  const target = createTargetServer(true)
54
- await target.listen({ port: 0 })
55
- t.teardown(target.close.bind(target))
54
+ await new Promise(resolve => target.listen({ port: 0 }, resolve))
55
+ t.after(() => target.close())
56
56
 
57
57
  const instance = Fastify()
58
58
 
@@ -64,20 +64,20 @@ test('Should retry on 503 HTTP error with Retry-After response header', async fu
64
64
  reply.from()
65
65
  })
66
66
 
67
- t.teardown(instance.close.bind(instance))
67
+ t.after(() => instance.close())
68
68
  await instance.listen({ port: 0 })
69
69
 
70
- const res = await got.get(`http://localhost:${instance.server.address().port}`, { retry: 0 })
71
- t.equal(res.headers['content-type'], 'text/plain')
72
- t.equal(res.statusCode, 205)
73
- t.equal(res.body.toString(), 'Hello World 2!')
70
+ const res = await request(`http://localhost:${instance.server.address().port}`)
71
+ t.assert.strictEqual(res.headers['content-type'], 'text/plain')
72
+ t.assert.strictEqual(res.statusCode, 205)
73
+ t.assert.strictEqual(await res.body.text(), 'Hello World 2!')
74
74
  })
75
75
 
76
76
  test('Should abort if server is always returning 503', async function (t) {
77
77
  t.plan(2)
78
78
  const target = createTargetServer(true, Number.MAX_SAFE_INTEGER)
79
- await target.listen({ port: 0 })
80
- t.teardown(target.close.bind(target))
79
+ await new Promise(resolve => target.listen({ port: 0 }, resolve))
80
+ t.after(() => target.close())
81
81
 
82
82
  const instance = Fastify()
83
83
 
@@ -89,13 +89,14 @@ test('Should abort if server is always returning 503', async function (t) {
89
89
  reply.from()
90
90
  })
91
91
 
92
- t.teardown(instance.close.bind(instance))
92
+ t.after(() => instance.close())
93
93
  await instance.listen({ port: 0 })
94
- try {
95
- await got.get(`http://localhost:${instance.server.address().port}`, { retry: 0 })
96
- t.fail()
97
- } catch (err) {
98
- t.equal(err.response.statusCode, 503)
99
- t.equal(err.response.body.toString(), 'This Service is Unavailable')
100
- }
94
+
95
+ await request(`http://localhost:${instance.server.address().port}`)
96
+ await request(`http://localhost:${instance.server.address().port}`)
97
+ await request(`http://localhost:${instance.server.address().port}`)
98
+ await request(`http://localhost:${instance.server.address().port}`)
99
+ const result = await request(`http://localhost:${instance.server.address().port}`)
100
+ t.assert.strictEqual(result.statusCode, 503)
101
+ t.assert.strictEqual(await result.body.text(), 'This Service is Unavailable')
101
102
  })