1
0
silverbullet/packages/plugos/hooks/endpoint.test.ts

51 lines
1.4 KiB
TypeScript
Raw Normal View History

import { createSandbox } from "../environments/node_sandbox";
2022-03-23 14:41:12 +00:00
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";
import { EndpointHook, EndpointHookT } from "./endpoint";
2022-03-23 14:41:12 +00:00
import { System } from "../system";
2022-03-21 14:21:34 +00:00
2022-03-27 09:31:12 +00:00
test("Run a plugos endpoint server", async () => {
let system = new System<EndpointHookT>("server");
2022-03-21 14:21:34 +00:00
let plug = await system.load(
{
2022-04-26 17:04:36 +00:00
name: "test",
2022-03-21 14:21:34 +00:00
functions: {
testhandler: {
2022-03-27 09:26:13 +00:00
http: {
path: "/",
},
2022-03-21 14:21:34 +00:00
code: `(() => {
return {
default: (req) => {
console.log("Req", req);
return {status: 200, body: [1, 2, 3], headers: {"Content-type": "application/json"}};
}
};
})()`,
},
},
} as Manifest<EndpointHookT>,
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
system.addHook(new EndpointHook(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();
2022-07-04 07:34:11 +00:00
await system.unloadAll();
2022-03-21 14:21:34 +00:00
});