Skip to content

Commit 1edbc0b

Browse files
committed
fix: make givenHttpServerConfig typing compatible with TypeScript 3.6
Signed-off-by: Miroslav Bajtoš <mbajtoss@gmail.com>
1 parent 6651e11 commit 1edbc0b

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

packages/http-server/src/__tests__/integration/http-server.integration.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import * as fs from 'fs';
1515
import {IncomingMessage, Server, ServerResponse} from 'http';
1616
import * as os from 'os';
1717
import * as path from 'path';
18-
import {HttpOptions, HttpServer, HttpServerOptions} from '../../';
18+
import {HttpOptions, HttpServer, HttpsOptions} from '../../';
1919

2020
describe('HttpServer (integration)', () => {
2121
let server: HttpServer | undefined;
@@ -181,10 +181,7 @@ describe('HttpServer (integration)', () => {
181181
});
182182

183183
it('supports HTTPS protocol with a pfx file', async () => {
184-
const serverOptions = givenHttpServerConfig({
185-
usePfx: true,
186-
});
187-
const httpsServer: HttpServer = givenHttpsServer(serverOptions);
184+
const httpsServer: HttpServer = givenHttpsServer({usePfx: true});
188185
await httpsServer.start();
189186
const response = await httpsGetAsync(httpsServer.url);
190187
expect(response.statusCode).to.equal(200);
@@ -279,7 +276,10 @@ describe('HttpServer (integration)', () => {
279276
usePfx?: boolean;
280277
host?: string;
281278
}): HttpServer {
282-
const options: HttpServerOptions = {protocol: 'https', host};
279+
const options = givenHttpServerConfig<HttpsOptions>({
280+
protocol: 'https',
281+
host,
282+
});
283283
const certDir = path.resolve(__dirname, '../../../fixtures');
284284
if (usePfx) {
285285
const pfxPath = path.join(certDir, 'pfx.pfx');

packages/rest/src/__tests__/integration/rest.server.integration.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -672,13 +672,12 @@ paths:
672672
it('supports HTTPS protocol with key and certificate files', async () => {
673673
const keyPath = path.join(FIXTURES, 'key.pem');
674674
const certPath = path.join(FIXTURES, 'cert.pem');
675-
const options = {
675+
const serverOptions = givenHttpServerConfig({
676676
port: 0,
677677
protocol: 'https',
678678
key: fs.readFileSync(keyPath),
679679
cert: fs.readFileSync(certPath),
680-
};
681-
const serverOptions = givenHttpServerConfig(options);
680+
});
682681
const server = await givenAServer({rest: serverOptions});
683682
server.handler(dummyRequestHandler);
684683
await server.start();
@@ -689,13 +688,12 @@ paths:
689688

690689
it('supports HTTPS protocol with a pfx file', async () => {
691690
const pfxPath = path.join(FIXTURES, 'pfx.pfx');
692-
const options = {
691+
const serverOptions = givenHttpServerConfig({
693692
port: 0,
694-
protocol: 'https',
693+
protocol: 'https' as 'https',
695694
pfx: fs.readFileSync(pfxPath),
696695
passphrase: 'loopback4',
697-
};
698-
const serverOptions = givenHttpServerConfig(options);
696+
});
699697
const server = await givenAServer({rest: serverOptions});
700698
server.handler(dummyRequestHandler);
701699
await server.start();
@@ -756,13 +754,12 @@ paths:
756754
it('honors HTTPS config binding after instantiation', async () => {
757755
const keyPath = path.join(FIXTURES, 'key.pem');
758756
const certPath = path.join(FIXTURES, 'cert.pem');
759-
const options = {
757+
const serverOptions = givenHttpServerConfig({
760758
port: 0,
761759
protocol: 'https',
762760
key: undefined,
763761
cert: undefined,
764-
};
765-
const serverOptions = givenHttpServerConfig(options);
762+
});
766763
const server = await givenAServer({rest: serverOptions});
767764

768765
server.handler(dummyRequestHandler);

packages/testlab/src/http-server-config.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,29 @@
33
// This file is licensed under the MIT License.
44
// License text available at https://opensource.org/licenses/MIT
55

6-
import * as path from 'path';
76
import {readFileSync} from 'fs';
87
import {ServerOptions as HttpsServerOptions} from 'https';
8+
import {ListenOptions} from 'net';
9+
import * as path from 'path';
910

1011
const FIXTURES = path.resolve(__dirname, '../fixtures');
1112
const DUMMY_TLS_CONFIG = {
1213
key: readFileSync(path.join(FIXTURES, 'key.pem')),
1314
cert: readFileSync(path.join(FIXTURES, 'cert.pem')),
1415
};
1516

16-
export type ConfigRetval<T extends object> = T & {
17+
export interface HttpOptions extends ListenOptions {
18+
protocol?: 'http';
19+
}
20+
21+
export interface HttpsOptions extends ListenOptions, HttpsServerOptions {
22+
protocol: 'https';
23+
}
24+
25+
export type HostPort = {
1726
host: string;
1827
port: number;
19-
} & HttpsServerOptions;
28+
};
2029

2130
/**
2231
* Create an HTTP-server configuration that works well in test environments.
@@ -27,18 +36,18 @@ export type ConfigRetval<T extends object> = T & {
2736
*
2837
* @param customConfig - Additional configuration options to apply.
2938
*/
30-
export function givenHttpServerConfig<T extends object>(
31-
customConfig?: T & {protocol?: string},
32-
): ConfigRetval<T> {
39+
export function givenHttpServerConfig<T extends HttpOptions | HttpsOptions>(
40+
customConfig?: T,
41+
): HostPort & T {
3342
const defaults = {
3443
host: '127.0.0.1',
3544
port: 0,
3645
protocol: undefined,
3746
};
38-
const config: ConfigRetval<T> = Object.assign({}, defaults, customConfig);
47+
const config = Object.assign({}, defaults, customConfig);
3948
if (config.host === undefined) config.host = defaults.host;
4049
if (config.port === undefined) config.port = defaults.port;
41-
if (customConfig && customConfig.protocol === 'https') {
50+
if (isHttpsConfig(config)) {
4251
setupTlsConfig(config);
4352
}
4453
return config;
@@ -49,3 +58,9 @@ function setupTlsConfig(config: HttpsServerOptions) {
4958
if ('pfx' in config) return;
5059
Object.assign(config, DUMMY_TLS_CONFIG);
5160
}
61+
62+
function isHttpsConfig(
63+
config: HttpOptions | HttpsOptions,
64+
): config is HttpsOptions {
65+
return config && config.protocol === 'https';
66+
}

0 commit comments

Comments
 (0)