From 5ceb68a5775999bd5eb314f58e5cc7ce5a3502e5 Mon Sep 17 00:00:00 2001 From: srgooglo Date: Thu, 5 May 2022 10:39:56 +0200 Subject: [PATCH] update tests --- test/clientTest.js | 30 ++++++++++++++++++++ test/index.js | 70 ---------------------------------------------- test/serverTest.js | 60 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 70 deletions(-) create mode 100644 test/clientTest.js delete mode 100644 test/index.js create mode 100644 test/serverTest.js 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