MockHttpServer
Local HTTP server for backend testing.
This server can be used to mock external backends during testing. It supports both httpbin-style behavior and custom response patterns.
Constructor
MockHttpServer(host: str = '127.0.0.1', port: int = 0, responses: dict[str, dict[str, Any]] | None = None)Initialize the test server.
Parameters
- host: stroptional =
'127.0.0.1' The host interface to bind to (default: "127.0.0.1")
- port: intoptional =
0 The port to bind to (default: 0 for auto-assignment)
- responses: dict[str, dict[str, Any]] | Noneoptional =
None Optional dict mapping paths to response configs. Each response config can contain...
"status"
HTTP status code (default: 200)"headers"
Dict of HTTP headers"body"
Response body (dict will be JSON-encoded)Example::
{"/api/test": {"status": 200, "body": {"success": True}}}
Properties
Get the base URL of the running server.
Methods
__exit__
View source__exit__(exc_type, exc_val, exc_tb)Context manager exit.
Parameters
- exc_type: Any
- exc_val: Any
- exc_tb: Any
start
View sourcestart() -> strStart the test server.
Returns
str
The base URL of the started server (e.g., "http://127.0.0.1:12345")