MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / withServer

Function withServer

packages/core/sdk/src/hosted-http-client.test.ts:20–53  ·  view source on GitHub ↗
(
  f: (input: {
    readonly baseUrl: string;
    readonly received: Array<{ readonly method: string; readonly body: string }>;
  }) => Promise<A>,
)

Source from the content-addressed store, hash-verified

18// body whose init omits `duplex`, and no fake will ever say so. This drives a
19// real server so the transport's own requirements are the assertion.
20const withServer = <A>(
21 f: (input: {
22 readonly baseUrl: string;
23 readonly received: Array<{ readonly method: string; readonly body: string }>;
24 }) => Promise<A>,
25) =>
26 new Promise<A>((resolve, reject) => {
27 const received: Array<{ method: string; body: string }> = [];
28 const server: Server = createServer((request, response) => {
29 const chunks: Array<Buffer> = [];
30 request.on("data", (chunk: Buffer) => chunks.push(chunk));
31 request.on("end", () => {
32 received.push({
33 method: request.method ?? "",
34 body: Buffer.concat(chunks).toString("utf8"),
35 });
36 response.statusCode = 200;
37 response.end("ok");
38 });
39 });
40
41 server.listen(0, "127.0.0.1", () => {
42 const address = server.address();
43 if (!address || typeof address === "string") {
44 server.close();
45 // oxlint-disable-next-line executor/no-promise-reject, executor/no-error-constructor -- boundary: Node listen callback is adapted into the test Promise failure path
46 reject(new Error("Server did not bind to a TCP port"));
47 return;
48 }
49 f({ baseUrl: `http://127.0.0.1:${address.port}`, received })
50 .then(resolve, reject)
51 .finally(() => server.close());
52 });
53 });
54
55// Reproduces the raw Promise rejection node:dns/promises produces at the
56// adapter boundary under test. The rejection value is deliberately opaque:

Callers 1

Calls 7

rejectFunction · 0.85
pushMethod · 0.80
toStringMethod · 0.80
endMethod · 0.80
listenMethod · 0.80
addressMethod · 0.80
closeMethod · 0.65

Tested by

no test coverage detected