forked from octokit/request.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock-request-http-server.ts
41 lines (34 loc) · 1.06 KB
/
mock-request-http-server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { createServer, RequestListener } from "node:http";
import { type AddressInfo } from "node:net";
import { once } from "node:stream";
import { endpoint } from "@octokit/endpoint";
import type { RequestInterface } from "@octokit/types";
import withDefaults from "../src/with-defaults.ts";
import defaults from "../src/defaults.ts";
export default async function mockRequestHttpServer(
requestListener: RequestListener,
fetch = globalThis.fetch,
): Promise<
RequestInterface<object> & {
closeMockServer: () => void;
baseUrlMockServer: string;
}
> {
const server = createServer(requestListener);
server.listen(0);
await once(server, "listening");
const baseUrl = `http://localhost:${(server.address() as AddressInfo).port}`;
const request = withDefaults(endpoint, {
...defaults,
baseUrl,
request: {
fetch,
},
}) as RequestInterface<object> & {
closeMockServer: () => void;
baseUrlMockServer: string;
};
request.baseUrlMockServer = baseUrl;
request.closeMockServer = server.close.bind(server);
return request;
}