Merge pull request #152 from strongloop/bug/add-config-var-checks

Add config variable checks
This commit is contained in:
Simon Ho 2015-08-31 20:15:57 -07:00
commit 7cbb8f6b40
2 changed files with 46 additions and 3 deletions

View File

@ -322,7 +322,6 @@ function setupMiddleware(app, instructions) {
}
function getUpdatedConfigObject(app, config) {
var DYNAMIC_CONFIG_PARAM = /\$\{(\w+)\}$/;
function getConfigVariable(param) {
@ -341,11 +340,24 @@ function getUpdatedConfigObject(app, config) {
}
function interpolateVariables(config) {
// config is a string and contains a config variable ('${var}')
if (typeof config === 'string')
return getConfigVariable(config);
// anything but an array or object
if (typeof config !== 'object' || config == null)
return config;
// recurse into array elements
if (Array.isArray(config))
return config.map(interpolateVariables);
// recurse into object props
var interpolated = {};
Object.keys(config).forEach(function(configKey) {
var value = config[configKey];
if (Array.isArray(value)) {
interpolated[configKey] = value.map(getConfigVariable);
interpolated[configKey] = value.map(interpolateVariables);
} else if (typeof value === 'string') {
interpolated[configKey] = getConfigVariable(value);
} else if (typeof value === 'object' && Object.keys(value).length) {

View File

@ -1,3 +1,4 @@
var async = require('async');
var boot = require('../');
var path = require('path');
var loopback = require('loopback');
@ -426,7 +427,6 @@ describe('executor', function() {
});
describe('with middleware.json', function() {
it('should parse a simple config variable', function(done) {
boot.execute(app, simpleMiddlewareConfig('routes',
{ path: '${restApiRoot}' }
@ -494,6 +494,37 @@ describe('executor', function() {
});
});
it('should not parse invalid config variables', function(done) {
var invalidDataTypes = [undefined, function() {}];
async.each(invalidDataTypes, function(invalidDataType, cb) {
var config = simpleMiddlewareConfig('routes', {
path: invalidDataType
});
boot.execute(app, config);
supertest(app)
.get('/')
.end(function(err, res) {
expect(err).to.be.null();
expect(res.body.path).to.be.undefined();
cb();
});
}, done);
});
it('should parse valid config variables', function(done) {
var config = simpleMiddlewareConfig('routes', {
props: ['a', '${vVar}', 1, true, function() {}, {x:1, y: '${y}'}]
});
boot.execute(app, config);
supertest(app)
.get('/')
.end(function(err, res) {
expect(err).to.be.null();
done();
});
});
});
describe('with component-config.json', function() {