MCPcopy
hub / github.com/sindresorhus/ky / createHttpTestServer

Function createHttpTestServer

test/helpers/create-http-test-server.ts:21–59  ·  view source on GitHub ↗
(
	tOrOptions?: ExecutionContext | HttpServerOptions,
	options?: HttpServerOptions,
)

Source from the content-addressed store, hash-verified

19export async function createHttpTestServer(t: ExecutionContext, options?: HttpServerOptions): Promise<ExtendedHttpTestServer>;
20export async function createHttpTestServer(options?: HttpServerOptions): Promise<ExtendedHttpTestServer>;
21export async function createHttpTestServer(
22 tOrOptions?: ExecutionContext | HttpServerOptions,
23 options?: HttpServerOptions,
24): Promise<ExtendedHttpTestServer> {
25 const isExecutionContext = typeof (tOrOptions as ExecutionContext)?.teardown === 'function';
26 const t = isExecutionContext ? tOrOptions as ExecutionContext : undefined;
27 const resolvedOptions = (isExecutionContext ? options : tOrOptions as HttpServerOptions) ?? {};
28
29 const server = express() as ExtendedHttpTestServer;
30 server.http = http.createServer(server);
31
32 server.set('etag', false);
33 server.http.keepAliveTimeout = 0;
34 server.http.unref();
35
36 if (resolvedOptions.bodyParser !== false) {
37 server.use(express.json({limit: '1mb', type: 'application/json'}));
38 server.use(express.text({limit: '1mb', type: 'text/plain'}));
39 server.use(express.urlencoded({limit: '1mb', type: 'application/x-www-form-urlencoded', extended: true}));
40 server.use(express.raw({limit: '1mb', type: 'application/octet-stream'}));
41 }
42
43 await promisify(server.http.listen.bind(server.http))();
44
45 server.port = (server.http.address() as net.AddressInfo).port;
46 server.url = `http://localhost:${server.port}`;
47 server.hostname = 'localhost';
48
49 server.close = async () => {
50 server.http.closeAllConnections();
51 await promisify(server.http.close.bind(server.http))();
52 };
53
54 if (t) {
55 t.teardown(server.close);
56 }
57
58 return server;
59}

Callers 15

bytes.tsFile · 0.85
headers.tsFile · 0.85
methods.tsFile · 0.85
createEsmTestServerFunction · 0.85
context.tsFile · 0.85
http-error.tsFile · 0.85
prefix.tsFile · 0.85
hooks.tsFile · 0.85
main.tsFile · 0.85
memory-leak.tsFile · 0.85
stream.tsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…