Skip to content

Commit 903ae07

Browse files
committed
Test that encoded commas in compare URLs are decoded correctly
1 parent ccde26b commit 903ae07

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

internal/ui/compare/handler_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,38 @@ func TestHandleCompare(t *testing.T) {
6262
}
6363
}
6464

65+
func TestHandleCompare_EncodedComma(t *testing.T) {
66+
manifest, _ := layout.NewManifest([]byte(`{}`))
67+
var lookedUp []string
68+
repo := &mockRepo{
69+
findSeriesByNameFunc: func(ctx context.Context, name string, _, _, _, _ int) (*packages.PackagePopularityList, error) {
70+
lookedUp = append(lookedUp, name)
71+
return &packages.PackagePopularityList{
72+
Total: 1,
73+
PackagePopularities: []packages.PackagePopularity{
74+
{Name: name, StartMonth: 202501, EndMonth: 202501, Popularity: 10.5},
75+
},
76+
}, nil
77+
},
78+
}
79+
handler := NewHandler(repo, manifest)
80+
81+
// %2C is a comma encoded by other apps when sharing the URL
82+
req := httptest.NewRequest(http.MethodGet, "/compare/packages/pacman%2Cglibc", nil)
83+
req.SetPathValue("names", "pacman,glibc")
84+
rr := httptest.NewRecorder()
85+
86+
handler.HandleCompare(rr, req)
87+
88+
if rr.Code != http.StatusOK {
89+
t.Errorf("expected status 200, got %d", rr.Code)
90+
}
91+
92+
if len(lookedUp) != 2 || lookedUp[0] != "pacman" || lookedUp[1] != "glibc" {
93+
t.Errorf("expected individual lookups for [pacman glibc], got %v", lookedUp)
94+
}
95+
}
96+
6597
func TestHandleCompare_SeriesError(t *testing.T) {
6698
manifest, _ := layout.NewManifest([]byte(`{}`))
6799
repo := &mockRepo{

internal/ui/packagelist/handler_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,32 @@ func TestHandlePackages_WithCompare(t *testing.T) {
121121
}
122122
}
123123

124+
func TestHandlePackages_WithCompareEncoded(t *testing.T) {
125+
manifest, _ := layout.NewManifest([]byte(`{}`))
126+
var lookedUp []string
127+
repo := &mockRepo{
128+
findByNameFunc: func(_ context.Context, name string, _, _ int) (*packages.PackagePopularity, error) {
129+
lookedUp = append(lookedUp, name)
130+
return &packages.PackagePopularity{Name: name, Popularity: 5.0}, nil
131+
},
132+
}
133+
handler := NewHandler(repo, manifest)
134+
135+
// %2C is a comma encoded by other apps when sharing the URL
136+
req := httptest.NewRequest(http.MethodGet, "/packages?compare=glibc%2Clinux", nil)
137+
rr := httptest.NewRecorder()
138+
139+
handler.HandlePackages(rr, req)
140+
141+
if rr.Code != http.StatusOK {
142+
t.Errorf("expected status 200, got %d", rr.Code)
143+
}
144+
145+
if len(lookedUp) != 2 || lookedUp[0] != "glibc" || lookedUp[1] != "linux" {
146+
t.Errorf("expected individual lookups for [glibc linux], got %v", lookedUp)
147+
}
148+
}
149+
124150
func TestHandlePackages_WithCompareAndQuery(t *testing.T) {
125151
manifest, _ := layout.NewManifest([]byte(`{}`))
126152
findAllCalled := false

0 commit comments

Comments
 (0)