Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip compression in case of empty string body #1080

Merged
merged 3 commits into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions lib/Transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,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) {
Expand Down
3 changes: 0 additions & 3 deletions test/unit/events.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
},
Expand Down Expand Up @@ -86,7 +85,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'
}
},
Expand Down Expand Up @@ -136,7 +134,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'
}
},
Expand Down
49 changes: 49 additions & 0 deletions test/unit/transport.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1813,6 +1813,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()
})

Expand Down