Skip to content

Commit

Permalink
fix(analyze): Clean consecutive slashes
Browse files Browse the repository at this point in the history
  • Loading branch information
iBug committed Dec 6, 2024
1 parent c1a21af commit bba3e1e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
9 changes: 7 additions & 2 deletions pkg/analyze/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,19 @@ func AdjacentPrefix(p netip.Prefix) netip.Prefix {

func TruncateURLPath(input string) string {
parts := strings.SplitN(input, "?", 2)
path := parts[0]
path := filepath.Clean(parts[0])
if strings.HasSuffix(parts[0], "/") {
// filepath.Clean removes trailing slash
// Add it back to preserve directory notation
path += "/"
}
args := ""
if len(parts) == 2 {
args = "?..."
}
count := strings.Count(path, "/")
if count <= 2 {
return input + args
return path + args
}
parts = strings.Split(path, "/")
if parts[len(parts)-1] == "" {
Expand Down
3 changes: 3 additions & 0 deletions pkg/analyze/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func TestAdjacentPrefix(t *testing.T) {
func TestTruncateURLPath(t *testing.T) {
testCases := [][2]string{
{"/example/a/b/c/d/e/file.ext", "/example/.../file.ext"},
{"///example//merge/slashes///file.ext", "/example/.../file.ext"},
{"/example/a/b/c/d/e/dir/", "/example/.../dir/"},
{"/short/", "/short/"},
{"/short/file.ext", "/short/file.ext"},
Expand All @@ -43,12 +44,14 @@ func TestTruncateURLPathLen(t *testing.T) {
testCases := []testCase{
{"/example/a/b/c/d/e/file.ext", 30, "/example/.../file.ext"},
{"/example/a/b/c/d/e/dir/", 30, "/example/.../dir/"},
{"///example//merge/slashes///dir/", 30, "/example/.../dir/"},
{"/with/args/?a=1&b=2", 30, "/with/args/?..."},
{"/with/args/?a=1&b=2", 13, "/with/args/?"},
{"/with/args/?a=1&b=2", 12, "/with/args/?"},
{"/with/args/?a=1&b=2", 11, "/with/args/"},
{"/with/args/?a=1&b=2", 8, "/with/*/"},
{"/with/args/?a=1&b=2", 4, "/wit"},
{"///with//args/?a=1&b=2", 4, "/wit"},

{"/example/a/b/c/d/e/file.with.long.name.ext", 30, "/example/.../file.with.l...ext"},
{"/example/file.with.very.long.name.ext", 30, "/example/file.with.very....ext"},
Expand Down

0 comments on commit bba3e1e

Please sign in to comment.