fix(api): serve per-package analytics for names with slashes

The route matched one path segment, so scoped npm names and Go module
paths returned 404. It now takes the rest of the path as the name.
This commit is contained in:
2026-11-24 00:49:27 +00:00
parent a511544d7a
commit b248bfed31
4 changed files with 23 additions and 4 deletions
+18
View File
@@ -9,6 +9,7 @@ import (
"testing"
"time"
"github.com/lukaszraczylo/gohoarder/pkg/analytics"
"github.com/lukaszraczylo/gohoarder/pkg/config"
"github.com/lukaszraczylo/gohoarder/pkg/metadata"
)
@@ -251,3 +252,20 @@ func TestVulnerabilityDetailsExplainVerdictAndScanners(t *testing.T) {
t.Errorf("clean decision = %v", plain["decision"])
}
}
// Scoped npm names and Go module paths contain slashes; the per-package analytics route must accept them.
func TestPackageAnalyticsAcceptsNamesWithSlashes(t *testing.T) {
a := newTestApp(t, nil)
for _, name := range []string{"@types/node", "github.com/google/uuid/@v/v1.6.0.zip", "lodash"} {
a.analyticsEngine.TrackDownload(analytics.PackageDownload{Registry: "npm", Name: name, Version: "1.0.0", Timestamp: time.Now(), BytesSize: 5})
}
for _, name := range []string{"@types/node", "github.com/google/uuid/@v/v1.6.0.zip", "lodash"} {
out, body := getJSON(t, a, "/api/analytics/package/npm/"+name)
if jsonAt(out, "package", "TotalDownloads") != float64(1) {
t.Errorf("analytics for %q = %s, want one download", name, body)
}
}
if code, _, _ := doRequest(t, a, "/api/analytics/package/npm/@types/missing", nil); code != http.StatusNotFound {
t.Errorf("unknown package = %d, want 404", code)
}
}