diff --git a/app/client/src/utils/AppsmithUtils.test.ts b/app/client/src/utils/AppsmithUtils.test.ts index 865aa3eb39..cd4dbcb2a8 100644 --- a/app/client/src/utils/AppsmithUtils.test.ts +++ b/app/client/src/utils/AppsmithUtils.test.ts @@ -1,4 +1,5 @@ import { areArraysEqual, getCamelCaseString } from "utils/AppsmithUtils"; +import { isURL } from "./TypeHelpers"; describe("getCamelCaseString", () => { it("Should return a string in camelCase", () => { @@ -29,3 +30,29 @@ describe("test areArraysEqual", () => { expect(areArraysEqual(OGArray, testArray)).toBe(true); }); }); + +describe("isURL", () => { + test("returns true for valid URLs", () => { + expect(isURL("http://example.com")).toBe(true); + expect(isURL("https://www.google.com/search?q=javascript")).toBe(true); + expect(isURL("https://en.wikipedia.org/wiki/Regular_expression")).toBe( + true, + ); + expect( + isURL("https://www.example.com/path(withparentheses)/file.html"), + ).toBe(true); + expect( + isURL("https://www.example.com/path[withparentheses]/file_(1)[2].html"), + ).toBe(true); + }); + + test("returns false for invalid URLs", () => { + expect(isURL("http://localhost:3000")).toBe(false); + expect(isURL("not a URL")).toBe(false); + expect(isURL("ftp:/example.com")).toBe(false); + expect(isURL("http://example.")).toBe(false); + expect(isURL("http://localhost:port")).toBe(false); + expect(isURL("notAURL")).toBe(false); + expect(isURL("httpsnotAURL")).toBe(false); + }); +}); diff --git a/app/client/src/utils/TypeHelpers.ts b/app/client/src/utils/TypeHelpers.ts index 39f1aff515..ab99fa606f 100644 --- a/app/client/src/utils/TypeHelpers.ts +++ b/app/client/src/utils/TypeHelpers.ts @@ -27,10 +27,10 @@ export const getType = (value: unknown) => { export function isURL(str: string) { const pattern = new RegExp( - "^((blob:)?https?:\\/\\/)?" + // protocol + "^((blob:)?https?:\\/\\/)?" + //protocol "((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|" + // domain name "((\\d{1,3}\\.){3}\\d{1,3}))" + // OR ip (v4) address - "(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*" + // port and path + "(\\:\\d+)?(/[^?#]*)?" + // port and path "(\\?[;&a-z\\d%_.~+=-]*)?" + // query string "(\\#[-a-z\\d_]*)?$", "i",