Skip to content
Myr

create-http-test-server.ts

This file

Path

test/helpers/create-http-test-server.ts

59 lines · 2.0 KiB · TypeScript

Last changed by

Simplify testsSindre Sorhusbefore adoptiondfa3f59

History of this file

Take it

The walk stopped after 200 commits: lines older than 37fc250 point at it.

Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250

lines 1–2why, one step back
1import http from 'node:http';
2import type net from 'node:net';

Require Node.js 22Sindre Sorhusbefore adoptionf1da0fc

line 3why, one step back
3import {promisify} from 'node:util';

Simplify testsSindre Sorhusbefore adoptiondfa3f59

line 4why, one step back
4import type {ExecutionContext} from 'ava';

Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250

lines 5–7why, one step back
5import express from 'express';
67export type HttpServerOptions = {

Require Node.js 22Sindre Sorhusbefore adoptionf1da0fc

line 8why, one step back
8 bodyParser?: false;

Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250

lines 9–15why, one step back
9};
1011export type ExtendedHttpTestServer = {12 http: http.Server;13 url: string;14 port: number;15 hostname: string;

Simplify testsSindre Sorhusbefore adoptiondfa3f59

line 16why, one step back
16 close: () => Promise<void>;

Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250

lines 17–18why, one step back
17} & express.Express;
18

Simplify testsSindre Sorhusbefore adoptiondfa3f59

lines 19–28why, one step back
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

Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250

lines 29–32why, one step back
29 const server = express() as ExtendedHttpTestServer;
30 server.http = http.createServer(server);3132 server.set('etag', false);

Require Node.js 22Sindre Sorhusbefore adoptionf1da0fc

lines 33–34why, one step back
33 server.http.keepAliveTimeout = 0;
34 server.http.unref();

Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250

line 35why, one step back
35

Simplify testsSindre Sorhusbefore adoptiondfa3f59

line 36why, one step back
36 if (resolvedOptions.bodyParser !== false) {

Require Node.js 22Sindre Sorhusbefore adoptionf1da0fc

lines 37–40why, one step back
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'}));

Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250

lines 41–42why, one step back
41 }
42

Require Node.js 22Sindre Sorhusbefore adoptionf1da0fc

line 43why, one step back
43 await promisify(server.http.listen.bind(server.http))();

Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250

lines 44–48why, one step back
44
45 server.port = (server.http.address() as net.AddressInfo).port;46 server.url = `http://localhost:${server.port}`;47 server.hostname = 'localhost';48

Require Node.js 22Sindre Sorhusbefore adoptionf1da0fc

lines 49–50why, one step back
49 server.close = async () => {
50 server.http.closeAllConnections();

Simplify testsSindre Sorhusbefore adoptiondfa3f59

line 51why, one step back
51 await promisify(server.http.close.bind(server.http))();

Require Node.js 22Sindre Sorhusbefore adoptionf1da0fc

line 52why, one step back
52 };

Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250

line 53why, one step back
53

Simplify testsSindre Sorhusbefore adoptiondfa3f59

lines 54–57why, one step back
54 if (t) {
55 t.teardown(server.close);56 }57

Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250

line 58why, one step back
58 return server;

Simplify testsSindre Sorhusbefore adoptiondfa3f59

line 59why, one step back
59}