Files
homarr/packages/common/src/test/url.spec.ts
T
StrandedTurtleandMeier Lukas 7a1272146c fix(tracearr): support absolute image urls (#5654)
* Update base URL handling for tracearr integration

* fix: remove unnecessary prefix from bcrypt hashing

* refactor: add is-absolute-url check

* test: add unit tests

---------

Co-authored-by: Meier Lukas <meierschlumpf@gmail.com>
2026-05-18 21:39:01 +02:00

59 lines
1.5 KiB
TypeScript

import { describe, expect, test } from "vitest";
import { getPortFromUrl, isAbsoluteUrl } from "../url.js";
describe("getPortFromUrl", () => {
test.each([
[80, "http"],
[443, "https"],
])("should return %s for %s protocol without port", (expectedPort, protocol) => {
// Arrange
const url = new URL(`${protocol}://example.com`);
// Act
const port = getPortFromUrl(url);
// Assert
expect(port).toBe(expectedPort);
});
test.each([["http"], ["https"], ["anything"]])("should return the specified port for %s protocol", (protocol) => {
// Arrange
const expectedPort = 3000;
const url = new URL(`${protocol}://example.com:${expectedPort}`);
// Act
const port = getPortFromUrl(url);
// Assert
expect(port).toBe(expectedPort);
});
test("should throw an error for unsupported protocol", () => {
// Arrange
const url = new URL("ftp://example.com");
// Act
const act = () => getPortFromUrl(url);
// Act & Assert
expect(act).toThrowError("Unsupported protocol: ftp:");
});
});
describe("isAbsoluteUrl", () => {
test.each([
["http://example.com", true],
["https://example.com", true],
["ftp://example.com", true],
["file:///path/to/file", true],
["mailto:johndoe@example.com", true],
["/relative/path", false],
["relative/path", false],
])("should return %s for URL: %s", (urlString, expected) => {
// Act
const result = isAbsoluteUrl(urlString);
// Assert
expect(result).toBe(expected);
});
});