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.8.tar.gz (49.1 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.8-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: caddy_in_the_middle-0.4.8.tar.gz
  • Upload date:
  • Size: 49.1 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.4.8.tar.gz
Algorithm Hash digest
SHA256 3bb595b53573637a113def280d2a7010ad55560893d0be1099e88ce6e6606935
MD5 526bfdab2db63e6c4fbb67027bc93895
BLAKE2b-256 25198d8d6def8971ce5f2187e62dd7dd7c742e065fe47bb908719a8e92617dcd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: caddy_in_the_middle-0.4.8-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.4.8-py3-none-any.whl
Algorithm Hash digest
SHA256 efc33fb36a8eac45e7b13596cbb45cc932f130f82455c8c094a05eedcec567db
MD5 2d52c057aa3efc5c4ae09e1b60c7d91f
BLAKE2b-256 b873c5124266a20e37ba650ae2402f503b3b78f506227c4c8156ef5fe274d1a3

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