Watch
1
0
Fork
You've already forked pkg-proxy
1
mirror of https://github.com/git-pkgs/proxy.git synced 2026-08-01 10:08:08 -04:00
pkg-proxy/internal/handler/go_test.go

82 lines
1.8 KiB
Go
Raw Permalink Normal View History

2026-01-20 21:52:44 +00:00
package handler
import (
"errors"
"net/http"
"net/http/httptest"
2026-01-20 21:52:44 +00:00
"testing"
"github.com/git-pkgs/registries/fetch"
2026-01-20 21:52:44 +00:00
)
func TestGoModuleDownloadUpstreamErrors(t *testing.T) {
tests := []struct {
name string
fetchErr error
wantStatus int
}{
{
name: "module not found",
fetchErr: fetch.ErrNotFound,
wantStatus: http.StatusNotFound,
},
{
name: "upstream failure",
fetchErr: errors.New("connection refused"),
wantStatus: http.StatusBadGateway,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
proxy, _, _, fetcher := setupTestProxy(t)
fetcher.fetchErr = tt.fetchErr
handler := NewGoHandler(proxy, "http://localhost:8080")
req := httptest.NewRequest(http.MethodGet, "/example.com/mod/@v/v1.0.0.zip", nil)
resp := httptest.NewRecorder()
handler.Routes().ServeHTTP(resp, req)
if resp.Code != tt.wantStatus {
t.Fatalf("status = %d, want %d", resp.Code, tt.wantStatus)
}
})
}
}
2026-01-20 21:52:44 +00:00
func TestDecodeGoModule(t *testing.T) {
tests := []struct {
encoded string
want string
}{
{"github.com/user/repo", "github.com/user/repo"},
{"github.com/!user/!repo", "github.com/User/Repo"},
{"golang.org/x/text", "golang.org/x/text"},
{"!azure!s!d!k", "AzureSDK"},
}
for _, tt := range tests {
got := decodeGoModule(tt.encoded)
if got != tt.want {
t.Errorf("decodeGoModule(%q) = %q, want %q", tt.encoded, got, tt.want)
}
}
}
func TestLastComponent(t *testing.T) {
tests := []struct {
path string
want string
}{
{"github.com/user/repo", "repo"},
{"golang.org/x/text", "text"},
{"simple", "simple"},
}
for _, tt := range tests {
got := lastComponent(tt.path)
if got != tt.want {
t.Errorf("lastComponent(%q) = %q, want %q", tt.path, got, tt.want)
}
}
}