@fastify/reply-from 12.1.0 → 12.3.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 (108) hide show
  1. package/.claude/settings.local.json +9 -0
  2. package/.github/workflows/ci.yml +3 -0
  3. package/LICENSE +4 -1
  4. package/README.md +20 -0
  5. package/index.js +10 -0
  6. package/lib/request.js +13 -3
  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 +49 -42
  51. package/test/http2-canceled-streams-cleanup.test.js +117 -0
  52. package/test/http2-goaway.test.js +126 -0
  53. package/test/http2-http2.test.js +8 -8
  54. package/test/http2-https.test.js +29 -27
  55. package/test/http2-invalid-base.test.js +2 -2
  56. package/test/http2-invalid-target.test.js +13 -17
  57. package/test/http2-target-crash.test.js +16 -20
  58. package/test/http2-target-multi-crash.test.js +22 -27
  59. package/test/http2-timeout-disabled.test.js +20 -15
  60. package/test/http2-timeout.test.js +58 -65
  61. package/test/http2-unix-socket.test.js +2 -2
  62. package/test/https-agents.test.js +31 -30
  63. package/test/https-global-agent.test.js +22 -31
  64. package/test/method.test.js +37 -42
  65. package/test/modifyCoreObjects-false.test.js +27 -30
  66. package/test/no-body-opts-with-get.test.js +25 -31
  67. package/test/no-body-opts-with-head.test.js +28 -32
  68. package/test/no-stream-body-option.test.js +34 -36
  69. package/test/on-error.test.js +21 -23
  70. package/test/on-invalid-upstream-response.test.js +30 -37
  71. package/test/onResponse.test.js +25 -32
  72. package/test/padded-body.test.js +38 -43
  73. package/test/post-formbody.test.js +35 -40
  74. package/test/post-plain-text.test.js +35 -39
  75. package/test/post-with-custom-encoded-contenttype.test.js +37 -39
  76. package/test/post-with-octet-stream.test.js +18 -88
  77. package/test/retry-on-503.test.js +27 -26
  78. package/test/retry-with-a-custom-handler.test.js +33 -40
  79. package/test/rewrite-headers-type.test.js +24 -29
  80. package/test/rewrite-headers.test.js +31 -36
  81. package/test/rewrite-request-headers-type.test.js +25 -28
  82. package/test/rewrite-request-headers.test.js +26 -30
  83. package/test/transform-body.test.js +37 -43
  84. package/test/undici-agent.test.js +27 -31
  85. package/test/undici-body.test.js +37 -42
  86. package/test/undici-chaining.test.js +35 -42
  87. package/test/undici-connect-timeout.test.js +13 -13
  88. package/test/undici-custom-dispatcher.test.js +11 -11
  89. package/test/undici-global-agent.test.js +14 -33
  90. package/test/undici-no-destroy.test.js +2 -2
  91. package/test/undici-options.test.js +21 -24
  92. package/test/undici-proxy-agent.test.js +4 -3
  93. package/test/undici-retry.test.js +24 -31
  94. package/test/undici-timeout-body-partial.test.js +14 -16
  95. package/test/undici-timeout-body.test.js +17 -19
  96. package/test/undici-timeout.test.js +41 -37
  97. package/test/undici-with-path-in-base.test.js +28 -31
  98. package/test/undici.test.js +28 -31
  99. package/test/unexpected-error.test.js +13 -18
  100. package/test/unix-http-undici-from.test.js +22 -31
  101. package/test/unix-http-undici.test.js +32 -40
  102. package/test/unix-http.test.js +31 -39
  103. package/test/unix-https-undici.test.js +30 -35
  104. package/test/unix-https.test.js +35 -40
  105. package/types/index.d.ts +9 -2
  106. package/types/index.test-d.ts +3 -4
  107. package/test/get-with-body.test.js +0 -55
  108. 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, Agent } = require('undici')
5
6
  const From = require('..')
6
7
  const https = require('node:https')
7
- const get = require('simple-get').concat
8
8
  const fs = require('node:fs')
9
9
  const path = require('node:path')
