@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,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({ modifyCoreObjects: false })
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, '/')
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('modifyCoreObjects false', 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, '/')
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('/', (_request, reply) => {
25
- reply.from()
26
- })
25
+ instance.get('/', (_request, reply) => {
26
+ reply.from()
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
  base: `http://localhost:${target.address().port}`
35
35
  })
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}`, (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}`)
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,49 +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(6)
12
- t.teardown(instance.close.bind(instance))
11
+ t.test('no body opts with get', async (t) => {
12
+ t.plan(3)
13
+ t.after(() => instance.close())
13
14
 
14
- const target = http.createServer((_req, res) => {
15
- t.fail('this should never get called')
16
- res.end('hello world')
17
- })
15
+ const target = http.createServer((_req, res) => {
16
+ t.fail('this should never get called')
17
+ res.end('hello world')
18
+ })
18
19
 
19
- instance.get('/', (_request, reply) => {
20
- try {
21
- reply.from(null, { body: 'this is the new body' })
22
- } catch (e) {
23
- t.equal(e.message, 'Rewriting the body when doing a GET is not allowed')
24
- reply.send('hello world')
25
- }
26
- })
20
+ instance.get('/', (_request, reply) => {
21
+ try {
22
+ reply.from(null, { body: 'this is the new body' })
23
+ } catch (e) {
24
+ t.assert.strictEqual(e.message, 'Rewriting the body when doing a GET is not allowed')
25
+ reply.send('hello world')
26
+ }
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
  base: `http://localhost:${target.address().port}`
35
35
  })
36
36
 
37
- instance.listen({ port: 0 }, (err) => {
38
- t.error(err)
39
-
40
- get({
41
- url: `http://localhost:${instance.server.address().port}`,
42
- method: 'GET'
43
- }, (err, res, data) => {
44
- t.error(err)
45
- t.equal(res.statusCode, 200)
46
- t.equal(data.toString(), 'hello world')
47
- })
48
- })
37
+ await new Promise(resolve => instance.listen({ port: 0 }, resolve))
38
+
39
+ const result = await request(`http://localhost:${instance.server.address().port}`)
40
+
41
+ t.assert.strictEqual(result.statusCode, 200)
42
+ t.assert.strictEqual(await result.body.text(), 'hello world')
49
43
  })
