diff options
| author | Ethan Lee <ethanalee@google.com> | 2026-03-11 21:04:11 +0000 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2026-03-23 09:41:31 -0700 |
| commit | c43b677a694ff5791b1b696298c9e29ae6d221bb (patch) | |
| tree | f980f6f50b3251785b6b1925516147a4e9d52e32 /internal/api/api_test.go | |
| parent | 21d5c77a01c8874095d68e8c10a03c100fcd2070 (diff) | |
| download | go-x-pkgsite-c43b677a694ff5791b1b696298c9e29ae6d221bb.tar.xz | |
internal/api: implement module versions endpoint
- Implement module versions endpoint using ds.GetVersionsForPath
- Introduce paginate helper to generalize paginaton logic
- Update fakedatasource to correctly use V1Path to return all versions
Change-Id: Icc028bf8ca9c13978bb6eba84afe9736ccd6bcee
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/754862
Reviewed-by: Jonathan Amsterdam <jba@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Ethan Lee <ethanalee@google.com>
kokoro-CI: kokoro <noreply+kokoro@google.com>
Diffstat (limited to 'internal/api/api_test.go')
| -rw-r--r-- | internal/api/api_test.go | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/internal/api/api_test.go b/internal/api/api_test.go index f61c8581..9112c576 100644 --- a/internal/api/api_test.go +++ b/internal/api/api_test.go @@ -247,3 +247,77 @@ func TestServeModule(t *testing.T) { }) } } + +func TestServeModuleVersions(t *testing.T) { + ctx := context.Background() + ds := fakedatasource.New() + + ds.MustInsertModule(ctx, &internal.Module{ + ModuleInfo: internal.ModuleInfo{ModulePath: "example.com", Version: "v1.0.0"}, + Units: []*internal.Unit{{UnitMeta: internal.UnitMeta{Path: "example.com"}}}, + }) + ds.MustInsertModule(ctx, &internal.Module{ + ModuleInfo: internal.ModuleInfo{ModulePath: "example.com", Version: "v1.1.0"}, + Units: []*internal.Unit{{UnitMeta: internal.UnitMeta{Path: "example.com"}}}, + }) + ds.MustInsertModule(ctx, &internal.Module{ + ModuleInfo: internal.ModuleInfo{ModulePath: "example.com/v2", Version: "v2.0.0"}, + Units: []*internal.Unit{{UnitMeta: internal.UnitMeta{Path: "example.com/v2"}}}, + }) + + for _, test := range []struct { + name string + url string + wantStatus int + wantCount int + }{ + { + name: "all versions (cross-major)", + url: "/v1/versions/example.com", + wantStatus: http.StatusOK, + wantCount: 3, + }, + { + name: "with limit", + url: "/v1/versions/example.com?limit=1", + wantStatus: http.StatusOK, + wantCount: 1, + }, + { + name: "pagination", + url: "/v1/versions/example.com?limit=1&token=1", + wantStatus: http.StatusOK, + wantCount: 1, + }, + } { + t.Run(test.name, func(t *testing.T) { + r := httptest.NewRequest("GET", test.url, nil) + w := httptest.NewRecorder() + + err := ServeModuleVersions(w, r, ds) + if err != nil { + t.Fatalf("ServeModuleVersions 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[internal.ModuleInfo] + if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil { + t.Fatalf("json.Unmarshal: %v", err) + } + 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") + } + if test.name == "pagination" && got.NextPageToken != "2" { + t.Errorf("nextPageToken = %q, want %q", got.NextPageToken, "2") + } + } + }) + } +} |
