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 fileTake 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 back1import http from 'node:http';2import type net from 'node:net';Require Node.js 22Sindre Sorhusbefore adoptionf1da0fc
line 3why, one step back3import {promisify} from 'node:util';Simplify testsSindre Sorhusbefore adoptiondfa3f59
line 4why, one step back4import type {ExecutionContext} from 'ava';Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250
lines 5–7why, one step back5import express from 'express';67export type HttpServerOptions = {Require Node.js 22Sindre Sorhusbefore adoptionf1da0fc
line 8why, one step back8 bodyParser?: false;Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250
lines 9–15why, one step back9};1011export type ExtendedHttpTestServer = {12 http: http.Server;13 url: string;14 port: number;15 hostname: string;Simplify testsSindre Sorhusbefore adoptiondfa3f59
line 16why, one step back16 close: () => Promise<void>;Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250
lines 17–18why, one step back17} & express.Express;18Simplify testsSindre Sorhusbefore adoptiondfa3f59
lines 19–28why, one step back19export 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) ?? {};28Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250
lines 29–32why, one step back29 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 back33 server.http.keepAliveTimeout = 0;34 server.http.unref();Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250
line 35why, one step back35Simplify testsSindre Sorhusbefore adoptiondfa3f59
line 36why, one step back36 if (resolvedOptions.bodyParser !== false) {Require Node.js 22Sindre Sorhusbefore adoptionf1da0fc
lines 37–40why, one step back37 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 back41 }42Require Node.js 22Sindre Sorhusbefore adoptionf1da0fc
line 43why, one step back43 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 back4445 server.port = (server.http.address() as net.AddressInfo).port;46 server.url = `http://localhost:${server.port}`;47 server.hostname = 'localhost';48Require Node.js 22Sindre Sorhusbefore adoptionf1da0fc
lines 49–50why, one step back49 server.close = async () => {50 server.http.closeAllConnections();Simplify testsSindre Sorhusbefore adoptiondfa3f59
line 51why, one step back51 await promisify(server.http.close.bind(server.http))();Require Node.js 22Sindre Sorhusbefore adoptionf1da0fc
line 52why, one step back52 };Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250
line 53why, one step back53Simplify testsSindre Sorhusbefore adoptiondfa3f59
lines 54–57why, one step back54 if (t) {55 t.teardown(server.close);56 }57Add retry count to `afterResponse` and `beforeError` hooks (#763)Sindre Sorhusbefore adoption37fc250
line 58why, one step back58 return server;Simplify testsSindre Sorhusbefore adoptiondfa3f59
line 59why, one step back59}