1 parent
c60d2b1de2
commit
52701266ac
3 files changed
+47
No files matched your search
@@ -103,6 +103,10 @@
|
||||
"types": "./src/dpopCommon.ts",
|
||||
"import": "./src/dpopCommon.ts"
|
||||
},
|
||||
"./urlDiagnostics": {
|
||||
"types": "./src/urlDiagnostics.ts",
|
||||
"import": "./src/urlDiagnostics.ts"
|
||||
},
|
||||
"./relayAuth": {
|
||||
"types": "./src/relayAuth.ts",
|
||||
"import": "./src/relayAuth.ts"
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import { assert, describe, it } from "@effect/vitest";
|
||||
|
||||
import { getUrlDiagnostics } from "./urlDiagnostics.ts";
|
||||
|
||||
describe("getUrlDiagnostics", () => {
|
||||
it("retains only input length, protocol, and hostname for valid URLs", () => {
|
||||
const input =
|
||||
"https://user:[email protected]:8443/private/path?access_token=secret#fragment";
|
||||
|
||||
assert.deepStrictEqual(getUrlDiagnostics(input), {
|
||||
inputLength: input.length,
|
||||
protocol: "https:",
|
||||
hostname: "example.com",
|
||||
});
|
||||
});
|
||||
|
||||
it("returns only input length for invalid URLs", () => {
|
||||
const input = "not a URL?access_token=secret";
|
||||
|
||||
assert.deepStrictEqual(getUrlDiagnostics(input), {
|
||||
inputLength: input.length,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,19 @@
|
||||
export interface UrlDiagnostics {
|
||||
readonly inputLength: number;
|
||||
readonly protocol?: string;
|
||||
readonly hostname?: string;
|
||||
}
|
||||
|
||||
export function getUrlDiagnostics(input: string): UrlDiagnostics {
|
||||
const inputLength = input.length;
|
||||
try {
|
||||
const url = new URL(input);
|
||||
return {
|
||||
inputLength,
|
||||
protocol: url.protocol,
|
||||
hostname: url.hostname,
|
||||
};
|
||||
} catch {
|
||||
return { inputLength };
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user