additional tests for the new form-data integration
This commit is contained in:
parent
18b0f5090a
commit
565ec4b35a
|
@ -5,7 +5,11 @@ Changelog
|
|||
|
||||
# 1.x release
|
||||
|
||||
## v1.3.1 (master)
|
||||
## v1.3.2 (master)
|
||||
|
||||
- Enhance: allow auto detect of form-data input (no `FormData` spec on node.js, this is form-data specific feature)
|
||||
|
||||
## v1.3.1
|
||||
|
||||
- Enhance: allow custom host header to be set (server-side only feature, as it's a forbidden header on client-side)
|
||||
|
||||
|
|
14
README.md
14
README.md
|
@ -97,7 +97,7 @@ fetch('http://httpbin.org/post', { method: 'POST', body: stream })
|
|||
console.log(json);
|
||||
});
|
||||
|
||||
// post with form-data
|
||||
// post with form-data (detect multipart)
|
||||
|
||||
var FormData = require('form-data');
|
||||
var form = new FormData();
|
||||
|
@ -109,6 +109,18 @@ fetch('http://httpbin.org/post', { method: 'POST', body: form })
|
|||
console.log(json);
|
||||
});
|
||||
|
||||
// post with form-data (custom headers)
|
||||
|
||||
var FormData = require('form-data');
|
||||
var form = new FormData();
|
||||
form.append('a', 1);
|
||||
fetch('http://httpbin.org/post', { method: 'POST', body: form, headers: form.getHeaders() })
|
||||
.then(function(res) {
|
||||
return res.json();
|
||||
}).then(function(json) {
|
||||
console.log(json);
|
||||
});
|
||||
|
||||
// node 0.11+, yield with co
|
||||
|
||||
var co = require('co');
|
||||
|
|
3
index.js
3
index.js
|
@ -77,7 +77,8 @@ function Fetch(url, opts) {
|
|||
headers.set('accept', '*/*');
|
||||
}
|
||||
|
||||
if (!headers.has('content-type') && options.body && typeof options.body.getBoundary === "function") {
|
||||
// detect form data input from form-data module, this hack avoid the need to pass multipart header manually
|
||||
if (!headers.has('content-type') && options.body && typeof options.body.getBoundary === 'function') {
|
||||
headers.set('content-type', 'multipart/form-data; boundary=' + options.body.getBoundary());
|
||||
}
|
||||
|
||||
|
|
|
@ -31,6 +31,7 @@
|
|||
"form-data": "^1.0.0-rc1",
|
||||
"istanbul": "^0.3.5",
|
||||
"mocha": "^2.1.0",
|
||||
"parted": "^0.1.1",
|
||||
"promise": "^6.1.0",
|
||||
"resumer": "0.0.0"
|
||||
},
|
||||
|
|
|
@ -4,6 +4,7 @@ var parse = require('url').parse;
|
|||
var zlib = require('zlib');
|
||||
var stream = require('stream');
|
||||
var convert = require('encoding').convert;
|
||||
var Multipart = require('parted').multipart;
|
||||
|
||||
module.exports = TestServer;
|
||||
|
||||
|
@ -244,4 +245,22 @@ TestServer.prototype.router = function(req, res) {
|
|||
});
|
||||
}
|
||||
|
||||
if (p === '/multipart') {
|
||||
res.statusCode = 200;
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
var parser = new Multipart(req.headers['content-type']);
|
||||
var body = '';
|
||||
parser.on('part', function(field, part) {
|
||||
body += field + '=' + part;
|
||||
});
|
||||
parser.on('end', function() {
|
||||
res.end(JSON.stringify({
|
||||
method: req.method,
|
||||
url: req.url,
|
||||
headers: req.headers,
|
||||
body: body
|
||||
}));
|
||||
});
|
||||
req.pipe(parser);
|
||||
}
|
||||
}
|
||||
|
|
30
test/test.js
30
test/test.js
|
@ -449,9 +449,9 @@ describe('node-fetch', function() {
|
|||
|
||||
it('should allow POST request with form-data as body', function() {
|
||||
var form = new FormData();
|
||||
form.append('a', '1');
|
||||
form.append('a','1');
|
||||
|
||||
url = base + '/inspect';
|
||||
url = base + '/multipart';
|
||||
opts = {
|
||||
method: 'POST'
|
||||
, body: form
|
||||
|
@ -460,11 +460,33 @@ describe('node-fetch', function() {
|
|||
return res.json();
|
||||
}).then(function(res) {
|
||||
expect(res.method).to.equal('POST');
|
||||
expect(res.body).to.contain('Content-Disposition: form-data;')
|
||||
.and.to.contain('name="a"');
|
||||
expect(res.headers['content-type']).to.contain('multipart/form-data');
|
||||
expect(res.body).to.equal('a=1');
|
||||
});
|
||||
});
|
||||
|
||||
it('should allow POST request with form-data as body and custom headers', function() {
|
||||
var form = new FormData();
|
||||
form.append('a','1');
|
||||
|
||||
var headers = form.getHeaders();
|
||||
headers['b'] = '2';
|
||||
|
||||
url = base + '/multipart';
|
||||
opts = {
|
||||
method: 'POST'
|
||||
, body: form
|
||||
, headers: headers
|
||||
};
|
||||
return fetch(url, opts).then(function(res) {
|
||||
return res.json();
|
||||
}).then(function(res) {
|
||||
expect(res.method).to.equal('POST');
|
||||
expect(res.headers['content-type']).to.contain('multipart/form-data');
|
||||
expect(res.headers.b).to.equal('2');
|
||||
expect(res.body).to.equal('a=1');
|
||||
});
|
||||
});
|
||||
|
||||
it('should allow PUT request', function() {
|
||||
url = base + '/inspect';
|
||||
|
|
Loading…
Reference in New Issue