Skip to content

Commit

Permalink
dont vavlidate in constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
cynicaljoy committed Nov 13, 2024
1 parent 86b3039 commit 5bb53aa
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
20 changes: 19 additions & 1 deletion Fauna.Test/Configuration.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -63,7 +80,7 @@ public void ConstructorThrowsWithNullSecret()
{
Environment.SetEnvironmentVariable("FAUNA_SECRET", null);
var b = new Configuration();

b.Validate();
});
Environment.SetEnvironmentVariable("FAUNA_SECRET", currentVal);
}
Expand All @@ -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]
Expand Down
1 change: 1 addition & 0 deletions Fauna/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public Client(string secret) :
/// <param name="config">The configuration settings for the client.</param>
public Client(Configuration config)
{
config.Validate();
_config = config;
StatsCollector = config.StatsCollector;
_connection = new Connection(config);
Expand Down
14 changes: 9 additions & 5 deletions Fauna/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,6 @@ public record class Configuration
/// <param name="logger">A logger. If null, a default logger is used.</param>
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;
Expand All @@ -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."
);
}
}

0 comments on commit 5bb53aa

Please sign in to comment.