Skip to content
Browse files

tests

  • Loading branch information...
1 parent 05664c4 commit 759fe6398d1f88455156d7410e5e5294ed802917 @chjj chjj committed
Showing with 105 additions and 99 deletions.
  1. +0 −32 lib/encoded.js
  2. +1 −55 lib/json.js
  3. +104 −12 test/index.js
View
32 lib/encoded.js
@@ -195,35 +195,3 @@ Parser.handle = function(req, res, next, options) {
*/
module.exports = Parser;
-
-/**
- * Crude Test
- */
-
-if (!module.parent) (function() {
- var assert = require('assert')
- , qs = require('querystring')
- , obj = {};
-
- var TEST_ENCODED = 'a=1&b=2&c=3&d=hello%20world&e=hi%20world&f=testing';
-
- var parser = new Parser();
-
- parser.on('value', function(key, value) {
- console.log(key, value);
- obj[key] = value;
- });
-
- parser.on('end', function() {
- console.log(obj);
- assert.deepEqual(obj, qs.parse(TEST_ENCODED), 'Not deep equal.'
- + require('util').inspect(qs.parse(TEST_ENCODED)));
- assert.equal(qs.stringify(obj), TEST_ENCODED, 'Not equal.'
- + qs.stringify(obj));
- console.log('Completed.');
- });
-
- parser.write(new Buffer(TEST_ENCODED.slice(0, 25), 'utf8'));
- parser.write(new Buffer(TEST_ENCODED.slice(25), 'utf8'));
- parser.end();
-})();
View
56 lib/json.js
@@ -463,8 +463,7 @@ var parsers = new FreeList('parsers', 20, function() {
* Expose
*/
-module.exports = exports = Parser.create;
-exports.Parser = Parser;
+module.exports = exports = Parser;
exports.middleware = function(options) {
return function(req, res, next) {
@@ -513,56 +512,3 @@ exports.handle = function(req, res, next, options) {
});
}
};
-
-/**
- * Crude Test
- */
-
-if (!module.parent) (function() {
- var assert = require('assert');
- var TEST_JSON = '{"a":{"b":100,"c":[2,3]},"d":["e"],"f":true}';
-
- var parser = Parser.create();
-
- parser.on('end', function(obj) {
- console.log(obj);
- assert.deepEqual(obj, JSON.parse(TEST_JSON), 'Not deep equal.');
- assert.equal(JSON.stringify(obj), TEST_JSON, 'Not equal.');
- console.log('Completed.');
- });
-
- var emit = parser.emit;
- parser.emit = function(type, val) {
- console.log(type, val);
- return emit.apply(this, arguments);
- };
-
- parser.write(TEST_JSON.slice(0, 25));
- parser.write(TEST_JSON.slice(25));
- parser.end();
-
- /*
- (function emit(a, b) {
- parser.emit(a, b);
- return emit;
- })
- ('object start')
- ('object key', 'hello')
- ('object start')
- ('object key', 'world')
- ('number', 100)
- ('object key', 'test')
- ('array start')
- ('number', 2)
- ('number', 3)
- ('array end')
- ('object end')
- ('object key', 'hi')
- ('array start')
- ('string', 'an array!')
- ('array end')
- ('object end')
- ('end', parser.data);
- */
-})();
-
View
116 test/index.js
@@ -19,8 +19,8 @@ parted.root = files;
// create a mock request
var request = function(size, file) {
file = __dirname + '/' + file + '.part';
- var stream = fs.createReadStream(file, {
- bufferSize: size
+ var stream = fs.createReadStream(file, {
+ bufferSize: size
});
var boundary = fs
.readFileSync(file)
@@ -75,9 +75,9 @@ var message = function(size, file, func) {
assert.ok(!!parts, 'No parts.');
assert.ok(!!parts.content, 'No file path.');
- assert.ok(parts.text === 'hello',
+ assert.ok(parts.text === 'hello',
'Bad text. Got: ' + parts.text);
- assert.ok(parts.hello === expect_text[file],
+ assert.ok(parts.hello === expect_text[file],
'Bad text. Got: ' + parts.hello);
var got = fs.readFileSync(parts.content)
@@ -85,9 +85,9 @@ var message = function(size, file, func) {
, i = 0
, len = expect.length;
- assert.ok(got.length === len,
- 'Sizes not equal.'
- + ' Expected: ' + len
+ assert.ok(got.length === len,
+ 'Sizes not equal.'
+ + ' Expected: ' + len
+ '. Got: ' + got.length + '.'
);
@@ -117,10 +117,102 @@ var multiple = function(file, func) {
}
};
-multiple('chrome', function() {
- multiple('opera', function() {
- multiple('firefox', function() {
- console.log('DONE');
+var main = function(argv) {
+ if (!argv.length || ~argv.indexOf('--multipart')) {
+ multiple('chrome', function() {
+ multiple('opera', function() {
+ multiple('firefox', function() {
+ console.log('DONE');
+ });
+ });
});
+ }
+
+ if (~argv.indexOf('--json')) {
+ json();
+ }
+
+ if (~argv.indexOf('--encoded')) {
+ encoded();
+ }
+};
+
+var json = function() {
+ var assert = require('assert');
+ var TEST_JSON = '{"a":{"b":100,"c":[2,3]},"d":["e"],"f":true}';
+ var Parser = require('../lib/json');
+
+ var parser = Parser.create();
+
+ parser.on('end', function(obj) {
+ console.log(obj);
+ assert.deepEqual(obj, JSON.parse(TEST_JSON), 'Not deep equal.');
+ assert.equal(JSON.stringify(obj), TEST_JSON, 'Not equal.');
+ console.log('Completed.');
});
-});
+
+ var emit = parser.emit;
+ parser.emit = function(type, val) {
+ console.log(type, val);
+ return emit.apply(this, arguments);
+ };
+
+ parser.write(TEST_JSON.slice(0, 25));
+ parser.write(TEST_JSON.slice(25));
+ parser.end();
+
+ /*
+ (function emit(a, b) {
+ parser.emit(a, b);
+ return emit;
+ })
+ ('object start')
+ ('object key', 'hello')
+ ('object start')
+ ('object key', 'world')
+ ('number', 100)
+ ('object key', 'test')
+ ('array start')
+ ('number', 2)
+ ('number', 3)
+ ('array end')
+ ('object end')
+ ('object key', 'hi')
+ ('array start')
+ ('string', 'an array!')
+ ('array end')
+ ('object end')
+ ('end', parser.data);
+ */
+};
+
+var encoded = function() {
+ var assert = require('assert')
+ , qs = require('querystring')
+ , obj = {}
+ , Parser = require('../lib/encoded');
+
+ var TEST_ENCODED = 'a=1&b=2&c=3&d=hello%20world&e=hi%20world&f=testing';
+
+ var parser = new Parser();
+
+ parser.on('value', function(key, value) {
+ console.log(key, value);
+ obj[key] = value;
+ });
+
+ parser.on('end', function() {
+ console.log(obj);
+ assert.deepEqual(obj, qs.parse(TEST_ENCODED), 'Not deep equal.'
+ + require('util').inspect(qs.parse(TEST_ENCODED)));
+ assert.equal(qs.stringify(obj), TEST_ENCODED, 'Not equal.'
+ + qs.stringify(obj));
+ console.log('Completed.');
+ });
+
+ parser.write(new Buffer(TEST_ENCODED.slice(0, 25), 'utf8'));
+ parser.write(new Buffer(TEST_ENCODED.slice(25), 'utf8'));
+ parser.end();
+};
+
+main(process.argv.slice(2));

0 comments on commit 759fe63

Please sign in to comment.
Something went wrong with that request. Please try again.