Files

39 lines
1.5 KiB
Go

package did
import "testing"
// TestGenerateDIDFromURL covers host extraction and port encoding for did:web.
func TestGenerateDIDFromURL(t *testing.T) {
cases := []struct {
name string
publicURL string
want string
}{
{"https no port", "https://hold.example.com", "did:web:hold.example.com"},
{"http no port", "http://hold.example.com", "did:web:hold.example.com"},
{"https port 443 stripped", "https://hold.example.com:443", "did:web:hold.example.com"},
{"http port 80 stripped", "http://hold.example.com:80", "did:web:hold.example.com"},
{"non-standard port encoded", "https://hold.example.com:8443", "did:web:hold.example.com%3A8443"},
{"localhost with port", "http://localhost:3000", "did:web:localhost%3A3000"},
{"trailing path ignored", "https://hold.example.com/foo/bar", "did:web:hold.example.com"},
{"subdomain preserved", "https://api.hold.example.com", "did:web:api.hold.example.com"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := GenerateDIDFromURL(tc.publicURL)
if got != tc.want {
t.Errorf("GenerateDIDFromURL(%q): got %s want %s", tc.publicURL, got, tc.want)
}
})
}
}
// TestGenerateDIDFromURL_EmptyHost confirms a URL without a hostname falls back to localhost.
// (url.Parse on a bare path does not error, so the fallback is the only signal we have.)
func TestGenerateDIDFromURL_EmptyHost(t *testing.T) {
got := GenerateDIDFromURL("")
if got != "did:web:localhost" {
t.Errorf("empty URL: got %s want did:web:localhost", got)
}
}