@fastify/reply-from 7.0.1 → 8.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 (81) hide show
  1. package/.github/workflows/ci.yml +8 -51
  2. package/README.md +15 -4
  3. package/example.js +2 -2
  4. package/index.d.ts +2 -1
  5. package/index.js +22 -8
  6. package/lib/errors.js +13 -0
  7. package/lib/request.js +9 -4
  8. package/package.json +20 -23
  9. package/test/async-route-handler.js +2 -2
  10. package/test/base-get.js +2 -2
  11. package/test/base-path.js +1 -1
  12. package/test/base-querystring.js +2 -2
  13. package/test/core-with-path-in-base.js +2 -2
  14. package/test/custom-undici-instance.js +3 -3
  15. package/test/disable-request-logging.js +159 -0
  16. package/test/fastify-multipart-incompatibility.js +2 -2
  17. package/test/full-get.js +2 -2
  18. package/test/full-https-get.js +2 -2
  19. package/test/full-post-extended-content-type.js +2 -2
  20. package/test/full-post-http2.js +2 -2
  21. package/test/full-post-stream-core.js +2 -2
  22. package/test/full-post-stream.js +2 -2
  23. package/test/full-post.js +2 -2
  24. package/test/full-querystring-rewrite-option-complex.js +2 -2
  25. package/test/full-querystring-rewrite-option-function.js +2 -2
  26. package/test/full-querystring-rewrite-option.js +2 -2
  27. package/test/full-querystring-rewrite-string.js +2 -2
  28. package/test/full-querystring-rewrite.js +2 -2
  29. package/test/full-querystring.js +2 -2
  30. package/test/full-rewrite-body-content-type.js +2 -2
  31. package/test/full-rewrite-body-http.js +2 -2
  32. package/test/full-rewrite-body-to-empty-string.js +2 -2
  33. package/test/full-rewrite-body-to-null.js +2 -2
  34. package/test/full-rewrite-body.js +2 -2
  35. package/test/get-upstream-http.js +3 -3
  36. package/test/get-upstream-undici.js +2 -2
  37. package/test/get-with-body.js +2 -2
  38. package/test/head-with-body.js +2 -2
  39. package/test/host-header.js +2 -2
  40. package/test/http-agents.js +2 -2
  41. package/test/http-http2.js +2 -2
  42. package/test/http-invalid-target.js +2 -1
  43. package/test/http-retry.js +6 -6
  44. package/test/http-timeout.js +3 -2
  45. package/test/http2-http2.js +2 -2
  46. package/test/http2-https.js +2 -2
  47. package/test/http2-invalid-target.js +2 -1
  48. package/test/http2-target-crash.js +3 -2
  49. package/test/http2-target-multi-crash.js +4 -3
  50. package/test/http2-timeout.js +6 -4
  51. package/test/https-agents.js +2 -2
  52. package/test/index.test-d.ts +3 -2
  53. package/test/modifyCoreObjects-false.js +2 -2
  54. package/test/no-body-opts-with-get.js +2 -2
  55. package/test/no-body-opts-with-head.js +2 -2
  56. package/test/no-stream-body-option.js +2 -2
  57. package/test/on-error.js +10 -4
  58. package/test/onResponse.js +2 -2
  59. package/test/padded-body.js +2 -2
  60. package/test/post-formbody.js +2 -2
  61. package/test/post-plain-text.js +2 -2
  62. package/test/post-with-custom-encoded-contenttype.js +2 -2
  63. package/test/retry-on-503.js +6 -6
  64. package/test/rewrite-headers.js +2 -2
  65. package/test/rewrite-request-headers.js +2 -2
  66. package/test/transform-body.js +2 -2
  67. package/test/undici-agent.js +2 -2
  68. package/test/undici-body.js +2 -2
  69. package/test/undici-options.js +2 -2
  70. package/test/undici-retry.js +6 -6
  71. package/test/undici-timeout-body-partial.js +2 -2
  72. package/test/undici-timeout-body.js +2 -2
  73. package/test/undici-timeout.js +3 -2
  74. package/test/undici-with-path-in-base.js +2 -2
  75. package/test/undici.js +2 -2
  76. package/test/unexpected-error.js +2 -1
  77. package/test/unix-http-undici-from.js +1 -1
  78. package/test/unix-http-undici.js +1 -1
  79. package/test/unix-http.js +1 -1
  80. package/test/unix-https-undici.js +1 -1
  81. package/test/unix-https.js +1 -1
