1
0
silverbullet/plugbox/feature/endpoint.test.ts

50 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-03-23 14:41:12 +00:00
import { createSandbox } from "../environment/node_sandbox";
import { expect, test } from "@jest/globals";
import { Manifest } from "../types";
2022-03-21 14:21:34 +00:00
import express from "express";
import request from "supertest";
2022-03-23 14:41:12 +00:00
import { EndpointFeature, EndpointHook } from "./endpoint";
import { System } from "../system";
2022-03-21 14:21:34 +00:00
test("Run a plugbox endpoint server", async () => {
2022-03-23 14:41:12 +00:00
let system = new System<EndpointHook>("server");
2022-03-21 14:21:34 +00:00
let plug = await system.load(
"test",
{
functions: {
testhandler: {
code: `(() => {
return {
default: (req) => {
console.log("Req", req);
return {status: 200, body: [1, 2, 3], headers: {"Content-type": "application/json"}};
}
};
})()`,
},
},
hooks: {
endpoints: [{ method: "GET", path: "/", handler: "testhandler" }],
},
} as Manifest<EndpointHook>,
2022-03-25 11:03:06 +00:00
createSandbox
2022-03-21 14:21:34 +00:00
);
const app = express();
const port = 3123;
2022-03-23 14:41:12 +00:00
2022-03-27 07:55:29 +00:00
system.addFeature(new EndpointFeature(app, "/_"));
2022-03-23 14:41:12 +00:00
2022-03-21 14:21:34 +00:00
let server = app.listen(port, () => {
console.log(`Listening on port ${port}`);
});
let resp = await request(app)
.get("/_/test/?name=Pete")
.expect((resp) => {
expect(resp.status).toBe(200);
expect(resp.header["content-type"]).toContain("application/json");
expect(resp.text).toBe(JSON.stringify([1, 2, 3]));
});
server.close();
});