From afd20c1028c90381ca23dee128adfc68a660722d Mon Sep 17 00:00:00 2001 From: Dobes Vandermeer Date: Sat, 27 Jun 2020 20:18:29 -0700 Subject: [PATCH] fix: allow unset to prevent some defaults (#1560) for Content-Type and Accept-Encoding --- src/node/index.js | 6 +++++- src/request-base.js | 8 +++++--- test/node/basic.js | 37 ++++++++++++++++++++++++++++++++++--- 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/node/index.js b/src/node/index.js index 7c3e0cd9b..7544986a3 100644 --- a/src/node/index.js +++ b/src/node/index.js @@ -131,6 +131,10 @@ exports.buffer = {}; * @api private */ function _initHeaders(req) { + req._unset = { + // lowercase headers that were unset; this is used to suppress some default + // headers, such as Accept-Encoding and Content-Type + }; req._header = { // coerces header names to lowercase }; @@ -776,7 +780,7 @@ Request.prototype.request = function() { // set tcp no delay req.setNoDelay(true); - if (options.method !== 'HEAD') { + if (options.method !== 'HEAD' && !('accept-encoding' in this._unset)) { req.setHeader('Accept-Encoding', 'gzip, deflate'); } diff --git a/src/request-base.js b/src/request-base.js index 5ccca0c9c..d4addf11f 100644 --- a/src/request-base.js +++ b/src/request-base.js @@ -360,6 +360,7 @@ RequestBase.prototype.set = function(field, val) { this._header[field.toLowerCase()] = val; this.header[field] = val; + delete this._unset[field.toLowerCase()]; return this; }; @@ -370,7 +371,7 @@ RequestBase.prototype.set = function(field, val) { * Example: * * req.get('/') - * .unset('User-Agent') + * .unset('Accept-Encoding') * .end(callback); * * @param {String} field field name @@ -378,6 +379,7 @@ RequestBase.prototype.set = function(field, val) { RequestBase.prototype.unset = function(field) { delete this._header[field.toLowerCase()]; delete this.header[field]; + this._unset[field.toLowerCase()] = true; return this; }; @@ -617,7 +619,7 @@ RequestBase.prototype.send = function(data) { } } else if (typeof data === 'string') { // default to x-www-form-urlencoded - if (!type) this.type('form'); + if (!type && !('content-type' in this._unset)) this.type('form'); type = this._header['content-type']; if (type === 'application/x-www-form-urlencoded') { this._data = this._data ? `${this._data}&${data}` : data; @@ -633,7 +635,7 @@ RequestBase.prototype.send = function(data) { } // default to json - if (!type) this.type('json'); + if (!type && !('content-type' in this._unset)) this.type('json'); return this; }; diff --git a/test/node/basic.js b/test/node/basic.js index 16448f120..54bbcaf10 100644 --- a/test/node/basic.js +++ b/test/node/basic.js @@ -67,12 +67,43 @@ describe('[node] request', () => { }); describe('req.unset(field)', () => { - it('should remove the header field', done => { + it('should remove a header added using set', done => { request .post(`${base}/echo`) - .unset('User-Agent') + .set('X-Header', 'value') + .unset('X-Header') .end((err, res) => { - assert.equal(void 0, res.header['user-agent']); + assert.equal(void 0, res.header['x-header']); + done(); + }); + }); + it('should not prevent set from being used after to set the header again', done => { + request + .post(`${base}/echo`) + .set('X-Header', 'value 1') + .unset('X-Header') + .set('X-Header', 'value') + .end((err, res) => { + assert.equal('value', res.header['x-header']); + done(); + }); + }); + it('should prevent the Accept-Encoding header from being added automatically', done => { + request + .post(`${base}/echo`) + .unset('Accept-Encoding') + .end((err, res) => { + assert.equal(void 0, res.header['accept-encoding']); + done(); + }); + }); + it('should prevent the Content-Type field from being added automatically', done => { + request + .post(`${base}/echo`) + .send('hello=world') + .unset('Content-Type') + .end((err, res) => { + assert.equal(void 0, res.header['content-type']); done(); }); });