|
1 | 1 | import { Server } from "node:net";
|
| 2 | +import { networkInterfaces } from "node:os"; |
2 | 3 | import { describe, test, expect, beforeEach, afterEach, vi } from "vitest";
|
3 | 4 | import { getPort, getRandomPort } from "../src";
|
4 |
| -import { _generateRange } from "../src/_internal"; |
| 5 | +import { _generateRange, _getLocalHosts } from "../src/_internal"; |
5 | 6 | import { blockPort } from "./utils";
|
6 | 7 |
|
7 | 8 | const isWindows = process.platform === "win32";
|
@@ -165,3 +166,61 @@ describe("internal tools", () => {
|
165 | 166 | });
|
166 | 167 | });
|
167 | 168 | });
|
| 169 | + |
| 170 | +vi.mock("node:os", () => { |
| 171 | + return { |
| 172 | + networkInterfaces: vi.fn(), |
| 173 | + }; |
| 174 | +}); |
| 175 | + |
| 176 | +describe("_getLocalHosts", () => { |
| 177 | + test("should return the allowed host addresses", () => { |
| 178 | + vi.mocked(networkInterfaces).mockImplementation(() => ({ |
| 179 | + eth0: [ |
| 180 | + { |
| 181 | + address: "192.168.1.100", |
| 182 | + family: "IPv4", |
| 183 | + internal: false, |
| 184 | + netmask: "0", |
| 185 | + mac: "0", |
| 186 | + cidr: "", |
| 187 | + }, |
| 188 | + { |
| 189 | + address: "fe80::1", |
| 190 | + family: "IPv6", |
| 191 | + internal: false, |
| 192 | + scopeid: 1, |
| 193 | + netmask: "0", |
| 194 | + mac: "0", |
| 195 | + cidr: "", |
| 196 | + }, |
| 197 | + ], |
| 198 | + lo: [ |
| 199 | + { |
| 200 | + address: "127.0.0.1", |
| 201 | + family: "IPv4", |
| 202 | + internal: true, |
| 203 | + netmask: "0", |
| 204 | + mac: "0", |
| 205 | + cidr: "", |
| 206 | + }, |
| 207 | + { |
| 208 | + address: "169.254.0.1", |
| 209 | + family: "IPv4", |
| 210 | + internal: false, |
| 211 | + netmask: "0", |
| 212 | + mac: "0", |
| 213 | + cidr: "", |
| 214 | + }, |
| 215 | + ], |
| 216 | + })); |
| 217 | + |
| 218 | + // call the function with additional hosts |
| 219 | + const additionalHosts = ["192.168.1.200"]; |
| 220 | + const result = _getLocalHosts(additionalHosts); |
| 221 | + |
| 222 | + expect(result).toEqual(["192.168.1.200", "192.168.1.100"]); |
| 223 | + |
| 224 | + vi.clearAllMocks(); |
| 225 | + }); |
| 226 | +}); |
0 commit comments