namespace Cepis.Tests;
/// What the client accepts as configuration, and what it refuses.
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(
() => new CepisClient(new CepisClientOptions { MaxAttempts = attempts }));
[Fact]
public void ATimeoutOfZeroIsRefused() =>
Assert.Throws(
() => new CepisClient(new CepisClientOptions { Timeout = TimeSpan.Zero }));
[Fact]
public void AnEmptyBaseUrlIsRefused() =>
Assert.Throws(
() => new CepisClient(new CepisClientOptions { BaseUrl = " " }));
[Fact]
public void ATotalTimeoutOfZeroIsRefused() =>
Assert.Throws(
() => 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);
}