Skip to content

Commit

Permalink
Revert "fix: allow unset to prevent some defaults (#1560)"
Browse files Browse the repository at this point in the history
This reverts commit afd20c1.
  • Loading branch information
niftylettuce committed Jun 28, 2020
1 parent 993b764 commit b411b66
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 44 deletions.
6 changes: 1 addition & 5 deletions src/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,6 @@ 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 @@ -792,7 +788,7 @@ Request.prototype.request = function() {
// set tcp no delay
req.setNoDelay(true);

if (options.method !== 'HEAD' && !('accept-encoding' in this._unset)) {
if (options.method !== 'HEAD') {
req.setHeader('Accept-Encoding', 'gzip, deflate');
}

Expand Down
8 changes: 3 additions & 5 deletions src/request-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,6 @@ RequestBase.prototype.set = function(field, val) {

this._header[field.toLowerCase()] = val;
this.header[field] = val;
delete this._unset[field.toLowerCase()];
return this;
};

Expand All @@ -375,15 +374,14 @@ RequestBase.prototype.set = function(field, val) {
* Example:
*
* req.get('/')
* .unset('Accept-Encoding')
* .unset('User-Agent')
* .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 @@ -623,7 +621,7 @@ RequestBase.prototype.send = function(data) {
}
} else if (typeof data === 'string') {
// default to x-www-form-urlencoded
if (!type && !('content-type' in this._unset)) this.type('form');
if (!type) 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 @@ -639,7 +637,7 @@ RequestBase.prototype.send = function(data) {
}

// default to json
if (!type && !('content-type' in this._unset)) this.type('json');
if (!type) this.type('json');
return this;
};

Expand Down
37 changes: 3 additions & 34 deletions test/node/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,43 +67,12 @@ describe('[node] request', () => {
});

describe('req.unset(field)', () => {
it('should remove a header added using set', done => {
it('should remove the header field', done => {
request
.post(`${base}/echo`)
.set('X-Header', 'value')
.unset('X-Header')
.unset('User-Agent')
.end((err, res) => {
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']);
assert.equal(void 0, res.header['user-agent']);
done();
});
});
Expand Down

0 comments on commit b411b66

Please sign in to comment.