diff options
| author | Ethan Lee <ethanalee@google.com> | 2026-03-11 21:00:08 +0000 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2026-03-19 11:14:09 -0700 |
| commit | ab4b6a99d7828154fc803870bbb2f18835d6470b (patch) | |
| tree | 5c0613956d2f6498d2cb4d5e38d69b80ebe6f9ba /internal/api/api_test.go | |
| parent | 6d0faac5e074a8a4e101da820e7e21a8a6c2ff2d (diff) | |
| download | go-x-pkgsite-ab4b6a99d7828154fc803870bbb2f18835d6470b.tar.xz | |
internal/api: implement module metadata endpoint
Change-Id: Id6b8686012803c88c9b7ea71e4b1c0058b7967b0
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/754861
Auto-Submit: Ethan Lee <ethanalee@google.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
kokoro-CI: kokoro <noreply+kokoro@google.com>
Diffstat (limited to 'internal/api/api_test.go')
| -rw-r--r-- | internal/api/api_test.go | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/internal/api/api_test.go b/internal/api/api_test.go index e087b13e..f61c8581 100644 --- a/internal/api/api_test.go +++ b/internal/api/api_test.go @@ -166,3 +166,84 @@ func TestServePackage(t *testing.T) { }) } } + +func TestServeModule(t *testing.T) { + ctx := context.Background() + ds := fakedatasource.New() + + const ( + modulePath = "example.com" + version = "v1.2.3" + ) + + ds.MustInsertModule(ctx, &internal.Module{ + ModuleInfo: internal.ModuleInfo{ + ModulePath: modulePath, + Version: version, + }, + Units: []*internal.Unit{{ + UnitMeta: internal.UnitMeta{ + Path: modulePath, + ModuleInfo: internal.ModuleInfo{ + ModulePath: modulePath, + Version: version, + }, + }, + Readme: &internal.Readme{Filepath: "README.md", Contents: "Hello world"}, + }}, + }) + + for _, test := range []struct { + name string + url string + wantStatus int + want *Module + }{ + { + name: "basic module metadata", + url: "/v1/module/example.com?version=v1.2.3", + wantStatus: http.StatusOK, + want: &Module{ + Path: modulePath, + Version: version, + }, + }, + { + name: "module with readme", + url: "/v1/module/example.com?version=v1.2.3&readme=true", + wantStatus: http.StatusOK, + want: &Module{ + Path: modulePath, + Version: version, + Readme: &Readme{ + Filepath: "README.md", + Contents: "Hello world", + }, + }, + }, + } { + t.Run(test.name, func(t *testing.T) { + r := httptest.NewRequest("GET", test.url, nil) + w := httptest.NewRecorder() + + err := ServeModule(w, r, ds) + if err != nil { + t.Fatalf("ServeModule returned error: %v", err) + } + + if w.Code != test.wantStatus { + t.Errorf("status = %d, want %d", w.Code, test.wantStatus) + } + + if test.want != nil { + var got Module + if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil { + t.Fatalf("json.Unmarshal: %v", err) + } + if diff := cmp.Diff(test.want, &got); diff != "" { + t.Errorf("mismatch (-want +got):\n%s", diff) + } + } + }) + } +} |
