From cc18e3378f073eeff659e83430c72ee04491fdec Mon Sep 17 00:00:00 2001 From: Darren Cunningham Date: Mon, 2 Dec 2024 09:06:45 -0500 Subject: [PATCH] fix: NewClient respects FAUNA_ENDPOINT environment variable (#189) --- client.go | 7 ++++++- client_test.go | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/client.go b/client.go index 745e286..594eaaa 100644 --- a/client.go +++ b/client.go @@ -167,11 +167,16 @@ func NewClient(secret string, timeouts Timeouts, configFns ...ClientConfigFn) *C defaultHeaders[HeaderQueryTimeoutMs] = fmt.Sprintf("%v", timeouts.QueryTimeout.Milliseconds()) } + endpointURL, urlFound := os.LookupEnv(EnvFaunaEndpoint) + if !urlFound { + endpointURL = EndpointDefault + } + client := &Client{ ctx: context.TODO(), secret: secret, http: httpClient, - url: EndpointDefault, + url: endpointURL, headers: defaultHeaders, lastTxnTime: txnTime{}, typeCheckingEnabled: false, diff --git a/client_test.go b/client_test.go index cb2e652..27614a3 100644 --- a/client_test.go +++ b/client_test.go @@ -198,6 +198,13 @@ func TestNewClient(t *testing.T) { assert.NoError(t, clientErr) }) + t.Run("new client respects env var", func(t *testing.T) { + t.Setenv(fauna.EnvFaunaEndpoint, fauna.EndpointLocal) + client := fauna.NewClient("secret", fauna.DefaultTimeouts()) + assert.NotNil(t, client) + assert.Equal(t, client.String(), fauna.EndpointLocal) + }) + t.Run("stringify", func(t *testing.T) { client := fauna.NewClient("secret", fauna.DefaultTimeouts(), fauna.URL(fauna.EndpointLocal)) assert.Equal(t, client.String(), fauna.EndpointLocal, "client toString should be equal to the endpoint to ensure we don't expose secrets")