Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extra validations when specifying custom endpoint #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
50 changes: 50 additions & 0 deletions posthog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down