@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,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
  })
@@ -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 querystring = require('node:querystring')
9
9
 
10
10
  const instance = Fastify()
@@ -14,45 +14,39 @@ instance.addHook('preHandler', (request, _reply, done) => {
14
14
  done()
15
15
  })
16
16
 
17
- t.plan(10)
18
- t.teardown(instance.close.bind(instance))
19
-
20
- const target = http.createServer((req, res) => {
21
- t.pass('request proxied')
22
- t.equal(req.method, 'GET')
23
- t.equal(req.url, '/world?q=test')
24
- res.statusCode = 205
25
- res.setHeader('Content-Type', 'text/plain')
26
- res.setHeader('x-my-header', 'hello!')
27
- res.end('hello world')
28
- })
17
+ t.test('full querystring rewrite option function request', async (t) => {
18
+ t.plan(7)
19
+ t.after(() => instance.close())
20
+
21
+ const target = http.createServer((req, res) => {
22
+ t.assert.ok('request proxied')
23
+ t.assert.strictEqual(req.method, 'GET')
24
+ t.assert.strictEqual(req.url, '/world?q=test')
25
+ res.statusCode = 205
26
+ res.setHeader('Content-Type', 'text/plain')
27
+ res.setHeader('x-my-header', 'hello!')
28
+ res.end('hello world')
29
+ })
29
30
 
30
- instance.get('/hello', (_request, reply) => {
31
- reply.from(`http://localhost:${target.address().port}/world?a=b`, {
32
- queryString (_search, _reqUrl, request) {
33
- return querystring.stringify({ q: request.addedVal })
34
- }
31
+ instance.get('/hello', (_request, reply) => {
32
+ reply.from(`http://localhost:${target.address().port}/world?a=b`, {
33
+ queryString (_search, _reqUrl, request) {
34
+ return querystring.stringify({ q: request.addedVal })
35
+ }
36
+ })
35
37
  })
36
- })
37
38
 
38
- t.teardown(target.close.bind(target))
39
+ t.after(() => target.close())
39
40
 
40
- target.listen({ port: 0 }, (err) => {
41
- t.error(err)
41
+ await new Promise(resolve => target.listen({ port: 0 }, resolve))
42
42
 
43
43
  instance.register(From)
44
44
 
45
- instance.listen({ port: 0 }, (err) => {
46
- t.error(err)
47
-
48
- get(`http://localhost:${instance.server.address().port}/hello?a=b`, (err, res, data) => {
49
- t.error(err)
50
- t.equal(res.headers['content-type'], 'text/plain')
51
- t.equal(res.headers['x-my-header'], 'hello!')
52
- t.equal(res.statusCode, 205)
53
- t.equal(data.toString(), 'hello world')
54
- instance.close()
55
- target.close()
56
- })
57
- })
45
+ await new Promise(resolve => instance.listen({ port: 0 }, resolve))
46
+
47
+ const result = await request(`http://localhost:${instance.server.address().port}/hello?a=b`)
48
+ t.assert.strictEqual(result.headers['content-type'], 'text/plain')
49
+ t.assert.strictEqual(result.headers['x-my-header'], 'hello!')
50
+ t.assert.strictEqual(result.statusCode, 205)
51
+ t.assert.strictEqual(await result.body.text(), 'hello world')
58
52
  })
@@ -1,51 +1,48 @@
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 querystring = require('node:querystring')
9
9
 
10
10
  const instance = Fastify()
11
11
 
12
- t.plan(10)
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, 'GET')
18
- t.equal(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
- })
12
+ t.test('full querystring rewrite option function', async (t) => {
13
+ t.plan(7)
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, 'GET')
19
+ t.assert.strictEqual(req.url, '/world?b=c')
20
+ res.statusCode = 205
21
+ res.setHeader('Content-Type', 'text/plain')
22
+ res.setHeader('x-my-header', 'hello!')
23
+ res.end('hello world')
24
+ })
24
25
 
25
- instance.get('/hello', (_request, reply) => {
26
- reply.from(`http://localhost:${target.address().port}/world?a=b`, {
27
- queryString () {
28
- return querystring.stringify({ b: 'c' })
29
- }
26
+ instance.get('/hello', (_request, reply) => {
27
+ reply.from(`http://localhost:${target.address().port}/world?a=b`, {
28
+ queryString () {
29
+ return querystring.stringify({ b: 'c' })
30
+ }
31
+ })
30
32
  })
31
- })
32
33
 
33
- t.teardown(target.close.bind(target))
34
+ t.after(() => target.close())
34
35
 
35
- target.listen({ port: 0 }, (err) => {
36
- t.error(err)
36
+ await new Promise(resolve => target.listen({ port: 0 }, resolve))
37
37
 
38
38
  instance.register(From)
39
39
 
40
- instance.listen({ port: 0 }, (err) => {
41
- t.error(err)
40
+ await new Promise(resolve => instance.listen({ port: 0 }, resolve))
42
41
 
43
- get(`http://localhost:${instance.server.address().port}/hello?a=b`, (err, res, data) => {
44
- t.error(err)
45
- t.equal(res.headers['content-type'], 'text/plain')
46
- t.equal(res.headers['x-my-header'], 'hello!')
47
- t.equal(res.statusCode, 205)
48
- t.equal(data.toString(), 'hello world')
49
- })
50
- })
42
+ const result = await request(`http://localhost:${instance.server.address().port}/hello?a=b`)
43
+
44
+ t.assert.strictEqual(result.headers['content-type'], 'text/plain')
45
+ t.assert.strictEqual(result.headers['x-my-header'], 'hello!')
46
+ t.assert.strictEqual(result.statusCode, 205)
47
+ t.assert.strictEqual(await result.body.text(), 'hello world')
51
48
  })
@@ -1,48 +1,45 @@
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', 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`, {
26
- queryString: { b: 'c' }
25
+ instance.get('/hello', (_request, reply) => {
26
+ reply.from(`http://localhost:${target.address().port}/world`, {
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
+
41
+ t.assert.strictEqual(result.headers['content-type'], 'text/plain')
42
+ t.assert.strictEqual(result.headers['x-my-header'], 'hello!')
43
+ t.assert.strictEqual(result.statusCode, 205)
44
+ t.assert.strictEqual(await result.body.text(), 'hello world')
48
45
  })
@@ -1,46 +1,43 @@
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 string', 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?b=c`)
26
- })
25
+ instance.get('/hello', (_request, reply) => {
26
+ reply.from(`http://localhost:${target.address().port}/world?b=c`)
27
+ })
27
28
 
28
- t.teardown(target.close.bind(target))
29
+ t.after(() => target.close())
29
30
 
30
- target.listen({ port: 0 }, (err) => {
31
- t.error(err)
31
+ await new Promise(resolve => target.listen({ port: 0 }, resolve))
32
32
 
33
33
  instance.register(From)
34
34
 
35
- instance.listen({ port: 0 }, (err) => {
36
- t.error(err)
35
+ await new Promise(resolve => instance.listen({ port: 0 }, resolve))
37
36
 
38
- get(`http://localhost:${instance.server.address().port}/hello?a=b`, (err, res, data) => {
39
- t.error(err)
40
- t.equal(res.headers['content-type'], 'text/plain')
41
- t.equal(res.headers['x-my-header'], 'hello!')
42
- t.equal(res.statusCode, 205)
43
- t.equal(data.toString(), 'hello world')
44
- })
45
- })
37
+ const result = await request(`http://localhost:${instance.server.address().port}/hello?a=b`)
38
+
39
+ t.assert.strictEqual(result.headers['content-type'], 'text/plain')
40
+ t.assert.strictEqual(result.headers['x-my-header'], 'hello!')
41
+ t.assert.strictEqual(result.statusCode, 205)
42
+ t.assert.strictEqual(await result.body.text(), 'hello world')
46
43
  })
@@ -1,46 +1,43 @@
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?a=b')
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', 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?a=b')
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`)
26
- })
25
+ instance.get('/hello', (_request, reply) => {
26
+ reply.from(`http://localhost:${target.address().port}/world`)
27
+ })
27
28
 
