From 58080fe0c6f1298a7578f6a9a29bf97362aabe68 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Fri, 27 Mar 2026 14:22:13 -0400 Subject: internal/api: use unmarshalResponse for ServeModule Also add a test case. Change-Id: Iae30ecca8ebaaa412e9072de5123026ca4f78476 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/760480 kokoro-CI: kokoro LUCI-TryBot-Result: Go LUCI Reviewed-by: Ethan Lee --- internal/api/api_test.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/internal/api/api_test.go b/internal/api/api_test.go index caa620b4..90373b66 100644 --- a/internal/api/api_test.go +++ b/internal/api/api_test.go @@ -250,7 +250,7 @@ func TestServeModule(t *testing.T) { name string url string wantStatus int - want *Module + want any }{ { name: "basic module metadata", @@ -261,6 +261,12 @@ func TestServeModule(t *testing.T) { Version: version, }, }, + { + name: "bad version", + url: "/v1/module/example.com?version=nope", + wantStatus: http.StatusNotFound, + want: &Error{Code: 404, Message: "could not find module for import path example.com: not found"}, + }, { name: "module with readme", url: "/v1/module/example.com?version=v1.2.3&readme=true", @@ -289,11 +295,11 @@ func TestServeModule(t *testing.T) { } if test.want != nil { - var got Module - if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil { - t.Fatalf("json.Unmarshal: %v", err) + got, err := unmarshalResponse[Module](w.Body.Bytes()) + if err != nil { + t.Fatalf("unmarshaling: %v", err) } - if diff := cmp.Diff(test.want, &got); diff != "" { + if diff := cmp.Diff(test.want, got); diff != "" { t.Errorf("mismatch (-want +got):\n%s", diff) } } -- cgit v1.3