Sync shared snapshot from de9b599
This commit is contained in:
1 parent
eb113a7029
commit
49e3c7d561
9 files changed
+697
-44
No files matched your search
@@ -75,10 +75,14 @@ func WithCookieName(name string) Option {
|
||||
return func(c *Client) { c.cookieName = name }
|
||||
}
|
||||
|
||||
// New creates a Client for the AuthServer instance at baseURL.
|
||||
// New creates a Client for the AuthServer instance at baseURL. baseURL may
|
||||
// be given without a scheme (e.g. "auth.example.com" or "192.168.1.50:8080")
|
||||
// - "https://" is assumed in that case, so a deployment only needs to name
|
||||
// the host, not repeat the scheme; an explicit "http://" (for a plain-HTTP
|
||||
// local/LAN AuthServer) is left as-is.
|
||||
func New(baseURL string, opts ...Option) *Client {
|
||||
c := &Client{
|
||||
baseURL: strings.TrimRight(strings.TrimSpace(baseURL), "/"),
|
||||
baseURL: normalizeBaseURL(baseURL),
|
||||
httpClient: &http.Client{Timeout: 5 * time.Second},
|
||||
cookieName: DefaultCookieName,
|
||||
}
|
||||
@@ -88,6 +92,18 @@ func New(baseURL string, opts ...Option) *Client {
|
||||
return c
|
||||
}
|
||||
|
||||
func normalizeBaseURL(baseURL string) string {
|
||||
trimmed := strings.TrimRight(strings.TrimSpace(baseURL), "/")
|
||||
if trimmed == "" {
|
||||
return trimmed
|
||||
}
|
||||
lower := strings.ToLower(trimmed)
|
||||
if strings.HasPrefix(lower, "http://") || strings.HasPrefix(lower, "https://") {
|
||||
return trimmed
|
||||
}
|
||||
return "https://" + trimmed
|
||||
}
|
||||
|
||||
// Validate calls AuthServer's GET /api/session/validate with token as a
|
||||
// Bearer credential and returns the resolved user. It returns
|
||||
// ErrInvalidSession (wrapped) when AuthServer reports the session as not
|
||||
|
||||
@@ -8,6 +8,32 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewNormalizesBaseURL(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
input string
|
||||
want string
|
||||
}{
|
||||
{name: "bare host gets https", input: "auth.example.com", want: "https://auth.example.com"},
|
||||
{name: "bare host:port gets https", input: "192.168.1.50:8080", want: "https://192.168.1.50:8080"},
|
||||
{name: "explicit http is left alone", input: "http://auth.example.com", want: "http://auth.example.com"},
|
||||
{name: "explicit https is left alone", input: "https://auth.example.com", want: "https://auth.example.com"},
|
||||
{name: "scheme match is case-insensitive", input: "HTTP://auth.example.com", want: "HTTP://auth.example.com"},
|
||||
{name: "trailing slash stripped before scheme check", input: "auth.example.com/", want: "https://auth.example.com"},
|
||||
{name: "surrounding whitespace trimmed", input: " auth.example.com ", want: "https://auth.example.com"},
|
||||
{name: "empty stays empty", input: "", want: ""},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
client := New(tc.input)
|
||||
if got := client.baseURL; got != tc.want {
|
||||
t.Fatalf("New(%q).baseURL = %q, want %q", tc.input, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetupStatus(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
|
||||
Reference in new issue
Block a user