@@ -1,60 +1,17 @@
1
1
  name: CI
2
- 'on':
2
+
3
+ on:
3
4
  push:
4
5
  paths-ignore:
5
- - docs/**
6
+ - 'docs/**'
6
7
  - '*.md'
7
8
  pull_request:
8
9
  paths-ignore:
9
- - docs/**
10
+ - 'docs/**'
10
11
  - '*.md'
12
+
11
13
  jobs:
12
14
  test:
13
- runs-on: ${{ matrix.os }}
14
- strategy:
15
- matrix:
16
- node-version:
17
- - 12
18
- - 14
19
- - 16
20
- os:
21
- - macos-latest
22
- - ubuntu-latest
23
- - windows-latest
24
- steps:
25
- - uses: actions/checkout@v3
26
- - name: Use Node.js
27
- uses: actions/setup-node@v3
28
- with:
29
- node-version: ${{ matrix.node-version }}
30
- - name: Install Dependencies
31
- run: |
32
- npm install --ignore-scripts
33
- - name: Run Tests
34
- run: |
35
- npm run test:ci
36
- - name: Coveralls Parallel
37
- uses: coverallsapp/github-action@1.1.3
38
- with:
39
- github-token: ${{ secrets.GITHUB_TOKEN }}
40
- parallel: true
41
- flag-name: run-${{ matrix.node-version }}-${{ matrix.os }}
42
- coverage:
43
- needs: test
44
- runs-on: ubuntu-latest
45
- steps:
46
- - name: Coveralls Finished
47
- uses: coverallsapp/github-action@1.1.3
48
- with:
49
- github-token: ${{ secrets.GITHUB_TOKEN }}
50
- parallel-finished: true
51
- automerge:
52
- needs: test
53
- runs-on: ubuntu-latest
54
- permissions:
55
- pull-requests: write
56
- contents: write
57
- steps:
58
- - uses: fastify/github-action-merge-dependabot@v3.1.4
59
- with:
60
- github-token: ${{ secrets.GITHUB_TOKEN }}
15
+ uses: fastify/workflows/.github/workflows/plugins-ci.yml@v3
16
+ with:
17
+ license-check: true
package/README.md CHANGED
@@ -2,8 +2,6 @@
2
2
 
3
3
  ![CI](https://github.com/fastify/fastify-reply-from/workflows/CI/badge.svg)
4
4
  [![NPM version](https://img.shields.io/npm/v/@fastify/reply-from.svg?style=flat)](https://www.npmjs.com/package/@fastify/reply-from)
5
- [![Known Vulnerabilities](https://snyk.io/test/github/fastify/fastify-reply-from/badge.svg)](https://snyk.io/test/github/fastify/fastify-reply-from)
6
- [![Coverage Status](https://coveralls.io/repos/github/fastify/fastify-reply-from/badge.svg?branch=master)](https://coveralls.io/github/fastify/fastify-reply-from?branch=master)
7
5
  [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
8
6
 
9
7
  Fastify plugin to forward the current HTTP request to another server.
@@ -49,12 +47,12 @@ proxy.get('/', (request, reply) => {
49
47
  reply.from('/')
50
48
  })
51
49
 
52
- target.listen(3001, (err) => {
50
+ target.listen({ port: 3001 }, (err) => {
53
51
  if (err) {
54
52
  throw err
55
53
  }
56
54
 
57
- proxy.listen(3000, (err) => {
55
+ proxy.listen({ port: 3000 }, (err) => {
58
56
  if (err) {
59
57
  throw err
60
58
  }
@@ -175,6 +173,19 @@ proxy.register(require('@fastify/reply-from'), {
175
173
  })
176
174
  ```
177
175
 
176
+ #### `disableRequestLogging`
177
+
178
+ By default package will issue log messages when a request is received. By setting this option to true, these log messages will be disabled.
179
+
180
+ Default for `disableRequestLogging` will be `false`. To disable the log messages set `disableRequestLogging` to `true`.
181
+
182
+ ```js
183
+ proxy.register(require('@fastify/reply-from'), {
184
+ base: 'http://localhost:3001/',
185
+ disableRequestLogging: true // request log messages will be disabled
186
+ })
187
+ ```
188
+
178
189
  #### `cacheURLs`
179
190
 
180
191
  The number of parsed URLs that will be cached. Default: `100`.
package/example.js CHANGED
@@ -23,12 +23,12 @@ proxy.get('/', (request, reply) => {
23
23
  reply.from('/')
24
24
  })
25
25
 
26
- target.listen(3001, (err) => {
26
+ target.listen({ port: 3001 }, (err) => {
27
27
  if (err) {
28
28
  throw err
29
29
  }
30
30
 
31
- proxy.listen(3000, (err) => {
31
+ proxy.listen({ port: 3000 }, (err) => {
32
32
  if (err) {
33
33
  throw err
34
34
  }
package/index.d.ts CHANGED
@@ -85,12 +85,13 @@ export interface FastifyReplyFromOptions {
85
85
  base?: string;
86
86
  cacheURLs?: number;
87
87
  disableCache?: boolean;
88
- http?: HttpOptions;
88
+ http?: HttpOptions | boolean;
89
89
  http2?: Http2Options | boolean;
90
90
  undici?: Pool.Options;
91
91
  contentTypesToEncode?: string[];
92
92
  retryMethods?: (HTTPMethods | 'TRACE')[];
93
93
  maxRetriesOn503?: number;
94
+ disableRequestLogging?: boolean;
94
95
  }
95
96
 
96
97
  declare const fastifyReplyFrom:
package/index.js CHANGED
@@ -4,7 +4,6 @@ const fp = require('fastify-plugin')
4
4
  const lru = require('tiny-lru')
5
5
  const querystring = require('querystring')
6
6
  const Stream = require('stream')
7
- const createError = require('http-errors')
8
7
  const buildRequest = require('./lib/request')
9
8
  const {
10
9
  filterPseudoHeaders,
@@ -13,7 +12,14 @@ const {
13
12
  buildURL
14
13
  } = require('./lib/utils')
15
14
 
16
- const { TimeoutError } = buildRequest
15
+ const {
16
+ TimeoutError,
17
+ ServiceUnavailableError,
18
+ GatewayTimeoutError,
19
+ ConnectionResetError,
20
+ UndiciSocketError,
21
+ InternalServerError
22
+ } = require('./lib/errors')
17
23
 
18
24
  module.exports = fp(function from (fastify, opts, next) {
19
25
  const contentTypesToEncode = new Set([
@@ -33,6 +39,7 @@ module.exports = fp(function from (fastify, opts, next) {
33
39
  base,
34
40
  undici: opts.undici
35
41
  })
42
+ const disableRequestLogging = opts.disableRequestLogging || false
36
43
 
37
44
  fastify.decorateReply('from', function (source, opts) {
38
45
  opts = opts || {}
@@ -120,7 +127,7 @@ module.exports = fp(function from (fastify, opts, next) {
120
127
  }
121
128
  }
122
129
 
123
- this.request.log.info({ source }, 'fetching from remote server')
130
+ !disableRequestLogging && this.request.log.info({ source }, 'fetching from remote server')
124
131
 
125
132
  const requestHeaders = rewriteRequestHeaders(req, headers)
126
133
  const contentLength = requestHeaders['content-length']
@@ -136,16 +143,20 @@ module.exports = fp(function from (fastify, opts, next) {
136
143
  this.request.log.warn(err, 'response errored')
137
144
  if (!this.sent) {
138
145
  if (err.code === 'ERR_HTTP2_STREAM_CANCEL' || err.code === 'ENOTFOUND') {
139
- onError(this, { error: new createError.ServiceUnavailable() })
146
+ onError(this, { error: ServiceUnavailableError() })
140
147
  } else if (err instanceof TimeoutError || err.code === 'UND_ERR_HEADERS_TIMEOUT') {
141
- onError(this, { error: new createError.GatewayTimeout() })
148
+ onError(this, { error: new GatewayTimeoutError() })
149
+ } else if (err.code === 'ECONNRESET') {
150
+ onError(this, { error: new ConnectionResetError() })
151
+ } else if (err.code === 'UND_ERR_SOCKET') {
152
+ onError(this, { error: new UndiciSocketError() })
142
153
  } else {
143
- onError(this, { error: createError(500, err) })
154
+ onError(this, { error: new InternalServerError(err.message) })
144
155
  }
145
156
  }
146
157
  return
147
158
  }
148
- this.request.log.info('response received')
159
+ !disableRequestLogging && this.request.log.info('response received')
149
160
  if (sourceHttp2) {
150
161
  copyHeaders(
151
162
  rewriteHeaders(stripHttp1ConnectionHeaders(res.headers), req),
@@ -179,7 +190,10 @@ module.exports = fp(function from (fastify, opts, next) {
179
190
  })
180
191
 
181
192
  next()
182
- }, '>=3')
193
+ }, {
194
+ fastify: '4.x',
195
+ name: '@fastify/reply-from'
196
+ })
183
197
 
184
198
  function getQueryString (search, reqUrl, opts) {
185
199
  if (typeof opts.queryString === 'function') {
package/lib/errors.js ADDED
@@ -0,0 +1,13 @@
1
+ 'use strict'
2
+
3
+ const createError = require('@fastify/error')
4
+
5
+ module.exports.TimeoutError = createError('FST_REPLY_FROM_TIMEOUT', 'Timeout', 504)
6
+ module.exports.HttpRequestTimeoutError = createError('FST_REPLY_FROM_HTTP_REQUEST_TIMEOUT', 'HTTP request timed out', 504, module.exports.TimeoutError)
7
+ module.exports.Http2RequestTimeoutError = createError('FST_REPLY_FROM_HTTP2_REQUEST_TIMEOUT', 'HTTP/2 request timed out', 504, module.exports.TimeoutError)
8
+ module.exports.Http2SessionTimeoutError = createError('FST_REPLY_FROM_HTTP2_SESSION_TIMEOUT', 'HTTP/2 session timed out', 504, module.exports.TimeoutError)
9
+ module.exports.ServiceUnavailableError = createError('FST_REPLY_FROM_SERVICE_UNAVAILABLE', 'Service Unavailable', 503)
10
+ module.exports.GatewayTimeoutError = createError('FST_REPLY_FROM_GATEWAY_TIMEOUT', 'Gateway Timeout', 504)
11
+ module.exports.ConnectionResetError = createError('ECONNRESET', 'Connection Reset', 500)
12
+ module.exports.UndiciSocketError = createError('UND_ERR_SOCKET', 'Undici Socket Error', 500)
13
+ module.exports.InternalServerError = createError('FST_REPLY_FROM_INTERNAL_SERVER_ERROR', '%s', 500)
package/lib/request.js CHANGED
@@ -9,7 +9,12 @@ const undici = require('undici')
9
9
  const { stripHttp1ConnectionHeaders } = require('./utils')
10
10
  const http2 = require('http2')
11
11
 
12
- class TimeoutError extends Error {}
12
+ const {
13
+ TimeoutError,
14
+ Http2RequestTimeoutError,
15
+ Http2SessionTimeoutError,
16
+ HttpRequestTimeoutError
17
+ } = require('./errors')
13
18
 
14
19
  function shouldUseUndici (opts) {
15
20
  if (opts.undici === false || opts.http || opts.http2) {
@@ -102,7 +107,7 @@ function buildRequest (opts) {
102
107
  done(null, { statusCode: res.statusCode, headers: res.headers, stream: res })
103
108
  })
104
109
  req.once('timeout', () => {
105
- const err = new TimeoutError('HTTP request timed out')
110
+ const err = new HttpRequestTimeoutError()
106
111
  req.abort()
107
112
  done(err)
108
113
  })
@@ -183,13 +188,13 @@ function buildRequest (opts) {
183
188
  end(req, opts.body, done)
184
189
  }
185
190
  req.setTimeout(http2Opts.requestTimeout, () => {
186
- const err = new TimeoutError('HTTP/2 request timed out')
191
+ const err = new Http2RequestTimeoutError()
187
192
  req.close(http2.constants.NGHTTP2_CANCEL)
188
193
  done(err)
189
194
  })
190
195
  req.once('close', () => {
191
196
  if (sessionTimedOut) {
192
- const err = new TimeoutError('HTTP/2 session timed out')
197
+ const err = new Http2SessionTimeoutError()
193
198
  done(err)
194
199
  }
195
200
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fastify/reply-from",
3
- "version": "7.0.1",
3
+ "version": "8.2.0",
4
4
  "description": "forward your HTTP request to another server, for fastify",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -26,40 +26,37 @@
26
26
  "bugs": {
27
27
  "url": "https://github.com/fastify/fastify-reply-from/issues"
28
28
  },
29
- "engines": {
30
- "node": ">=12.18"
31
- },
32
29
  "homepage": "https://github.com/fastify/fastify-reply-from#readme",
33
30
  "devDependencies": {
34
- "@fastify/formbody": "^6.0.0",
35
- "@fastify/multipart": "^6.0.0",
36
- "@sinonjs/fake-timers": "^9.0.0",
37
- "@types/node": "^17.0.0",
38
- "@types/tap": "^15.0.3",
39
- "fastify": "^3.17.0",
31
+ "@fastify/formbody": "^7.0.1",
32
+ "@fastify/multipart": "^7.1.0",
33
+ "@fastify/pre-commit": "^2.0.2",
34
+ "@sinonjs/fake-timers": "^9.1.2",
35
+ "@types/node": "^18.0.0",
36
+ "@types/tap": "^15.0.7",
37
+ "fastify": "^4.0.2",
40
38
  "form-data": "^4.0.0",
41
39
  "got": "^11.8.2",
42
40
  "h2url": "^0.2.0",
43
- "msgpack5": "^6.0.0",
44
- "nock": "^13.1.0",
45
- "pre-commit": "^1.2.2",
41
+ "msgpack5": "^6.0.1",
42
+ "nock": "^13.2.6",
46
43
  "proxyquire": "^2.1.3",
47
- "simple-get": "^4.0.0",
44
+ "simple-get": "^4.0.1",
48
45
  "snazzy": "^9.0.0",
49
- "split2": "^4.0.0",
46
+ "split2": "^4.1.0",
50
47
  "standard": "^17.0.0",
51
- "tap": "^16.0.0",
52
- "tsd": "^0.20.0",
53
- "typescript": "^4.3.2"
48
+ "tap": "^16.2.0",
49
+ "tsd": "^0.22.0",
50
+ "typescript": "^4.7.3"
54
51
  },
55
52
  "dependencies": {
53
+ "@fastify/error": "^3.0.0",
56
54
  "end-of-stream": "^1.4.4",
57
- "fastify-plugin": "^3.0.0",
58
- "http-errors": "^2.0.0",
55
+ "fastify-plugin": "^4.0.0",
59
56
  "pump": "^3.0.0",
60
- "semver": "^7.3.5",
61
- "tiny-lru": "^8.0.1",
62
- "undici": "^5.0.0"
57
+ "semver": "^7.3.7",
58
+ "tiny-lru": "^8.0.2",
59
+ "undici": "^5.5.1"
63
60
  },
64
61
  "tsd": {
65
62
  "directory": "test"
@@ -29,14 +29,14 @@ instance.get('/', async (request, reply) => {
29
29
 
30
30
  t.teardown(target.close.bind(target))
31
31
 
32
- target.listen(0, (err) => {
32
+ target.listen({ port: 0 }, (err) => {
33
33
  t.error(err)
34
34
 
35
35
  instance.register(From, {
36
36
  base: `http://localhost:${target.address().port}`
37
37
  })
38
38
 
39
- instance.listen(0, (err) => {
39
+ instance.listen({ port: 0 }, (err) => {
40
40
  t.error(err)
41
41
 
42
42
  get(`http://localhost:${instance.server.address().port}`, (err, res, data) => {
package/test/base-get.js CHANGED
@@ -27,14 +27,14 @@ instance.get('/', (request, reply) => {
27
27
 
28
28
  t.teardown(target.close.bind(target))
29
29
 
30
- target.listen(0, (err) => {
30
+ target.listen({ port: 0 }, (err) => {
31
31
  t.error(err)
32
32
 
33
33
  instance.register(From, {
34
34
  base: `http://localhost:${target.address().port}`
35
35
  })
36
36
 
37
- instance.listen(0, (err) => {
37
+ instance.listen({ port: 0 }, (err) => {
38
38
  t.error(err)
39
39
 
40
40
  get(`http://localhost:${instance.server.address().port}`, (err, res, data) => {
package/test/base-path.js CHANGED
@@ -26,7 +26,7 @@ instance.register(From, {
26
26
  undici: false
27
27
  })
28
28
 
29
- instance.listen(0, (err) => {
29
+ instance.listen({ port: 0 }, (err) => {
30
30
  t.error(err)
31
31
 
32
32
  get(`http://localhost:${instance.server.address().port}`, (err, res, data) => {
@@ -27,14 +27,14 @@ instance.get('/hello', (request, reply) => {
27
27
 
28
28
  t.teardown(target.close.bind(target))
29
29
 
30
- target.listen(0, (err) => {
30
+ target.listen({ port: 0 }, (err) => {
31
31
  t.error(err)
32
32
 
33
33
  instance.register(From, {
34
34
  base: `http://localhost:${target.address().port}`
35
35
  })
36
36
 
37
- instance.listen(0, (err) => {
37
+ instance.listen({ port: 0 }, (err) => {
38
38
  t.error(err)
39
39
 
40
40
  get(`http://localhost:${instance.server.address().port}/hello?a=b`, (err, res, data) => {
@@ -28,7 +28,7 @@ instance.get('/', (request, reply) => {
28
28
 
29
29
  t.teardown(target.close.bind(target))
30
30
 
31
- target.listen(0, (err) => {
31
+ target.listen({ port: 0 }, (err) => {
32
32
  t.error(err)
33
33
 
34
34
  instance.register(From, {
@@ -36,7 +36,7 @@ target.listen(0, (err) => {
36
36
  http: true
37
37
  })
38
38
 
39
- instance.listen(0, (err) => {
39
+ instance.listen({ port: 0 }, (err) => {
40
40
  t.error(err)
41
41
 
42
42
  get(`http://localhost:${instance.server.address().port}`, (err, res, data) => {
@@ -21,7 +21,7 @@ const target = http.createServer((req, res) => {
21
21
  t.test('use a custom instance of \'undici\'', async t => {
22
22
  t.teardown(target.close.bind(target))
23
23
 
24
- await new Promise((resolve, reject) => target.listen(0, err => err ? reject(err) : resolve()))
24
+ await new Promise((resolve, reject) => target.listen({ port: 0 }, err => err ? reject(err) : resolve()))
25
25
 
26
26
  t.test('custom Pool', t => {
27
27
  const instance = Fastify()
@@ -35,7 +35,7 @@ t.test('use a custom instance of \'undici\'', async t => {
35
35
  reply.from()
36
36
  })
37
37
 
38
- instance.listen(0, (err) => {
38
+ instance.listen({ port: 0 }, (err) => {
39
39
  t.error(err)
40
40
 
41
41
  get(`http://localhost:${instance.server.address().port}`, (err, res, data) => {
@@ -61,7 +61,7 @@ t.test('use a custom instance of \'undici\'', async t => {
61
61
  reply.from()
62
62
  })
63
63
 
64
- instance.listen(0, (err) => {
64
+ instance.listen({ port: 0 }, (err) => {
65
65
  t.error(err)
66
66
 
67
67
  get(`http://localhost:${instance.server.address().port}`, (err, res, data) => {
@@ -0,0 +1,159 @@
1
+ 'use strict'
2
+
3
+ const t = require('tap')
4
+ const Fastify = require('fastify')
5
+ const From = require('..')
6
+ const http = require('http')
7
+ const get = require('simple-get').concat
8
+ const split = require('split2')
9
+
10
+ const target = http.createServer((req, res) => {
11
+ t.pass('request proxied')
12
+ t.equal(req.method, 'GET')
13
+ t.equal(req.url, '/')
14
+ t.equal(req.headers.connection, 'keep-alive')
15
+ res.statusCode = 205
16
+ res.setHeader('Content-Type', 'text/plain')
17
+ res.setHeader('x-my-header', 'hello!')
18
+ res.end('hello world')
19
+ })
20
+
21
+ t.test('use a custom instance of \'undici\'', async t => {
22
+ t.teardown(target.close.bind(target))
23
+
24
+ await new Promise((resolve, reject) => target.listen({ port: 0 }, err => err ? reject(err) : resolve()))
25
+
26
+ t.test('disableRequestLogging is set to true', t => {
27
+ t.plan(10)
28
+ const logStream = split(JSON.parse)
29
+ const instance = Fastify({
30
+ logger: {
31
+ level: 'info',
32
+ stream: logStream
33
+ }
34
+ })
35
+ t.teardown(instance.close.bind(instance))
36
+ instance.register(From, {
37
+ base: `http://localhost:${target.address().port}`,
38
+ disableRequestLogging: true
39
+ })
40
+
41
+ instance.get('/', (request, reply) => {
42
+ reply.from()
43
+ })
44
+
45
+ logStream.on('data', (log) => {
46
+ if (
47
+ log.level === 30 &&
48
+ (
49
+ !log.msg.match('response received') ||
50
+ !log.msg.match('fetching from remote server')
51
+ )
52
+ ) {
53
+ t.pass('request log message does not logged')
54
+ }
55
+ })
56
+
57
+ instance.listen({ port: 0 }, (err) => {
58
+ t.error(err)
59
+
60
+ get(`http://localhost:${instance.server.address().port}`, (err, res, data) => {
61
+ t.error(err)
62
+ t.equal(res.headers['content-type'], 'text/plain')
63
+ t.equal(res.headers['x-my-header'], 'hello!')
64
+ t.equal(res.statusCode, 205)
65
+ t.equal(data.toString(), 'hello world')
66
+ t.end()
67
+ })
68
+ })
69
+ })
70
+
71
+ t.test('disableRequestLogging is set to false', t => {
72
+ t.plan(8)
73
+ const logStream = split(JSON.parse)
74
+ const instance = Fastify({
75
+ logger: {
76
+ level: 'info',
77
+ stream: logStream
78
+ }
79
+ })
80
+ t.teardown(instance.close.bind(instance))
81
+ instance.register(From, {
82
+ base: `http://localhost:${target.address().port}`,
83
+ disableRequestLogging: false
84
+ })
85
+
86
+ instance.get('/', (request, reply) => {
87
+ reply.from()
88
+ })
89
+
90
+ logStream.on('data', (log) => {
91
+ if (
92
+ log.level === 30 &&
93
+ (
94
+ log.msg.match('response received') ||
95
+ log.msg.match('fetching from remote server')
96
+ )
97
+ ) {
98
+ t.pass('request log message does not logged')
99
+ }
100
+ })
101
+
102
+ instance.listen({ port: 0 }, (err) => {
103
+ t.error(err)
104
+
105
+ get(`http://localhost:${instance.server.address().port}`, (err, res, data) => {
106
+ t.error(err)
107
+ t.equal(res.headers['content-type'], 'text/plain')
108
+ t.equal(res.headers['x-my-header'], 'hello!')
109
+ t.equal(res.statusCode, 205)
110
+ t.equal(data.toString(), 'hello world')
111
+ t.end()
112
+ })
113
+ })
114
+ })
115
+
116
+ t.test('disableRequestLogging is not defined', t => {
117
+ t.plan(8)
118
+ const logStream = split(JSON.parse)
119
+ const instance = Fastify({
120
+ logger: {
121
+ level: 'info',
122
+ stream: logStream
123
+ }
124
+ })
125
+ t.teardown(instance.close.bind(instance))
126
+ instance.register(From, {
127
+ base: `http://localhost:${target.address().port}`
128
+ })
129
+
130
+ instance.get('/', (request, reply) => {
131
+ reply.from()
132
+ })
133
+
134
+ logStream.on('data', (log) => {
135
+ if (
136
+ log.level === 30 &&
137
+ (
138
+ log.msg.match('response received') ||
139
+ log.msg.match('fetching from remote server')
140
+ )
141
+ ) {
142
+ t.pass('request log message does not logged')
143
+ }
144
+ })
145
+
146
+ instance.listen({ port: 0 }, (err) => {
147
+ t.error(err)
148
+
149
+ get(`http://localhost:${instance.server.address().port}`, (err, res, data) => {
150
+ t.error(err)
151
+ t.equal(res.headers['content-type'], 'text/plain')
152
+ t.equal(res.headers['x-my-header'], 'hello!')
153
+ t.equal(res.statusCode, 205)
154
+ t.equal(data.toString(), 'hello world')
155
+ t.end()
156
+ })
157
+ })
158
+ })
159
+ })
@@ -56,7 +56,7 @@ instance.post('/', (request, reply) => {
56
56
 
57
57
  t.teardown(target.close.bind(target))
58
58
 
59
- instance.listen(0, (err) => {
59
+ instance.listen({ port: 0 }, (err) => {
60
60
  t.error(err)
61
61
 
62
62
  logStream.on('data', (log) => {
@@ -68,7 +68,7 @@ instance.listen(0, (err) => {
68
68
  }
69
69
  })
70
70
 
71
- target.listen(0, (err) => {
71
+ target.listen({ port: 0 }, (err) => {
72
72
  t.error(err)
73
73
 
74
74
  const form = new FormData()
package/test/full-get.js CHANGED
@@ -28,10 +28,10 @@ instance.get('/', (request, reply) => {
28
28
 
29
29
  t.teardown(target.close.bind(target))
30
30
 
31
- instance.listen(0, (err) => {
31
+ instance.listen({ port: 0 }, (err) => {
32
32
  t.error(err)
33
33
 
34
- target.listen(0, (err) => {
34
+ target.listen({ port: 0 }, (err) => {
35
35
  t.error(err)
36
36
 
37
37
  get(`http://localhost:${instance.server.address().port}`, (err, res, data) => {
@@ -35,10 +35,10 @@ instance.get('/', (request, reply) => {
35
35
 
36
36
  t.teardown(target.close.bind(target))
37
37
 
38
- instance.listen(0, (err) => {
38
+ instance.listen({ port: 0 }, (err) => {
39
39
  t.error(err)
40
40
 
41
- target.listen(0, (err) => {
41
+ target.listen({ port: 0 }, (err) => {
42
42
  t.error(err)
43
43
 
44
44
  get({
@@ -35,10 +35,10 @@ instance.post('/', (request, reply) => {
35
35
 
36
36
  t.teardown(target.close.bind(target))
37
37
 
38
- instance.listen(0, (err) => {
38
+ instance.listen({ port: 0 }, (err) => {
39
39
  t.error(err)
40
40
 
41
- target.listen(0, (err) => {
41
+ target.listen({ port: 0 }, (err) => {
42
42
  t.error(err)
43
43
 
44
44
  get.concat({