Skip to content

Commit 30310e7

Browse files
committed
test: cover Go module proxy HTTP responses; lift timeout to const
Add an httptest-backed test for ValidateGoModuleExists covering 200/404/410/500 responses and asserting the case-encoded proxy request path. Exporting the function makes it testable from the external test package without hitting the real proxy. Also lift the hardcoded 10s client timeout to a package const.
1 parent f6fa124 commit 30310e7

2 files changed

Lines changed: 45 additions & 5 deletions

File tree

‎internal/validators/registries/go.go‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var (
1919
const (
2020
goGitHubNamespacePrefix = "io.github."
2121
goGitHubModulePrefix = "github.com/"
22+
goProxyRequestTimeout = 10 * time.Second
2223
)
2324

2425
// ValidateGo validates that a Go module package is owned by the publisher and
@@ -58,7 +59,7 @@ func ValidateGo(ctx context.Context, pkg model.Package, serverName string) error
5859
return err
5960
}
6061

61-
return validateGoModuleExists(ctx, pkg.RegistryBaseURL, pkg.Identifier, pkg.Version)
62+
return ValidateGoModuleExists(ctx, pkg.RegistryBaseURL, pkg.Identifier, pkg.Version)
6263
}
6364

6465
// ValidateGoModuleOwnership checks that the module path is rooted at the GitHub
@@ -85,9 +86,9 @@ func ValidateGoModuleOwnership(modulePath, serverName string) error {
8586
return nil
8687
}
8788

88-
// validateGoModuleExists confirms the module version is available on the Go
89-
// module proxy.
90-
func validateGoModuleExists(ctx context.Context, baseURL, modulePath, version string) error {
89+
// ValidateGoModuleExists confirms the module version is available on the Go
90+
// module proxy at baseURL.
91+
func ValidateGoModuleExists(ctx context.Context, baseURL, modulePath, version string) error {
9192
escapedModule, err := EscapeGoModulePath(modulePath)
9293
if err != nil {
9394
return fmt.Errorf("invalid Go module path '%s': %w", modulePath, err)
@@ -99,7 +100,7 @@ func validateGoModuleExists(ctx context.Context, baseURL, modulePath, version st
99100

100101
requestURL := fmt.Sprintf("%s/%s/@v/%s.info", strings.TrimRight(baseURL, "/"), escapedModule, escapedVersion)
101102

102-
client := &http.Client{Timeout: 10 * time.Second}
103+
client := &http.Client{Timeout: goProxyRequestTimeout}
103104
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestURL, nil)
104105
if err != nil {
105106
return fmt.Errorf("failed to create request: %w", err)

‎internal/validators/registries/go_test.go‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package registries_test
22

33
import (
44
"context"
5+
"net/http"
6+
"net/http/httptest"
57
"strings"
68
"testing"
79

@@ -64,6 +66,43 @@ func TestEscapeGoModulePath(t *testing.T) {
6466
}
6567
}
6668

69+
func TestValidateGoModuleExists(t *testing.T) {
70+
cases := []struct {
71+
name string
72+
status int
73+
wantErr bool
74+
wantSubstr string
75+
}{
76+
{"found", http.StatusOK, false, ""},
77+
{"not found", http.StatusNotFound, true, "not found"},
78+
{"gone", http.StatusGone, true, "not found"},
79+
{"server error", http.StatusInternalServerError, true, "unexpected status"},
80+
}
81+
82+
for _, tc := range cases {
83+
t.Run(tc.name, func(t *testing.T) {
84+
var gotPath string
85+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
86+
gotPath = r.URL.Path
87+
w.WriteHeader(tc.status)
88+
}))
89+
defer srv.Close()
90+
91+
err := registries.ValidateGoModuleExists(context.Background(), srv.URL, "github.com/Alice/mcp-foo", "v1.0.0")
92+
if (err != nil) != tc.wantErr {
93+
t.Fatalf("ValidateGoModuleExists() error = %v, wantErr %v", err, tc.wantErr)
94+
}
95+
if tc.wantErr && !strings.Contains(err.Error(), tc.wantSubstr) {
96+
t.Errorf("error = %q, want substring %q", err.Error(), tc.wantSubstr)
97+
}
98+
// The module path is case-encoded for the proxy ('Alice' -> '!alice').
99+
if want := "/github.com/!alice/mcp-foo/@v/v1.0.0.info"; gotPath != want {
100+
t.Errorf("proxy request path = %q, want %q", gotPath, want)
101+
}
102+
})
103+
}
104+
}
105+
67106
func TestValidateGoGuardErrors(t *testing.T) {
68107
ctx := context.Background()
69108
base := model.Package{

0 commit comments

Comments
 (0)