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

fix: allow unset to prevent default values for Content-Type and Accept-Encoding #1560

Merged
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
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