deno.land / std@0.224.0 / net / get_available_port_test.ts

get_available_port_test.ts
View Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { getAvailablePort } from "./get_available_port.ts";import { assertEquals } from "../assert/mod.ts";
/** Helper function to see if a port is indeed available for listening (race-y) */async function testWithPort(port: number) { const server = Deno.serve({ port, async onListen() { const resp = await fetch(`http://localhost:${port}`); const text = await resp.text(); assertEquals(text, "hello"); server.shutdown(); }, }, () => new Response("hello"));
await server.finished;}
Deno.test("getAvailablePort() gets an available port", async () => { const port = getAvailablePort(); await testWithPort(port);});
Deno.test("getAvailablePort() gets an available port with a preferred port", async () => { const port = getAvailablePort({ preferredPort: 9563 }); await testWithPort(port);});
std

Version Info

Tagged at
6 months ago