Skip to content

Commit 30b6850

Browse files
committed
Support baseURL without /api suffix
1 parent bc30e7b commit 30b6850

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

backstage/backstage.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,14 @@ type Client struct {
4242
// NewClient returns a new Backstage API client. If a nil httpClient is provided, a new http.Client will be used.
4343
// To use API methods which require authentication, provide a http.Client that will perform the authentication.
4444
func NewClient(baseURL string, defaultNamespace string, httpClient *http.Client) (*Client, error) {
45-
baseEndpoint, err := url.Parse(strings.TrimSuffix(baseURL, "/"))
45+
const apiPath = "/api"
46+
47+
baseURL = strings.TrimSuffix(baseURL, "/")
48+
if !strings.HasSuffix(baseURL, apiPath) {
49+
baseURL += apiPath
50+
}
51+
52+
baseEndpoint, err := url.Parse(baseURL)
4653
if err != nil {
4754
return nil, err
4855
}

backstage/backstage_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ func TestNewClient(t *testing.T) {
2828
assert.Equal(t, nameSpace, c.DefaultNamespace, "Default namespace should match the one provided")
2929
}
3030

31+
// TestNewClient_NoApiSuffix tests the creation of a new Backstage client when base URL does not contain /api suffix.
32+
func TestNewClient_NoApiSuffix(t *testing.T) {
33+
const baseURL = "http://localhost:7007"
34+
35+
c, err := NewClient(baseURL, "", nil)
36+
37+
assert.NoError(t, err, "New client should not return an error")
38+
assert.Equal(t, baseURL+"/api", c.BaseURL.String(), "Base URL should match the one provided")
39+
assert.Equal(t, "default", c.DefaultNamespace, "Default namespace should be 'default'")
40+
}
41+
3142
// TestNewClient tests the creation of a new Backstage client from an existing HTTP client.
3243
func TestNewClient_ExistingHTTPClient(t *testing.T) {
3344
const baseURL = "http://localhost:7007/api"

0 commit comments

Comments
 (0)