Skip to main content

Testcontainers module for CaddyInTheMiddle

Project description

Testcontainers CaddyInTheMiddle Module (Python)

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 Python test code.

Getting Started

  1. Install the package: (Assuming local development or future package name)

    # using uv
    uv add caddy-in-the-middle
    
    # or pip
    pip install caddy-in-the-middle
    
  2. Generate Test Certificates: Integration tests typically require trusted certificates. This library includes a helper to generate valid self-signed Root CA certificates on the fly.

  3. Start the Container: Use the CitmContainer class to configure and build the container instance.

Usage Example

Here is a complete example using pytest:

import pytest
from pathlib import Path
import tempfile
import shutil
from caddy_in_the_middle import CitmContainer, generate_root_ca

@pytest.fixture(scope="module")
def citm_container():
    # Create a temporary directory for certs
    certs_dir = tempfile.mkdtemp()
    certs_path = Path(certs_dir)
    
    try:
        # Generate the Root CA certificates
        generate_root_ca(certs_path)

        # Configure and start the container
        # Note: DOCKER_HOST is auto-detected if not set (e.g. for OrbStack)
        with CitmContainer().with_certs_directory(certs_path) as container:
            yield container
    finally:
        if Path(certs_dir).exists():
           shutil.rmtree(certs_dir)

def test_should_proxy_request(citm_container):
    # Create a requests.Session configured to use the container's proxy
    # This session ignores SSL errors by default since we use self-signed certs
    session = citm_container.create_client(ignore_ssl_errors=True)

    # Make a request through MITMProxy in the citm container
    # Note: 'example.com' will be proxied
    response = session.get("https://registered-dns-name-in-citm-network:1234/blabla")
    assert response.status_code == 200

Configuration

The CitmContainer provides a fluent API for customization:

  • with_certs_directory(path: Path) (Required): Path to the directory containing rootCA.pem and rootCA-key.pem.
  • with_mocks_directory(path: Path): Mounts a directory of mock templates (e.g., *.mako files) into the container.
  • with_caddyfile_directory(path: Path): Mounts a directory containing a custom Caddyfile if you need advanced Caddy configuration.
  • with_citm_network(network_name: str): Connects the container to a specific Docker network. This enables automatic service discovery: if other containers on this network have the citm_dns_names label, their DNS names will be automatically resolved by the dnsmasq instance running inside the CITM container.
  • with_dns_names(*names: str): Sets the citm_dns_names label on the container. This leverages CITM's built-in service discovery to register these DNS names.
  • with_mock_paths(patterns: List[str]): explicit list of mock file patterns to load. Note: Paths are validated to prevent traversal outside the mocks directory.

Helper Methods

Once the container is running and healthy, you can access helpful properties and methods:

  • create_client(ignore_ssl_errors=True): Returns a requests.Session pre-configured with the correct proxy settings.
  • get_http_proxy_address(): Returns the address of the HTTP proxy (e.g., http://localhost:32768).
  • get_socks_proxy_address(): Returns the address of the SOCKS5 proxy.
  • get_admin_base_url(): Returns the base URL for the Caddy admin API.
  • get_caddy_http_base_url() / get_caddy_https_base_url(): Returns the base URLs for direct access to Caddy.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

caddy_in_the_middle-0.5.0.tar.gz (49.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

caddy_in_the_middle-0.5.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file caddy_in_the_middle-0.5.0.tar.gz.

File metadata

  • Download URL: caddy_in_the_middle-0.5.0.tar.gz
  • Upload date:
  • Size: 49.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for caddy_in_the_middle-0.5.0.tar.gz
Algorithm Hash digest
SHA256 082b11ba7b05a50a9f75b44d4ec1e4e38f6ff1980fad5038c5fef4085dcf53c1
MD5 665044cbd43f11e445e2f5785abd727b
BLAKE2b-256 9e7bdc98bc8cfc751122adc2cf62c4f18ba86e0140f55ce08c04370cb13fe8db

See more details on using hashes here.

File details

Details for the file caddy_in_the_middle-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: caddy_in_the_middle-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for caddy_in_the_middle-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 690225ed2f1da38e1b08ee0e96d77ae3131120676ca15ffdddb75921c5c98c63
MD5 bdd131e28fce25388ab642b71266a667
BLAKE2b-256 1661594cf4222f3029d6dc14e0f9d66acd28172ea05804cffc39013dadbab254

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page