fix(unifi): port is ignored (#2995)
This commit is contained in:
40
packages/common/src/test/url.spec.ts
Normal file
40
packages/common/src/test/url.spec.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
|
||||
import { getPortFromUrl } from "../url";
|
||||
|
||||
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:");
|
||||
});
|
||||
});
|
||||
@@ -21,3 +21,20 @@ export const extractBaseUrlFromHeaders = (
|
||||
|
||||
return `${protocol}://${host}`;
|
||||
};
|
||||
|
||||
export const getPortFromUrl = (url: URL): number => {
|
||||
const port = url.port;
|
||||
if (port) {
|
||||
return Number(port);
|
||||
}
|
||||
|
||||
if (url.protocol === "https:") {
|
||||
return 443;
|
||||
}
|
||||
|
||||
if (url.protocol === "http:") {
|
||||
return 80;
|
||||
}
|
||||
|
||||
throw new Error(`Unsupported protocol: ${url.protocol}`);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user