From 722bb07544b0a699c471c03984ac5b6bd509c132 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 22 Sep 2017 15:44:38 -0500 Subject: [PATCH 1/5] split into files --- index.js | 141 +---------------------------------------- lib/defaults.js | 7 ++ lib/getCompletePath.js | 44 +++++++++++++ lib/routeLoader.js | 94 +++++++++++++++++++++++++++ 4 files changed, 146 insertions(+), 140 deletions(-) create mode 100644 lib/defaults.js create mode 100644 lib/getCompletePath.js create mode 100644 lib/routeLoader.js diff --git a/index.js b/index.js index 64680de..46d934e 100644 --- a/index.js +++ b/index.js @@ -1,150 +1,11 @@ 'use strict'; -const fs = require('fs'); -const glob = require('glob'); -const path = require('path'); -const _ = require('lodash'); -const async = require('async'); -const defaults = { - path: `${process.cwd()}/routes`, - base: '/', - prefix: '', - verbose: false, - autoLoad: true -}; +exports.routeLoader = require('./lib/routeLoader'); exports.register = (server, options, next) => { exports.routeLoader(server, options, next, true); }; -// the 'base' of the path for all routes defined in a given file. -// eg "/api/folder1/myfile.js" will return "/api/folder/myfile/", -const getRoutePathBase = (options, fileName) => { - // gets relative path, minus absolute path specifier: - const segment = path.dirname(fileName.replace(options.path, '')); - let base = options.base ? options.base : defaults.base; - if (segment === '.' || !segment) { - return base; - } - if (!_.endsWith(options.base, '/')) { - base += '/'; - } - return `${base}${segment}`; -}; - -// get the full route path: -const getCompletePath = (options, fileName, originalPath) => { - const prefix = options.prefix ? options.prefix : defaults.prefix; - // if the originalPath started with a slash just return it, there is no basePath: - if (_.startsWith(originalPath, '/')) { - let resultPath = `${prefix}${originalPath}`; - if (_.endsWith(resultPath, '/') && resultPath !== '/') { - resultPath = resultPath.substr(0, resultPath.length - 1); - } - - return resultPath; - } - // otherwise add the basePath to the returnPath: - const basePath = getRoutePathBase(options, fileName); - let returnPath = path.join(basePath, originalPath ? originalPath : '').replace(new RegExp('(\\\\)', 'g'), '/'); - returnPath = `${prefix}${returnPath}`; - // if there's a trailing slash, make sure it should be there: - if (_.endsWith(returnPath, '/') && (!_.endsWith(basePath, '/') || basePath === '/') && returnPath !== '/') { - returnPath = returnPath.substr(0, returnPath.length - 1); - } - if (_.startsWith(returnPath, '//')) { - returnPath = returnPath.replace('//', '/'); - } - return returnPath; -}; - -exports.routeLoader = (server, options, next) => { - const load = (loadOptions, loadDone) => { - const stub = () => {}; - loadDone = loadDone || stub; - const settings = _.clone(loadOptions); - _.defaults(settings, defaults); - // the main flow is here: - async.auto({ - // confirm that settings.path exists and is a directory: - confirmDirectoryExists: (done) => { - fs.exists(settings.path, (exists) => { - if (!exists) { - server.log(['hapi-route-loader', 'warning'], { message: 'path doesnt exist', path: settings.path }); - return done(); - } - fs.stat(settings.path, (err, stat) => { - if (err) { - return done(err); - } - if (!stat.isDirectory()) { - server.log(['hapi-route-loader', 'warning'], { message: 'path not a directory', path: settings.path }); - return done(`path ${settings.path} not a directory`); - } - return done(); - }); - }); - }, - // get the list of all matching route-containing files - files: ['confirmDirectoryExists', (results, done) => { - glob('**/*.js', { - cwd: settings.path - }, (globErr, files) => { - if (globErr) { - return done(globErr); - } - done(null, files); - }); - }], - // for each filename, get a list of configured routes defined by it - configureAllRoutes: ['files', (results, done) => { - const routeConfigs = {}; - results.files.forEach((fileName) => { - const fileRouteList = []; - const moduleRoutes = require(path.join(settings.path, fileName)); - _.forIn(moduleRoutes, (originalRouteConfig) => { - const processedRouteConfig = _.clone(originalRouteConfig); - // set up the route's 'config' option: - if (options.routeConfig) { - if (originalRouteConfig.config) { - processedRouteConfig.config = _.defaults(options.routeConfig, originalRouteConfig.config); - } else { - processedRouteConfig.config = options.routeConfig; - } - } - // set up the route's 'path' option: - processedRouteConfig.path = getCompletePath(options, fileName, originalRouteConfig.path); - fileRouteList.push(processedRouteConfig); - }); - routeConfigs[fileName] = fileRouteList; - }); - done(null, routeConfigs); - }], - // register all routes with server.route: - registerAllRoutes: ['configureAllRoutes', (results, done) => { - Object.keys(results.configureAllRoutes).forEach((fileName) => { - results.configureAllRoutes[fileName].forEach((routeConfig) => { - if (options.verbose) { - server.log(['debug', 'hapi-route-loader'], { msg: 'registering', data: routeConfig }); - } - server.route(routeConfig); - }); - }); - done(); - }] - }, (err2) => { - if (err2) { - server.log(['hapi-route-loader', 'error'], err2); - } - return loadDone(err2); - }); - }; - if (options.autoLoad === false) { - return next(); - } - load(options, next); -}; - exports.register.attributes = { pkg: require('./package.json') }; diff --git a/lib/defaults.js b/lib/defaults.js new file mode 100644 index 0000000..8d435c8 --- /dev/null +++ b/lib/defaults.js @@ -0,0 +1,7 @@ +module.exports = { + path: `${process.cwd()}/routes`, + base: '/', + prefix: '', + verbose: false, + autoLoad: true +}; diff --git a/lib/getCompletePath.js b/lib/getCompletePath.js new file mode 100644 index 0000000..1c9c24e --- /dev/null +++ b/lib/getCompletePath.js @@ -0,0 +1,44 @@ +const path = require('path'); +const _ = require('lodash'); +const defaults = require('./defaults'); + +// the 'base' of the path for all routes defined in a given file. +// eg "/api/folder1/myfile.js" will return "/api/folder/myfile/", +const getRoutePathBase = (options, fileName) => { + // gets relative path, minus absolute path specifier: + const segment = path.dirname(fileName.replace(options.path, '')); + let base = options.base ? options.base : defaults.base; + if (segment === '.' || !segment) { + return base; + } + if (!_.endsWith(options.base, '/')) { + base += '/'; + } + return `${base}${segment}`; +}; + +// get the full route path: +module.exports = (options, fileName, originalPath) => { + const prefix = options.prefix ? options.prefix : defaults.prefix; + // if the originalPath started with a slash just return it, there is no basePath: + if (_.startsWith(originalPath, '/')) { + let resultPath = `${prefix}${originalPath}`; + if (_.endsWith(resultPath, '/') && resultPath !== '/') { + resultPath = resultPath.substr(0, resultPath.length - 1); + } + + return resultPath; + } + // otherwise add the basePath to the returnPath: + const basePath = getRoutePathBase(options, fileName); + let returnPath = path.join(basePath, originalPath || '').replace(new RegExp('(\\\\)', 'g'), '/'); + returnPath = `${prefix}${returnPath}`; + // if there's a trailing slash, make sure it should be there: + if (_.endsWith(returnPath, '/') && (!_.endsWith(basePath, '/') || basePath === '/') && returnPath !== '/') { + returnPath = returnPath.substr(0, returnPath.length - 1); + } + if (_.startsWith(returnPath, '//')) { + returnPath = returnPath.replace('//', '/'); + } + return returnPath; +}; diff --git a/lib/routeLoader.js b/lib/routeLoader.js new file mode 100644 index 0000000..81e50cd --- /dev/null +++ b/lib/routeLoader.js @@ -0,0 +1,94 @@ +const fs = require('fs'); +const glob = require('glob'); +const path = require('path'); +const _ = require('lodash'); +const async = require('async'); +const defaults = require('./defaults'); +const getCompletePath = require('./getCompletePath'); + +module.exports = (server, options, next) => { + const load = (loadOptions, loadDone) => { + const stub = () => {}; + loadDone = loadDone || stub; + const settings = _.clone(loadOptions); + _.defaults(settings, defaults); + // the main flow is here: + async.autoInject({ + // confirm that settings.path exists and is a directory: + confirmDirectoryExists(done) { + fs.exists(settings.path, (exists) => { + if (!exists) { + server.log(['hapi-route-loader', 'warning'], { message: 'path doesnt exist', path: settings.path }); + return done(); + } + fs.stat(settings.path, (err, stat) => { + if (err) { + return done(err); + } + if (!stat.isDirectory()) { + server.log(['hapi-route-loader', 'warning'], { message: 'path not a directory', path: settings.path }); + return done(`path ${settings.path} not a directory`); + } + return done(); + }); + }); + }, + // get the list of all matching route-containing files + files(confirmDirectoryExists, done) { + glob('**/*.js', { + cwd: settings.path + }, (globErr, files) => { + if (globErr) { + return done(globErr); + } + done(null, files); + }); + }, + // for each filename, get a list of configured routes defined by it + configureAllRoutes(files, done) { + const routeConfigs = {}; + files.forEach((fileName) => { + const fileRouteList = []; + const moduleRoutes = require(path.join(settings.path, fileName)); + _.forIn(moduleRoutes, (originalRouteConfig) => { + const processedRouteConfig = _.clone(originalRouteConfig); + // set up the route's 'config' option: + if (options.routeConfig) { + if (originalRouteConfig.config) { + processedRouteConfig.config = _.defaults(options.routeConfig, originalRouteConfig.config); + } else { + processedRouteConfig.config = options.routeConfig; + } + } + // set up the route's 'path' option: + processedRouteConfig.path = getCompletePath(options, fileName, originalRouteConfig.path); + fileRouteList.push(processedRouteConfig); + }); + routeConfigs[fileName] = fileRouteList; + }); + done(null, routeConfigs); + }, + // register all routes with server.route: + registerAllRoutes: (configureAllRoutes, done) => { + Object.keys(configureAllRoutes).forEach((fileName) => { + configureAllRoutes[fileName].forEach((routeConfig) => { + if (options.verbose) { + server.log(['debug', 'hapi-route-loader'], { msg: 'registering', data: routeConfig }); + } + server.route(routeConfig); + }); + }); + done(); + } + }, (err2) => { + if (err2) { + server.log(['hapi-route-loader', 'error'], err2); + } + return loadDone(err2); + }); + }; + if (options.autoLoad === false) { + return next(); + } + load(options, next); +}; From 2d62022b07623117bc9908b2ed2f97fbe6c36976 Mon Sep 17 00:00:00 2001 From: Chris Date: Fri, 22 Sep 2017 15:50:37 -0500 Subject: [PATCH 2/5] travis node 8 and 6 --- .travis.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index b9879cc..c576a02 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,4 @@ language: node_js node_js: + - "8" - "6" - - "5" - - "4" From 78659f39d7b74025aabd23eab3f54b4046de370e Mon Sep 17 00:00:00 2001 From: orthagonal Date: Thu, 5 Oct 2017 16:17:33 -0500 Subject: [PATCH 3/5] remove lodash.ends/startsWith --- lib/getCompletePath.js | 25 +++++-------------------- lib/getRoutePathBase.js | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 20 deletions(-) create mode 100644 lib/getRoutePathBase.js diff --git a/lib/getCompletePath.js b/lib/getCompletePath.js index 1c9c24e..92ab64f 100644 --- a/lib/getCompletePath.js +++ b/lib/getCompletePath.js @@ -1,29 +1,14 @@ const path = require('path'); -const _ = require('lodash'); const defaults = require('./defaults'); - -// the 'base' of the path for all routes defined in a given file. -// eg "/api/folder1/myfile.js" will return "/api/folder/myfile/", -const getRoutePathBase = (options, fileName) => { - // gets relative path, minus absolute path specifier: - const segment = path.dirname(fileName.replace(options.path, '')); - let base = options.base ? options.base : defaults.base; - if (segment === '.' || !segment) { - return base; - } - if (!_.endsWith(options.base, '/')) { - base += '/'; - } - return `${base}${segment}`; -}; +const getRoutePathBase = require('./getRoutePathBase'); // get the full route path: module.exports = (options, fileName, originalPath) => { const prefix = options.prefix ? options.prefix : defaults.prefix; // if the originalPath started with a slash just return it, there is no basePath: - if (_.startsWith(originalPath, '/')) { + if (originalPath.startsWith('/')) { let resultPath = `${prefix}${originalPath}`; - if (_.endsWith(resultPath, '/') && resultPath !== '/') { + if (resultPath.endsWith('/') && resultPath !== '/') { resultPath = resultPath.substr(0, resultPath.length - 1); } @@ -34,10 +19,10 @@ module.exports = (options, fileName, originalPath) => { let returnPath = path.join(basePath, originalPath || '').replace(new RegExp('(\\\\)', 'g'), '/'); returnPath = `${prefix}${returnPath}`; // if there's a trailing slash, make sure it should be there: - if (_.endsWith(returnPath, '/') && (!_.endsWith(basePath, '/') || basePath === '/') && returnPath !== '/') { + if (returnPath.endsWith('/') && (basePath.endsWith('/') || basePath === '/') && returnPath !== '/') { returnPath = returnPath.substr(0, returnPath.length - 1); } - if (_.startsWith(returnPath, '//')) { + if (returnPath.endsWith('//')) { returnPath = returnPath.replace('//', '/'); } return returnPath; diff --git a/lib/getRoutePathBase.js b/lib/getRoutePathBase.js new file mode 100644 index 0000000..d986c4b --- /dev/null +++ b/lib/getRoutePathBase.js @@ -0,0 +1,19 @@ +const path = require('path'); +const defaults = require('./defaults'); + +// the 'base' of the path for all routes defined in a given file. +// eg "/api/folder1/myfile.js" will return "/api/folder/myfile/", +const getRoutePathBase = (options, fileName) => { + // gets relative path, minus absolute path specifier: + const segment = path.dirname(fileName.replace(options.path, '')); + let base = options.base ? options.base : defaults.base; + if (segment === '.' || !segment) { + return base; + } + if (options.base.endsWith('/')) { + base += '/'; + } + return `${base}${segment}`; +}; + +module.exports = getRoutePathBase; From 83f215d444056c2e42e8cf6de94a4b7f2e489089 Mon Sep 17 00:00:00 2001 From: orthagonal Date: Fri, 6 Oct 2017 16:11:50 -0500 Subject: [PATCH 4/5] updates --- lib/configureRoute.js | 16 +++++ lib/getRoutePathBase.js | 2 +- lib/getRoutesFromFiles.js | 18 ++++++ lib/routeLoader.js | 81 +++++-------------------- lib/validateRoutesDirectory.js | 22 +++++++ package.json | 3 +- test/route.methods.js | 107 +++++++++++++++++++++++++++++++++ 7 files changed, 179 insertions(+), 70 deletions(-) create mode 100644 lib/configureRoute.js create mode 100644 lib/getRoutesFromFiles.js create mode 100644 lib/validateRoutesDirectory.js create mode 100644 test/route.methods.js diff --git a/lib/configureRoute.js b/lib/configureRoute.js new file mode 100644 index 0000000..04b25b4 --- /dev/null +++ b/lib/configureRoute.js @@ -0,0 +1,16 @@ +const getCompletePath = require('./getCompletePath'); + +module.exports = (settings, fileName, routeConfig) => { + const finalRouteConfig = Object.assign({}, routeConfig); + // set up the route's 'config' option: + if (settings.routeConfig) { + if (routeConfig.config) { + finalRouteConfig.config = Object.assign({}, routeConfig.config, settings.routeConfig); + } else { + finalRouteConfig.config = settings.routeConfig; + } + } + // set up the route's 'path' option: + finalRouteConfig.path = getCompletePath(settings, fileName, routeConfig.path); + return finalRouteConfig; +}; diff --git a/lib/getRoutePathBase.js b/lib/getRoutePathBase.js index 6484f93..ca90bec 100644 --- a/lib/getRoutePathBase.js +++ b/lib/getRoutePathBase.js @@ -10,7 +10,7 @@ const getRoutePathBase = (options, fileName) => { if (segment === '.' || !segment) { return base; } - if (options.base && options.base.endsWith('/')) { + if (options.base && !options.base.endsWith('/')) { base += '/'; } return `${base}${segment}`; diff --git a/lib/getRoutesFromFiles.js b/lib/getRoutesFromFiles.js new file mode 100644 index 0000000..b62f380 --- /dev/null +++ b/lib/getRoutesFromFiles.js @@ -0,0 +1,18 @@ +'use strict'; +const path = require('path'); +const configureRoute = require('./configureRoute'); + +module.exports = (server, settings, files, done) => { + const configuredRoutes = []; + files.forEach((fileName) => { + const moduleRoutes = require(path.join(settings.path, fileName)); + Object.keys(moduleRoutes).forEach((originalRouteConfigKey) => { + let originalRouteConfig = moduleRoutes[originalRouteConfigKey]; + if (typeof originalRouteConfig === 'function') { + originalRouteConfig = originalRouteConfig(server, settings); + } + configuredRoutes.push(configureRoute(settings, fileName, originalRouteConfig)); + }); + }); + return done(null, configuredRoutes); +}; diff --git a/lib/routeLoader.js b/lib/routeLoader.js index e57d298..ee48b62 100644 --- a/lib/routeLoader.js +++ b/lib/routeLoader.js @@ -1,94 +1,41 @@ const fs = require('fs'); const glob = require('glob'); -const path = require('path'); -const _ = require('lodash'); const async = require('async'); const defaults = require('./defaults'); -const getCompletePath = require('./getCompletePath'); +const validateRoutesDirectory = require('./validateRoutesDirectory'); +const getRoutesFromFiles = require('./getRoutesFromFiles'); module.exports = (server, options, next) => { const load = (loadOptions, loadDone) => { const stub = () => {}; loadDone = loadDone || stub; - const settings = _.clone(loadOptions); - _.defaults(settings, defaults); + const settings = Object.assign({}, defaults, loadOptions); // the main flow is here: async.autoInject({ // confirm that settings.path exists and is a directory: confirmDirectoryExists(done) { - fs.exists(settings.path, (exists) => { - if (!exists) { - server.log(['hapi-route-loader', 'warning'], { message: 'path doesnt exist', path: settings.path }); - return done(); - } - fs.stat(settings.path, (err, stat) => { - if (err) { - return done(err); - } - if (!stat.isDirectory()) { - server.log(['hapi-route-loader', 'warning'], { message: 'path not a directory', path: settings.path }); - return done(`path ${settings.path} not a directory`); - } - return done(); - }); - }); + validateRoutesDirectory(server, settings, done); }, // get the list of all matching route-containing files files(confirmDirectoryExists, done) { glob('**/*.js', { cwd: settings.path - }, (globErr, files) => { - if (globErr) { - return done(globErr); - } - done(null, files); - }); + }, done); }, - // for each filename, get a list of configured routes defined by it - configureAllRoutes(files, done) { - const routeConfigs = {}; - files.forEach((fileName) => { - const fileRouteList = []; - const moduleRoutes = require(path.join(settings.path, fileName)); - _.forIn(moduleRoutes, (originalRouteConfig) => { - if (typeof originalRouteConfig === 'function') { - originalRouteConfig = originalRouteConfig(server, settings); - } - const processedRouteConfig = _.clone(originalRouteConfig); - // set up the route's 'config' option: - if (options.routeConfig) { - if (originalRouteConfig.config) { - processedRouteConfig.config = _.defaults(options.routeConfig, originalRouteConfig.config); - } else { - processedRouteConfig.config = options.routeConfig; - } - } - // set up the route's 'path' option: - processedRouteConfig.path = getCompletePath(options, fileName, originalRouteConfig.path); - fileRouteList.push(processedRouteConfig); - }); - routeConfigs[fileName] = fileRouteList; - }); - done(null, routeConfigs); + configuredRoutes(files, done) { + getRoutesFromFiles(server, settings, files, done); }, // register all routes with server.route: - registerAllRoutes: (configureAllRoutes, done) => { - Object.keys(configureAllRoutes).forEach((fileName) => { - configureAllRoutes[fileName].forEach((routeConfig) => { - if (options.verbose) { - server.log(['debug', 'hapi-route-loader'], { msg: 'registering', data: routeConfig }); - } - server.route(routeConfig); - }); + registerAllRoutes: (configuredRoutes, done) => { + configuredRoutes.forEach((routeConfig) => { + if (options.verbose) { + server.log(['debug', 'hapi-route-loader'], { msg: 'registering', data: routeConfig }); + } + server.route(routeConfig); }); done(); } - }, (err2) => { - if (err2) { - server.log(['hapi-route-loader', 'error'], err2); - } - return loadDone(err2); - }); + }, loadDone); }; if (options.autoLoad === false) { return next(); diff --git a/lib/validateRoutesDirectory.js b/lib/validateRoutesDirectory.js new file mode 100644 index 0000000..a5f5bfa --- /dev/null +++ b/lib/validateRoutesDirectory.js @@ -0,0 +1,22 @@ +const fs = require('fs'); + +const validateRoutesDirectory = (server, settings, done) => { + fs.exists(settings.path, (exists) => { + if (!exists) { + server.log(['hapi-route-loader', 'warning'], { message: 'path doesnt exist', path: settings.path }); + return done(); + } + fs.stat(settings.path, (err, stat) => { + if (err) { + return done(err); + } + if (!stat.isDirectory()) { + server.log(['hapi-route-loader', 'warning'], { message: 'path not a directory', path: settings.path }); + return done(`path ${settings.path} not a directory`); + } + return done(); + }); + }); +}; + +module.exports = validateRoutesDirectory; diff --git a/package.json b/package.json index 1550bd1..76fd1bb 100644 --- a/package.json +++ b/package.json @@ -22,8 +22,7 @@ "homepage": "https://github.com/firstandthird/hapi-route-loader#readme", "dependencies": { "async": "^2.1.2", - "glob": "^7.1.1", - "lodash": "^4.17.2" + "glob": "^7.1.1" }, "devDependencies": { "chai": "^3.4.1", diff --git a/test/route.methods.js b/test/route.methods.js new file mode 100644 index 0000000..15cc1f1 --- /dev/null +++ b/test/route.methods.js @@ -0,0 +1,107 @@ +const chai = require('chai'); +const assert = chai.assert; +const getRoutePathBase = require('../lib/getRoutePathBase'); +const getCompletePath = require('../lib/getCompletePath'); +const getRoutesFromFiles = require('../lib/getRoutesFromFiles'); +const configureRoute = require('../lib/configureRoute'); + +describe('getCompletePath ', () => { + it('get route when originalPath starts with /', (done) => { + const result = getCompletePath({}, 'routes/test/testerson.js', '/originalPath'); + assert(result === '/originalPath'); + done(); + }); + + it('get route when originalPath does not start with /', (done) => { + const result = getCompletePath({}, 'routes/test/testerson.js', 'originalPath'); + assert(result === '/routes/test/originalPath'); + done(); + }); + + it('get route when originalPath ends with /', (done) => { + const result = getCompletePath({}, 'routes/test/testerson.js', 'originalPath/'); + assert(result === '/routes/test/originalPath/'); + done(); + }); +}); + +describe('getRoutePathBase', () => { + it('turn a file path into the base of the route name', (done) => { + const result = getRoutePathBase({}, 'routes/test/testerson.js'); + assert(result === '/routes/test'); + done(); + }); + + it('appends the file base to the route name', (done) => { + const result = getRoutePathBase({ base: 'base' }, 'routes/test/testerson.js'); + assert(result === 'base/routes/test'); + done(); + }); +}); + +describe('getRoutesFromFiles', () => { + it('gets routes from a file when provided as a module export:', (done) => { + const settings = { + path: `${__dirname}/routes` + }; + const server = { + value: 'hello world' + }; + getRoutesFromFiles(server, settings, ['route.js'], (err, configuredRoutes) => { + assert(err === null); + assert(configuredRoutes.length === 8); + configuredRoutes.forEach((route) => { + assert(typeof route === 'object'); + assert(route.path !== undefined); + assert(route.method !== undefined); + assert(route.handler !== undefined); + }); + done(); + }); + }); + it('gets routes from a file when provided as a function:', (done) => { + const settings = { + path: `${__dirname}/functionRoutes`, + functionTestThingy: 'test' + }; + const server = { + version: 'hello world' + }; + getRoutesFromFiles(server, settings, ['function.js'], (err, configuredRoutes) => { + assert(err === null); + assert(configuredRoutes.length === 1); + const route = configuredRoutes[0]; + assert(typeof route === 'object'); + assert(route.path !== undefined); + assert(route.method !== undefined); + assert(route.handler !== undefined); + route.handler({}, (value) => { + assert.equal(value, `${server.version},${settings.functionTestThingy}`); + done(); + }); + }); + }); +}); + +describe('configureRoute', () => { + it('configures a route from its definition', (done) => { + const settings = { + path: `${__dirname}/routes`, + base: 'first', + routeConfig: { + strategy: 'none' + } + }; + const routeConfig = { + path: 'crooked', + method: 'also crooked', + handler(request, reply) { + return reply(); + } + }; + const result = configureRoute(settings, 'route.js', routeConfig); + assert(result.path === 'first/crooked'); + assert(result.config.strategy === 'none'); + done(); + }); +}); From f17231af11f854d0fff844b213a0fdf011d7f1ec Mon Sep 17 00:00:00 2001 From: orthagonal Date: Fri, 6 Oct 2017 16:24:20 -0500 Subject: [PATCH 5/5] tests for validate directory --- test/route.methods.js | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/test/route.methods.js b/test/route.methods.js index 15cc1f1..bcfd142 100644 --- a/test/route.methods.js +++ b/test/route.methods.js @@ -1,10 +1,51 @@ const chai = require('chai'); const assert = chai.assert; +const validateRoutesDirectory = require('../lib/validateRoutesDirectory'); const getRoutePathBase = require('../lib/getRoutePathBase'); const getCompletePath = require('../lib/getCompletePath'); const getRoutesFromFiles = require('../lib/getRoutesFromFiles'); const configureRoute = require('../lib/configureRoute'); +describe('validateRoutesDirectory', () => { + it('logs if a directory does not exist', (done) => { + const server = { + log(tags, msg) { + assert(msg.message === 'path doesnt exist'); + } + }; + const settings = { + path: 'nonono' + }; + validateRoutesDirectory(server, settings, (outcome) => { + done(); + }); + }); + it('logs if a directory is actually a file', (done) => { + const server = { + log(tags, msg) { + assert(msg.message === 'path not a directory'); + } + }; + const settings = { + path: __filename + }; + validateRoutesDirectory(server, settings, (outcome) => { + assert(outcome.indexOf('not a directory')); + done(); + }); + }); + it('is fine with valid directories', (done) => { + const server = {}; + const settings = { + path: __dirname + }; + validateRoutesDirectory(server, settings, (outcome) => { + assert(outcome === undefined); + done(); + }); + }); +}); + describe('getCompletePath ', () => { it('get route when originalPath starts with /', (done) => { const result = getCompletePath({}, 'routes/test/testerson.js', '/originalPath');