diff --git a/src/server/index.js b/src/server/index.js index 136d2d0..ccc1db1 100755 --- a/src/server/index.js +++ b/src/server/index.js @@ -214,11 +214,17 @@ class Server { ...this.endpointsMap[endpoint.method], [endpoint.route]: { route: endpoint.route, - } + enabled: endpoint.enabled ?? true, + }, } this.httpInterface[endpoint.method](endpoint.route, ...middlewares, async (req, res) => { try { + // check if endpoint is disabled + if (!this.endpointsMap[endpoint.method][endpoint.route].enabled) { + throw new Error("Endpoint is disabled!") + } + return await endpoint.fn(req, res) } catch (error) { if (typeof this.params.onRouteError === "function") { @@ -300,6 +306,18 @@ class Server { this.httpInterface.close() this.wsInterface.io.close() } + + toogleEndpointReachability = (method, route, enabled) => { + if (typeof this.endpointsMap[method] !== "object") { + throw new Error(`Cannot toogle endpoint, method [${method}] not set!`) + } + + if (typeof this.endpointsMap[method][route] !== "object") { + throw new Error(`Cannot toogle endpoint [${route}], is not registered!`) + } + + this.endpointsMap[method][route].enabled = enabled ?? !this.endpointsMap[method][route].enabled + } } module.exports = Server \ No newline at end of file diff --git a/test/clientTest.js b/test/clientTest.js new file mode 100644 index 0000000..ad5f03d --- /dev/null +++ b/test/clientTest.js @@ -0,0 +1,30 @@ +const assert = require("assert") +const linebridgeClient = require("../dist/client/index.js") + +const exampleTestHTTPPort = 3010 + +let exampleBridge = null + +describe("[Client]", async function () { + it("exports fine", function () { + assert.equal(typeof linebridgeClient, "object") + console.log(linebridgeClient) + }) + + it("create test bridge", async () => { + exampleBridge = new linebridgeClient.Bridge({ + origin: `http://0.0.0.0:${exampleTestHTTPPort}`, + }) + + await exampleBridge.initialize() + }) + + it("bridge.endpoints is an object", async () => { + assert.equal(typeof exampleBridge.endpoints, "object") + }) + + it("test endpoint should correctly respond", async () => { + let response = await exampleBridge.endpoints.get.test() + assert.equal(response.test, "testing") + }) +}) \ No newline at end of file diff --git a/test/index.js b/test/index.js deleted file mode 100644 index acf3b03..0000000 --- a/test/index.js +++ /dev/null @@ -1,70 +0,0 @@ -const assert = require('assert') -const linebridgeClient = require('../dist/client/index.js') -const linebridgeServer = require('../dist/server/index.js') - -const exampleTestHTTPPort = 3010 - -let exampleServer = null -let exampleController = null -let exampleBridge = null - -describe('[Linebridge Server]', async function () { - it('should export', function () { - assert.equal(typeof linebridgeServer, 'function') - }) - - it("initialize example server", async function () { - exampleServer = new linebridgeServer({ - port: exampleTestHTTPPort, - endpoints: [ - { - route: '/test', - method: 'GET', - fn: function (req, res) { - return res.send({test: 'testing'}) - } - } - ] - }) - - await exampleServer.init() - }) -}) - -describe('[Linebridge Client]', async function () { - it('exports should be lib objects and functions', function () { - assert.equal(typeof linebridgeClient, 'object') - }) - - it("create example controller", async function () { - exampleController = new linebridgeClient.Controller({ - servers: [ - `http://localhost:${exampleTestHTTPPort}` - ] - }) - - await exampleController.initialize() - - console.log(exampleController) - }) - - it("create example bridge", async () => { - exampleBridge = new linebridgeClient.Bridge({ - origin: `http://localhost:${exampleTestHTTPPort}`, - headers: { - 'Content-Type': 'application/json' - } - }) - - await exampleBridge.initialize() - }) - - it("bridge.endpoints is an object", async () => { - assert.equal(typeof exampleBridge.endpoints, 'object') - }) - - it("test endpoint should correctly respond", async () => { - let response = await exampleBridge.endpoints.get.test() - assert.equal(response.test, 'testing') - }) -}) \ No newline at end of file diff --git a/test/serverTest.js b/test/serverTest.js new file mode 100644 index 0000000..aca480d --- /dev/null +++ b/test/serverTest.js @@ -0,0 +1,60 @@ +const assert = require("assert") +const linebridgeServer = require("../dist/server/index.js") + +const exampleTestHTTPPort = 3010 + +const testEndpoints = [ + { + route: "/test", + method: "GET", + fn: function (req, res) { + return res.send({ test: "testing" }) + } + }, + { + route: "/disabledByDefault", + method: "GET", + fn: function (req, res) { + return res.send("This must not be sended") + }, + enabled: false, + }, + { + route: "/shouldBeDisabled", + method: "GET", + fn: function (req, res) { + return res.send("This must not be sended after use `toogleEndpointReachability`") + } + } +] + +let exampleServer = null + +describe("[Server]", async function () { + it("should export", function () { + assert.equal(typeof linebridgeServer, "function") + }) + + it("create server", async function () { + exampleServer = new linebridgeServer({ + port: exampleTestHTTPPort, + }) + }) + + it("register test controllers", async function () { + testEndpoints.forEach((endpoint) => { + exampleServer.registerHTTPEndpoint(endpoint) + }) + }) + + it("initialize server", async function () { + await exampleServer.initialize() + }) + + it("toogleEndpointReachability", async function () { + exampleServer.toogleEndpointReachability("get", "/shouldBeDisabled", false) + + // check if endpoint is disabled + assert.equal(exampleServer.endpointsMap["get"]["/shouldBeDisabled"].enabled, false) + }) +}) \ No newline at end of file