From 6cc2462c70e859f67a4a21e7641c6e7b33af888e Mon Sep 17 00:00:00 2001 From: Marcello Barile Date: Fri, 15 Feb 2019 17:00:13 +0100 Subject: [PATCH 1/2] Avoiding to set the "Content-Length" header for GET requests --- lib/node/index.js | 7 ++++++- test/request.js | 13 +++++++++++++ test/support/server.js | 12 ++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/node/index.js b/lib/node/index.js index 2f948a911..be01ae007 100644 --- a/lib/node/index.js +++ b/lib/node/index.js @@ -886,6 +886,7 @@ Request.prototype._end = function() { let data = this._data; const req = this.req; const method = this.method; + const methodsWithBody = ['PUT', 'POST', 'PATCH']; this._setTimeouts(); @@ -904,7 +905,11 @@ Request.prototype._end = function() { } // content-length - if (data && !req.getHeader('Content-Length')) { + if ( + methodsWithBody.includes(method) + && data + && !req.getHeader('Content-Length') + ) { req.setHeader('Content-Length', Buffer.isBuffer(data) ? data.length : Buffer.byteLength(data)); } } diff --git a/test/request.js b/test/request.js index 7b470df65..323fd6944 100644 --- a/test/request.js +++ b/test/request.js @@ -569,6 +569,19 @@ it('POST shorthand without callback', next => { }); }); +it('GET should not send the content-length header', next => { + request + .get(`${uri}/content-length`) + .send({foo: 'bar'}) + .then((res) => { + try { + assert(!res.badRequest); + next(); + } catch(e) { next(e); } + }) + .catch(next); +}); + it('GET querystring object with array', next => { request .get(`${uri}/querystring`) diff --git a/test/support/server.js b/test/support/server.js index 5721b4d14..a628b23b0 100644 --- a/test/support/server.js +++ b/test/support/server.js @@ -547,5 +547,17 @@ app.get('/error/redirect-error:id', (req, res) => { } }); +app.get('/content-length', (req, res) => { + const headers = req.headers; + if ( + headers.hasOwnProperty('content-length') + && headers['content-length'] > 0 + ) { + res.status(400).send('bad request'); + } else { + res.status(200).send('ok'); + } +}); + let server = http.createServer(app); server.listen(process.env.ZUUL_PORT); From 4c5fb5cdb92a727a8bea5120423b8cd605b3b49d Mon Sep 17 00:00:00 2001 From: Marcello Barile Date: Tue, 2 Apr 2019 16:07:24 +0200 Subject: [PATCH 2/2] code formatting --- src/node/index.js | 6 +++++- test/request.js | 8 +++++--- test/support/server.js | 6 +++--- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/node/index.js b/src/node/index.js index a3e10b7ff..c7e77c5de 100644 --- a/src/node/index.js +++ b/src/node/index.js @@ -972,7 +972,11 @@ Request.prototype._end = function() { } // content-length - if (methodsWithBody.includes(method) && data && !req.getHeader('Content-Length')) { + if ( + methodsWithBody.includes(method) && + data && + !req.getHeader('Content-Length') + ) { req.setHeader( 'Content-Length', Buffer.isBuffer(data) ? data.length : Buffer.byteLength(data) diff --git a/test/request.js b/test/request.js index 5aedc30bb..8c36a4167 100644 --- a/test/request.js +++ b/test/request.js @@ -175,12 +175,14 @@ describe('request', function() { it('GET should not send the content-length header', next => { request .get(`${uri}/content-length`) - .send({foo: 'bar'}) - .then((res) => { + .send({ foo: 'bar' }) + .then(res => { try { assert(!res.badRequest); next(); - } catch(e) { next(e); } + } catch (err) { + next(err); + } }) .catch(next); }); diff --git a/test/support/server.js b/test/support/server.js index b00b6c222..0cc9af08a 100644 --- a/test/support/server.js +++ b/test/support/server.js @@ -579,10 +579,10 @@ app.get('/error/redirect-error:id', (req, res) => { }); app.get('/content-length', (req, res) => { - const headers = req.headers; + const { headers } = req; if ( - headers.hasOwnProperty('content-length') - && headers['content-length'] > 0 + headers.hasOwnProperty('content-length') && + headers['content-length'] > 0 ) { res.status(400).send('bad request'); } else {