Wrote url normalizer function.

This commit is contained in:
Jay
2026-04-14 17:27:20 -04:00
parent c4dc9a688b
commit 6d9b0ebdd4
2 changed files with 92 additions and 0 deletions

View File

@@ -91,3 +91,75 @@ func TestParseURL(t *testing.T) {
})
}
}
func TestNormalizeURL(t *testing.T) {
cases := []struct {
name string
input string
expected string
}{
{
name: "strip trailing slash",
input: "wss://relay.example.com/",
expected: "wss://relay.example.com",
},
{
name: "strip multiple trailing slashes",
input: "wss://relay.example.com//",
expected: "wss://relay.example.com",
},
{
name: "strip trailing slash with path",
input: "wss://relay.example.com/path/",
expected: "wss://relay.example.com/path",
},
{
name: "lowercase scheme",
input: "WSS://relay.example.com",
expected: "wss://relay.example.com",
},
{
name: "lowercase host",
input: "wss://Relay.Example.Com",
expected: "wss://relay.example.com",
},
{
name: "strip default wss port",
input: "wss://relay.example.com:443",
expected: "wss://relay.example.com",
},
{
name: "strip default ws port",
input: "ws://relay.example.com:80",
expected: "ws://relay.example.com",
},
{
name: "preserve non-default port",
input: "wss://relay.example.com:8080",
expected: "wss://relay.example.com:8080",
},
{
name: "preserve path",
input: "wss://relay.example.com/nostr",
expected: "wss://relay.example.com/nostr",
},
{
name: "no change needed",
input: "wss://relay.example.com",
expected: "wss://relay.example.com",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got, err := NormalizeURL(tc.input)
assert.NoError(t, err)
assert.Equal(t, tc.expected, got)
})
}
}
func TestNormalizeURLError(t *testing.T) {
_, err := NormalizeURL("http://relay.example.com")
assert.ErrorIs(t, err, errors.InvalidProtocol)
}