Files
at-container-registry/pkg/atproto/utils_test.go
T

190 lines
5.0 KiB
Go

package atproto
import "testing"
func TestResolveHoldURL(t *testing.T) {
tests := []struct {
name string
holdIdentifier string
want string
}{
// URL passthrough tests
{
name: "http URL passthrough",
holdIdentifier: "http://hold.example.com",
want: "http://hold.example.com",
},
{
name: "https URL passthrough",
holdIdentifier: "https://hold.example.com",
want: "https://hold.example.com",
},
{
name: "http URL with port passthrough",
holdIdentifier: "http://hold.example.com:8080",
want: "http://hold.example.com:8080",
},
{
name: "https URL with port passthrough",
holdIdentifier: "https://hold.example.com:8443",
want: "https://hold.example.com:8443",
},
{
name: "http URL with path passthrough",
holdIdentifier: "http://hold.example.com/some/path",
want: "http://hold.example.com/some/path",
},
// did:web to HTTPS (domain names)
{
name: "did:web domain to https",
holdIdentifier: "did:web:hold01.atcr.io",
want: "https://hold01.atcr.io",
},
{
name: "did:web subdomain to https",
holdIdentifier: "did:web:my-hold.example.com",
want: "https://my-hold.example.com",
},
{
name: "did:web simple domain to https",
holdIdentifier: "did:web:example.com",
want: "https://example.com",
},
// did:web to HTTP (ports)
{
name: "did:web with port to http",
holdIdentifier: "did:web:172.28.0.3:8080",
want: "http://172.28.0.3:8080",
},
{
name: "did:web domain with port to http",
holdIdentifier: "did:web:hold.example.com:8080",
want: "http://hold.example.com:8080",
},
{
name: "did:web localhost with port to http",
holdIdentifier: "did:web:localhost:8080",
want: "http://localhost:8080",
},
// did:web to HTTP (localhost)
{
name: "did:web localhost to http",
holdIdentifier: "did:web:localhost",
want: "http://localhost",
},
// did:web to HTTP (127.0.0.1)
{
name: "did:web 127.0.0.1 to http",
holdIdentifier: "did:web:127.0.0.1",
want: "http://127.0.0.1",
},
{
name: "did:web 127.0.0.1 with port to http",
holdIdentifier: "did:web:127.0.0.1:8080",
want: "http://127.0.0.1:8080",
},
// did:web to HTTP (IP addresses)
{
name: "did:web IPv4 address to http",
holdIdentifier: "did:web:192.168.1.1",
want: "http://192.168.1.1",
},
{
name: "did:web IPv4 with port to http",
holdIdentifier: "did:web:10.0.0.5:3000",
want: "http://10.0.0.5:3000",
},
{
name: "did:web private IP to http",
holdIdentifier: "did:web:172.16.0.1",
want: "http://172.16.0.1",
},
// Fallback behavior (plain hostname)
{
name: "plain hostname fallback to https",
holdIdentifier: "hold.example.com",
want: "https://hold.example.com",
},
{
name: "plain single word fallback to https",
holdIdentifier: "myhold",
want: "https://myhold",
},
// Edge cases
{
name: "empty string fallback",
holdIdentifier: "",
want: "https://",
},
{
name: "did:web empty hostname",
holdIdentifier: "did:web:",
want: "https://",
},
{
name: "just did:web prefix",
holdIdentifier: "did:web",
want: "https://did:web",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ResolveHoldURL(tt.holdIdentifier)
if got != tt.want {
t.Errorf("ResolveHoldURL(%q) = %q, want %q", tt.holdIdentifier, got, tt.want)
}
})
}
}
// TestResolveHoldURLRoundTrip tests that converting back and forth works
func TestResolveHoldURLRoundTrip(t *testing.T) {
tests := []struct {
name string
input string
wantHTTP bool // true if result should be http, false for https
}{
{"domain to https and idempotent", "did:web:hold.atcr.io", false},
{"IP to http and idempotent", "did:web:192.168.1.1", true},
{"port to http and idempotent", "did:web:example.com:8080", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// First conversion
first := ResolveHoldURL(tt.input)
// Second conversion (should be idempotent since output is URL)
second := ResolveHoldURL(first)
if first != second {
t.Errorf("ResolveHoldURL is not idempotent: first=%q, second=%q", first, second)
}
// Verify correct protocol
if tt.wantHTTP {
if !hasPrefix(first, "http://") {
t.Errorf("Expected http:// prefix, got %q", first)
}
} else {
if !hasPrefix(first, "https://") {
t.Errorf("Expected https:// prefix, got %q", first)
}
}
})
}
}
// Helper function to check prefix
func hasPrefix(s, prefix string) bool {
return len(s) >= len(prefix) && s[:len(prefix)] == prefix
}