Skip to content

Commit 8190bf5

Browse files
committed
Skip compression in case of empty string body (#1080)
* Fix #1069 * Updated test * Updated test
1 parent eb31fa6 commit 8190bf5

File tree

3 files changed

+58
-10
lines changed

3 files changed

+58
-10
lines changed

lib/Transport.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,17 @@ class Transport {
130130
return callback(err, result)
131131
}
132132
}
133-
headers['Content-Type'] = headers['Content-Type'] || 'application/json'
134133

135-
if (compression === 'gzip') {
136-
if (isStream(params.body) === false) {
137-
params.body = intoStream(params.body).pipe(createGzip())
138-
} else {
139-
params.body = params.body.pipe(createGzip())
134+
if (params.body !== '') {
135+
headers['Content-Type'] = headers['Content-Type'] || 'application/json'
136+
if (compression === 'gzip') {
137+
if (isStream(params.body) === false) {
138+
params.body = intoStream(params.body).pipe(createGzip())
139+
} else {
140+
params.body = params.body.pipe(createGzip())
141+
}
142+
headers['Content-Encoding'] = compression
140143
}
141-
headers['Content-Encoding'] = compression
142144
}
143145

144146
if (isStream(params.body) === false) {

test/unit/events.test.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ test('Should emit a request event when a request is performed', t => {
3434
body: '',
3535
querystring: 'q=foo%3Abar',
3636
headers: {
37-
'Content-Type': 'application/json',
3837
'Content-Length': '0'
3938
}
4039
},
@@ -87,7 +86,6 @@ test('Should emit a response event in case of a successful response', t => {
8786
body: '',
8887
querystring: 'q=foo%3Abar',
8988
headers: {
90-
'Content-Type': 'application/json',
9189
'Content-Length': '0'
9290
}
9391
},
@@ -138,7 +136,6 @@ test('Should emit a response event with the error set', t => {
138136
body: '',
139137
querystring: 'q=foo%3Abar',
140138
headers: {
141-
'Content-Type': 'application/json',
142139
'Content-Length': '0'
143140
}
144141
},

test/unit/transport.test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,6 +1813,55 @@ test('Compress request', t => {
18131813
}
18141814
})
18151815

1816+
t.test('Should skip the compression for empty strings/null/undefined', t => {
1817+
t.plan(9)
1818+
1819+
function handler (req, res) {
1820+
t.strictEqual(req.headers['content-encoding'], undefined)
1821+
t.strictEqual(req.headers['content-type'], undefined)
1822+
res.end()
1823+
}
1824+
1825+
buildServer(handler, ({ port }, server) => {
1826+
const pool = new ConnectionPool({ Connection })
1827+
pool.addConnection(`http://localhost:${port}`)
1828+
1829+
const transport = new Transport({
1830+
emit: () => {},
1831+
connectionPool: pool,
1832+
serializer: new Serializer(),
1833+
maxRetries: 3,
1834+
compression: 'gzip',
1835+
requestTimeout: 30000,
1836+
sniffInterval: false,
1837+
sniffOnStart: false
1838+
})
1839+
1840+
transport.request({
1841+
method: 'DELETE',
1842+
path: '/hello',
1843+
body: ''
1844+
}, (err, { body }) => {
1845+
t.error(err)
1846+
transport.request({
1847+
method: 'GET',
1848+
path: '/hello',
1849+
body: null
1850+
}, (err, { body }) => {
1851+
t.error(err)
1852+
transport.request({
1853+
method: 'GET',
1854+
path: '/hello',
1855+
body: undefined
1856+
}, (err, { body }) => {
1857+
t.error(err)
1858+
server.stop()
1859+
})
1860+
})
1861+
})
1862+
})
1863+
})
1864+
18161865
t.end()
18171866
})
18181867

0 commit comments

Comments
 (0)