Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

More setup

  • Loading branch information...
commit 71e9f83f1c8baacb6073aa5de124bddf0601c931 1 parent db0036e
@tj tj authored
Showing with 83 additions and 4 deletions.
  1. +48 −3 index.js
  2. +35 −1 test/multipart.test.js
View
51 index.js
@@ -5,8 +5,53 @@
* MIT Licensed
*/
-module.exports = function multipart(){
+/**
+ * Module dependencies.
+ */
+
+var utils = require('connect/utils'),
+ formidable = require('./support/formidable');
+
+/**
+ * Setup multipart with the given `options`.
+ *
+ * Options:
+ *
+ * - `encoding` Encoding used for incoming forms. Defaults to utf8
+ * - `uploadDir` Directory to save uploads. Defaults to "/tmp"
+ * - `keepExtensions` Include original extensions. Defaults to `false`
+ *
+ * @param {Object} options
+ * @return {Function}
+ * @api public
+ */
+
+module.exports = function multipart(options){
+ options = options || {};
return function(req, res, next){
-
+ if (multipartRequest(req)) {
+ var 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));
+ });
+ } else {
+ next();
+ }
};
-};
+};
+
+/**
+ * Check if `req` is a multipart request.
+ *
+ * @param {IncomingMessage} req
+ * @return {Boolean}
+ * @api private
+ */
+
+function multipartRequest(req){
+ return (req.method === 'POST'
+ || req.method === 'PUT')
+ && req.headers['content-type'].indexOf('multipart/form-data') >= 0;
+}
View
36 test/multipart.test.js
@@ -3,4 +3,38 @@
* Module dependencies.
*/
-var multipart = require('./../index');
+var connect = require('connect'),
+ multipart = require('./../index');
+
+// Test server
+
+var server = connect.createServer(
+ multipart(),
+ function(req, res, next){
+ next();
+ }
+);
+
+// Tests
+
+exports['test single field'] = 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: '/' },
+ { body: 'Cannot find /' });
+
+ assert.response(server,
+ { url: '/', method: 'POST', headers: headers, data: body },
+ { body: '' });
+};
Please sign in to comment.
Something went wrong with that request. Please try again.