silverbullet/plugos/hooks/endpoint.test.ts

49 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-10-06 16:54:15 +08:00
import { createSandbox } from "../environments/deno_sandbox.ts";
import { Manifest } from "../types.ts";
import { EndpointHook, EndpointHookT } from "./endpoint.ts";
import { System } from "../system.ts";
2022-03-21 22:21:34 +08:00
2022-10-08 22:36:06 +08:00
import { Application } from "../../server/deps.ts";
import { assertEquals } from "../../test_deps.ts";
2022-10-06 16:54:15 +08:00
Deno.test("Run a plugos endpoint server", async () => {
let system = new System<EndpointHookT>("server");
2022-03-21 22:21:34 +08:00
let plug = await system.load(
{
2022-04-27 01:04:36 +08:00
name: "test",
2022-03-21 22:21:34 +08:00
functions: {
testhandler: {
2022-03-27 17:26:13 +08:00
http: {
path: "/",
},
2022-03-21 22:21:34 +08: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-10-06 16:54:15 +08:00
createSandbox,
2022-03-21 22:21:34 +08:00
);
2022-10-06 16:54:15 +08:00
const app = new Application();
2022-03-21 22:21:34 +08:00
const port = 3123;
2022-03-23 22:41:12 +08:00
system.addHook(new EndpointHook(app, "/_"));
2022-03-23 22:41:12 +08:00
2022-10-06 16:54:15 +08:00
const controller = new AbortController();
app.listen({ port: port, signal: controller.signal });
const res = await fetch(`http://localhost:${port}/_/test/?name=Pete`);
assertEquals(res.status, 200);
assertEquals(res.headers.get("Content-type"), "application/json");
assertEquals(await res.json(), [1, 2, 3]);
console.log("Aborting");
controller.abort();
2022-07-04 15:34:11 +08:00
await system.unloadAll();
2022-03-21 22:21:34 +08:00
});