From 500703e1fbd6e04c8ef9640feffdd5332cc2152b Mon Sep 17 00:00:00 2001 From: Ritchie Martori Date: Tue, 18 Feb 2014 11:20:23 -0800 Subject: [PATCH] Remove coercion from port check --- lib/application.js | 20 +++++++++++--------- test/app.test.js | 24 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/lib/application.js b/lib/application.js index d1c2b17e..f1f19953 100644 --- a/lib/application.js +++ b/lib/application.js @@ -7,6 +7,7 @@ var DataSource = require('loopback-datasource-juggler').DataSource , compat = require('./compat') , assert = require('assert') , fs = require('fs') + , _ = require('underscore') , RemoteObjects = require('strong-remoting') , swagger = require('strong-remoting/ext/swagger') , stringUtils = require('underscore.string') @@ -431,15 +432,16 @@ app.boot = function(options) { process.env.npm_package_config_host || app.get('host'); - appConfig.port = - process.env.npm_config_port || - process.env.OPENSHIFT_SLS_PORT || - process.env.OPENSHIFT_NODEJS_PORT || - process.env.PORT || - appConfig.port || - process.env.npm_package_config_port || - app.get('port') || - 3000; + appConfig.port = _.find([ + process.env.npm_config_port, + process.env.OPENSHIFT_SLS_PORT, + process.env.OPENSHIFT_NODEJS_PORT, + process.env.PORT, + appConfig.port, + process.env.npm_package_config_port, + app.get('port'), + 3000 + ], _.isFinite); appConfig.restApiRoot = appConfig.restApiRoot || diff --git a/test/app.test.js b/test/app.test.js index 701d22fe..b003f883 100644 --- a/test/app.test.js +++ b/test/app.test.js @@ -189,6 +189,12 @@ describe('app', function() { var app = this.boot(); assert.equal(app.get('host'), process.env.npm_config_host); + delete process.env.npm_config_host; + delete process.env.OPENSHIFT_SLS_IP; + delete process.env.OPENSHIFT_NODEJS_IP; + delete process.env.HOST; + delete process.env.npm_package_config_host; + process.env.npm_config_port = randomPort(); process.env.OPENSHIFT_SLS_PORT = randomPort(); process.env.OPENSHIFT_NODEJS_PORT = randomPort(); @@ -198,6 +204,12 @@ describe('app', function() { var app = this.boot(); assert.equal(app.get('host'), process.env.npm_config_host); assert.equal(app.get('port'), process.env.npm_config_port); + + delete process.env.npm_config_port; + delete process.env.OPENSHIFT_SLS_PORT; + delete process.env.OPENSHIFT_NODEJS_PORT; + delete process.env.PORT; + delete process.env.npm_package_config_port; }); function randomHost() { @@ -207,6 +219,18 @@ describe('app', function() { function randomPort() { return Math.floor(Math.random() * 10000); } + + it('should honor 0 for free port', function () { + var app = loopback(); + app.boot({app: {port: 0}}); + assert.equal(app.get('port'), 0); + }); + + it('should default to port 3000', function () { + var app = loopback(); + app.boot({app: {port: undefined}}); + assert.equal(app.get('port'), 3000); + }); }); it('Instantiate models', function () {