Skip to content

Commit ee28b50

Browse files
committed
fix(common): servers#listen() port number validation
Fixes hyperledger-cacti#383 Ensures that the when specifying port 0 the liste() method does not throw an exception since it is perfectly valid to bind to port zero in general. Signed-off-by: Peter Somogyvari <[email protected]>
1 parent 4d93c72 commit ee28b50

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

packages/cactus-common/src/main/typescript/servers.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ export class Servers {
4343

4444
Checks.truthy(options, `${fnTag} arg options`);
4545
Checks.truthy(options.server, `${fnTag} arg options.server`);
46-
Checks.truthy(options.port, `${fnTag} arg options.port`);
46+
Checks.truthy(
47+
options.port || options.port === 0,
48+
`${fnTag} arg options.port`
49+
);
4750
Checks.truthy(options.hostname, `${fnTag} arg options.hostname`);
4851
const { server, port, hostname } = options;
4952

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { createServer } from "http";
2+
3+
import test, { Test } from "tape-promise/tape";
4+
5+
import { Servers } from "../../../main/typescript/index";
6+
7+
test("Servers", async (tParent: Test) => {
8+
test("Servers#listen()", async (t: Test) => {
9+
{
10+
const server = createServer();
11+
await t.rejects(
12+
Servers.listen({ hostname: "x", port: "" as any, server }),
13+
/options\.port/,
14+
"Rejects when port specified as empty string OK"
15+
);
16+
}
17+
18+
{
19+
const server = createServer();
20+
await t.rejects(
21+
Servers.listen({ hostname: "localhost", port: false as any, server }),
22+
/options\.port/,
23+
"Rejects when port specified as literal false boolean OK"
24+
);
25+
// await Servers.shutdown(server);
26+
}
27+
28+
{
29+
const server = createServer();
30+
await t.doesNotReject(
31+
Servers.listen({ hostname: "localhost", port: 0, server }),
32+
"Does not rejects when port specified as zero OK"
33+
);
34+
await Servers.shutdown(server);
35+
}
36+
37+
t.end();
38+
});
39+
40+
tParent.end();
41+
});

0 commit comments

Comments
 (0)