Permalink
Browse files

Merge pull request #54 from alFReD-NSH/master

Conflicts:
	lib/director/http/index.js
  • Loading branch information...
2 parents 84e283c + bde6163 commit 02292da66d031cc233847f37586c59a7e5081782 @pksunkara pksunkara committed Apr 2, 2012
Showing with 110 additions and 5 deletions.
  1. +6 −5 lib/director/http/index.js
  2. +1 −0 lib/director/router.js
  3. +103 −0 test/server/http/bodyParsing-test.js
View
@@ -123,11 +123,12 @@ Router.prototype.dispatch = function (req, res, callback) {
if (!stream) {
- req.chunks = [];
-
- req.on('data', function (data) {
- req.chunks.push(data.toString());
- });
+ if (this.parseBody) {
+ req.chunks = [];
+ req.on('data', function (data) {
+ req.chunks.push(data.toString());
+ });
+ }
process.nextTick(function () {
//
View
@@ -143,6 +143,7 @@ Router.prototype.configure = function (options) {
this.strict = typeof options.strict === 'undefined' ? true : options.strict;
this.notfound = options.notfound;
this.resource = options.resource;
+ this.parseBody = typeof options.parseBody === 'undefined' ? true : options.parseBody;
//
// TODO: Global once
@@ -0,0 +1,103 @@
+// Body Parsing Tests
+
+var assert = require('assert'),
+http = require('http'),
+vows = require('vows'),
+request = require('request'),
+director = require('../../../lib/director');
+
+function helloWorld(id) {
+ this.res.writeHead(200, {
+ 'Content-Type': 'text/plain'
+ });
+ this.res.end(JSON.stringify(this.req.body));
+}
+
+var bufferBody;
+
+function createServer (router) {
+ return http.createServer(function (req, res) {
+ req.body = '';
+ req.setEncoding('utf8');
+ router.dispatch(req, res, function (err) {
+ if (err) {
+ res.writeHead(404);
+ res.end();
+ }
+ });
+ });
+}
+
+
+vows.describe('director/server/http/bodyParsing').addBatch({
+ "An instance of director.http.Router": {
+ "instantiated with a Routing table with body parsing": {
+ topic: new director.http.Router({
+ '/': {
+ get: helloWorld
+ }
+ }),
+ "should have the correct routes defined": function (router) {
+ assert.isObject(router.routes);
+ assert.isFunction(router.routes.get);
+ },
+ "when passed to an http.Server instance": {
+ topic: function (router) {
+ var server = createServer(router),
+ that = this;
+ server.listen(9190, this.callback);
+ },
+ "a request to /": {
+ topic: function () {
+ request({
+ uri: 'http://localhost:9190/',
+ json : {
+ me:'I am the body'
+ }
+ }, this.callback);
+ },
+ "should respond with Object": function (err, res, body) {
+ assert.isNull(err);
+ assert.equal(res.statusCode, 200);
+ assert.equal(JSON.stringify(body), '{"me":"I am the body"}');
+ }
+ }
+ }
+ },
+ "instantiated with a Routing table without body parsing": {
+ topic: new director.http.Router({
+ '/': {
+ get: helloWorld
+ }
+ }).configure({
+ parseBody : false
+ }),
+ "should have the correct routes defined": function (router) {
+ assert.isObject(router.routes);
+ assert.isFunction(router.routes.get);
+ },
+ "when passed to an http.Server instance": {
+ topic: function (router) {
+ var server = createServer(router),
+ that = this;
+ server.listen(9191, this.callback);
+ },
+ "a request to /": {
+ topic: function () {
+ request({
+ uri: 'http://localhost:9191/',
+ json : {
+ me:'I am the body'
+ }
+ }, this.callback);
+ },
+ "should respond with string": function (err, res, body) {
+ assert.isNull(err);
+ assert.equal(res.statusCode, 200);
+ assert.equal(JSON.stringify(body), '{}');
+ }
+ }
+ }
+ }
+ }
+}).export(module);

0 comments on commit 02292da

Please sign in to comment.