diff --git a/Fauna.Test/Configuration.Tests.cs b/Fauna.Test/Configuration.Tests.cs index 21367698..f0701089 100644 --- a/Fauna.Test/Configuration.Tests.cs +++ b/Fauna.Test/Configuration.Tests.cs @@ -17,6 +17,20 @@ public void ConstructorWorksFine() Assert.IsTrue(b.DisposeHttpClient); } + [Test] + public void ConstructorWithSecretVar() + { + string? currentVal = Environment.GetEnvironmentVariable("FAUNA_ENDPOINT"); + Environment.SetEnvironmentVariable("FAUNA_SECRET", ""); + + Configuration config = new Configuration { Secret = "secret" }; + Assert.AreEqual("secret", config.Secret); + + Environment.SetEnvironmentVariable("FAUNA_SECRET", currentVal); + + Assert.DoesNotThrow(() => config.Validate()); + } + [Test] public void ConstructorWithEndpointEnvVar() { @@ -28,6 +42,8 @@ public void ConstructorWithEndpointEnvVar() Assert.AreEqual("http://localhost:8443/", config.Endpoint.ToString()); Environment.SetEnvironmentVariable("FAUNA_ENDPOINT", currentVal); + + Assert.DoesNotThrow(() => config.Validate()); } [Test] @@ -53,6 +69,7 @@ public void ConstructorUsesEnvVar() Assert.AreEqual("secret", b.Secret); Assert.AreEqual(Endpoints.Default, b.Endpoint); Assert.IsTrue(b.DisposeHttpClient); + Assert.DoesNotThrow(() => b.Validate()); } [Test] @@ -63,7 +80,7 @@ public void ConstructorThrowsWithNullSecret() { Environment.SetEnvironmentVariable("FAUNA_SECRET", null); var b = new Configuration(); - + b.Validate(); }); Environment.SetEnvironmentVariable("FAUNA_SECRET", currentVal); } @@ -76,6 +93,7 @@ public void ConstructorWithHttpClient() Assert.AreEqual("secret", b.Secret); Assert.AreEqual(Endpoints.Default, b.Endpoint); Assert.IsFalse(b.DisposeHttpClient); + Assert.DoesNotThrow(() => b.Validate()); } [Test] diff --git a/Fauna/Client.cs b/Fauna/Client.cs index 38ec0918..4257e5d9 100644 --- a/Fauna/Client.cs +++ b/Fauna/Client.cs @@ -59,6 +59,7 @@ public Client(string secret) : /// The configuration settings for the client. public Client(Configuration config) { + config.Validate(); _config = config; StatsCollector = config.StatsCollector; _connection = new Connection(config); diff --git a/Fauna/Configuration.cs b/Fauna/Configuration.cs index 582d3546..cb58bcab 100644 --- a/Fauna/Configuration.cs +++ b/Fauna/Configuration.cs @@ -61,11 +61,6 @@ public record class Configuration /// A logger. If null, a default logger is used. public Configuration(string secret = "", HttpClient? httpClient = null, ILogger? logger = null) { - if (string.IsNullOrEmpty(secret) && string.IsNullOrEmpty(Secret)) - { - throw new ArgumentNullException(nameof(Secret), "Need to set FAUNA_SECRET environment variable or pass a secret as a parameter when creating the Client."); - } - if (!string.IsNullOrEmpty(secret)) { Secret = secret; @@ -86,4 +81,13 @@ public Configuration(string secret = "", HttpClient? httpClient = null, ILogger? Logger.Initialize(logger); } } + + internal void Validate() + { + if (string.IsNullOrEmpty(Secret)) + throw new ArgumentNullException( + nameof(Secret), + "Need to set FAUNA_SECRET environment variable or pass a secret as a parameter when creating the Client." + ); + } }