send delete body with content-length

This commit is contained in:
David Frank 2016-05-26 02:00:25 +08:00
parent 8a6213198b
commit a2607719ce
2 changed files with 34 additions and 2 deletions

View File

@ -87,8 +87,8 @@ function Fetch(url, opts) {
headers.set('content-type', 'multipart/form-data; boundary=' + options.body.getBoundary());
}
// bring node-fetch closer to browser behavior by setting content-length automatically for POST, PUT, PATCH requests when body is empty or string
if (!headers.has('content-length') && options.method.substr(0, 1).toUpperCase() === 'P') {
// bring node-fetch closer to browser behavior by setting content-length automatically
if (!headers.has('content-length') && /post|put|patch|delete/i.test(options.method)) {
if (typeof options.body === 'string') {
headers.set('content-length', Buffer.byteLength(options.body));
// detect form data input from form-data module, this hack avoid the need to add content-length header manually

View File

@ -715,6 +715,38 @@ describe('node-fetch', function() {
});
});
it('should allow POST request with string body', function() {
url = base + '/inspect';
opts = {
method: 'POST'
, body: 'a=1'
};
return fetch(url, opts).then(function(res) {
return res.json();
}).then(function(res) {
expect(res.method).to.equal('POST');
expect(res.body).to.equal('a=1');
expect(res.headers['transfer-encoding']).to.be.undefined;
expect(res.headers['content-length']).to.equal('3');
});
});
it('should allow DELETE request with string body', function() {
url = base + '/inspect';
opts = {
method: 'DELETE'
, body: 'a=1'
};
return fetch(url, opts).then(function(res) {
return res.json();
}).then(function(res) {
expect(res.method).to.equal('DELETE');
expect(res.body).to.equal('a=1');
expect(res.headers['transfer-encoding']).to.be.undefined;
expect(res.headers['content-length']).to.equal('3');
});
});
it('should allow PATCH request', function() {
url = base + '/inspect';
opts = {