From 2f0cddd45bd0d7b56e8571e8caf329274f658289 Mon Sep 17 00:00:00 2001 From: Jari Bakken Date: Wed, 3 Dec 2014 15:22:03 +0100 Subject: [PATCH] Split app and server. No need to start the server for these tests. --- app.js | 46 +++++++++++++++++++++++++++++++++++++++++++++ server.js | 47 ++-------------------------------------------- test/specHelper.js | 6 ++++-- 3 files changed, 52 insertions(+), 47 deletions(-) create mode 100644 app.js diff --git a/app.js b/app.js new file mode 100644 index 0000000000..f26691d066 --- /dev/null +++ b/app.js @@ -0,0 +1,46 @@ +var express = require('express'), + bodyParser = require('body-parser'), + log4js = require('log4js'), + logger = require('./lib/logger'), + routes = require('./lib/routes'), + eventApi = require('./lib/eventApi'), + featureApi = require('./lib/featureApi'), + strategyApi = require('./lib/strategyApi'), + validator = require('express-validator'), + app = express(), + router = express.Router(), + baseUriPath = process.env.BASE_URI_PATH || ''; + +if(app.get('env') === 'development') { + app.use(require('errorhandler')()); + + var webpack = require('webpack'), + webpackDevMiddleware = require('webpack-dev-middleware'), + webpackConfig = require('./webpack.config'), + compiler = webpack(webpackConfig), + config = { + publicPath: '/js', + noInfo: true + }; + + app.use(baseUriPath, webpackDevMiddleware(compiler, config)); +} + +app.use(validator([])); + +app.set('trust proxy'); +app.locals.baseUriPath = baseUriPath; + +app.use(log4js.connectLogger(logger, {format: ':remote-addr :status :method :url :response-timems'})); +app.set('port', process.env.HTTP_PORT || process.env.PORT || 4242); + +app.use(baseUriPath, express.static(__dirname + '/public')); +app.use(bodyParser.json({strict: false})); + +eventApi(router); +featureApi(router); +strategyApi(router); +routes(router); +app.use(baseUriPath, router); + +module.exports = app; \ No newline at end of file diff --git a/server.js b/server.js index 1a35f864de..238ac698f7 100644 --- a/server.js +++ b/server.js @@ -1,47 +1,5 @@ -var express = require('express'), - bodyParser = require('body-parser'), - log4js = require('log4js'), - logger = require('./lib/logger'), - routes = require('./lib/routes'), - eventApi = require('./lib/eventApi'), - featureApi = require('./lib/featureApi'), - strategyApi = require('./lib/strategyApi'), - validator = require('express-validator'), - app = express(), - router = express.Router(), - baseUriPath = process.env.BASE_URI_PATH || ''; - -if(app.get('env') === 'development') { - app.use(require('errorhandler')()); - - var webpack = require('webpack'), - webpackDevMiddleware = require('webpack-dev-middleware'), - webpackConfig = require('./webpack.config'), - compiler = webpack(webpackConfig), - config = { - publicPath: '/js', - noInfo: true - }; - - app.use(baseUriPath, webpackDevMiddleware(compiler, config)); -} - -app.use(validator([])); - -app.set('trust proxy'); -app.locals.baseUriPath = baseUriPath; - -app.use(log4js.connectLogger(logger, {format: ':remote-addr :status :method :url :response-timems'})); -app.set('port', process.env.HTTP_PORT || process.env.PORT || 4242); - -app.use(baseUriPath, express.static(__dirname + '/public')); -app.use(bodyParser.json({strict: false})); - -eventApi(router); -featureApi(router); -strategyApi(router); -routes(router); -app.use(baseUriPath, router); +var app = require('./app'); +var logger = require('./lib/logger'); var server = app.listen(app.get('port'), function() { logger.info('unleash started on ' + app.get('port')); @@ -52,7 +10,6 @@ process.on('uncaughtException', function(err) { logger.error(err.stack); }); - module.exports = { app: app, server: server diff --git a/test/specHelper.js b/test/specHelper.js index 840a6f9864..0323ff1658 100644 --- a/test/specHelper.js +++ b/test/specHelper.js @@ -1,6 +1,8 @@ var request = require('supertest'); var mockery = require('mockery'); +process.env.NODE_ENV = 'test'; + var server; function setupMockServer() { @@ -14,9 +16,9 @@ function setupMockServer() { mockery.registerSubstitute('./featureDb', '../test/featureDbMock'); mockery.registerSubstitute('./strategyDb', '../test/strategyDbMock'); - server = require('../server'); + var app = require('../app'); - return request('http://localhost:' + server.app.get('port')); + return request(app); } function tearDownMockServer() {