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.4.4.tar.gz (48.8 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.4.4-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: caddy_in_the_middle-0.4.4.tar.gz
  • Upload date:
  • Size: 48.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","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.4.4.tar.gz
Algorithm Hash digest
SHA256 c8ed078ac924ea451667bf60817b041a67ea89a9d3fb38b58cd315d78615b5ff
MD5 462b2cc6a2b0f86a3c3ab2e8e1d36a62
BLAKE2b-256 8246a2d66ec49e7096d6fa185698945131f2396c4efda47c5fd21980b10d0c4a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: caddy_in_the_middle-0.4.4-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","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.4.4-py3-none-any.whl
Algorithm Hash digest
SHA256 9242a5cd6ab62535c642d22786e184c7a19190b6f322d9dfab50684abc83fae9
MD5 664d6faba695e5b5f4507aa13417ce29
BLAKE2b-256 2f8acfb490d3d6aef59a4fb0e55f51a88fc93207476f796cedefbd2c337b2a03

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