aboutsummaryrefslogtreecommitdiff
path: root/internal/api/api_test.go
diff options
context:
space:
mode:
authorEthan Lee <ethanalee@google.com>2026-03-11 21:08:19 +0000
committerGopher Robot <gobot@golang.org>2026-03-24 10:46:32 -0700
commit9da8f15f1c5721ddc1866bda9091a7f49c63fa10 (patch)
treecaa115cf134e6d93321dc671f0adfd85c6826a1a /internal/api/api_test.go
parent3619741c8e028c954fd53a4d554c3d4efe9a2356 (diff)
downloadgo-x-pkgsite-9da8f15f1c5721ddc1866bda9091a7f49c63fa10.tar.xz
internal/api: implement module packages endpoint
- Implement the ServeModulePackages handler and introduce GetModulePackages to return the list of packages for a given module version. Change-Id: I20c1618e2fdbf0126cb913665a1d7457b8951177 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/754863 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> kokoro-CI: kokoro <noreply+kokoro@google.com> Auto-Submit: Ethan Lee <ethanalee@google.com> Reviewed-by: Jonathan Amsterdam <jba@google.com>
Diffstat (limited to 'internal/api/api_test.go')
-rw-r--r--internal/api/api_test.go58
1 files changed, 54 insertions, 4 deletions
diff --git a/internal/api/api_test.go b/internal/api/api_test.go
index 9983a8cf..a0b63904 100644
--- a/internal/api/api_test.go
+++ b/internal/api/api_test.go
@@ -322,11 +322,61 @@ func TestServeModuleVersions(t *testing.T) {
if len(got.Items) != test.wantCount {
t.Errorf("count = %d, want %d", len(got.Items), test.wantCount)
}
- if test.name == "with limit" && got.NextPageToken != "1" {
- t.Errorf("nextPageToken = %q, want %q", got.NextPageToken, "1")
+ }
+ })
+ }
+}
+
+func TestServeModulePackages(t *testing.T) {
+ ctx := context.Background()
+ ds := fakedatasource.New()
+
+ const (
+ modulePath = "example.com"
+ version = "v1.0.0"
+ )
+
+ ds.MustInsertModule(ctx, &internal.Module{
+ ModuleInfo: internal.ModuleInfo{ModulePath: modulePath, Version: version},
+ Units: []*internal.Unit{
+ {UnitMeta: internal.UnitMeta{Path: modulePath, Name: "pkg1"}},
+ {UnitMeta: internal.UnitMeta{Path: modulePath + "/sub", Name: "pkg2"}},
+ },
+ })
+
+ for _, test := range []struct {
+ name string
+ url string
+ wantStatus int
+ wantCount int
+ }{
+ {
+ name: "all packages",
+ url: "/v1/packages/example.com?version=v1.0.0",
+ wantStatus: http.StatusOK,
+ wantCount: 2,
+ },
+ } {
+ t.Run(test.name, func(t *testing.T) {
+ r := httptest.NewRequest("GET", test.url, nil)
+ w := httptest.NewRecorder()
+
+ err := ServeModulePackages(w, r, ds)
+ if err != nil {
+ t.Fatalf("ServeModulePackages returned error: %v", err)
+ }
+
+ if w.Code != test.wantStatus {
+ t.Errorf("status = %d, want %d", w.Code, test.wantStatus)
+ }
+
+ if test.wantStatus == http.StatusOK {
+ var got PaginatedResponse[Package]
+ if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
+ t.Fatalf("json.Unmarshal: %v", err)
}
- if test.name == "pagination" && got.NextPageToken != "2" {
- t.Errorf("nextPageToken = %q, want %q", got.NextPageToken, "2")
+ if len(got.Items) != test.wantCount {
+ t.Errorf("count = %d, want %d", len(got.Items), test.wantCount)
}
}
})