Skip to content

Commit

Permalink
added test for hostname only
Browse files Browse the repository at this point in the history
  • Loading branch information
aricart committed Jan 31, 2022
1 parent 09d03a1 commit 48c90cf
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
3 changes: 2 additions & 1 deletion nats-base-client/servers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
7 changes: 6 additions & 1 deletion tests/servers_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
});

0 comments on commit 48c90cf

Please sign in to comment.