diff --git a/lib/Transport.js b/lib/Transport.js index 298e21fcf..cf8bcc206 100644 --- a/lib/Transport.js +++ b/lib/Transport.js @@ -123,15 +123,17 @@ class Transport { return callback(err, result) } } - headers['Content-Type'] = headers['Content-Type'] || 'application/json' - if (compression === 'gzip') { - if (isStream(params.body) === false) { - params.body = intoStream(params.body).pipe(createGzip()) - } else { - params.body = params.body.pipe(createGzip()) + if (params.body !== '') { + headers['Content-Type'] = headers['Content-Type'] || 'application/json' + if (compression === 'gzip') { + if (isStream(params.body) === false) { + params.body = intoStream(params.body).pipe(createGzip()) + } else { + params.body = params.body.pipe(createGzip()) + } + headers['Content-Encoding'] = compression } - headers['Content-Encoding'] = compression } if (isStream(params.body) === false) { diff --git a/test/unit/events.test.js b/test/unit/events.test.js index ce86e04b6..cd904193f 100644 --- a/test/unit/events.test.js +++ b/test/unit/events.test.js @@ -34,7 +34,6 @@ test('Should emit a request event when a request is performed', t => { body: '', querystring: 'q=foo%3Abar', headers: { - 'Content-Type': 'application/json', 'Content-Length': '0' } }, @@ -87,7 +86,6 @@ test('Should emit a response event in case of a successful response', t => { body: '', querystring: 'q=foo%3Abar', headers: { - 'Content-Type': 'application/json', 'Content-Length': '0' } }, @@ -138,7 +136,6 @@ test('Should emit a response event with the error set', t => { body: '', querystring: 'q=foo%3Abar', headers: { - 'Content-Type': 'application/json', 'Content-Length': '0' } }, diff --git a/test/unit/transport.test.js b/test/unit/transport.test.js index 03adcce72..7d2e31abf 100644 --- a/test/unit/transport.test.js +++ b/test/unit/transport.test.js @@ -1865,6 +1865,55 @@ test('Compress request', t => { } }) + t.test('Should skip the compression for empty strings/null/undefined', t => { + t.plan(9) + + function handler (req, res) { + t.strictEqual(req.headers['content-encoding'], undefined) + t.strictEqual(req.headers['content-type'], undefined) + res.end() + } + + buildServer(handler, ({ port }, server) => { + const pool = new ConnectionPool({ Connection }) + pool.addConnection(`http://localhost:${port}`) + + const transport = new Transport({ + emit: () => {}, + connectionPool: pool, + serializer: new Serializer(), + maxRetries: 3, + compression: 'gzip', + requestTimeout: 30000, + sniffInterval: false, + sniffOnStart: false + }) + + transport.request({ + method: 'DELETE', + path: '/hello', + body: '' + }, (err, { body }) => { + t.error(err) + transport.request({ + method: 'GET', + path: '/hello', + body: null + }, (err, { body }) => { + t.error(err) + transport.request({ + method: 'GET', + path: '/hello', + body: undefined + }, (err, { body }) => { + t.error(err) + server.stop() + }) + }) + }) + }) + }) + t.end() })