testcontainers-mockserver 7.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- testcontainers_mockserver/__init__.py +20 -0
- testcontainers_mockserver/container.py +139 -0
- testcontainers_mockserver-7.1.0.dist-info/METADATA +101 -0
- testcontainers_mockserver-7.1.0.dist-info/RECORD +6 -0
- testcontainers_mockserver-7.1.0.dist-info/WHEEL +5 -0
- testcontainers_mockserver-7.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"""Testcontainers module for MockServer.
|
|
2
|
+
|
|
3
|
+
Provides a ``MockServerContainer`` that starts a ``mockserver/mockserver`` Docker
|
|
4
|
+
image, waits for readiness, and exposes convenience accessors for the mapped
|
|
5
|
+
host, port, and base URL.
|
|
6
|
+
|
|
7
|
+
Example::
|
|
8
|
+
|
|
9
|
+
from testcontainers_mockserver import MockServerContainer
|
|
10
|
+
|
|
11
|
+
with MockServerContainer() as mockserver:
|
|
12
|
+
url = mockserver.get_url()
|
|
13
|
+
# url is e.g. "http://localhost:49152"
|
|
14
|
+
# Use requests or any HTTP client to interact with MockServer at this URL.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from testcontainers_mockserver.container import MockServerContainer
|
|
18
|
+
|
|
19
|
+
__all__ = ["MockServerContainer"]
|
|
20
|
+
__version__ = "7.0.0"
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"""MockServerContainer — Testcontainers module for MockServer.
|
|
2
|
+
|
|
3
|
+
This module provides a thin wrapper around testcontainers' ``DockerContainer`` that
|
|
4
|
+
starts a ``mockserver/mockserver`` image, waits for readiness via the
|
|
5
|
+
``/mockserver/status`` HTTP endpoint (PUT, returns 200), and exposes convenience
|
|
6
|
+
methods for the mapped host, port, and URL.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from typing import Optional
|
|
12
|
+
|
|
13
|
+
from testcontainers.core.container import DockerContainer
|
|
14
|
+
from testcontainers.core.wait_strategies import HttpWaitStrategy
|
|
15
|
+
|
|
16
|
+
#: Default port MockServer listens on inside the container (HTTP, HTTPS, SOCKS,
|
|
17
|
+
#: and HTTP CONNECT all served on this single unified port).
|
|
18
|
+
MOCKSERVER_PORT = 1080
|
|
19
|
+
|
|
20
|
+
#: Default Docker image name for MockServer.
|
|
21
|
+
_IMAGE_NAME = "mockserver/mockserver"
|
|
22
|
+
|
|
23
|
+
#: Default image tag, pinned to the current MockServer release version.
|
|
24
|
+
_DEFAULT_TAG = "mockserver-7.0.0"
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class MockServerContainer(DockerContainer):
|
|
28
|
+
"""A Testcontainers wrapper that starts a MockServer Docker container.
|
|
29
|
+
|
|
30
|
+
The container starts the ``mockserver/mockserver`` image on port 1080 and waits
|
|
31
|
+
for the ``PUT /mockserver/status`` endpoint to return HTTP 200 before yielding
|
|
32
|
+
control.
|
|
33
|
+
|
|
34
|
+
Parameters
|
|
35
|
+
----------
|
|
36
|
+
image : str, optional
|
|
37
|
+
Full Docker image reference. Defaults to
|
|
38
|
+
``mockserver/mockserver:mockserver-7.0.0``.
|
|
39
|
+
port : int, optional
|
|
40
|
+
The port MockServer listens on inside the container. Defaults to 1080.
|
|
41
|
+
|
|
42
|
+
Example
|
|
43
|
+
-------
|
|
44
|
+
::
|
|
45
|
+
|
|
46
|
+
from testcontainers_mockserver import MockServerContainer
|
|
47
|
+
|
|
48
|
+
with MockServerContainer() as server:
|
|
49
|
+
url = server.get_url()
|
|
50
|
+
# e.g. "http://localhost:49152"
|
|
51
|
+
"""
|
|
52
|
+
|
|
53
|
+
def __init__(
|
|
54
|
+
self,
|
|
55
|
+
image: str = f"{_IMAGE_NAME}:{_DEFAULT_TAG}",
|
|
56
|
+
port: int = MOCKSERVER_PORT,
|
|
57
|
+
**kwargs,
|
|
58
|
+
) -> None:
|
|
59
|
+
super().__init__(image=image, **kwargs)
|
|
60
|
+
self._port = port
|
|
61
|
+
self.with_exposed_ports(self._port)
|
|
62
|
+
self.with_env("SERVER_PORT", str(self._port))
|
|
63
|
+
|
|
64
|
+
def _build_wait_strategy(self) -> HttpWaitStrategy:
|
|
65
|
+
"""Build the readiness wait strategy.
|
|
66
|
+
|
|
67
|
+
MockServer's ``/mockserver/status`` endpoint requires a PUT request and
|
|
68
|
+
returns HTTP 200 with a JSON body containing ``{ "ports": [...] }`` when
|
|
69
|
+
the server is ready.
|
|
70
|
+
"""
|
|
71
|
+
return (
|
|
72
|
+
HttpWaitStrategy(self._port, "/mockserver/status")
|
|
73
|
+
.with_method("PUT")
|
|
74
|
+
.with_startup_timeout(60)
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
def start(self) -> "MockServerContainer":
|
|
78
|
+
"""Start the container and wait for MockServer to become ready."""
|
|
79
|
+
super().start()
|
|
80
|
+
self._build_wait_strategy().wait_until_ready(self)
|
|
81
|
+
return self
|
|
82
|
+
|
|
83
|
+
# ------------------------------------------------------------------
|
|
84
|
+
# Convenience accessors
|
|
85
|
+
# ------------------------------------------------------------------
|
|
86
|
+
|
|
87
|
+
def get_host(self) -> str:
|
|
88
|
+
"""Return the host IP to connect to MockServer from the test process."""
|
|
89
|
+
return self.get_container_host_ip()
|
|
90
|
+
|
|
91
|
+
def get_port(self) -> int:
|
|
92
|
+
"""Return the mapped host port for MockServer's container port."""
|
|
93
|
+
return int(self.get_exposed_port(self._port))
|
|
94
|
+
|
|
95
|
+
def get_url(self) -> str:
|
|
96
|
+
"""Return the HTTP base URL for MockServer (e.g. ``http://localhost:49152``)."""
|
|
97
|
+
return f"http://{self.get_host()}:{self.get_port()}"
|
|
98
|
+
|
|
99
|
+
def get_secure_url(self) -> str:
|
|
100
|
+
"""Return the HTTPS base URL for MockServer (same port, different scheme)."""
|
|
101
|
+
return f"https://{self.get_host()}:{self.get_port()}"
|
|
102
|
+
|
|
103
|
+
# ------------------------------------------------------------------
|
|
104
|
+
# Configuration helpers (fluent)
|
|
105
|
+
# ------------------------------------------------------------------
|
|
106
|
+
|
|
107
|
+
def with_server_port(self, port: int) -> "MockServerContainer":
|
|
108
|
+
"""Override the MockServer listen port inside the container.
|
|
109
|
+
|
|
110
|
+
This replaces the exposed port so the wait strategy targets the correct port.
|
|
111
|
+
"""
|
|
112
|
+
self._port = port
|
|
113
|
+
# Reset exposed ports to only the new port
|
|
114
|
+
self.ports = {}
|
|
115
|
+
self.with_exposed_ports(port)
|
|
116
|
+
self.with_env("SERVER_PORT", str(port))
|
|
117
|
+
return self
|
|
118
|
+
|
|
119
|
+
def with_log_level(self, level: str) -> "MockServerContainer":
|
|
120
|
+
"""Set the MockServer log level (e.g. INFO, DEBUG, WARN, ERROR, TRACE)."""
|
|
121
|
+
self.with_env("MOCKSERVER_LOG_LEVEL", level)
|
|
122
|
+
return self
|
|
123
|
+
|
|
124
|
+
def with_property(self, key: str, value: str) -> "MockServerContainer":
|
|
125
|
+
"""Set a MockServer configuration property as an environment variable.
|
|
126
|
+
|
|
127
|
+
The key must be in MockServer env-var form (e.g. ``MOCKSERVER_LOG_LEVEL``).
|
|
128
|
+
"""
|
|
129
|
+
self.with_env(key, value)
|
|
130
|
+
return self
|
|
131
|
+
|
|
132
|
+
def with_initialization_json(self, container_path: str) -> "MockServerContainer":
|
|
133
|
+
"""Configure MockServer to load expectations from a JSON file at startup.
|
|
134
|
+
|
|
135
|
+
The file must already be mounted/copied into the container at *container_path*.
|
|
136
|
+
This sets the ``MOCKSERVER_INITIALIZATION_JSON_PATH`` environment variable.
|
|
137
|
+
"""
|
|
138
|
+
self.with_env("MOCKSERVER_INITIALIZATION_JSON_PATH", container_path)
|
|
139
|
+
return self
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: testcontainers-mockserver
|
|
3
|
+
Version: 7.1.0
|
|
4
|
+
Summary: Testcontainers module for MockServer
|
|
5
|
+
Author-email: James D Bloom <jamesdbloom@gmail.com>
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://www.mock-server.com
|
|
8
|
+
Project-URL: Repository, https://github.com/mock-server/mockserver
|
|
9
|
+
Project-URL: Documentation, https://www.mock-server.com/mock_server/mockserver_testcontainers.html
|
|
10
|
+
Keywords: testing,docker,mockserver,testcontainers,mocking
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
20
|
+
Classifier: Topic :: Software Development :: Testing
|
|
21
|
+
Requires-Python: >=3.9
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
Requires-Dist: testcontainers>=4.0.0
|
|
24
|
+
Provides-Extra: test
|
|
25
|
+
Requires-Dist: pytest>=7.0; extra == "test"
|
|
26
|
+
Requires-Dist: requests>=2.20; extra == "test"
|
|
27
|
+
|
|
28
|
+
# testcontainers-mockserver
|
|
29
|
+
|
|
30
|
+
A [Testcontainers](https://testcontainers.com) module for [MockServer](https://www.mock-server.com) in Python.
|
|
31
|
+
|
|
32
|
+
Starts a `mockserver/mockserver` Docker container, waits for readiness, and provides
|
|
33
|
+
convenient accessors for the mapped host, port, and URL.
|
|
34
|
+
|
|
35
|
+
## Installation
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
pip install testcontainers-mockserver
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Usage
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from testcontainers_mockserver import MockServerContainer
|
|
45
|
+
import requests
|
|
46
|
+
|
|
47
|
+
with MockServerContainer() as mockserver:
|
|
48
|
+
url = mockserver.get_url() # e.g. "http://localhost:49152"
|
|
49
|
+
|
|
50
|
+
# Create an expectation
|
|
51
|
+
requests.put(f"{url}/mockserver/expectation", json={
|
|
52
|
+
"httpRequest": {"method": "GET", "path": "/hello"},
|
|
53
|
+
"httpResponse": {"statusCode": 200, "body": "world"},
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
# Match it
|
|
57
|
+
resp = requests.get(f"{url}/hello")
|
|
58
|
+
assert resp.text == "world"
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## API
|
|
62
|
+
|
|
63
|
+
### `MockServerContainer(image=..., port=1080)`
|
|
64
|
+
|
|
65
|
+
- `image` — Docker image to use. Defaults to `mockserver/mockserver:mockserver-7.1.0`.
|
|
66
|
+
- `port` — Container port MockServer listens on. Defaults to `1080`.
|
|
67
|
+
|
|
68
|
+
### Methods
|
|
69
|
+
|
|
70
|
+
| Method | Returns | Description |
|
|
71
|
+
|--------|---------|-------------|
|
|
72
|
+
| `get_url()` | `str` | HTTP base URL (e.g. `http://localhost:49152`) |
|
|
73
|
+
| `get_secure_url()` | `str` | HTTPS base URL (same port, `https://` scheme) |
|
|
74
|
+
| `get_host()` | `str` | Mapped host IP |
|
|
75
|
+
| `get_port()` | `int` | Mapped host port |
|
|
76
|
+
| `with_server_port(port)` | `self` | Override the listen port |
|
|
77
|
+
| `with_log_level(level)` | `self` | Set `MOCKSERVER_LOG_LEVEL` |
|
|
78
|
+
| `with_property(key, value)` | `self` | Set any MockServer env var |
|
|
79
|
+
| `with_initialization_json(path)` | `self` | Point to a startup expectations JSON file |
|
|
80
|
+
|
|
81
|
+
## Building and Testing
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
# Install in editable mode with test dependencies
|
|
85
|
+
pip install -e .[test]
|
|
86
|
+
|
|
87
|
+
# Run unit tests (no Docker needed)
|
|
88
|
+
pytest tests/test_container_config.py
|
|
89
|
+
|
|
90
|
+
# Run all tests including integration (requires Docker)
|
|
91
|
+
pytest
|
|
92
|
+
|
|
93
|
+
# Skip Docker-dependent tests
|
|
94
|
+
pytest -m "not docker"
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Requirements
|
|
98
|
+
|
|
99
|
+
- Python >= 3.9
|
|
100
|
+
- Docker (for integration tests and actual usage)
|
|
101
|
+
- `testcontainers` >= 4.0.0
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
testcontainers_mockserver/__init__.py,sha256=R_s74zXKFen4EkQLE50aCBdFPrDMuIihI66YLtFetD0,643
|
|
2
|
+
testcontainers_mockserver/container.py,sha256=BMzdJzSGmExZYBSFltw3F_KVWR9QYE16-wp4IQXwvq4,5181
|
|
3
|
+
testcontainers_mockserver-7.1.0.dist-info/METADATA,sha256=c0MjNZ4EoQD3qV6blHXB_IfnpquwS1uVH7kqt8VTqVY,3286
|
|
4
|
+
testcontainers_mockserver-7.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
5
|
+
testcontainers_mockserver-7.1.0.dist-info/top_level.txt,sha256=qXB8zu0gPvNlC2F2oRYU9hqeWIT8rjmftL_uvi_aghw,26
|
|
6
|
+
testcontainers_mockserver-7.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
testcontainers_mockserver
|