2026-10-25 21:48:59 +01:00
|
|
|
package mirror
|
|
|
|
|
|
2026-10-26 16:43:42 +01:00
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/git-pkgs/registries"
|
|
|
|
|
)
|
2026-10-25 21:48:59 +01:00
|
|
|
|
|
|
|
|
func TestSwiftIdentity(t *testing.T) {
|
2026-10-26 16:43:42 +01:00
|
|
|
registry := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if r.URL.Path != "/identifiers" || r.Header.Get("Accept") != "application/vnd.swift.registry.v1+json" {
|
|
|
|
|
http.Error(w, "unexpected request", http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
switch r.URL.Query().Get("url") {
|
|
|
|
|
case "https://github.com/apple/swift-nio":
|
|
|
|
|
_, _ = w.Write([]byte(`{"identifiers":["apple.swift-nio","mirror.swift-nio"]}`))
|
|
|
|
|
case "https://github.com/acme/empty":
|
|
|
|
|
_, _ = w.Write([]byte(`{"identifiers":[]}`))
|
|
|
|
|
default:
|
|
|
|
|
http.NotFound(w, r)
|
|
|
|
|
}
|
|
|
|
|
}))
|
|
|
|
|
defer registry.Close()
|
|
|
|
|
|
2026-10-25 21:48:59 +01:00
|
|
|
for name, want := range map[string]string{
|
|
|
|
|
"apple/swift-nio": "apple.swift-nio",
|
2026-10-26 16:43:42 +01:00
|
|
|
"github.com/apple/swift-nio": "apple.swift-nio",
|
|
|
|
|
"github.com/acme/empty": "",
|
|
|
|
|
"github.com/acme/unknown": "",
|
2026-10-25 21:48:59 +01:00
|
|
|
"swift-nio": "",
|
2026-10-26 16:43:42 +01:00
|
|
|
"apple/": "",
|
|
|
|
|
"apple/swift-nio/extra": "",
|
2026-10-25 21:48:59 +01:00
|
|
|
} {
|
2026-10-26 16:43:42 +01:00
|
|
|
got, err := swiftIdentity(context.Background(), registries.DefaultClient(), registry.URL, name)
|
2026-10-25 21:48:59 +01:00
|
|
|
if got != want || (want == "") != (err != nil) {
|
|
|
|
|
t.Errorf("swiftIdentity(%q) = %q, %v; want %q", name, got, err, want)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|