Skip to content

Commit

Permalink
Merge pull request #27 from grafana/matiasb/allow-empty-string-token
Browse files Browse the repository at this point in the history
Allow instantiating the client with an empty string token
  • Loading branch information
matiasb authored Dec 16, 2024
2 parents 63aded6 + c4dc183 commit 1e17613
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
8 changes: 0 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ type Client struct {
}

func NewWithGrafanaURL(base_url, token, grafana_url string) (*Client, error) {
if token == "" {
return nil, fmt.Errorf("Token required")
}

if base_url == "" {
return nil, fmt.Errorf("BaseUrl required")
}
Expand All @@ -77,10 +73,6 @@ func NewWithGrafanaURL(base_url, token, grafana_url string) (*Client, error) {
}

func New(base_url, token string) (*Client, error) {
if token == "" {
return nil, fmt.Errorf("Token required")
}

if base_url == "" {
return nil, fmt.Errorf("BaseUrl required")
}
Expand Down
30 changes: 30 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ func TestNewClient(t *testing.T) {
}
}

func TestNewClientEmptyToken(t *testing.T) {
c, err := New("base_url", "")
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}

expectedBaseURL := "base_url/" + apiVersionPath

if c.BaseURL().String() != expectedBaseURL {
t.Errorf("NewClient BaseURL is %s, want %s", c.BaseURL().String(), expectedBaseURL)
}
}

func TestNewClientWithGrafanaURL(t *testing.T) {
c, err := NewWithGrafanaURL("base_url", "token", "grafana_url")
if err != nil {
Expand All @@ -64,6 +77,23 @@ func TestNewClientWithGrafanaURL(t *testing.T) {
}
}

func TestNewClientWithGrafanaURLEmptyToken(t *testing.T) {
c, err := NewWithGrafanaURL("base_url", "", "grafana_url")
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}

expectedBaseURL := "base_url/" + apiVersionPath

if c.BaseURL().String() != expectedBaseURL {
t.Errorf("NewClient BaseURL is %s, want %s", c.BaseURL().String(), expectedBaseURL)
}

if c.GrafanaURL().String() != "grafana_url" {
t.Errorf("NewClient GrafanaURL is %s, want grafana_url", c.GrafanaURL().String())
}
}

func TestCheckRequest(t *testing.T) {
c, err := New("base_url", "token")
if err != nil {
Expand Down

0 comments on commit 1e17613

Please sign in to comment.