I have created a custom ConfigurationProvider in order to configure and start up a container from the testcontainers-dotnet
package.
I have run into an issue, where I need an HttpClient initialized by the provider available when I set up dependency injection for my tests, but I’m unsure how I can make it accessible.
The process has to happen in this order, steps 2 or 3 could happen outside of the provider if I am able somehow to make either the container or httpClient created in it available to my WebApplicationFactory.
- Provider configures and starts up TestContainer
- HttpClient is created from the started container
- HttpClient is injected as a dependency
As an example of my provider approach working well, this is a stripped down version of the basic configuration for the WireMock module.
I’m able start the container and then send the mappings to the WireMock client that will be used in the tests. I can also utilise the Set()
function, inherited from ConfigurationProvider, to override values that would normally come from my appsettings file.
public class WireMockConfigurationSource : IConfigurationSource
{
public IList<MappingModel> Mappings { get; }
public WireMockConfigurationSource(IList<MappingModel> mappings)
{
Mappings = mappings;
}
public IConfigurationProvider Build(IConfigurationBuilder builder) => new WireMockConfigurationProvider(this);
}
The bulk of the work happens in the ConfigurationProvider
(IDisposable code ommited)
public class WireMockConfigurationProvider : ConfigurationProvider
{
private static readonly TaskFactory TaskFactory = new(CancellationToken.None, TaskCreationOptions.None,
TaskContinuationOptions.None, TaskScheduler.Default);
private readonly WireMockContainer _container;
private readonly WireMockConfigurationSource _source;
private CancellationTokenSource? _cts;
public WireMockConfigurationProvider(WireMockConfigurationSource source)
{
_source = source;
_container = new WireMockContainerBuilder()
.WithAutoRemove(true)
.WithCleanUp(true)
.Build();
}
public override void Load()
{
if (_cts is not null)
{
return;
}
_cts = new CancellationTokenSource();
TaskFactory.StartNew(() => LoadAsync(_cts.Token), _cts.Token).Unwrap().ConfigureAwait(false)
.GetAwaiter()
.GetResult();
}
public async Task LoadAsync(CancellationToken ct)
{
// My container is started
await _container.StartAsync(ct);
// The URL for the container provided to my settings
SetAppSettingsForWireMockedComponent();
// Some data is set up on the container for tests
await InitializeMappings(ct);
}
private async Task InitializeMappings(CancellationToken ct)
{
var wireMockAdminClient = _container.CreateWireMockAdminClient();
await wireMockAdminClient.PostMappingsAsync(_source.Mappings, ct);
}
private void SetAppSettingsForWireMockedComponent()
{
var containerUrl = _container.GetPublicUrl().TrimEnd('/');
Set("MyAppSetting:ApiUrl", containerUrl);
}
}
This is then used by ConfigureAppConfiguration
from my WebApplicationFactory
public class ApiFactory<TProgram> : WebApplicationFactory<TProgram>
where TProgram : class
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureAppConfiguration(configure => configure.Add(new WireMockConfigurationSource()));
builder.ConfigureServices(
(context, services) =>
{
// DI for test run
// This is where I would have to inject my HttpClient
});
}
}
The would like to reuse the same approach for the CosmosDB module. However, I need to be able to access the WireMockContainer
that’s started up in the provider outside of the ConfigurationProvider. This is because the container creates a HttpClient with a trusted certificate, so you can send authorized requests to the CosmosDB instance,
var cosmosClient = new CosmosClient(
// connection string for the emulator, this can be passed to app settings via "Set()" so no issues accessing the value outside of the Provider.
_container.GetConnectionString(),
new CosmosClientOptions
{
// The HTTP client created from the container automatically trusts the emulators certificate.
// Without this HttpClient, you get 401 denied when sending requests to CosmosDB.
// It will be needed outside of the provider to create a CosmosClient for dependency injection
HttpClientFactory = () => _container.HttpClient,
ConnectionMode = ConnectionMode.Gateway,
});