@@ -1,51 +1,47 @@
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(7)
12
- t.teardown(instance.close.bind(instance))
11
+ t.test('no body opts with head', async (t) => {
12
+ t.plan(4)
13
+ t.after(() => instance.close())
13
14
 
14
- const target = http.createServer((_req, res) => {
15
- t.fail('this should never get called')
16
- res.end('hello world')
17
- })
15
+ const target = http.createServer((_req, res) => {
16
+ t.fail('this should never get called')
17
+ res.end('hello world')
18
+ })
18
19
 
19
- instance.head('/', (_request, reply) => {
20
- try {
21
- reply.from(null, { body: 'this is the new body' })
22
- } catch (e) {
23
- t.equal(e.message, 'Rewriting the body when doing a HEAD is not allowed')
24
- reply.header('x-http-error', '1')
25
- reply.send('hello world')
26
- }
27
- })
20
+ instance.head('/', (_request, reply) => {
21
+ try {
22
+ reply.from(null, { body: 'this is the new body' })
23
+ } catch (e) {
24
+ t.assert.strictEqual(e.message, 'Rewriting the body when doing a HEAD is not allowed')
25
+ reply.header('x-http-error', '1')
26
+ reply.send('hello world')
27
+ }
28
+ })
28
29
 
29
- t.teardown(target.close.bind(target))
30
+ t.after(() => target.close())
30
31
 
31
- target.listen({ port: 0 }, (err) => {
32
- t.error(err)
32
+ await new Promise(resolve => target.listen({ port: 0 }, resolve))
33
33
 
34
34
  instance.register(From, {
35
35
  base: `http://localhost:${target.address().port}`
36
36
  })
37
37
 
38
- instance.listen({ port: 0 }, (err) => {
39
- t.error(err)
40
-
41
- get({
42
- url: `http://localhost:${instance.server.address().port}`,
43
- method: 'HEAD'
44
- }, (err, res, data) => {
45
- t.error(err)
46
- t.equal(res.statusCode, 200)
47
- t.equal(res.headers['x-http-error'], '1')
48
- t.equal(data.toString(), '')
49
- })
38
+ await new Promise(resolve => instance.listen({ port: 0 }, resolve))
39
+
40
+ const result = await request(`http://localhost:${instance.server.address().port}`, {
41
+ method: 'HEAD'
50
42
  })
43
+
44
+ t.assert.strictEqual(result.statusCode, 200)
45
+ t.assert.strictEqual(result.headers['x-http-error'], '1')
46
+ t.assert.strictEqual(await result.body.text(), '')
51
47
  })
@@ -1,58 +1,56 @@
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 Readable = require('node:stream').Readable
9
9
 
10
10
  const instance = Fastify()
11
11
  instance.register(From)
12
12
 
13
- t.plan(5)
14
- t.teardown(instance.close.bind(instance))
13
+ t.test('no stream body option', async (t) => {
14
+ t.plan(2)
15
+ t.after(() => instance.close())
15
16
 
16
- const target = http.createServer((_req, res) => {
17
- t.fail('the target server should never be called')
18
- res.end()
19
- })
20
-
21
- instance.post('/', (_request, reply) => {
22
- const body = new Readable({
23
- read: function () {
24
- t.fail('the read function should never be called')
25
- }
17
+ const target = http.createServer((_req, res) => {
18
+ t.fail('the target server should never be called')
19
+ res.end()
26
20
  })
27
21
 
28
- t.throws(() => {
29
- reply.from(`http://localhost:${target.address().port}`, {
30
- body
22
+ instance.post('/', (_request, reply) => {
23
+ const body = new Readable({
24
+ read: function () {
25
+ t.fail('the read function should never be called')
26
+ }
31
27
  })
32
- })
33
28
 
34
- // return a 500
35
- reply.code(500).send({ an: 'error' })
36
- })
29
+ t.assert.throws(() => {
30
+ reply.from(`http://localhost:${target.address().port}`, {
31
+ body
32
+ })
33
+ })
37
34
 
38
- t.teardown(target.close.bind(target))
35
+ // return a 500
36
+ reply.code(500).send({ an: 'error' })
37
+ })
39
38
 
40
- instance.listen({ port: 0 }, (err) => {
41
- t.error(err)
39
+ t.after(() => target.close())
42
40
 
43
- target.listen({ port: 0 }, (err) => {
44
- t.error(err)
41
+ await new Promise(resolve => instance.listen({ port: 0 }, resolve))
45
42
 
46
- get({
47
- url: `http://localhost:${instance.server.address().port}`,
48
- method: 'POST',
49
- json: true,
50
- body: {
51
- hello: 'world'
52
- }
53
- }, (err, res) => {
54
- t.error(err)
55
- t.equal(res.statusCode, 500)
43
+ await new Promise(resolve => target.listen({ port: 0 }, resolve))
44
+
45
+ const result = await request(`http://localhost:${instance.server.address().port}`, {
46
+ method: 'POST',
47
+ headers: {
48
+ 'content-type': 'application/json'
49
+ },
50
+ body: JSON.stringify({
51
+ hello: 'world'
56
52
  })
57
53
  })
54
+
55
+ t.assert.strictEqual(result.statusCode, 500)
58
56
  })
@@ -1,64 +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, Agent } = require('undici')
5
6
  const From = require('..')
6
- const got = require('got')
7
7
  const FakeTimers = require('@sinonjs/fake-timers')
8
8
 
9
9
  const clock = FakeTimers.createClock()
10
10
 
11
11
  t.test('on-error', async (t) => {
12
12
  const target = Fastify()
13
- t.teardown(target.close.bind(target))
13
+ t.after(() => target.close())
14
14
 
15
15
  target.get('/', (_request, reply) => {
16
- t.pass('request arrives')
16
+ t.assert.ok('request arrives')
17
17
 
18
18
  clock.setTimeout(() => {
19
19
  reply.status(200).send('hello world')
20
- t.end()
21
20
  }, 1000)
22
21
  })
23
22
 
24
23
  await target.listen({ port: 0 })
25
24
 
26
25
  const instance = Fastify()
27
- t.teardown(instance.close.bind(instance))
26
+ t.after(() => instance.close())
28
27
 
29
28
  instance.register(From, { http: { requestOptions: { timeout: 100 } } })
30
29
 
31
30
  instance.get('/', (_request, reply) => {
32
31
  reply.from(`http://localhost:${target.server.address().port}/`,
33
32
  {
34
- onError: (reply, { error }) => {
35
- t.same(error, {
33
+ onError: (reply, { error: { stack, ...errorContent } }) => {
34
+ t.assert.deepStrictEqual(errorContent, {
36
35
  statusCode: 504,
37
36
  name: 'FastifyError',
38
37
  code: 'FST_REPLY_FROM_GATEWAY_TIMEOUT',
39
38
  message: 'Gateway Timeout'
40
39
  })
41
- reply.code(error.statusCode).send(error)
40
+ reply.code(errorContent.statusCode).send(errorContent)
42
41
  }
43
42
  })
44
43
  })
45
44
 
46
45
  await instance.listen({ port: 0 })
47
46
 
48
- try {
49
- await got.get(`http://localhost:${instance.server.address().port}/`, { retry: 0 })
50
- } catch (err) {
51
- t.equal(err.response.statusCode, 504)
52
- t.match(err.response.headers['content-type'], /application\/json/)
53
- t.same(JSON.parse(err.response.body), {
54
- statusCode: 504,
55
- code: 'FST_REPLY_FROM_GATEWAY_TIMEOUT',
56
- error: 'Gateway Timeout',
57
- message: 'Gateway Timeout'
47
+ const result = await request(`http://localhost:${instance.server.address().port}/`, {
48
+ dispatcher: new Agent({
49
+ pipelining: 0
58
50
  })
59
- clock.tick(1000)
60
- return
61
- }
51
+ })
62
52
 
63
- t.fail()
53
+ t.assert.strictEqual(result.statusCode, 504)
54
+ t.assert.match(result.headers['content-type'], /application\/json/)
55
+ t.assert.deepStrictEqual(await result.body.json(), {
56
+ statusCode: 504,
57
+ code: 'FST_REPLY_FROM_GATEWAY_TIMEOUT',
58
+ name: 'FastifyError',
59
+ message: 'Gateway Timeout'
60
+ })
61
+ clock.tick(1000)
64
62
  })
@@ -1,52 +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
  instance.register(From)
11
11
 
12
- t.plan(8)
13
- t.teardown(instance.close.bind(instance))
12
+ t.test('on-invalid-upstream-response', async (t) => {
13
+ t.plan(5)
14
+ t.after(() => instance.close())
14
15
 
15
- const target = http.createServer((req, res) => {
16
- t.pass('request proxied')
17
- t.equal(req.method, 'GET')
18
- res.statusCode = 888
19
- res.end('non-standard status code')
20
- })
21
-
22
- instance.get('/', (_, reply) => {
23
- reply.from(`http://localhost:${target.address().port}`, {
24
- onResponse: (_, _reply, res) => {
25
- t.equal(res.statusCode, 888)
26
- }
16
+ const target = http.createServer((req, res) => {
17
+ t.assert.ok('request proxied')
18
+ t.assert.strictEqual(req.method, 'GET')
19
+ res.statusCode = 888
20
+ res.end('non-standard status code')
27
21
  })
28
- })
29
22
 
30
- t.teardown(target.close.bind(target))
31
-
32
- instance.listen({ port: 0 }, (err) => {
33
- t.error(err)
34
-
35
- target.listen({ port: 0 }, (err) => {
36
- t.error(err)
37
-
38
- get(
39
- `http://localhost:${instance.server.address().port}`,
40
- (err, res, data) => {
41
- t.error(err)
42
- t.equal(res.statusCode, 502)
43
- t.same(JSON.parse(data), {
44
- statusCode: 502,
45
- code: 'FST_REPLY_FROM_BAD_GATEWAY',
46
- error: 'Bad Gateway',
47
- message: 'Bad Gateway'
48
- })
23
+ instance.get('/', (_, reply) => {
24
+ reply.from(`http://localhost:${target.address().port}`, {
25
+ onResponse: (_, _reply, res) => {
26
+ t.assert.strictEqual(res.statusCode, 888)
49
27
  }
50
- )
28
+ })
29
+ })
30
+
31
+ t.after(() => target.close())
32
+
33
+ await new Promise(resolve => instance.listen({ port: 0 }, resolve))
34
+
35
+ await new Promise(resolve => target.listen({ port: 0 }, resolve))
36
+
37
+ const result = await request(`http://localhost:${instance.server.address().port}`)
38
+ t.assert.strictEqual(result.statusCode, 502)
39
+ t.assert.deepStrictEqual(await result.body.json(), {
40
+ statusCode: 502,
41
+ code: 'FST_REPLY_FROM_BAD_GATEWAY',
42
+ error: 'Bad Gateway',
43
+ message: 'Bad Gateway'
51
44
  })
52
45
  })
@@ -1,49 +1,42 @@
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))
12
+ t.test('onResponse', async (t) => {
13
+ t.plan(6)
14
+ t.after(() => instance.close())
14
15
 
15
- const target = http.createServer((req, res) => {
16
- t.pass('request proxied')
17
- t.equal(req.method, 'GET')
18
- res.statusCode = 200
19
- res.end('hello world')
20
- })
16
+ const target = http.createServer((req, res) => {
17
+ t.assert.ok('request proxied')
18
+ t.assert.strictEqual(req.method, 'GET')
19
+ res.statusCode = 200
20
+ res.end('hello world')
21
+ })
21
22
 
22
- instance.get('/', (request1, reply) => {
23
- reply.from(`http://localhost:${target.address().port}`, {
24
- onResponse: (request2, reply, res) => {
25
- t.equal(res.statusCode, 200)
26
- t.equal(request1.raw, request2.raw)
27
- reply.send(res.stream)
28
- }
23
+ instance.get('/', (request1, reply) => {
24
+ reply.from(`http://localhost:${target.address().port}`, {
25
+ onResponse: (request2, reply, res) => {
26
+ t.assert.strictEqual(res.statusCode, 200)
27
+ t.assert.strictEqual(request1.raw, request2.raw)
28
+ reply.send(res.stream)
29
+ }
30
+ })
29
31
  })
30
- })
31
32
 
32
- t.teardown(target.close.bind(target))
33
+ t.after(() => target.close())
33
34
 
34
- instance.listen({ port: 0 }, (err) => {
35
- t.error(err)
35
+ await new Promise(resolve => instance.listen({ port: 0 }, resolve))
36
36
 
37
- target.listen({ port: 0 }, (err) => {
38
- t.error(err)
37
+ await new Promise(resolve => target.listen({ port: 0 }, resolve))
39
38
 
40
- get(
41
- `http://localhost:${instance.server.address().port}`,
42
- (err, res, data) => {
43
- t.error(err)
44
- t.equal(res.statusCode, 200)
45
- t.equal(data.toString(), 'hello world')
46
- }
47
- )
48
- })
39
+ const result = await request(`http://localhost:${instance.server.address().port}`)
40
+ t.assert.strictEqual(result.statusCode, 200)
41
+ t.assert.strictEqual(await result.body.text(), 'hello world')
49
42
  })