mirror of
https://github.com/ragestudio/linebridge.git
synced 2025-06-09 02:24:17 +00:00
Merge pull request #3 from ragestudio/endpoint-disabler
Endpoint disabler
This commit is contained in:
commit
18da80d604
@ -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
|
30
test/clientTest.js
Normal file
30
test/clientTest.js
Normal file
@ -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")
|
||||
})
|
||||
})
|
@ -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')
|
||||
})
|
||||
})
|
60
test/serverTest.js
Normal file
60
test/serverTest.js
Normal file
@ -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)
|
||||
})
|
||||
})
|
Loading…
x
Reference in New Issue
Block a user