Skip to content

Commit c8a77cc

Browse files
committed
Allow custom content-type without setting a filename
1 parent 42c7637 commit c8a77cc

2 files changed

Lines changed: 93 additions & 0 deletions

File tree

lib/form_data.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ FormData.prototype._multiPartHeader = function(field, value, options) {
175175
options.contentType ||
176176
FormData.DEFAULT_CONTENT_TYPE
177177
);
178+
} else if (options.contentType) {
179+
headers['Content-Type'].push(options.contentType);
178180
}
179181

180182
for (var prop in headers) {
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
var common = require('../common');
2+
var assert = common.assert;
3+
var http = require('http');
4+
var mime = require('mime-types');
5+
var fs = require('fs');
6+
var FormData = require(common.dir.lib + '/form_data');
7+
8+
// wrap non simple values into function
9+
// just to deal with ReadStream "autostart"
10+
// Can't wait for 0.10
11+
var FIELDS = {
12+
'no_type': {
13+
value: 'my_value'
14+
},
15+
'custom_type': {
16+
value: 'my_value',
17+
type: 'image/png',
18+
options: {
19+
contentType: 'image/png'
20+
}
21+
},
22+
'default_type': {
23+
type: FormData.DEFAULT_CONTENT_TYPE,
24+
value: function(){ return new Buffer([1, 2, 3]); }
25+
},
26+
'implicit_type': {
27+
type: mime.lookup(common.dir.fixture + '/unicycle.jpg'),
28+
value: function(){ return fs.createReadStream(common.dir.fixture + '/unicycle.jpg'); }
29+
}
30+
};
31+
32+
var server = http.createServer(function(req, res) {
33+
var body = '';
34+
var boundry = req.headers['content-type'].split('boundary=').pop();
35+
36+
req.on('data', function (data) { body += data.toString('utf-8'); });
37+
req.on('end', function () {
38+
// Separate body into individual files/fields and remove leading and trailing content.
39+
var fields = body.split(boundry).slice(1, -1);
40+
41+
assert.ok(fields.length === 4);
42+
43+
assert.ok(fields[0].indexOf('name="no_type"') > -1);
44+
assert.ok(fields[0].indexOf('Content-Type"') === -1);
45+
46+
assert.ok(fields[1].indexOf('name="custom_type"') > -1);
47+
assert.ok(fields[1].indexOf('Content-Type: ' + FIELDS.custom_type.type) > -1);
48+
49+
assert.ok(fields[2].indexOf('name="default_type"') > -1);
50+
assert.ok(fields[2].indexOf('Content-Type: ' + FIELDS.default_type.type) > -1);
51+
52+
assert.ok(fields[3].indexOf('name="implicit_type"') > -1);
53+
assert.ok(fields[3].indexOf('Content-Type: ' + FIELDS.implicit_type.type) > -1);
54+
});
55+
res.end();
56+
57+
});
58+
59+
server.listen(common.port, function() {
60+
61+
var form = new FormData();
62+
63+
var field;
64+
for (var name in FIELDS) {
65+
field = FIELDS[name];
66+
// important to append ReadStreams within the same tick
67+
if ((typeof field.value == 'function')) {
68+
field.value = field.value();
69+
}
70+
form.append(name, field.value, field.options);
71+
}
72+
73+
// custom params object passed to submit
74+
form.submit({
75+
port: common.port,
76+
path: '/'
77+
}, function(err, res) {
78+
79+
if (err) {
80+
throw err;
81+
}
82+
83+
assert.strictEqual(res.statusCode, 200);
84+
85+
// unstuck new streams
86+
res.resume();
87+
88+
server.close();
89+
});
90+
91+
});

0 commit comments

Comments
 (0)