Permalink
Browse files

Added more tests

  • Loading branch information...
1 parent 71e9f83 commit 7108751d38b899c1a9f227766418d30b6aa25a7f @tj tj committed Jun 29, 2010
Showing with 84 additions and 8 deletions.
  1. +6 −5 index.js
  2. +78 −3 test/multipart.test.js
View
@@ -26,16 +26,17 @@ var utils = require('connect/utils'),
* @api public
*/
+// TODO: urlencoded too
+
module.exports = function multipart(options){
options = options || {};
return function(req, res, next){
if (multipartRequest(req)) {
- var form = new formidable.IncomingForm;
+ var form = req.form = new formidable.IncomingForm;
utils.merge(form, options);
- form.parse(req, function(fields, files){
- require('sys').puts(require('sys').inspect(fields));
- require('sys').puts(require('sys').inspect(files));
- });
+ form.onComplete = function(){};
+ next();
+ form.parse(req, form.onComplete);
} else {
next();
}
View
@@ -11,13 +11,23 @@ var connect = require('connect'),
var server = connect.createServer(
multipart(),
function(req, res, next){
- next();
+ if (req.form) {
+ req.form.onComplete = function(err, fields, files){
+ res.writeHead(200, {});
+ if (err) res.write(JSON.stringify(err.message));
+ res.write(JSON.stringify(fields));
+ res.write(JSON.stringify(files));
+ res.end();
+ };
+ } else {
+ next();
+ }
}
);
// Tests
-exports['test single field'] = function(assert){
+exports['test single multipart field'] = function(assert){
var headers = {
'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundaryMfJEpQcCbybb6A8U'
};
@@ -36,5 +46,70 @@ exports['test single field'] = function(assert){
assert.response(server,
{ url: '/', method: 'POST', headers: headers, data: body },
- { body: '' });
+ { body: '{"name":"foo"}{}' });
+};
+
+exports['test several multipart fields'] = function(assert){
+ var headers = {
+ 'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundaryMfJEpQcCbybb6A8U'
+ };
+
+ var body = [
+ '------WebKitFormBoundaryMfJEpQcCbybb6A8U',
+ 'Content-Disposition: form-data; name="name"',
+ '',
+ 'foo',
+ '------WebKitFormBoundaryMfJEpQcCbybb6A8U',
+ 'Content-Disposition: form-data: name="comments"',
+ '',
+ 'foo bar baz',
+ '------WebKitFormBoundaryMfJEpQcCbybb6A8U--\r\n'
+ ].join('\r\n');
+
+ assert.response(server,
+ { url: '/', method: 'POST', headers: headers, data: body },
+ { body: '{"name":"foo","comments":"foo bar baz"}{}' });
+};
+
+exports['test malformed multipart'] = function(assert){
+ var headers = {
+ 'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundaryMfJEpQcCbybb6A8U'
+ };
+
+ var body = [
+ '--WebKitFormBoundaryMfJEpQcCbybb6A8U',
+ 'Content-Disposition: form-data; name="name"',
+ '',
+ 'foo',
+ '------WebKitFormBoundaryMfJEpQcCbybb6A8U--\r\n'
+ ].join('\r\n');
+
+ assert.response(server,
+ { url: '/', method: 'POST', headers: headers, data: body },
+ { body: '"parser error, 2 of 134 bytes parsed"{}{}' });
+};
+
+exports['test multipart files'] = function(assert){
+ var headers = {
+ 'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundaryMfJEpQcCbybb6A8U'
+ };
+
+ var body = [
+ '------WebKitFormBoundaryMfJEpQcCbybb6A8U',
+ 'Content-Disposition: form-data; name="name"',
+ '',
+ 'foo',
+ '------WebKitFormBoundaryMfJEpQcCbybb6A8U',
+ 'Content-Disposition: form-data: name="text"; filename="foo.txt"',
+ '',
+ 'foo\nbar\nbaz\n',
+ '------WebKitFormBoundaryMfJEpQcCbybb6A8U--\r\n'
+ ].join('\r\n');
+
+ assert.response(server,
+ { url: '/', method: 'POST', headers: headers, data: body },
+ function(res){
+ assert.ok(res.body.indexOf('{"name":"foo"}') >= 0, 'Test field');
+ assert.ok(res.body.indexOf('{"path":"/tmp') >= 0, 'Test file path');
+ });
};

0 comments on commit 7108751

Please sign in to comment.