CaddyInTheMiddle.Testcontainers
0.5.0
dotnet add package CaddyInTheMiddle.Testcontainers --version 0.5.0
NuGet\Install-Package CaddyInTheMiddle.Testcontainers -Version 0.5.0
<PackageReference Include="CaddyInTheMiddle.Testcontainers" Version="0.5.0" />
<PackageVersion Include="CaddyInTheMiddle.Testcontainers" Version="0.5.0" />
<PackageReference Include="CaddyInTheMiddle.Testcontainers" />
paket add CaddyInTheMiddle.Testcontainers --version 0.5.0
#r "nuget: CaddyInTheMiddle.Testcontainers, 0.5.0"
#:package CaddyInTheMiddle.Testcontainers@0.5.0
#addin nuget:?package=CaddyInTheMiddle.Testcontainers&version=0.5.0
#tool nuget:?package=CaddyInTheMiddle.Testcontainers&version=0.5.0
Testcontainers CaddyInTheMiddle Module
A Testcontainers module for CaddyInTheMiddle, designed to simplify integration testing where you need a programmable reverse proxy or MITM proxy.
This library allows you to spin up a pre-configured Caddy instance in Docker, complete with mock responses, custom certificates, and proxy settings, all from your .NET test code.
Getting Started
Install the package:
dotnet add package Testcontainers.CaddyInTheMiddleGenerate Test Certificates: Integration tests typically require trusted certificates. This library includes a helper to generate valid self-signed Root CA certificates on the fly.
Start the Container: Use the
CaddyInTheMiddleBuilderto configure and build the container instance.
Usage Example
Here is a complete example using xUnit:
using Testcontainers.CaddyInTheMiddle;
using Xunit;
public class MyIntegrationTests : IAsyncLifetime
{
private readonly CaddyInTheMiddleContainer _container;
private readonly string _certsDir;
public MyIntegrationTests()
{
// Create a temporary directory for certs
_certsDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(_certsDir);
// Generate the Root CA certificates
CaddyInTheMiddleCertificates.Generate(_certsDir);
// Configure the container
_container = new CaddyInTheMiddleBuilder()
.WithCertsDirectory(_certsDir)
// Optional: Provide a directory of mock response files
// .WithMocksDirectory(Path.Combine(Directory.GetCurrentDirectory(), "mocks"))
.Build();
}
public Task InitializeAsync() => _container.StartAsync();
public async Task DisposeAsync()
{
await _container.DisposeAsync();
Directory.Delete(_certsDir, true);
}
[Fact]
public async Task ShouldProxyRequest()
{
// Create an HttpClient configured to use the container's proxy
using var handler = _container.CreateHttpClientHandler();
using var client = new HttpClient(handler);
// Make a request through MITMProxy in the citm container
var response = await client.GetAsync("https://registered-dns-name-in-citm-network:1234/blabla");
Assert.True(response.IsSuccessStatusCode);
}
}
Configuration
The CaddyInTheMiddleBuilder provides a fluent API for customization:
WithCertsDirectory(string path)(Required): Path to the directory containingrootCA.pemandrootCA-key.pem.WithMocksDirectory(string path): Mounts a directory of mock templates (e.g.,*.makofiles) into the container.WithCaddyfileDirectory(string path): Mounts a directory containing a customCaddyfileif you need advanced Caddy configuration.WithCitmNetwork(string networkName): Connects the container to a specific Docker network. This enables automatic service discovery: if other containers on this network have thecitm_dns_nameslabel, their DNS names will be automatically resolved by thednsmasqinstance running inside the CITM container.WithDnsNames(params string[] names): Sets thecitm_dns_nameslabel on the container. This leverages CITM's built-in service discovery to register these DNS names.
Helper Methods
Once the container is running and healthy, you can access helpful properties and methods:
CreateHttpClientHandler(bool ignoreSslErrors = true): Returns anHttpClientHandlerpre-configured with the correct proxy settings.GetHttpProxyAddress(): Returns the address of the HTTP proxy (e.g.,http://localhost:32768).GetSocksProxyAddress(): Returns the address of the SOCKS5 proxy.GetAdminBaseUrl(): Returns the base URL for the Caddy admin API.GetCaddyHttpBaseUrl()/GetCaddyHttpsBaseUrl(): Returns the base URLs for direct access to Caddy.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- JetBrains.Annotations (>= 2025.2.4)
- Testcontainers (>= 4.10.0)
-
net8.0
- JetBrains.Annotations (>= 2025.2.4)
- Testcontainers (>= 4.10.0)
-
net9.0
- JetBrains.Annotations (>= 2025.2.4)
- Testcontainers (>= 4.10.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.