Files
cypress/packages/https-proxy/test/helpers/https_server.ts
Bill Glesias 4d904e272f chore: refactor @packages/https-proxy to TypeScript and use vitest instead of mocha (#32718)
* chore: convert @packages/https-proxy to TypeScript and use vitest instead of mocha

* chore: address comments from code review

* Update packages/https-proxy/vitest.config.ts
2025-10-15 14:00:41 -04:00

52 lines
1.4 KiB
TypeScript

/* eslint-disable
no-console,
*/
import https from 'https'
import { allowDestroy } from '@packages/network'
import { options } from './certs'
import type { IncomingMessage, ServerResponse } from 'http'
const defaultOnRequest = function (req: IncomingMessage, res: ServerResponse) {
console.log('HTTPS SERVER REQUEST URL:', req.url)
console.log('HTTPS SERVER REQUEST HEADERS:', req.headers)
res.setHeader('Content-Type', 'text/html')
res.writeHead(200)
res.end('<html><head></head><body>https server</body></html>')
}
let servers: https.Server<typeof IncomingMessage, typeof ServerResponse>[] = []
export const create = (onRequest: (req: IncomingMessage, res: ServerResponse) => void) => {
return https.createServer(options, onRequest != null ? onRequest : defaultOnRequest)
}
export const start = (port: number, onRequest?: (req: IncomingMessage, res: ServerResponse) => void) => {
return new Promise<https.Server>((resolve) => {
const srv = create(onRequest)
allowDestroy(srv)
servers.push(srv)
srv.listen(port, () => {
console.log(`server listening on port: ${port}`)
resolve(srv)
})
})
}
export const stop = async () => {
const stopServer = (srv: https.Server) => {
return new Promise<Error | undefined>((resolve) => {
srv.destroy(resolve)
})
}
await Promise.all(servers.map((server) => stopServer(server)))
servers = []
}