summaryrefslogtreecommitdiffstats
path: root/tests/Cepis.Tests/ConfigurationTests.cs
blob: 2f6fab9c9aca2945069cca398088e3445eaa06bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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);
}