Permalink
Browse files

update App.prototype.start to be able to take an object parameter spe…

…cifying host, port, and other options or to take the node server style port, host parameters
  • Loading branch information...
1 parent 92eedc2 commit 0ba19b48407c6db457340256bb46e41f42114446 @nrstott committed Feb 5, 2013
Showing with 122 additions and 2 deletions.
  1. +14 −2 lib/bogart.js
  2. +108 −0 test/app.test.js
View
@@ -112,8 +112,20 @@ App.prototype.use = function(middleware /*, parameters */) {
this.emit('afterAddMiddleware', this, middleware);
};
-App.prototype.start = function(port, host) {
- var app, first;
+App.prototype.start = function(port, host, jsgiOpts) {
+ var app
+ , first
+ , args = Array.prototype.slice.call(arguments);
+
+ jsgiOpts = args.shift();
+ if (typeof jsgiOpts === 'object') {
+ port = jsgiOpts.port || 8080;
+ host = jsgiOpts.host || '127.0.0.1';
+ } else {
+ port = jsgiOpts;
+ host = args.shift();
+ jsgiOpts = args.shift() || {};
+ }
if (host === undefined) {
host = port;
View
@@ -0,0 +1,108 @@
+var bogart = require('../lib/bogart')
+ , Q = require('promised-io/promise')
+ , when = Q.when
+ , test = require('tap').test
+ , plan = require('tap').plan
+ , sinon = require('sinon');
+
+test('given a new app with a router to be started with no parameters', function(t) {
+ var app = bogart.app();
+
+ t.test('should not be started', function(t) {
+ t.equal(app.started, false);
+ t.plan(1);
+ });
+
+ t.test('should have use method', function(t) {
+ t.ok(typeof app.use === 'function');
+ t.plan(1);
+ });
+
+ t.test('should raise afterAddMiddleware with correct parameter', function(t) {
+ var router = bogart.router()
+ , appParam
+ , middlewareParam;
+
+ app.on('afterAddMiddleware', function(app, middleware) {
+ appParam = app;
+ middlewareParam = middleware;
+ });
+
+ app.use(router);
+
+ t.equal(appParam, app);
+ t.equal(middlewareParam, router);
+
+ t.plan(2);
+ });
+
+ t.test('starting the app with no parameters', function(t) {
+ var server = {}
+ , jsgiOptsFromSpy
+ , bogartStartSpy = sinon.stub(bogart, 'start', function(jsgiApp, opts) {
+ jsgiOptsFromSpy = opts;
+ return server;
+ });
+
+ app.start();
+
+ t.test('should be started', function(t) {
+ t.equal(app.started, true);
+ t.plan(1);
+ });
+
+ t.test('should have started with correct jsgi options parameter', function(t) {
+ t.equal(jsgiOptsFromSpy.port, 8080);
+ t.equal(jsgiOptsFromSpy.host, '127.0.0.1');
+ t.plan(2);
+ });
+
+ bogart.start.restore();
+ });
+});
+
+test('given a new app with a router to be started with object parameter', function(t) {
+ var app = bogart.app();
+
+ t.equal(app.started, false);
+
+ t.test('should raise afterAddMiddleware with correct parameter', function(t) {
+ var router = bogart.router()
+ , appParam
+ , middlewareParam;
+
+ app.on('afterAddMiddleware', function(app, middleware) {
+ appParam = app;
+ middlewareParam = middleware;
+ });
+
+ app.use(router);
+
+ t.equal(appParam, app);
+ t.equal(middlewareParam, router);
+
+ t.end();
+ });
+
+ t.test('starting the app', function(t) {
+ var opts = { port: 123, host: 'localhost' }
+ , server = {}
+ , jsgiOptsFromSpy
+ , bogartStartSpy = sinon.stub(bogart, 'start', function(jsgiApp, opts) {
+ jsgiOptsFromSpy = opts;
+ return server;
+ });
+
+ app.start(opts);
+
+ t.equal(app.started, true);
+
+ t.test('should have started with correct jsgi options parameter', function(t) {
+ t.equal(jsgiOptsFromSpy.port, 123);
+ t.equal(jsgiOptsFromSpy.host, 'localhost');
+ t.end();
+ });
+
+ t.end();
+ });
+});

0 comments on commit 0ba19b4

Please sign in to comment.