Skip to content

Commit

Permalink
TEsts for CreateClient
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianhall committed Apr 21, 2024
1 parent 169ce46 commit 351388b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ public class DatasyncHttpClientOptions : IDatasyncHttpClientOptions
/// <summary>
/// The base address for the datasync service.
/// </summary>
/// <remarks>The default address is explicitly invalid.</remarks>
public Uri BaseAddress { get; set; } = new Uri("https://localhost/");
public Uri BaseAddress { get; set; } = new("http://localhost/");

/// <summary>
/// The HTTP pipeline to use. It must be an ordered list of <see cref="DelegatingHandler"/> objects, potentially followed by a <see cref="HttpClientHandler"/> to use as a transport.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,88 @@
using CommunityToolkit.Datasync.Client.Http;
using CommunityToolkit.Datasync.Common.Test.Mocks;

// We use IHttpClientFactory in some tests to ensure we are calling the right methods.
#pragma warning disable CA1859 // Use concrete types when possible for improved performance

namespace CommunityToolkit.Datasync.Client.Test.Http;

[ExcludeFromCodeCoverage]
public class DatasyncHttpClientFactory_Tests
{
#region CreateClient
[Theory]
[InlineData("")]
[InlineData("default")]
[InlineData("test")]
public void CreateClient_Works(string name)
{
IHttpClientFactory factory = new DatasyncHttpClientFactory(new DatasyncHttpClientOptions { BaseAddress = new("http://localhost/") });
HttpClient client = factory.CreateClient(name);
client.BaseAddress.ToString().Should().Be("http://localhost/");
}

[Theory]
[InlineData("file://localhost/foo", false)]
[InlineData("http://foo.azurewebsites.net", false)]
[InlineData("http://foo.azure-api.net", false)]
[InlineData("http://[2001:db8:0:b:0:0:0:1A]", false)]
[InlineData("http://[2001:db8:0:b:0:0:0:1A]:3000", false)]
[InlineData("http://[2001:db8:0:b:0:0:0:1A]:3000/myapi", false)]
[InlineData("http://10.0.0.8", false)]
[InlineData("http://10.0.0.8:3000", false)]
[InlineData("http://10.0.0.8:3000/myapi", false)]
[InlineData("foo/bar", true)]
public void CreateClient_Throws(string endpoint, bool isRelative = false)
{
Uri sut = isRelative ? new Uri(endpoint, UriKind.Relative) : new Uri(endpoint);
DatasyncHttpClientOptions options = new() { BaseAddress = sut };
IHttpClientFactory factory = new DatasyncHttpClientFactory(options);
Assert.Throws<UriFormatException>(() => factory.CreateClient("name"));
}

[Fact]
public void CreateClient_ReturnsCachedCopy()
{
DatasyncHttpClientOptions options = new() { BaseAddress = new("https://localhost/") };
IHttpClientFactory factory = new DatasyncHttpClientFactory(options);

HttpClient client1 = factory.CreateClient("name");
HttpClient client2 = factory.CreateClient("name");
client1.Should().BeSameAs(client2);
}

[Fact]
public void CreateClient_CreatesDifferentCopies()
{
DatasyncHttpClientOptions options = new() { BaseAddress = new("https://localhost/") };
IHttpClientFactory factory = new DatasyncHttpClientFactory(options);

HttpClient client1 = factory.CreateClient("name1");
HttpClient client2 = factory.CreateClient("name2");
client1.Should().NotBeSameAs(client2);
}

[Fact]
public void CreateClient_SetsTimeout()
{
DatasyncHttpClientOptions options = new() { BaseAddress = new("https://localhost/"), HttpTimeout = TimeSpan.FromSeconds(10) };
IHttpClientFactory factory = new DatasyncHttpClientFactory(options);

HttpClient client = factory.CreateClient("name");
client.Timeout.Should().Be(TimeSpan.FromSeconds(10));
}

[Fact]
public void CreateClient_SetsHeaders()
{
DatasyncHttpClientOptions options = new() { BaseAddress = new("https://localhost/"), HttpRequestHeaders = new Dictionary<string, string> { ["X-Test"] = "Test" } };
IHttpClientFactory factory = new DatasyncHttpClientFactory(options);

HttpClient client = factory.CreateClient("name");
client.DefaultRequestHeaders.GetValues("X-Test").Should().Contain("Test");
}
#endregion

#region CreatePipeline
private static DatasyncHttpClientFactory CreateFactoryWithHandlers(IEnumerable<HttpMessageHandler> handlers)
{
Expand Down

0 comments on commit 351388b

Please sign in to comment.