Skip to content

Commit

Permalink
fix: allow unset to prevent some defaults (#1560)
Browse files Browse the repository at this point in the history
for Content-Type and Accept-Encoding
  • Loading branch information
dobesv committed Jun 28, 2020
1 parent 4babc5d commit afd20c1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand Down Expand Up @@ -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');
}

Expand Down
8 changes: 5 additions & 3 deletions src/request-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand All @@ -370,14 +371,15 @@ RequestBase.prototype.set = function(field, val) {
* Example:
*
* req.get('/')
* .unset('User-Agent')
* .unset('Accept-Encoding')
* .end(callback);
*
* @param {String} field field name
*/
RequestBase.prototype.unset = function(field) {
delete this._header[field.toLowerCase()];
delete this.header[field];
this._unset[field.toLowerCase()] = true;
return this;
};

Expand Down Expand Up @@ -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;
Expand All @@ -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;
};

Expand Down
37 changes: 34 additions & 3 deletions test/node/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
Expand Down

0 comments on commit afd20c1

Please sign in to comment.