28
- t.teardown(target.close.bind(target))
29
+ t.after(() => target.close())
29
30
 
30
- target.listen({ port: 0 }, (err) => {
31
- t.error(err)
31
+ await new Promise(resolve => target.listen({ port: 0 }, resolve))
32
32
 
33
33
  instance.register(From)
34
34
 
35
- instance.listen({ port: 0 }, (err) => {
36
- t.error(err)
35
+ await new Promise(resolve => instance.listen({ port: 0 }, resolve))
37
36
 
38
- get(`http://localhost:${instance.server.address().port}/hello?a=b`, (err, res, data) => {
39
- t.error(err)
40
- t.equal(res.headers['content-type'], 'text/plain')
41
- t.equal(res.headers['x-my-header'], 'hello!')
42
- t.equal(res.statusCode, 205)
43
- t.equal(data.toString(), 'hello world')
44
- })
45
- })
37
+ const result = await request(`http://localhost:${instance.server.address().port}/hello?a=b`)
38
+
39
+ t.assert.strictEqual(result.headers['content-type'], 'text/plain')
40
+ t.assert.strictEqual(result.headers['x-my-header'], 'hello!')
41
+ t.assert.strictEqual(result.statusCode, 205)
42
+ t.assert.strictEqual(await result.body.text(), 'hello world')
46
43
  })
@@ -1,46 +1,43 @@
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?a=b')
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', 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?a=b')
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`)
26
- })
25
+ instance.get('/hello', (_request, reply) => {
26
+ reply.from(`http://localhost:${target.address().port}/world`)
27
+ })
27
28
 
28
- t.teardown(target.close.bind(target))
29
+ t.after(() => target.close())
29
30
 
30
- target.listen({ port: 0 }, (err) => {
31
- t.error(err)
31
+ await new Promise(resolve => target.listen({ port: 0 }, resolve))
32
32
 
33
33
  instance.register(From)
34
34
 
35
- instance.listen({ port: 0 }, (err) => {
36
- t.error(err)
35
+ await new Promise(resolve => instance.listen({ port: 0 }, resolve))
37
36
 
38
- get(`http://localhost:${instance.server.address().port}/hello?a=b`, (err, res, data) => {
39
- t.error(err)
40
- t.equal(res.headers['content-type'], 'text/plain')
41
- t.equal(res.headers['x-my-header'], 'hello!')
42
- t.equal(res.statusCode, 205)
43
- t.equal(data.toString(), 'hello world')
44
- })
45
- })
37
+ const result = await request(`http://localhost:${instance.server.address().port}/hello?a=b`)
38
+
39
+ t.assert.strictEqual(result.headers['content-type'], 'text/plain')
40
+ t.assert.strictEqual(result.headers['x-my-header'], 'hello!')
41
+ t.assert.strictEqual(result.statusCode, 205)
42
+ t.assert.strictEqual(await result.body.text(), 'hello world')
46
43
  })