Skip to content
Merged
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
7 changes: 6 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"net/http"
"net/url"
"os"
"strings"
)

Expand All @@ -19,9 +20,13 @@ type Client struct {
}

func New(apiKey string) *Client {
baseURL := "https://customer.cloudamqp.com/api"
if envURL := os.Getenv("CLOUDAMQP_URL"); envURL != "" {
baseURL = envURL
}
return &Client{
apiKey: apiKey,
baseURL: "https://customer.cloudamqp.com/api",
baseURL: baseURL,
httpClient: &http.Client{},
}
}
Expand Down
26 changes: 26 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"

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

func TestNew_WithEnvironmentVariable(t *testing.T) {
// Save original environment variable
originalURL := os.Getenv("CLOUDAMQP_URL")
defer os.Setenv("CLOUDAMQP_URL", originalURL)

// Test with custom base URL from environment variable
customURL := "https://custom.example.com/api"
os.Setenv("CLOUDAMQP_URL", customURL)

apiKey := "test-api-key"
client := New(apiKey)

assert.NotNil(t, client)
assert.Equal(t, apiKey, client.apiKey)
assert.Equal(t, customURL, client.baseURL)
assert.NotNil(t, client.httpClient)

// Test with empty environment variable (should use default)
os.Setenv("CLOUDAMQP_URL", "")
client = New(apiKey)

assert.NotNil(t, client)
assert.Equal(t, "https://customer.cloudamqp.com/api", client.baseURL)
}

func TestMakeRequest_GET_Success(t *testing.T) {
// Mock server
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
Loading