require leading slash on basePath

This commit is contained in:
spalger 2015-11-17 11:38:28 -06:00
parent 13021c6534
commit f54095600a
2 changed files with 39 additions and 1 deletions

View file

@ -0,0 +1,38 @@
import schemaProvider from '../schema';
import expect from 'expect.js';
import Joi from 'joi';
describe('Config schema', function () {
let schema;
beforeEach(() => schema = schemaProvider());
function validate(data, options) {
return Joi.validate(data, schema, options);
}
describe('server', function () {
describe('basePath', function () {
it('accepts empty strings', function () {
const { error } = validate({ server: { basePath: '' }});
expect(error == null).to.be.ok();
});
it('accepts strings with leading slashes', function () {
const { error } = validate({ server: { basePath: '/path' }});
expect(error == null).to.be.ok();
});
it('rejects strings with trailing slashes', function () {
const { error } = validate({ server: { basePath: '/path/' }});
expect(error).to.have.property('details');
expect(error.details[0]).to.have.property('path', 'server.basePath');
});
it('rejects strings without leading slashes', function () {
const { error } = validate({ server: { basePath: 'path' }});
expect(error).to.have.property('details');
expect(error.details[0]).to.have.property('path', 'server.basePath');
});
});
});
});

View file

@ -30,7 +30,7 @@ module.exports = () => Joi.object({
port: Joi.number().default(5601),
autoListen: Joi.boolean().default(true),
defaultRoute: Joi.string(),
basePath: Joi.string().default('').allow('').regex(/[^\/]$/, 'no trailing slashes'),
basePath: Joi.string().default('').allow('').regex(/(^$|^\/.*[^\/]$)/, `start with a slash, don't end with one`),
ssl: Joi.object({
cert: Joi.string(),
key: Joi.string()