Skip to content

Commit e5d498a

Browse files
authored
Support alternative endpoints with CLOUDAMQP_URL (#11)
This is useful for development of both sides.
1 parent 2c33ceb commit e5d498a

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

client/client.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88
"net/http"
99
"net/url"
10+
"os"
1011
"strings"
1112
)
1213

@@ -19,9 +20,13 @@ type Client struct {
1920
}
2021

2122
func New(apiKey string) *Client {
23+
baseURL := "https://customer.cloudamqp.com/api"
24+
if envURL := os.Getenv("CLOUDAMQP_URL"); envURL != "" {
25+
baseURL = envURL
26+
}
2227
return &Client{
2328
apiKey: apiKey,
24-
baseURL: "https://customer.cloudamqp.com/api",
29+
baseURL: baseURL,
2530
httpClient: &http.Client{},
2631
}
2732
}

client/client_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"net/http"
55
"net/http/httptest"
66
"net/url"
7+
"os"
78
"testing"
89

910
"github.com/stretchr/testify/assert"
@@ -18,6 +19,31 @@ func TestNew(t *testing.T) {
1819
assert.NotNil(t, client.httpClient)
1920
}
2021

22+
func TestNew_WithEnvironmentVariable(t *testing.T) {
23+
// Save original environment variable
24+
originalURL := os.Getenv("CLOUDAMQP_URL")
25+
defer os.Setenv("CLOUDAMQP_URL", originalURL)
26+
27+
// Test with custom base URL from environment variable
28+
customURL := "https://custom.example.com/api"
29+
os.Setenv("CLOUDAMQP_URL", customURL)
30+
31+
apiKey := "test-api-key"
32+
client := New(apiKey)
33+
34+
assert.NotNil(t, client)
35+
assert.Equal(t, apiKey, client.apiKey)
36+
assert.Equal(t, customURL, client.baseURL)
37+
assert.NotNil(t, client.httpClient)
38+
39+
// Test with empty environment variable (should use default)
40+
os.Setenv("CLOUDAMQP_URL", "")
41+
client = New(apiKey)
42+
43+
assert.NotNil(t, client)
44+
assert.Equal(t, "https://customer.cloudamqp.com/api", client.baseURL)
45+
}
46+
2147
func TestMakeRequest_GET_Success(t *testing.T) {
2248
// Mock server
2349
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)