Skip to content

Commit 1d61c2d

Browse files
committed
#47 return request from .submit()
1 parent cbdd19f commit 1d61c2d

3 files changed

Lines changed: 130 additions & 37 deletions

File tree

lib/form_data.js

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -251,49 +251,56 @@ FormData.prototype.getLength = function(cb) {
251251
};
252252

253253
FormData.prototype.submit = function(params, cb) {
254+
255+
var request
256+
, options
257+
, defaults = {
258+
method : 'post',
259+
port : 80,
260+
headers: this.getHeaders()
261+
};
262+
263+
// parse provided url if it's string
264+
// or treat it as options object
265+
if (typeof params == 'string') {
266+
params = parseUrl(params);
267+
268+
options = populate({
269+
port: params.port,
270+
path: params.pathname,
271+
host: params.hostname
272+
}, defaults);
273+
}
274+
else // use custom params
275+
{
276+
options = populate(params, defaults);
277+
}
278+
279+
// https if specified, fallback to http in any other case
280+
if (params.protocol == 'https:') {
281+
// override default port
282+
if (!params.port) options.port = 443;
283+
request = https.request(options);
284+
} else {
285+
request = http.request(options);
286+
}
287+
288+
// get content length and fire away
254289
this.getLength(function(err, length) {
255290

256-
var request
257-
, options
258-
, defaults = {
259-
method : 'post',
260-
port : 80,
261-
headers: this.getHeaders({'Content-Length': length})
262-
};
263-
264-
// parse provided url if it's string
265-
// or treat it as options object
266-
if (typeof params == 'string') {
267-
params = parseUrl(params);
268-
269-
options = populate({
270-
port: params.port,
271-
path: params.pathname,
272-
host: params.hostname
273-
}, defaults);
274-
}
275-
else // use custom params
276-
{
277-
options = populate(params, defaults);
278-
}
291+
// TODO: Add chunked encoding when no length (if err)
279292

280-
// https if specified, fallback to http in any other case
281-
if (params.protocol == 'https:') {
282-
// override default port
283-
if (!params.port) options.port = 443;
284-
request = https.request(options);
285-
} else {
286-
request = http.request(options);
287-
}
293+
// add content length
294+
request.setHeader('Content-Length', length);
288295

289296
this.pipe(request);
290297
if (cb) {
291298
request.on('error', cb);
292299
request.on('response', cb.bind(this, null));
293300
}
294-
295-
return request;
296301
}.bind(this));
302+
303+
return request;
297304
};
298305

299306
FormData.prototype._error = function(err) {

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"author": "Felix Geisendörfer <[email protected]> (http://debuggable.com/)",
33
"name": "form-data",
44
"description": "A module to create readable \"multipart/form-data\" streams. Can be used to submit forms and file uploads to other web applications.",
5-
"version": "0.1.0",
5+
"version": "0.1.1",
66
"repository": {
77
"type": "git",
88
"url": "git://github.com/felixge/node-form-data.git"
@@ -16,19 +16,19 @@
1616
},
1717
"dependencies": {
1818
"combined-stream": "~0.0.4",
19-
"mime": "~1.2.9",
19+
"mime": "~1.2.11",
2020
"async": "~0.2.9"
2121
},
2222
"licenses": [
2323
{
2424
"type": "MIT",
2525
"url": "https://raw.github.com/felixge/node-form-data/master/License"
2626
}
27-
],
27+
],
2828
"devDependencies": {
2929
"fake": "~0.2.2",
3030
"far": "~0.0.7",
3131
"formidable": "~1.0.14",
32-
"request": "~2.22.0"
32+
"request": "~2.27.0"
3333
}
3434
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
test return http request, added for issue #47:
3+
https://github.com/felixge/node-form-data/issues/47
4+
5+
Checking correct length header and request object
6+
*/
7+
8+
var common = require('../common');
9+
var assert = common.assert;
10+
var http = require('http');
11+
12+
var FormData = require(common.dir.lib + '/form_data');
13+
14+
var CRLF = '\r\n';
15+
16+
var expectedLength;
17+
18+
var dataSize = 1000000;
19+
20+
var server = http.createServer(function(req, res) {
21+
var data = '', uploaded = 0;
22+
console.log(['R', req.headers['content-length']]);
23+
assert.ok( typeof req.headers['content-length'] !== 'undefined' );
24+
assert.equal(req.headers['content-length'], expectedLength);
25+
26+
// check for uploaded body
27+
req.on('data', function(data) {
28+
uploaded += data.length;
29+
});
30+
req.on('end', function()
31+
{
32+
// compare uploaded total to the expected length
33+
assert.equal(uploaded, expectedLength);
34+
35+
res.writeHead(200);
36+
res.end('done');
37+
});
38+
39+
});
40+
41+
42+
server.listen(common.port, function() {
43+
var R, oWrite, progress = 0, form = new FormData();
44+
45+
var bufferData = [];
46+
for (var z = 0; z < dataSize; z++) {
47+
bufferData.push(1);
48+
}
49+
var buffer = new Buffer(bufferData);
50+
51+
form.append('my_buffer', buffer);
52+
53+
// (available to req handler)
54+
expectedLength = form._lastBoundary().length + form._overheadLength + dataSize;
55+
56+
R = form.submit('http://localhost:' + common.port + '/', function(err, res) {
57+
if (err) {
58+
throw err;
59+
}
60+
61+
assert.strictEqual(res.statusCode, 200);
62+
63+
// unstuck new streams
64+
res.resume();
65+
66+
server.close();
67+
68+
// compare progress total to the expected length
69+
assert.equal(progress, expectedLength);
70+
});
71+
72+
// augment into request
73+
oWrite = R.write;
74+
R.write = function(chunk) {
75+
return oWrite.call(this, chunk, function() {
76+
form.emit('progress', chunk);
77+
});
78+
};
79+
80+
// track progres
81+
form.on('progress', function(chunk) {
82+
progress += chunk.length;
83+
assert.ok(progress <= expectedLength);
84+
});
85+
86+
});

0 commit comments

Comments
 (0)