Add tests for config type url

This commit is contained in:
9seconds
2021-03-12 12:16:35 +03:00
parent 428010880e
commit 7ca143d352
2 changed files with 123 additions and 0 deletions
+26
View File
@@ -2,6 +2,7 @@ package config
import (
"fmt"
"net"
"net/url"
)
@@ -19,6 +20,31 @@ func (c *TypeURL) UnmarshalText(data []byte) error {
return fmt.Errorf("incorrect URL: %w", err)
}
switch value.Scheme {
case "http", "https", "socks5":
case "":
return fmt.Errorf("url %s has to have a schema", value)
default:
return fmt.Errorf("unsupported schema %s", value.Scheme)
}
if value.Host == "" {
return fmt.Errorf("url %s has to have a host", value)
}
if _, _, err := net.SplitHostPort(value.Host); err != nil {
switch value.Scheme {
case "http":
value.Host = net.JoinHostPort(value.Host, "80")
case "https":
value.Host = net.JoinHostPort(value.Host, "443")
case "socks5":
value.Host = net.JoinHostPort(value.Host, "1080")
default:
return fmt.Errorf("cannot set a default port for %s", value)
}
}
c.value = value
return nil