diff options
| author | Murilo Ijanc <[email protected]> | 2026-07-26 12:42:38 -0300 |
|---|---|---|
| committer | Murilo Ijanc <[email protected]> | 2026-07-26 13:10:34 -0300 |
| commit | a2b10fd494fb389e556e40134031eb86e56e11e7 (patch) | |
| tree | 08b6df8a06f2c7c15488b90d57a6d8286cd57a22 /tests/Cepis.Tests/ConfigurationTests.cs | |
| download | csharp-a2b10fd494fb389e556e40134031eb86e56e11e7.tar.gz | |
Initial importv0.1.0
Diffstat (limited to 'tests/Cepis.Tests/ConfigurationTests.cs')
| -rw-r--r-- | tests/Cepis.Tests/ConfigurationTests.cs | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/tests/Cepis.Tests/ConfigurationTests.cs b/tests/Cepis.Tests/ConfigurationTests.cs new file mode 100644 index 0000000..2f6fab9 --- /dev/null +++ b/tests/Cepis.Tests/ConfigurationTests.cs @@ -0,0 +1,80 @@ +namespace Cepis.Tests; + +/// <summary>What the client accepts as configuration, and what it refuses.</summary> +public sealed class ConfigurationTests : HttpTestBase +{ + [Fact] + public void TheDefaultsAreTheDocumentedOnes() + { + var options = new CepisClientOptions(); + + Assert.Equal(CepisClient.DefaultUrl, options.BaseUrl); + Assert.Equal(TimeSpan.FromSeconds(10), options.Timeout); + Assert.Equal(3, options.MaxAttempts); + Assert.Equal(TimeSpan.FromMilliseconds(500), options.BackoffBase); + Assert.Equal(TimeSpan.FromSeconds(8), options.BackoffMax); + Assert.Null(options.TotalTimeout); + Assert.Null(options.UserAgent); + } + + [Fact] + public void TheDefaultUserAgentIdentifiesTheLibrary() + { + using var client = new CepisClient(); + + Assert.Equal($"cepis-csharp/{CepisClient.Version} (+https://cep.is)", client.UserAgent); + } + + [Fact] + public async Task ATrailingSlashCannotDoubleTheSeparator() + { + using var client = new CepisClient(new CepisClientOptions { BaseUrl = Server.Url + "///" }); + Server.Respond("/01001000", Reply.Of(200, Fixtures.SeAddress)); + + await client.LookupAsync("01001000"); + + Assert.Equal(Server.Url, client.BaseUrl); + Assert.Equal("/01001000", Assert.Single(Server.Calls).Path); + } + + [Theory] + [InlineData(0)] + [InlineData(-1)] + public void MaxAttemptsBelowOneIsRefused(int attempts) => + Assert.Throws<ArgumentException>( + () => new CepisClient(new CepisClientOptions { MaxAttempts = attempts })); + + [Fact] + public void ATimeoutOfZeroIsRefused() => + Assert.Throws<ArgumentException>( + () => new CepisClient(new CepisClientOptions { Timeout = TimeSpan.Zero })); + + [Fact] + public void AnEmptyBaseUrlIsRefused() => + Assert.Throws<ArgumentException>( + () => new CepisClient(new CepisClientOptions { BaseUrl = " " })); + + [Fact] + public void ATotalTimeoutOfZeroIsRefused() => + Assert.Throws<ArgumentException>( + () => new CepisClient(new CepisClientOptions { TotalTimeout = TimeSpan.Zero })); + + [Fact] + public async Task AnInjectedHttpClientIsUsedAndNotDisposed() + { + using var http = new HttpClient(); + Server.Respond("/01001000", Reply.Of(200, Fixtures.SeAddress)); + + using (var client = new CepisClient(http, new CepisClientOptions { BaseUrl = Server.Url })) + { + await client.LookupAsync("01001000"); + } + + Assert.Equal(1, Server.Count("/01001000")); + await http.GetAsync(new Uri(Server.Url + "/01001000")); + } + + [Fact] + public void TheSharedDefaultPointsAtTheRealApi() => + Assert.Equal(CepisClient.DefaultUrl, CepisClient.Default.BaseUrl); +} |