-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: unit test the port finding function
- Loading branch information
1 parent
983a0aa
commit a421822
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"use strict"; | ||
|
||
const net = require("net"); | ||
const util = require("util"); | ||
const getPort = require("../../lib/getPort"); | ||
|
||
it("it should bind to the preferred port", async () => { | ||
const preferredPort = 8080; | ||
const port = await getPort(8080); | ||
expect(port).toBe(preferredPort); | ||
}); | ||
|
||
it("should pick the next port if the preferred port is unavailable", async () => { | ||
const preferredPort = 8345; | ||
const server = net.createServer(); | ||
server.unref(); | ||
await util.promisify(server.listen.bind(server))(preferredPort); | ||
const port = await getPort(preferredPort); | ||
expect(port).toBe(preferredPort + 1); | ||
}); | ||
|
||
it("should reject privileged ports", async () => { | ||
try { | ||
await getPort(80); | ||
} catch (e) { | ||
expect(e.message).toBeDefined(); | ||
} | ||
}); | ||
|
||
it("should reject too high port numbers", async () => { | ||
try { | ||
await getPort(65536); | ||
} catch (e) { | ||
expect(e.message).toBeDefined(); | ||
} | ||
}); |