From 55f631de02c56733c3acddd213728ef298c4da6b Mon Sep 17 00:00:00 2001 From: Timo Hocker Date: Wed, 11 Dec 2019 09:32:40 +0100 Subject: [PATCH] basic tests, initialize readme --- README.md | 1 + index.js | 21 ++++++++++----------- test/main.js | 30 ++++++++++++++++++++++++++++++ test/onroot/all-root.js | 3 +++ test/onroot/delete-root.js | 3 +++ test/onroot/get-root.js | 3 +++ test/onroot/not-root.js | 3 +++ test/onroot/post-root.js | 3 +++ test/onroot/put-root.js | 3 +++ 9 files changed, 59 insertions(+), 11 deletions(-) create mode 100644 README.md create mode 100644 test/main.js create mode 100644 test/onroot/all-root.js create mode 100644 test/onroot/delete-root.js create mode 100644 test/onroot/get-root.js create mode 100644 test/onroot/not-root.js create mode 100644 test/onroot/post-root.js create mode 100644 test/onroot/put-root.js diff --git a/README.md b/README.md new file mode 100644 index 0000000..fb019cf --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# Requestor diff --git a/index.js b/index.js index c00b89d..77e7f00 100644 --- a/index.js +++ b/index.js @@ -1,40 +1,39 @@ const fs = require('fs'); -const path = require('path'); /** * Load all request handlers in the given folder * - * @param {any} express app + * @param {any} app express app * @param {string} modulefolder - * @param {any} object to pass to the handlers (for example database access) + * @param {any} opts object to pass to the handlers (for example database access) */ module.exports = function (app, modulefolder, opts) { for (const f of fs.readdirSync(modulefolder)) { const regex = /(.*?)-(.*?)\.js/; let [, method, url] = regex.exec(f); - url = '/' + url.replace(/^root/i, '').replace(/\./g, '/').replace(/\/\//g, ''); + url = '/' + url.replace(/^root/i, '').replace(/\./g, '/').replace(/\/+/g, '/'); - const handler = require(path.join('./', modulefolder, f)); + const handler = require(('./' + modulefolder + '/' + f).replace(/\/\.\//g, '/').replace(/\/+/g, '/')); const func = (req, res, next) => { handler(req, res, next, opts); }; switch (method) { case 'post': - app.post(path, func); + app.post(url, func); break; case 'get': - app.get(path, func); + app.get(url, func); break; case 'put': - app.put(path, func); + app.put(url, func); break; case 'delete': - app.delete(path, func); + app.delete(url, func); break; case 'all': - app.all(path, func); + app.all(url, func); break; } } -} +}; diff --git a/test/main.js b/test/main.js new file mode 100644 index 0000000..5ab48ad --- /dev/null +++ b/test/main.js @@ -0,0 +1,30 @@ +const {describe, it} = require('mocha'); +const expect = require('chai').expect; + +const requestor = require('../index'); + +const mock = { + registered: {}, + post: function (path, handler) { + this.registered['post-' + path] = handler; + }, + get: function (path, handler) { + this.registered['get-' + path] = handler; + }, + put: function (path, handler) { + this.registered['put-' + path] = handler; + }, + delete: function (path, handler) { + this.registered['delete-' + path] = handler; + }, + all: function (path, handler) { + this.registered['all-' + path] = handler; + } +}; + +describe('requestor', () => { + it('should detect all requests on root', () => { + requestor(mock, './test/onroot', {}); + expect(mock.registered).to.have.all.keys(['post-/', 'get-/', 'put-/', 'delete-/', 'all-/']); + }); +}); diff --git a/test/onroot/all-root.js b/test/onroot/all-root.js new file mode 100644 index 0000000..5b17be5 --- /dev/null +++ b/test/onroot/all-root.js @@ -0,0 +1,3 @@ +module.exports = (req, res, next, opts) => { + // dummy endpoint: do nothing +}; diff --git a/test/onroot/delete-root.js b/test/onroot/delete-root.js new file mode 100644 index 0000000..5b17be5 --- /dev/null +++ b/test/onroot/delete-root.js @@ -0,0 +1,3 @@ +module.exports = (req, res, next, opts) => { + // dummy endpoint: do nothing +}; diff --git a/test/onroot/get-root.js b/test/onroot/get-root.js new file mode 100644 index 0000000..5b17be5 --- /dev/null +++ b/test/onroot/get-root.js @@ -0,0 +1,3 @@ +module.exports = (req, res, next, opts) => { + // dummy endpoint: do nothing +}; diff --git a/test/onroot/not-root.js b/test/onroot/not-root.js new file mode 100644 index 0000000..5b17be5 --- /dev/null +++ b/test/onroot/not-root.js @@ -0,0 +1,3 @@ +module.exports = (req, res, next, opts) => { + // dummy endpoint: do nothing +}; diff --git a/test/onroot/post-root.js b/test/onroot/post-root.js new file mode 100644 index 0000000..5b17be5 --- /dev/null +++ b/test/onroot/post-root.js @@ -0,0 +1,3 @@ +module.exports = (req, res, next, opts) => { + // dummy endpoint: do nothing +}; diff --git a/test/onroot/put-root.js b/test/onroot/put-root.js new file mode 100644 index 0000000..5b17be5 --- /dev/null +++ b/test/onroot/put-root.js @@ -0,0 +1,3 @@ +module.exports = (req, res, next, opts) => { + // dummy endpoint: do nothing +};