Skip to content

Commit 0687cdc

Browse files
Merge pull request #362 from IABTechLab/sch-UID2-4073-high-level-tests-path-normalisation-metrics
sch-UID2-4073 high level tests for path normalisation in metrics
2 parents 311a548 + 961db70 commit 0687cdc

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.uid2.shared.util;
2+
3+
import io.vertx.core.http.impl.HttpUtils;
4+
import java.util.Set;
5+
6+
public class HTTPPathMetricFilter {
7+
public static String filterPath(String actualPath, Set<String> pathSet) {
8+
try {
9+
String normalized = HttpUtils.normalizePath(actualPath).split("\\?")[0];
10+
if (normalized.charAt(normalized.length() - 1) == '/') {
11+
normalized = normalized.substring(0, normalized.length() - 1);
12+
}
13+
normalized = normalized.toLowerCase();
14+
return pathSet == null || pathSet.isEmpty() || pathSet.contains(normalized) ? normalized : "/unknown";
15+
} catch (IllegalArgumentException e) {
16+
return "/parsing_error";
17+
}
18+
}
19+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.uid2.shared.util;
2+
3+
import org.junit.jupiter.params.ParameterizedTest;
4+
import org.junit.jupiter.params.provider.CsvSource;
5+
import org.junit.jupiter.params.provider.ValueSource;
6+
7+
import java.util.Set;
8+
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
11+
public class HTTPPathMetricFilterTest {
12+
final Set<String> pathSet = Set.of("/v1/identity/map", "/token/refresh");
13+
14+
@ParameterizedTest
15+
@ValueSource(strings = {
16+
"",
17+
"/",
18+
"/unknown-path",
19+
"../",
20+
"/v1/identity/map%55",
21+
})
22+
void testPathFiltering_InvalidPaths_Unknown(String actualPath) {
23+
String filteredPath = HTTPPathMetricFilter.filterPath(actualPath, pathSet);
24+
assertEquals("/unknown", filteredPath);
25+
}
26+
27+
@ParameterizedTest
28+
@ValueSource(strings = {
29+
"v1/identity/map?id=bad-escape-code%2",
30+
"token/refresh?refresh_token=SOME_TOKEN<%=7485*4353%>",
31+
})
32+
void testPathFiltering_InvalidPaths_ParsingError(String actualPath) {
33+
String filteredPath = HTTPPathMetricFilter.filterPath(actualPath, pathSet);
34+
assertEquals("/parsing_error", filteredPath);
35+
}
36+
37+
@ParameterizedTest
38+
@CsvSource(value = {
39+
"/v1/identity/map, /v1/identity/map",
40+
"v1/identity/map, /v1/identity/map",
41+
"V1/IdenTity/mAp, /v1/identity/map",
42+
"./v1//identity//map/, /v1/identity/map",
43+
"../v1/identity/./map, /v1/identity/map",
44+
"/v1/identity/new/path/../../map, /v1/identity/map",
45+
"token/refresh?refresh_token=123%20%23, /token/refresh",
46+
"v1/identity/map?identity/../map/, /v1/identity/map",
47+
48+
})
49+
void testPathFiltering_ValidPaths_KnownEndpoints(String actualPath, String expectedFilteredPath) {
50+
String filteredPath = HTTPPathMetricFilter.filterPath(actualPath, pathSet);
51+
assertEquals(expectedFilteredPath, filteredPath);
52+
}
53+
}

0 commit comments

Comments
 (0)