From 69266482364c2cae3e305755dbc1f4a8b455cee9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20L=C3=B6wenstein?= <32882329+MoritzLoewenstein@users.noreply.github.com> Date: Wed, 8 Feb 2023 22:29:05 +0100 Subject: [PATCH] fix #1732 (#2272) --- lib/winston/transports/http.js | 41 +++++++++-------------- test/unit/winston/transports/http.test.js | 3 +- 2 files changed, 18 insertions(+), 26 deletions(-) diff --git a/lib/winston/transports/http.js b/lib/winston/transports/http.js index bc5ffd8d0..0653d78bc 100644 --- a/lib/winston/transports/http.js +++ b/lib/winston/transports/http.js @@ -57,7 +57,7 @@ module.exports = class Http extends TransportStream { * @returns {undefined} */ log(info, callback) { - this._request(info, (err, res) => { + this._request(info, null, null, (err, res) => { if (res && res.statusCode !== 200) { err = new Error(`Invalid HTTP Status Code: ${res.statusCode}`); } @@ -93,17 +93,13 @@ module.exports = class Http extends TransportStream { params: this.normalizeQuery(options) }; - if (options.params.path) { - options.path = options.params.path; - delete options.params.path; - } + const auth = options.params.auth || null; + delete options.params.auth; - if (options.params.auth) { - options.auth = options.params.auth; - delete options.params.auth; - } + const path = options.params.path || null; + delete options.params.path; - this._request(options, (err, res, body) => { + this._request(options, auth, path, (err, res, body) => { if (res && res.statusCode !== 200) { err = new Error(`Invalid HTTP Status Code: ${res.statusCode}`); } @@ -136,18 +132,14 @@ module.exports = class Http extends TransportStream { params: options }; - if (options.params.path) { - options.path = options.params.path; - delete options.params.path; - } + const path = options.params.path || null; + delete options.params.path; - if (options.params.auth) { - options.auth = options.params.auth; - delete options.params.auth; - } + const auth = options.params.auth || null; + delete options.params.auth; let buff = ''; - const req = this._request(options); + const req = this._request(options, auth, path); stream.destroy = () => req.destroy(); req.on('data', data => { @@ -174,16 +166,15 @@ module.exports = class Http extends TransportStream { * Make a request to a winstond server or any http server which can * handle json-rpc. * @param {function} options - Options to sent the request. + * @param {Object?} auth - authentication options + * @param {string} path - request path * @param {function} callback - Continuation to respond to when complete. */ - _request(options, callback) { + _request(options, auth, path, callback) { options = options || {}; - const auth = options.auth || this.auth; - const path = options.path || this.path || ''; - - delete options.auth; - delete options.path; + auth = auth || this.auth; + path = path || this.path || ''; if (this.batch) { this._doBatch(options, callback, auth, path); diff --git a/test/unit/winston/transports/http.test.js b/test/unit/winston/transports/http.test.js index 3074811cc..f70011c7a 100644 --- a/test/unit/winston/transports/http.test.js +++ b/test/unit/winston/transports/http.test.js @@ -51,7 +51,8 @@ describe('Http({ host, port, path })', function () { const dummyLog = { level: 'info', message: 'hello', - meta: {} + meta: {}, + path: '/error' }; afterEach(function (done) {