10
10
  const certs = {
@@ -17,39 +17,39 @@ const instance = Fastify({
17
17
  })
18
18
  instance.register(From)
19
19
 
20
- t.plan(9)
21
- t.teardown(instance.close.bind(instance))
22
-
23
- const target = https.createServer(certs, (req, res) => {
24
- t.pass('request proxied')
25
- t.equal(req.method, 'GET')
26
- res.statusCode = 205
27
- res.setHeader('Content-Type', 'text/plain')
28
- res.setHeader('x-my-header', 'hello!')
29
- res.end('hello world')
30
- })
20
+ t.test('full-https-get', async (t) => {
21
+ t.plan(6)
22
+ t.after(() => instance.close())
23
+
24
+ const target = https.createServer(certs, (req, res) => {
25
+ t.assert.ok('request proxied')
26
+ t.assert.strictEqual(req.method, 'GET')
27
+ res.statusCode = 205
28
+ res.setHeader('Content-Type', 'text/plain')
29
+ res.setHeader('x-my-header', 'hello!')
30
+ res.end('hello world')
31
+ })
31
32
 
32
- instance.get('/', (_request, reply) => {
33
- reply.from(`https://localhost:${target.address().port}`)
34
- })
33
+ instance.get('/', (_request, reply) => {
34
+ reply.from(`https://localhost:${target.address().port}`)
35
+ })
35
36
 
36
- t.teardown(target.close.bind(target))
37
+ t.after(() => target.close())
37
38
 
38
- instance.listen({ port: 0 }, (err) => {
39
- t.error(err)
39
+ await new Promise(resolve => instance.listen({ port: 0 }, resolve))
40
40
 
41
- target.listen({ port: 0 }, (err) => {
42
- t.error(err)
41
+ await new Promise(resolve => target.listen({ port: 0 }, resolve))
43
42
 
44
- get({
45
- url: `https://localhost:${instance.server.address().port}`,
46
- rejectUnauthorized: false
47
- }, (err, res, data) => {
48
- t.error(err)
49
- t.equal(res.headers['content-type'], 'text/plain')
50
- t.equal(res.headers['x-my-header'], 'hello!')
51
- t.equal(res.statusCode, 205)
52
- t.equal(data.toString(), 'hello world')
43
+ const result = await request(`https://localhost:${instance.server.address().port}`, {
44
+ dispatcher: new Agent({
45
+ connect: {
46
+ rejectUnauthorized: false
47
+ }
53
48
  })
54
49
  })
50
+
51
+ t.assert.strictEqual(result.headers['content-type'], 'text/plain')
52
+ t.assert.strictEqual(result.headers['x-my-header'], 'hello!')
53
+ t.assert.strictEqual(result.statusCode, 205)
54
+ t.assert.strictEqual(await result.body.text(), 'hello world')
55
55
  })
@@ -1,59 +1,55 @@
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')
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'].startsWith('application/json'), true)
19
- let data = ''
20
- req.setEncoding('utf8')
21
- req.on('data', (d) => {
22
- data += d
12
+ t.test('full post extended content type', 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'].startsWith('application/json'), true)
20
+ let data = ''
21
+ req.setEncoding('utf8')
22
+ req.on('data', (d) => {
23
+ data += d
24
+ })
25
+ req.on('end', () => {
26
+ t.assert.deepStrictEqual(JSON.parse(data), { hello: 'world' })
27
+ res.statusCode = 200
28
+ res.setHeader('content-type', 'application/json')
29
+ res.end(JSON.stringify({ something: 'else' }))
30
+ })
23
31
  })
24
- req.on('end', () => {
25
- t.same(JSON.parse(data), { hello: 'world' })
26
- res.statusCode = 200
27
- res.setHeader('content-type', 'application/json')
28
- res.end(JSON.stringify({ something: 'else' }))
32
+
33
+ instance.post('/', (_request, reply) => {
34
+ reply.from(`http://localhost:${target.address().port}`)
29
35
  })
30
- })
31
36
 
32
- instance.post('/', (_request, reply) => {
33
- reply.from(`http://localhost:${target.address().port}`)
34
- })
37
+ t.after(() => target.close())
35
38
 
36
- t.teardown(target.close.bind(target))
37
-
38
- instance.listen({ port: 0 }, (err) => {
39
- t.error(err)
40
-
41
- target.listen({ port: 0 }, (err) => {
42
- t.error(err)
43
-
44
- get.concat({
45
- url: `http://localhost:${instance.server.address().port}`,
46
- method: 'POST',
47
- body: JSON.stringify({
48
- hello: 'world'
49
- }),
50
- headers: {
51
- 'content-type': 'application/json;charset=utf-8'
52
- }
53
- }, (err, res, data) => {
54
- t.error(err)
55
- t.equal(res.headers['content-type'], 'application/json')
56
- t.same(JSON.parse(data.toString()), { something: 'else' })
57
- })
39
+ await new Promise(resolve => instance.listen({ port: 0 }, resolve))
40
+
41
+ await new Promise(resolve => target.listen({ port: 0 }, resolve))
42
+
43
+ const result = await request(`http://localhost:${instance.server.address().port}`, {
44
+ method: 'POST',
45
+ body: JSON.stringify({
46
+ hello: 'world'
47
+ }),
48
+ headers: {
49
+ 'content-type': 'application/json;charset=utf-8'
50
+ }
58
51
  })
52
+
53
+ t.assert.strictEqual(result.headers['content-type'], 'application/json')
54
+ t.assert.deepStrictEqual(await result.body.json(), { something: 'else' })
59
55
  })
@@ -1,22 +1,22 @@
1
1
  'use strict'
2
2
 
3
- const { test } = require('tap')
3
+ const { test } = require('node:test')
4
4
  const Fastify = require('fastify')
5
5
  const From = require('..')
6
- const got = require('got')
6
+ const { request, Agent } = require('undici')
7
7
 
8
8
  test('http -> http2', async function (t) {
9
9
  const instance = Fastify()
10
10
 
11
- t.teardown(instance.close.bind(instance))
11
+ t.after(() => instance.close())
12
12
 
13
13
  const target = Fastify({
14
14
  http2: true
15
15
  })
16
16
 
17
17
  target.post('/', (request, reply) => {
18
- t.pass('request proxied')
19
- t.same(request.body, { something: 'else' })
18
+ t.assert.ok('request proxied')
19
+ t.assert.deepStrictEqual(request.body, { something: 'else' })
20
20
  reply.code(200).header('x-my-header', 'hello!').send({
21
21
  hello: 'world'
22
22
  })
@@ -26,7 +26,7 @@ test('http -> http2', async function (t) {
26
26
  reply.from()
27
27
  })
28
28
 
29
- t.teardown(target.close.bind(target))
29
+ t.after(() => target.close())
30
30
 
31
31
  await target.listen({ port: 0 })
32
32
 
@@ -37,15 +37,20 @@ test('http -> http2', async function (t) {
37
37
 
38
38
  await instance.listen({ port: 0 })
39
39
 
40
- const { headers, body, statusCode } = await got(`http://localhost:${instance.server.address().port}`, {
40
+ const { headers, body, statusCode } = await request(`http://localhost:${instance.server.address().port}`, {
41
41
  method: 'POST',
42
- json: { something: 'else' },
43
- responseType: 'json'
42
+ body: JSON.stringify({ something: 'else' }),
43
+ headers: {
44
+ 'content-type': 'application/json'
45
+ },
46
+ dispatcher: new Agent({
47
+ pipelining: 0
48
+ })
44
49
  })
45
- t.equal(statusCode, 200)
46
- t.equal(headers['x-my-header'], 'hello!')
47
- t.match(headers['content-type'], /application\/json/)
48
- t.same(body, { hello: 'world' })
50
+ t.assert.strictEqual(statusCode, 200)
51
+ t.assert.strictEqual(headers['x-my-header'], 'hello!')
52
+ t.assert.match(headers['content-type'], /application\/json/)
53
+ t.assert.deepStrictEqual(await body.json(), { hello: 'world' })
49
54
  instance.close()
50
55
  target.close()
51
56
  })
@@ -1,66 +1,62 @@
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
  http: true
12
12
  })
13
13
 
14
- t.plan(8)
15
- t.teardown(instance.close.bind(instance))
14
+ t.test('full post stream core', async (t) => {
15
+ t.plan(5)
16
+ t.after(() => instance.close())
16
17
 
17
- instance.addContentTypeParser('application/octet-stream', function (_req, payload, done) {
18
- done(null, payload)
19
- })
18
+ instance.addContentTypeParser('application/octet-stream', function (_req, payload, done) {
19
+ done(null, payload)
20
+ })
20
21
 
21
- t.teardown(instance.close.bind(instance))
22
+ t.after(() => instance.close())
22
23
 
23
- const target = http.createServer((req, res) => {
24
- t.pass('request proxied')
25
- t.equal(req.method, 'POST')
26
- t.equal(req.headers['content-type'], 'application/octet-stream')
27
- let data = ''
28
- req.setEncoding('utf8')
29
- req.on('data', (d) => {
30
- data += d
31
- })
32
- req.on('end', () => {
33
- t.same(JSON.parse(data), { hello: 'world' })
34
- res.statusCode = 200
35
- res.setHeader('content-type', 'application/octet-stream')
36
- res.end(JSON.stringify({ something: 'else' }))
24
+ const target = http.createServer((req, res) => {
25
+ t.assert.ok('request proxied')
26
+ t.assert.strictEqual(req.method, 'POST')
27
+ t.assert.strictEqual(req.headers['content-type'], 'application/octet-stream')
28
+ let data = ''
29
+ req.setEncoding('utf8')
30
+ req.on('data', (d) => {
31
+ data += d
32
+ })
33
+ req.on('end', () => {
34
+ t.assert.deepStrictEqual(JSON.parse(data), { hello: 'world' })
35
+ res.statusCode = 200
36
+ res.setHeader('content-type', 'application/octet-stream')
37
+ res.end(JSON.stringify({ something: 'else' }))
38
+ })
37
39
  })
38
- })
39
40
 
40
- instance.post('/', (_request, reply) => {
41
- reply.from(`http://localhost:${target.address().port}`)
42
- })
41
+ instance.post('/', (_request, reply) => {
42
+ reply.from(`http://localhost:${target.address().port}`)
43
+ })
43
44
 
44
- t.teardown(target.close.bind(target))
45
+ t.after(() => target.close())
45
46
 
46
- instance.listen({ port: 0 }, (err) => {
47
- t.error(err)
47
+ await new Promise(resolve => instance.listen({ port: 0 }, resolve))
48
48
 
49
- target.listen({ port: 0 }, (err) => {
50
- t.error(err)
49
+ await new Promise(resolve => target.listen({ port: 0 }, resolve))
51
50
 
52
- get({
53
- url: `http://localhost:${instance.server.address().port}`,
54
- method: 'POST',
55
- headers: {
56
- 'content-type': 'application/octet-stream'
57
- },
58
- body: JSON.stringify({
59
- hello: 'world'
60
- })
61
- }, (err, _res, data) => {
62
- t.error(err)
63
- t.same(JSON.parse(data), { something: 'else' })
51
+ const result = await request(`http://localhost:${instance.server.address().port}`, {
52
+ method: 'POST',
53
+ headers: {
54
+ 'content-type': 'application/octet-stream'
55
+ },
56
+ body: JSON.stringify({
57
+ hello: 'world'
64
58
  })
65
59
  })
60
+
61
+ t.assert.deepStrictEqual(await result.body.json(), { something: 'else' })
66
62
  })
@@ -1,64 +1,60 @@
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(8)
13
- t.teardown(instance.close.bind(instance))
12
+ t.test('full post stream', async (t) => {
13
+ t.plan(5)
14
+ t.after(() => instance.close())
14
15
 
15
- instance.addContentTypeParser('application/octet-stream', function (_req, payload, done) {
16
- done(null, payload)
17
- })
16
+ instance.addContentTypeParser('application/octet-stream', function (_req, payload, done) {
17
+ done(null, payload)
18
+ })
18
19
 
19
- t.teardown(instance.close.bind(instance))
20
+ t.after(() => instance.close())
20
21
 
21
- const target = http.createServer((req, res) => {
22
- t.pass('request proxied')
23
- t.equal(req.method, 'POST')
24
- t.equal(req.headers['content-type'], 'application/octet-stream')
25
- let data = ''
26
- req.setEncoding('utf8')
27
- req.on('data', (d) => {
28
- data += d
29
- })
30
- req.on('end', () => {
31
- t.same(JSON.parse(data), { hello: 'world' })
32
- res.statusCode = 200
33
- res.setHeader('content-type', 'application/octet-stream')
34
- res.end(JSON.stringify({ something: 'else' }))
22
+ const target = http.createServer((req, res) => {
23
+ t.assert.ok('request proxied')
24
+ t.assert.strictEqual(req.method, 'POST')
25
+ t.assert.strictEqual(req.headers['content-type'], 'application/octet-stream')
26
+ let data = ''
27
+ req.setEncoding('utf8')
28
+ req.on('data', (d) => {
29
+ data += d
30
+ })
31
+ req.on('end', () => {
32
+ t.assert.deepStrictEqual(JSON.parse(data), { hello: 'world' })
33
+ res.statusCode = 200
34
+ res.setHeader('content-type', 'application/octet-stream')
35
+ res.end(JSON.stringify({ something: 'else' }))
36
+ })
35
37
  })
36
- })
37
38
 
38
- instance.post('/', (_request, reply) => {
39
- reply.from(`http://localhost:${target.address().port}`)
40
- })
39
+ instance.post('/', (_request, reply) => {
40
+ reply.from(`http://localhost:${target.address().port}`)
41
+ })
41
42
 
42
- t.teardown(target.close.bind(target))
43
+ t.after(() => target.close())
43
44
 
44
- instance.listen({ port: 0 }, (err) => {
45
- t.error(err)
45
+ await new Promise(resolve => instance.listen({ port: 0 }, resolve))
46
46
 
47
- target.listen({ port: 0 }, (err) => {
48
- t.error(err)
47
+ await new Promise(resolve => target.listen({ port: 0 }, resolve))
49
48
 
50
- get({
51
- url: `http://localhost:${instance.server.address().port}`,
52
- method: 'POST',
53
- headers: {
54
- 'content-type': 'application/octet-stream'
55
- },
56
- body: JSON.stringify({
57
- hello: 'world'
58
- })
59
- }, (err, _res, data) => {
60
- t.error(err)
61
- t.same(JSON.parse(data), { something: 'else' })
49
+ const result = await request(`http://localhost:${instance.server.address().port}`, {
50
+ method: 'POST',
51
+ headers: {
52
+ 'content-type': 'application/octet-stream'
53
+ },
54
+ body: JSON.stringify({
55
+ hello: 'world'
62
56
  })
63
57
  })
58
+
59
+ t.assert.deepStrictEqual(await result.body.json(), { something: 'else' })
64
60
  })
@@ -1,56 +1,54 @@
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(8)
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'], 'application/json')
19
- let data = ''
20
- req.setEncoding('utf8')
21
- req.on('data', (d) => {
22
- data += d
23
- })
24
- req.on('end', () => {
25
- t.same(JSON.parse(data), { hello: 'world' })
26
- res.statusCode = 200
27
- res.setHeader('content-type', 'application/json')
28
- res.end(JSON.stringify({ something: 'else' }))
12
+ t.test('full post', async (t) => {
13
+ t.plan(5)
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'], 'application/json')
20
+ let data = ''
21
+ req.setEncoding('utf8')
22
+ req.on('data', (d) => {
23
+ data += d
24
+ })
25
+ req.on('end', () => {
26
+ t.assert.deepStrictEqual(JSON.parse(data), { hello: 'world' })
27
+ res.statusCode = 200
28
+ res.setHeader('content-type', 'application/json')
29
+ res.end(JSON.stringify({ something: 'else' }))
30
+ })
29
31
  })
30
- })
31
32
 
32
- instance.post('/', (_request, reply) => {
33
- reply.from(`http://localhost:${target.address().port}`)
34
- })
33
+ instance.post('/', (_request, reply) => {
34
+ reply.from(`http://localhost:${target.address().port}`)
35
+ })
35
36
 
36
- t.teardown(target.close.bind(target))
37
+ t.after(() => target.close())
37
38
 
38
- instance.listen({ port: 0 }, (err) => {
39
- t.error(err)
39
+ await new Promise(resolve => instance.listen({ port: 0 }, resolve))
40
40
 
41
- target.listen({ port: 0 }, (err) => {
42
- t.error(err)
41
+ await new Promise(resolve => target.listen({ port: 0 }, resolve))
43
42
 
44
- get({
45
- url: `http://localhost:${instance.server.address().port}`,
46
- method: 'POST',
47
- json: true,
48
- body: {
49
- hello: 'world'
50
- }
51
- }, (err, _res, data) => {
52
- t.error(err)
53
- t.same(data, { something: 'else' })
43
+ const result = await request(`http://localhost:${instance.server.address().port}`, {
44
+ method: 'POST',
45
+ headers: {
46
+ 'content-type': 'application/json'
47
+ },
48
+ body: JSON.stringify({
49
+ hello: 'world'
54
50
  })
55
51
  })
52
+
53
+ t.assert.deepStrictEqual(await result.body.json(), { something: 'else' })
56
54
  })
@@ -1,48 +1,44 @@
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
 
11
- t.plan(10)
12
- t.teardown(instance.close.bind(instance))
13
-
14
- const target = http.createServer((req, res) => {
15
- t.pass('request proxied')
16
- t.equal(req.method, 'GET')
17
- t.equal(req.url, '/world?b=c')
18
- res.statusCode = 205
19
- res.setHeader('Content-Type', 'text/plain')
20
- res.setHeader('x-my-header', 'hello!')
21
- res.end('hello world')
22
- })
11
+ t.test('full querystring rewrite option complex', async (t) => {
12
+ t.plan(7)
13
+ t.after(() => instance.close())
14
+
15
+ const target = http.createServer((req, res) => {
16
+ t.assert.ok('request proxied')
17
+ t.assert.strictEqual(req.method, 'GET')
18
+ t.assert.strictEqual(req.url, '/world?b=c')
19
+ res.statusCode = 205
20
+ res.setHeader('Content-Type', 'text/plain')
21
+ res.setHeader('x-my-header', 'hello!')
22
+ res.end('hello world')
23
+ })
23
24
 
24
- instance.get('/hello', (_request, reply) => {
25
- reply.from(`http://localhost:${target.address().port}/world?a=b`, {
26
- queryString: { b: 'c' }
25
+ instance.get('/hello', (_request, reply) => {
26
+ reply.from(`http://localhost:${target.address().port}/world?a=b`, {
27
+ queryString: { b: 'c' }
28
+ })
27
29
  })
28
- })
29
30
 
30
- t.teardown(target.close.bind(target))
31
+ t.after(() => target.close())
31
32
 
32
- target.listen({ port: 0 }, (err) => {
33
- t.error(err)
33
+ await new Promise(resolve => target.listen({ port: 0 }, resolve))
34
34
 
35
35
  instance.register(From)
36
36
 
37
- instance.listen({ port: 0 }, (err) => {
38
- t.error(err)
37
+ await new Promise(resolve => instance.listen({ port: 0 }, resolve))
39
38
 
40
- get(`http://localhost:${instance.server.address().port}/hello?a=b`, (err, res, data) => {
41
- t.error(err)
42
- t.equal(res.headers['content-type'], 'text/plain')
43
- t.equal(res.headers['x-my-header'], 'hello!')
44
- t.equal(res.statusCode, 205)
45
- t.equal(data.toString(), 'hello world')
46
- })
47
- })
39
+ const result = await request(`http://localhost:${instance.server.address().port}/hello?a=b`)
40
+ t.assert.strictEqual(result.headers['content-type'], 'text/plain')
41
+ t.assert.strictEqual(result.headers['x-my-header'], 'hello!')
42
+ t.assert.strictEqual(result.statusCode, 205)
43
+ t.assert.strictEqual(await result.body.text(), 'hello world')
48
44
  })