UNPKG

1.77 kBJavaScriptView Raw
1"use strict";
2// Copyright IBM Corp. 2018,2019. All Rights Reserved.
3// Node module: @loopback/testlab
4// This file is licensed under the MIT License.
5// License text available at https://opensource.org/licenses/MIT
6Object.defineProperty(exports, "__esModule", { value: true });
7const fs_1 = require("fs");
8const path = require("path");
9const FIXTURES = path.resolve(__dirname, '../fixtures');
10const DUMMY_TLS_CONFIG = {
11 key: fs_1.readFileSync(path.join(FIXTURES, 'key.pem')),
12 cert: fs_1.readFileSync(path.join(FIXTURES, 'cert.pem')),
13};
14/**
15 * Create an HTTP-server configuration that works well in test environments.
16 * - Ask the operating system to assign a free (ephemeral) port.
17 * - Use IPv4 localhost `127.0.0.1` to avoid known IPv6 issues in Docker-based
18 * environments like Travis-CI.
19 * - Provide default TLS key & cert when `protocol` is set to `https`.
20 *
21 * @param customConfig - Additional configuration options to apply.
22 */
23function givenHttpServerConfig(customConfig) {
24 const defaults = {
25 host: '127.0.0.1',
26 port: 0,
27 protocol: undefined,
28 };
29 const config = Object.assign({}, defaults, customConfig);
30 if (config.host === undefined)
31 config.host = defaults.host;
32 if (config.port === undefined)
33 config.port = defaults.port;
34 if (isHttpsConfig(config)) {
35 setupTlsConfig(config);
36 }
37 return config;
38}
39exports.givenHttpServerConfig = givenHttpServerConfig;
40function setupTlsConfig(config) {
41 if ('key' in config && 'cert' in config)
42 return;
43 if ('pfx' in config)
44 return;
45 Object.assign(config, DUMMY_TLS_CONFIG);
46}
47function isHttpsConfig(config) {
48 return config && config.protocol === 'https';
49}
50//# sourceMappingURL=http-server-config.js.map
\No newline at end of file