()
| 65 | // ── Tests ──────────────────────────────────────────────────────────── |
| 66 | |
| 67 | function runTests() { |
| 68 | console.log("Running path encoding tests...\n"); |
| 69 | |
| 70 | let passed = 0; |
| 71 | |
| 72 | // ── encodePath unit tests ──────────────────────────────────────── |
| 73 | |
| 74 | // Test 1: null/undefined/empty |
| 75 | { |
| 76 | assertEquals(encodePath(null), "", "null should return empty"); |
| 77 | assertEquals( |
| 78 | encodePath(undefined), |
| 79 | "", |
| 80 | "undefined should return empty" |
| 81 | ); |
| 82 | assertEquals(encodePath(""), "", "empty string should return empty"); |
| 83 | console.log(" PASS: encodePath handles null/undefined/empty"); |
| 84 | passed++; |
| 85 | } |
| 86 | |
| 87 | // Test 2: root path |
| 88 | { |
| 89 | assertEquals(encodePath("/"), "2f", "/ should encode to 2f"); |
| 90 | console.log(" PASS: encodePath encodes root path"); |
| 91 | passed++; |
| 92 | } |
| 93 | |
| 94 | // Test 3: alphanumeric passthrough |
| 95 | { |
| 96 | assertEquals(encodePath("/api"), "2fapi", "/api encodes slash only"); |
| 97 | assertEquals(encodePath("/v1"), "2fv1", "/v1 encodes slash only"); |
| 98 | assertEquals(encodePath("abc"), "abc", "plain alpha passes through"); |
| 99 | console.log(" PASS: encodePath preserves alphanumeric chars"); |
| 100 | passed++; |
| 101 | } |
| 102 | |
| 103 | // Test 4: all special chars produce unique hex |
| 104 | { |
| 105 | const paths = ["/a/b", "/a-b", "/a.b", "/a_b", "/a b"]; |
| 106 | const results = paths.map((p) => encodePath(p)); |
| 107 | const unique = new Set(results); |
| 108 | assertEquals( |
| 109 | unique.size, |
| 110 | paths.length, |
| 111 | "all special-char paths must produce unique encodings" |
| 112 | ); |
| 113 | console.log( |
| 114 | " PASS: encodePath produces unique output for different special chars" |
| 115 | ); |
| 116 | passed++; |
| 117 | } |
| 118 | |
| 119 | // Test 5: output is always alphanumeric (safe for Traefik names) |
| 120 | { |
| 121 | const paths = [ |
| 122 | "/", |
| 123 | "/api", |
| 124 | "/a/b", |
no test coverage detected