-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to configure the created HttpClient
- Loading branch information
1 parent
5cb46e9
commit f053107
Showing
5 changed files
with
120 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...odenizer.HttpClient.Testable.Tests.Unit/WhenConfiguringHandlerThroughHttpClientFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Reflection; | ||
using FluentAssertions; | ||
using Xunit; | ||
|
||
namespace Codenizer.HttpClient.Testable.Tests.Unit | ||
{ | ||
public class WhenConfiguringHandlerThroughHttpClientFactory | ||
{ | ||
private readonly TestableHttpClientFactory _factory = new(); | ||
private const string ClientName = "test client name"; | ||
|
||
[Fact] | ||
public void GivenClientNameWasNotConfigured_NewHandlerIsRegistered() | ||
{ | ||
var client = _factory.CreateClient(ClientName); | ||
|
||
client | ||
.Should() | ||
.NotBeNull(); | ||
} | ||
|
||
[Fact] | ||
public void GivenClientNameWasConfigured_ClientUsesConfiguredHandler() | ||
{ | ||
var handler = _factory.ConfigureClient(ClientName); | ||
|
||
var client = _factory.CreateClient(ClientName); | ||
|
||
TheHandlerOf(client) | ||
.Should() | ||
.Be(handler); | ||
} | ||
|
||
[Fact] | ||
public void GivenClientWasConfiguredWithSpecificHandler_ClientIsUsingSpecifiedHandler() | ||
{ | ||
var handler = new TestableMessageHandler(); | ||
_factory.ConfigureClient(ClientName, handler); | ||
|
||
var client = _factory.CreateClient(ClientName); | ||
|
||
TheHandlerOf(client) | ||
.Should() | ||
.Be(handler); | ||
} | ||
|
||
[Fact] | ||
public void GivenClientNameWasConfiguredWithClientConfiguration_ConfigurationIsAppliedToClient() | ||
{ | ||
_factory.ConfigureClient(ClientName, client => client.BaseAddress = new Uri("https://example.com")); | ||
|
||
var client = _factory.CreateClient(ClientName); | ||
|
||
client | ||
.BaseAddress | ||
.Should() | ||
.Be(new Uri("https://example.com")); | ||
} | ||
|
||
private HttpMessageHandler? TheHandlerOf(System.Net.Http.HttpClient client) | ||
{ | ||
var field = typeof(HttpMessageInvoker).GetField("_handler", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField); | ||
|
||
return field?.GetValue(client) as HttpMessageHandler; | ||
} | ||
} | ||
} |