Skip to content

Commit 5a0bcfd

Browse files
committed
Skip compression in case of empty string body (#1080)
* Fix #1069 * Updated test * Updated test
1 parent e23cc72 commit 5a0bcfd

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
@@ -123,15 +123,17 @@ class Transport {
123123
return callback(err, result)
124124
}
125125
}
126-
headers['Content-Type'] = headers['Content-Type'] || 'application/json'
127126

128-
if (compression === 'gzip') {
129-
if (isStream(params.body) === false) {
130-
params.body = intoStream(params.body).pipe(createGzip())
131-
} else {
132-
params.body = params.body.pipe(createGzip())
127+
if (params.body !== '') {
128+
headers['Content-Type'] = headers['Content-Type'] || 'application/json'
129+
if (compression === 'gzip') {
130+
if (isStream(params.body) === false) {
131+
params.body = intoStream(params.body).pipe(createGzip())
132+
} else {
133+
params.body = params.body.pipe(createGzip())
134+
}
135+
headers['Content-Encoding'] = compression
133136
}
134-
headers['Content-Encoding'] = compression
135137
}
136138

137139
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
@@ -1865,6 +1865,55 @@ test('Compress request', t => {
18651865
}
18661866
})
18671867

1868+
t.test('Should skip the compression for empty strings/null/undefined', t => {
1869+
t.plan(9)
1870+
1871+
function handler (req, res) {
1872+
t.strictEqual(req.headers['content-encoding'], undefined)
1873+
t.strictEqual(req.headers['content-type'], undefined)
1874+
res.end()
1875+
}
1876+
1877+
buildServer(handler, ({ port }, server) => {
1878+
const pool = new ConnectionPool({ Connection })
1879+
pool.addConnection(`http://localhost:${port}`)
1880+
1881+
const transport = new Transport({
1882+
emit: () => {},
1883+
connectionPool: pool,
1884+
serializer: new Serializer(),
1885+
maxRetries: 3,
1886+
compression: 'gzip',
1887+
requestTimeout: 30000,
1888+
sniffInterval: false,
1889+
sniffOnStart: false
1890+
})
1891+
1892+
transport.request({
1893+
method: 'DELETE',
1894+
path: '/hello',
1895+
body: ''
1896+
}, (err, { body }) => {
1897+
t.error(err)
1898+
transport.request({
1899+
method: 'GET',
1900+
path: '/hello',
1901+
body: null
1902+
}, (err, { body }) => {
1903+
t.error(err)
1904+
transport.request({
1905+
method: 'GET',
1906+
path: '/hello',
1907+
body: undefined
1908+
}, (err, { body }) => {
1909+
t.error(err)
1910+
server.stop()
1911+
})
1912+
})
1913+
})
1914+
})
1915+
})
1916+
18681917
t.end()
18691918
})
18701919

0 commit comments

Comments
 (0)