diff --git a/index.js b/index.js index 7d750e0..39f61b5 100644 --- a/index.js +++ b/index.js @@ -11,12 +11,11 @@ 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 + '/'; + url = url + .replace(/^\/root/i, '/') + .replace(/\./g, '/') + .replace(/\/+/g, '/'); const handler = require(('./' + modulefolder + '/' + f) .replace(/\/\.\//g, '/') diff --git a/test/main.js b/test/main.js index 57cb8c0..5f78ae0 100644 --- a/test/main.js +++ b/test/main.js @@ -1,4 +1,4 @@ -const { describe, it } = require('mocha'); +const { describe, it, beforeEach } = require('mocha'); const expect = require('chai').expect; const requestor = require('../index'); @@ -23,8 +23,12 @@ const mock = { }; describe('requestor', () => { + beforeEach(() => { + mock.registered = {}; + }); + it('should detect all requests on root', () => { - requestor(mock, './test/onroot', {}); + requestor(mock, './test/root', {}); expect(mock.registered).to.have.all.keys([ 'post-/', 'get-/', @@ -33,4 +37,27 @@ describe('requestor', () => { 'all-/' ]); }); + + it('should detect requests on root.subfolder', () => { + requestor(mock, './test/root.sub', {}); + expect(mock.registered).to.have.all.keys([ + 'post-/sub/', + 'get-/sub/', + 'put-/sub/', + 'delete-/sub/', + 'all-/sub/' + ]); + }); + + it('should detect requests on subfolder', () => { + requestor(mock, './test/sub', {}); + expect(mock.registered).to.have.all.keys([ + 'post-/sub/', + 'get-/sub/', + 'put-/sub/', + 'delete-/sub/', + 'all-/sub/', + 'get-/sub/lv1/lv2/lv3/' + ]); + }); }); diff --git a/test/onroot/all-root.js b/test/root.sub/all-root.sub.js similarity index 100% rename from test/onroot/all-root.js rename to test/root.sub/all-root.sub.js diff --git a/test/onroot/delete-root.js b/test/root.sub/delete-root.sub.js similarity index 100% rename from test/onroot/delete-root.js rename to test/root.sub/delete-root.sub.js diff --git a/test/onroot/get-root.js b/test/root.sub/get-root.sub.js similarity index 100% rename from test/onroot/get-root.js rename to test/root.sub/get-root.sub.js diff --git a/test/onroot/not-root.js b/test/root.sub/not-root.sub.js similarity index 100% rename from test/onroot/not-root.js rename to test/root.sub/not-root.sub.js diff --git a/test/onroot/post-root.js b/test/root.sub/post-root.sub.js similarity index 100% rename from test/onroot/post-root.js rename to test/root.sub/post-root.sub.js diff --git a/test/onroot/put-root.js b/test/root.sub/put-root.sub.js similarity index 100% rename from test/onroot/put-root.js rename to test/root.sub/put-root.sub.js diff --git a/test/root/all-root.js b/test/root/all-root.js new file mode 100644 index 0000000..5b17be5 --- /dev/null +++ b/test/root/all-root.js @@ -0,0 +1,3 @@ +module.exports = (req, res, next, opts) => { + // dummy endpoint: do nothing +}; diff --git a/test/root/delete-root.js b/test/root/delete-root.js new file mode 100644 index 0000000..5b17be5 --- /dev/null +++ b/test/root/delete-root.js @@ -0,0 +1,3 @@ +module.exports = (req, res, next, opts) => { + // dummy endpoint: do nothing +}; diff --git a/test/root/get-root.js b/test/root/get-root.js new file mode 100644 index 0000000..5b17be5 --- /dev/null +++ b/test/root/get-root.js @@ -0,0 +1,3 @@ +module.exports = (req, res, next, opts) => { + // dummy endpoint: do nothing +}; diff --git a/test/root/not-root.js b/test/root/not-root.js new file mode 100644 index 0000000..5b17be5 --- /dev/null +++ b/test/root/not-root.js @@ -0,0 +1,3 @@ +module.exports = (req, res, next, opts) => { + // dummy endpoint: do nothing +}; diff --git a/test/root/post-root.js b/test/root/post-root.js new file mode 100644 index 0000000..5b17be5 --- /dev/null +++ b/test/root/post-root.js @@ -0,0 +1,3 @@ +module.exports = (req, res, next, opts) => { + // dummy endpoint: do nothing +}; diff --git a/test/root/put-root.js b/test/root/put-root.js new file mode 100644 index 0000000..5b17be5 --- /dev/null +++ b/test/root/put-root.js @@ -0,0 +1,3 @@ +module.exports = (req, res, next, opts) => { + // dummy endpoint: do nothing +}; diff --git a/test/sub/all-sub.js b/test/sub/all-sub.js new file mode 100644 index 0000000..5b17be5 --- /dev/null +++ b/test/sub/all-sub.js @@ -0,0 +1,3 @@ +module.exports = (req, res, next, opts) => { + // dummy endpoint: do nothing +}; diff --git a/test/sub/delete-sub.js b/test/sub/delete-sub.js new file mode 100644 index 0000000..5b17be5 --- /dev/null +++ b/test/sub/delete-sub.js @@ -0,0 +1,3 @@ +module.exports = (req, res, next, opts) => { + // dummy endpoint: do nothing +}; diff --git a/test/sub/get-sub.js b/test/sub/get-sub.js new file mode 100644 index 0000000..5b17be5 --- /dev/null +++ b/test/sub/get-sub.js @@ -0,0 +1,3 @@ +module.exports = (req, res, next, opts) => { + // dummy endpoint: do nothing +}; diff --git a/test/sub/get-sub.lv1.lv2.lv3.js b/test/sub/get-sub.lv1.lv2.lv3.js new file mode 100644 index 0000000..e69de29 diff --git a/test/sub/not-sub.js b/test/sub/not-sub.js new file mode 100644 index 0000000..5b17be5 --- /dev/null +++ b/test/sub/not-sub.js @@ -0,0 +1,3 @@ +module.exports = (req, res, next, opts) => { + // dummy endpoint: do nothing +}; diff --git a/test/sub/post-sub.js b/test/sub/post-sub.js new file mode 100644 index 0000000..5b17be5 --- /dev/null +++ b/test/sub/post-sub.js @@ -0,0 +1,3 @@ +module.exports = (req, res, next, opts) => { + // dummy endpoint: do nothing +}; diff --git a/test/sub/put-sub.js b/test/sub/put-sub.js new file mode 100644 index 0000000..5b17be5 --- /dev/null +++ b/test/sub/put-sub.js @@ -0,0 +1,3 @@ +module.exports = (req, res, next, opts) => { + // dummy endpoint: do nothing +};