You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker Compose多组件应用E2E自动化测试及callback URL验证咨询

Great question! Let's walk through how to tackle this E2E test scenario for your Docker Compose app—since you've got Kafka, Postgres, and a REST API, we need to handle both the API submission and the asynchronous callback verification properly.

Pick a framework that aligns with your tech stack—here are the best fits for this scenario:

  • Python Stack: Pytest + Requests + pytest-httpserver
    Lightweight, easy to write, and pytest-httpserver solves the mock callback problem out of the box. Perfect for focused API E2E tests.
  • JavaScript/TypeScript Stack: Cypress or Playwright
    These tools handle API testing seamlessly, and can extend to UI tests if your app has a frontend. Both support custom tasks to spin up/tear down mock servers.
  • Java Stack: JUnit 5 + RestAssured + WireMock
    WireMock is a mature mock server tool in the Java ecosystem, pairing perfectly with RestAssured for API request sending and validation.
How to Verify the Callback URL Receives Data

The core challenge here is validating that your app sends the right data to the callback. Here's the step-by-step approach:

  1. Spin Up a Temporary Mock Callback Server
    You can't rely on external services for testing—instead, launch a short-lived mock server during your test lifecycle to capture callback requests. This lets you directly inspect the data your app sends.

    • For Python: Use pytest-httpserver (a pytest plugin that auto-starts/stops a local HTTP server per test).
    • For JavaScript: Write a simple Express server, or use cypress-mock-server to manage mock endpoints.
    • For Java: WireMock lets you configure request matching rules and capture incoming requests.
  2. Ensure Docker Containers Can Reach the Mock Server
    Since your app runs in Docker, you need to make sure it can connect to your mock service:

    • If testing on your host machine: Use host.docker.internal (supported by Docker Desktop) as the host address. For example, if your mock runs on port 5000, your callback URL would be http://host.docker.internal:5000/callback.
    • If testing in a Docker container: Add your test container to the same Docker network as your app services. You can then reference the mock server by its container name.
  3. Handle Asynchronous Flow (Thanks to Kafka)
    Kafka introduces asynchronous processing, so your app won't send the callback immediately. Account for this with:

    • Explicit waits (e.g., time.sleep() in Python, cy.wait() in Cypress) tailored to your Kafka processing speed.
    • Retry logic: Loop until the mock server receives the callback, or hit a reasonable timeout (10-30 seconds is typical).
Example: Pytest + pytest-httpserver

Here's a concrete code snippet to illustrate the workflow:

import requests
import time

def test_api_submit_triggers_callback(httpserver):
    # 1. Configure mock server to expect a POST to /callback
    test_data = {"user_id": 123, "payload": "test_content"}
    httpserver.expect_request("/callback", method="POST").respond_with_json({"status": "success"})

    # 2. Define API endpoint and callback URL
    api_endpoint = "http://localhost:8080/api/submit"  # Your API's exposed port
    callback_url = httpserver.url_for("/callback")

    # 3. Send submission request to your API
    submit_response = requests.post(
        api_endpoint,
        json={"data": test_data, "callback_url": callback_url}
    )
    assert submit_response.status_code == 200, "API should accept the submission"

    # 4. Wait for Kafka to process the message and trigger the callback
    time.sleep(5)  # Adjust based on your app's processing time

    # 5. Verify the mock server received the correct data
    httpserver.check_assertions()  # Confirms the expected request was received
    received_request = httpserver.last_request
    assert received_request.json == test_data, "Callback data doesn't match submitted data"
Pro Tips
  • Dedicated Docker Network: Add a test-specific network to your docker-compose.yml so all services (API, Kafka, Postgres, mock server) can communicate without port conflicts.
  • Auto-Cleanup: Most frameworks handle mock server teardown automatically (e.g., pytest-httpserver stops the server after each test), but double-check to avoid leftover processes.
  • Log Debugging: If callbacks aren't hitting your mock server, check your API and Kafka logs first—often the issue is with message consumption, not your test setup.

内容的提问来源于stack exchange,提问作者BAE

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:32:11