From 48c90cf2397cd53fba0408f300a86be1599d690f Mon Sep 17 00:00:00 2001 From: Alberto Ricart Date: Mon, 31 Jan 2022 17:38:06 -0600 Subject: [PATCH] added test for hostname only --- nats-base-client/servers.ts | 3 ++- tests/servers_test.ts | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/nats-base-client/servers.ts b/nats-base-client/servers.ts index a4a8dd7a..7175397a 100644 --- a/nats-base-client/servers.ts +++ b/nats-base-client/servers.ts @@ -25,13 +25,14 @@ import { defaultPort, getUrlParseFn } from "./transport.ts"; import { shuffle } from "./util.ts"; import { isIP } from "./ipparser.ts"; -function isIPV4OrHostname(hp: string): boolean { +export function isIPV4OrHostname(hp: string): boolean { if (hp.indexOf(".") !== -1) { return true; } if (hp.indexOf("[") !== -1 || hp.indexOf("::") !== -1) { return false; } + // if we have a plain hostname or host:port if (hp.split(":").length <= 2) { return true; } diff --git a/tests/servers_test.ts b/tests/servers_test.ts index 7d0caa98..b393d74a 100644 --- a/tests/servers_test.ts +++ b/tests/servers_test.ts @@ -13,7 +13,7 @@ * limitations under the License. * */ -import { Servers } from "../nats-base-client/servers.ts"; +import { isIPV4OrHostname, Servers } from "../nats-base-client/servers.ts"; import { assertEquals } from "https://deno.land/std@0.95.0/testing/asserts.ts"; import type { ServerInfo } from "../nats-base-client/types.ts"; import { setTransportFactory } from "../nats-base-client/internal_mod.ts"; @@ -126,3 +126,8 @@ Deno.test("servers - port 80", () => { t("[2001:db8:4006:812::200e]:8080", 8080); t("::1", 4222); }); + +Deno.test("servers - hostname only", () => { + assertEquals(isIPV4OrHostname("hostname"), true); + assertEquals(isIPV4OrHostname("hostname:40"), true); +});