Skip to content

Commit

Permalink
issue 114/improvement: move creation of httpclient instance to virtua…
Browse files Browse the repository at this point in the history
…l property (#119)

* improvement: move creation of httpclient instance to virtual

* update readme
  • Loading branch information
daveleek authored Dec 13, 2022
1 parent d755ca1 commit ba42074
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 5 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,33 @@ var settings = new UnleashSettings()
};
```

### HttpMessageHandlers/Custom HttpClient initialization
If you need to specify HttpMessageHandlers or to control the instantiation of the HttpClient, you can create a custom
HttpClientFactory that inherits from DefaultHttpClientFactory, and override the method CreateHttpClientInstance.
Then configure UnleashSettings to use your custom HttpClientFactory.

```csharp
internal class CustomHttpClientFactory : DefaultHttpClientFactory
{
protected override HttpClient CreateHttpClientInstance(Uri unleashApiUri)
{
var messageHandler = new CustomHttpMessageHandler();
var httpClient = new HttpClient(messageHandler)
{
BaseAddress = apiUri,
Timeout = TimeSpan.FromSeconds(5)
};
}
}

var settings = new UnleashSettings
{
AppName = "dotnet-test",
//...
HttpClientFactory = new CustomHttpClientFactory()
};
```

### Dynamic custom HTTP headers
If you need custom http headers that change during the lifetime of the client, a provider can be defined via the `UnleashSettings`.

Expand Down
17 changes: 12 additions & 5 deletions src/Unleash/DefaultHttpClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ public HttpClient Create(Uri unleashApiUri)

return _httpClientCache.GetOrAdd(key, k =>
{
var client = new HttpClient
{
BaseAddress = unleashApiUri,
Timeout = Timeout
};
var client = CreateHttpClientInstance(unleashApiUri);
// Refresh DNS cache each 60 seconds
var servicePoint = ServicePointManager.FindServicePoint(unleashApiUri);
ConfigureServicePoint(servicePoint);
Expand All @@ -51,6 +47,17 @@ public HttpClient Create(Uri unleashApiUri)
});
}

protected virtual HttpClient CreateHttpClientInstance(Uri unleashApiUri)
{
var client = new HttpClient
{
BaseAddress = unleashApiUri,
Timeout = Timeout
};

return client;
}

protected virtual void ConfigureHttpClient(HttpClient httpClient)
{
}
Expand Down
15 changes: 15 additions & 0 deletions tests/Unleash.Tests/DefaultUnleashTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using FluentAssertions;
using NUnit.Framework;
using System;
using Unleash.Tests.Mock;

namespace Unleash.Tests
{
Expand All @@ -27,5 +28,19 @@ public void ConfigureEvents_should_invoke_callback()
// Assert
callbackCalled.Should().BeTrue();
}

[Test]
public void Configure_Http_Client_Factory()
{
// Arrange
var factory = new HttpClientFactoryMock();
var apiUri = new Uri("http://localhost:8080/");

// Act
var client = factory.Create(apiUri);

// Assert
factory.CreateHttpClientInstanceCalled.Should().BeTrue();
}
}
}
22 changes: 22 additions & 0 deletions tests/Unleash.Tests/Mock/HttpClientFactoryMock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace Unleash.Tests.Mock
{
internal class HttpClientFactoryMock : DefaultHttpClientFactory
{
public bool CreateHttpClientInstanceCalled { get; private set; }

protected override HttpClient CreateHttpClientInstance(Uri unleashApiUri)
{
CreateHttpClientInstanceCalled = true;

return base.CreateHttpClientInstance(unleashApiUri);
}

}
}
1 change: 1 addition & 0 deletions tests/Unleash.Tests/Unleash.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
<Compile Include="IOTests.cs" />
<Compile Include="Metrics\MetricsBucketTests.cs" />
<Compile Include="Mock\ConfigurableMessageHandlerMock.cs" />
<Compile Include="Mock\HttpClientFactoryMock.cs" />
<Compile Include="Mock\MockApiClient.cs" />
<Compile Include="Mock\MockHttpMessageHandler.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down

0 comments on commit ba42074

Please sign in to comment.