@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.
- package/.github/workflows/ci.yml +6 -0
- package/CLAUDE.md +66 -0
- package/LICENSE +4 -1
- package/README.md +24 -0
- package/index.js +12 -1
- package/lib/request.js +16 -6
- package/lib/utils.js +1 -0
- package/package.json +6 -8
- package/test/async-route-handler.test.js +29 -32
- package/test/balanced-pool.test.js +48 -0
- package/test/base-get.test.js +27 -30
- package/test/base-path.test.js +27 -24
- package/test/base-querystring.test.js +27 -30
- package/test/build-url.test.js +17 -15
- package/test/core-with-path-in-base.test.js +30 -33
- package/test/custom-undici-instance.test.js +28 -36
- package/test/disable-request-logging.test.js +38 -52
- package/test/fastify-multipart-incompatibility.test.js +43 -49
- package/test/fix-GHSA-v2v2-hph8-q5xp.test.js +3 -2
- package/test/full-delete-http2.test.js +12 -11
- package/test/full-get-test.test.js +27 -30
- package/test/full-https-get.test.js +30 -30
- package/test/full-post-extended-content-type.test.js +40 -44
- package/test/full-post-http2.test.js +18 -13
- package/test/full-post-stream-core.test.js +39 -43
- package/test/full-post-stream.test.js +39 -43
- package/test/full-post.test.js +36 -38
- package/test/full-querystring-rewrite-option-complex.test.js +27 -31
- package/test/full-querystring-rewrite-option-function-request.test.js +30 -36
- package/test/full-querystring-rewrite-option-function.test.js +30 -33
- package/test/full-querystring-rewrite-option.test.js +28 -31
- package/test/full-querystring-rewrite-string.test.js +27 -30
- package/test/full-querystring-rewrite.test.js +27 -30
- package/test/full-querystring.test.js +27 -30
- package/test/full-rewrite-body-content-type.test.js +37 -41
- package/test/full-rewrite-body-http.test.js +39 -43
- package/test/full-rewrite-body-to-empty-string.test.js +37 -41
- package/test/full-rewrite-body-to-null.test.js +37 -41
- package/test/full-rewrite-body.test.js +39 -43
- package/test/get-upstream-cache.test.js +12 -11
- package/test/get-upstream-http.test.js +35 -41
- package/test/get-upstream-type.test.js +24 -27
- package/test/get-upstream-undici.test.js +23 -26
- package/test/host-header.test.js +22 -18
- package/test/http-agents.test.js +27 -30
- package/test/http-global-agent.test.js +20 -33
- package/test/http-http2.test.js +13 -18
- package/test/http-invalid-target.test.js +13 -17
- package/test/http-retry.test.js +22 -29
- package/test/http-timeout.test.js +87 -27
- package/test/http2-canceled-streams-cleanup.test.js +117 -0
- package/test/http2-http2.test.js +8 -8
- package/test/http2-https.test.js +29 -27
- package/test/http2-invalid-base.test.js +2 -2
- package/test/http2-invalid-target.test.js +13 -17
- package/test/http2-target-crash.test.js +16 -20
- package/test/http2-target-multi-crash.test.js +22 -27
- package/test/http2-timeout-disabled.test.js +20 -15
- package/test/http2-timeout.test.js +94 -43
- package/test/http2-unix-socket.test.js +2 -2
- package/test/https-agents.test.js +31 -30
- package/test/https-global-agent.test.js +22 -31
- package/test/method.test.js +37 -42
- package/test/modifyCoreObjects-false.test.js +27 -30
- package/test/no-body-opts-with-get.test.js +25 -31
- package/test/no-body-opts-with-head.test.js +28 -32
- package/test/no-stream-body-option.test.js +34 -36
- package/test/on-error.test.js +21 -23
- package/test/on-invalid-upstream-response.test.js +30 -37
- package/test/onResponse.test.js +25 -32
- package/test/padded-body.test.js +38 -43
- package/test/post-formbody.test.js +35 -40
- package/test/post-plain-text.test.js +35 -39
- package/test/post-with-custom-encoded-contenttype.test.js +37 -39
- package/test/post-with-octet-stream.test.js +18 -88
- package/test/retry-on-503.test.js +27 -26
- package/test/retry-with-a-custom-handler.test.js +33 -40
- package/test/rewrite-headers-type.test.js +24 -29
- package/test/rewrite-headers.test.js +31 -36
- package/test/rewrite-request-headers-type.test.js +25 -28
- package/test/rewrite-request-headers.test.js +26 -30
- package/test/transform-body.test.js +37 -43
- package/test/undici-agent.test.js +27 -31
- package/test/undici-body.test.js +37 -42
- package/test/undici-chaining.test.js +35 -42
- package/test/undici-connect-timeout.test.js +13 -13
- package/test/undici-custom-dispatcher.test.js +11 -11
- package/test/undici-global-agent.test.js +14 -33
- package/test/undici-no-destroy.test.js +2 -2
- package/test/undici-options.test.js +21 -24
- package/test/undici-proxy-agent.test.js +4 -3
- package/test/undici-retry.test.js +24 -31
- package/test/undici-timeout-body-partial.test.js +14 -16
- package/test/undici-timeout-body.test.js +17 -19
- package/test/undici-timeout.test.js +86 -23
- package/test/undici-with-path-in-base.test.js +28 -31
- package/test/undici.test.js +28 -31
- package/test/unexpected-error.test.js +13 -18
- package/test/unix-http-undici-from.test.js +22 -31
- package/test/unix-http-undici.test.js +32 -40
- package/test/unix-http.test.js +31 -39
- package/test/unix-https-undici.test.js +30 -35
- package/test/unix-https.test.js +35 -40
- package/types/index.d.ts +13 -5
- package/types/index.test-d.ts +6 -6
- package/test/get-with-body.test.js +0 -55
- package/test/head-with-body.test.js +0 -57
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const t = require('
|
|
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.
|
|
16
|
-
t.
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
data
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
-
|
|
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
|
-
|
|
43
|
-
t.error(err)
|
|
41
|
+
t.after(() => target.close())
|
|
44
42
|
|
|
45
|
-
|
|
46
|
-
|
|
43
|
+
await new Promise(resolve => instance.listen({ port: 0 }, resolve))
|
|
44
|
+
await new Promise(resolve => target.listen({ port: 0 }, resolve))
|
|
47
45
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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('
|
|
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.
|
|
13
|
-
t.
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
data
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
|
-
|
|
35
|
-
})
|
|
34
|
+
instance.post('/', (_request, reply) => {
|
|
35
|
+
reply.from(`http://localhost:${target.address().port}`)
|
|
36
|
+
})
|
|
36
37
|
|
|
37
|
-
t.
|
|
38
|
+
t.after(() => target.close())
|
|
38
39
|
|
|
39
|
-
instance.listen({ port: 0 },
|
|
40
|
-
t.error(err)
|
|
40
|
+
await new Promise(resolve => instance.listen({ port: 0 }, resolve))
|
|
41
41
|
|
|
42
|
-
target.listen({ port: 0 },
|
|
43
|
-
t.error(err)
|
|
42
|
+
await new Promise(resolve => target.listen({ port: 0 }, resolve))
|
|
44
43
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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('
|
|
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.
|
|
22
|
-
t.
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
data
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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
|
-
|
|
44
|
-
})
|
|
43
|
+
instance.post('/', (_request, reply) => {
|
|
44
|
+
reply.from(`http://localhost:${target.address().port}`)
|
|
45
|
+
})
|
|
45
46
|
|
|
46
|
-
t.
|
|
47
|
+
t.after(() => target.close())
|
|
47
48
|
|
|
48
|
-
instance.listen({ port: 0 },
|
|
49
|
-
t.error(err)
|
|
49
|
+
await new Promise(resolve => instance.listen({ port: 0 }, resolve))
|
|
50
50
|
|
|
51
|
-
target.listen({ port: 0 },
|
|
52
|
-
t.error(err)
|
|
51
|
+
await new Promise(resolve => target.listen({ port: 0 }, resolve))
|
|
53
52
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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('
|
|
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(
|
|
23
|
-
t.
|
|
22
|
+
t.plan(6)
|
|
23
|
+
t.after(() => instance.close())
|
|
24
24
|
|
|
25
25
|
const target = http.createServer((req, res) => {
|
|
26
|
-
t.
|
|
27
|
-
t.
|
|
28
|
-
t.
|
|
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.
|
|
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.
|
|
47
|
+
t.after(() => target.close())
|
|
48
48
|
|
|
49
|
-
instance.listen({ port: 0 },
|
|
50
|
-
t.error(err)
|
|
49
|
+
await new Promise(resolve => instance.listen({ port: 0 }, resolve))
|
|
51
50
|
|
|
52
|
-
|
|
53
|
-
t.error(err)
|
|
51
|
+
await new Promise(resolve => target.listen({ port: 0 }, resolve))
|
|
54
52
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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.
|
|
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('
|
|
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.
|
|
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.
|
|
42
|
+
t.after(() => instance.close())
|
|
43
43
|
await instance.listen({ port: 0 })
|
|
44
44
|
|
|
45
|
-
const res = await
|
|
46
|
-
t.
|
|
47
|
-
t.
|
|
48
|
-
t.
|
|
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.
|
|
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.
|
|
67
|
+
t.after(() => instance.close())
|
|
68
68
|
await instance.listen({ port: 0 })
|
|
69
69
|
|
|
70
|
-
const res = await
|
|
71
|
-
t.
|
|
72
|
-
t.
|
|
73
|
-
t.
|
|
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.
|
|
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.
|
|
92
|
+
t.after(() => instance.close())
|
|
93
93
|
await instance.listen({ port: 0 })
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
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
|
})
|