diff --git a/config.go b/config.go index cdd56c2..f69e334 100644 --- a/config.go +++ b/config.go @@ -1,9 +1,12 @@ package posthog import ( - "github.com/xtgo/uuid" "net/http" + "net/url" + "strings" "time" + + "github.com/xtgo/uuid" ) // Instances of this type carry the different configuration options that may @@ -13,8 +16,9 @@ import ( // default value defined by the library. type Config struct { - // The endpoint to which the client connect and send their messages, set to - // `DefaultEndpoint` by default. + // The endpoint to which the client connects and send their messages to; + // endpoint should be a URL such as https://your.domain.com; + // set to `DefaultEndpoint` by default. Endpoint string // You must specify a Personal API Key to use feature flags @@ -116,6 +120,29 @@ func (c *Config) validate() error { } } + if c.Endpoint != "" { + u, err := url.ParseRequestURI(c.Endpoint) + if err != nil { + return ConfigError{ + Reason: "invalid endpoint URL", + Field: "Endpoint", + Value: c.Endpoint, + } + } + + // Need a stricter check because url.ParseRequestURI() can parse a host + // address as the scheme in some situations (ie. "localhost:8080") + if u.Scheme != "" && u.Host == "" { + return ConfigError{ + Reason: "missing URL scheme in endpoint URL", + Field: "Endpoint", + Value: c.Endpoint, + } + } + + c.Endpoint = strings.TrimSuffix(c.Endpoint, "/") + } + return nil } diff --git a/posthog_test.go b/posthog_test.go index 3b7c77a..70d955b 100644 --- a/posthog_test.go +++ b/posthog_test.go @@ -503,6 +503,56 @@ func TestClientConfigError(t *testing.T) { } } +func TestNewWithConfigEndpoints(t *testing.T) { + type testCase struct { + description string + value string + mustError bool + } + + testCases := []testCase{ + { + description: "valid config 1", + value: "http://localhost:9999", + }, + { + description: "valid config 2", + value: "https://some-domain/", + }, + { + description: "invalid url", + value: "invalid-url", + mustError: true, + }, + { + description: "URL without scheme", + value: "localhost:9999", + mustError: true, + }, + } + + for _, v := range testCases { + client, err := NewWithConfig("0123456789", Config{ + Endpoint: v.value, + }) + if client != nil { + defer client.Close() + } + + if err != nil && !v.mustError { + t.Errorf("expected no error, got error (case: '%s', value: '%s'): %s", + v.description, v.value, err) + continue + } + + if err == nil && v.mustError { + t.Errorf("expected error, got nil (case: '%s', value: '%s'): %s", + v.description, v.value, err) + continue + } + } +} + func TestClientEnqueueError(t *testing.T) { client := New("0123456789") defer client.Close()