1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.createMockHttp2Server = exports.createMockHttpServer = exports.createMockHttpsServer = exports.createContinueResponseFunction = exports.createResponseFunctionWithDelay = exports.createResponseFunction = void 0;
|
4 | const fs_1 = require("fs");
|
5 | const http_1 = require("http");
|
6 | const http2_1 = require("http2");
|
7 | const https_1 = require("https");
|
8 | const path_1 = require("path");
|
9 | const stream_1 = require("stream");
|
10 | const fixturesDir = (0, path_1.join)(__dirname, "..", "fixtures");
|
11 | const setResponseHeaders = (response, headers) => {
|
12 | for (const [key, value] of Object.entries(headers)) {
|
13 | response.setHeader(key, value);
|
14 | }
|
15 | };
|
16 | const setResponseBody = (response, body) => {
|
17 | if (body instanceof stream_1.Readable) {
|
18 | body.pipe(response);
|
19 | }
|
20 | else {
|
21 | response.end(body);
|
22 | }
|
23 | };
|
24 | const createResponseFunction = (httpResp) => (request, response) => {
|
25 | response.statusCode = httpResp.statusCode;
|
26 | setResponseHeaders(response, httpResp.headers);
|
27 | setResponseBody(response, httpResp.body);
|
28 | };
|
29 | exports.createResponseFunction = createResponseFunction;
|
30 | const createResponseFunctionWithDelay = (httpResp, delay) => (request, response) => {
|
31 | response.statusCode = httpResp.statusCode;
|
32 | setResponseHeaders(response, httpResp.headers);
|
33 | setTimeout(() => setResponseBody(response, httpResp.body), delay);
|
34 | };
|
35 | exports.createResponseFunctionWithDelay = createResponseFunctionWithDelay;
|
36 | const createContinueResponseFunction = (httpResp) => (request, response) => {
|
37 | response.writeContinue();
|
38 | setTimeout(() => {
|
39 | (0, exports.createResponseFunction)(httpResp)(request, response);
|
40 | }, 100);
|
41 | };
|
42 | exports.createContinueResponseFunction = createContinueResponseFunction;
|
43 | const createMockHttpsServer = () => {
|
44 | const server = (0, https_1.createServer)({
|
45 | key: (0, fs_1.readFileSync)((0, path_1.join)(fixturesDir, "test-server-key.pem")),
|
46 | cert: (0, fs_1.readFileSync)((0, path_1.join)(fixturesDir, "test-server-cert.pem")),
|
47 | });
|
48 | return server;
|
49 | };
|
50 | exports.createMockHttpsServer = createMockHttpsServer;
|
51 | const createMockHttpServer = () => {
|
52 | const server = (0, http_1.createServer)();
|
53 | return server;
|
54 | };
|
55 | exports.createMockHttpServer = createMockHttpServer;
|
56 | const createMockHttp2Server = () => {
|
57 | const server = (0, http2_1.createServer)();
|
58 | return server;
|
59 | };
|
60 | exports.createMockHttp2Server = createMockHttp2Server;
|