mockserver-client 0.0.6__tar.gz → 6.0.0__tar.gz
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.
- mockserver_client-6.0.0/PKG-INFO +291 -0
- mockserver_client-6.0.0/README.md +261 -0
- mockserver_client-6.0.0/mockserver/__init__.py +85 -0
- mockserver_client-6.0.0/mockserver/async_client.py +425 -0
- mockserver_client-6.0.0/mockserver/client.py +207 -0
- mockserver_client-6.0.0/mockserver/exceptions.py +21 -0
- mockserver_client-6.0.0/mockserver/fluent.py +116 -0
- mockserver_client-6.0.0/mockserver/models.py +1358 -0
- mockserver_client-6.0.0/mockserver/websocket_client.py +314 -0
- mockserver_client-6.0.0/mockserver_client.egg-info/PKG-INFO +291 -0
- mockserver_client-6.0.0/mockserver_client.egg-info/SOURCES.txt +22 -0
- mockserver_client-6.0.0/mockserver_client.egg-info/requires.txt +6 -0
- mockserver_client-6.0.0/pyproject.toml +52 -0
- mockserver_client-6.0.0/setup.cfg +4 -0
- mockserver_client-6.0.0/tests/test_async_client.py +437 -0
- mockserver_client-6.0.0/tests/test_client.py +294 -0
- mockserver_client-6.0.0/tests/test_exceptions.py +43 -0
- mockserver_client-6.0.0/tests/test_fluent.py +231 -0
- mockserver_client-6.0.0/tests/test_init.py +57 -0
- mockserver_client-6.0.0/tests/test_integration.py +325 -0
- mockserver_client-6.0.0/tests/test_models.py +2034 -0
- mockserver_client-6.0.0/tests/test_websocket_client.py +459 -0
- mockserver-client-0.0.6/.travis.yml +0 -43
- mockserver-client-0.0.6/AUTHORS +0 -4
- mockserver-client-0.0.6/ChangeLog +0 -39
- mockserver-client-0.0.6/LICENSE +0 -176
- mockserver-client-0.0.6/PKG-INFO +0 -237
- mockserver-client-0.0.6/README.md +0 -218
- mockserver-client-0.0.6/constraints.txt +0 -11
- mockserver-client-0.0.6/docker-compose.yml +0 -11
- mockserver-client-0.0.6/mockserver/__init__.py +0 -175
- mockserver-client-0.0.6/mockserver_client.egg-info/PKG-INFO +0 -237
- mockserver-client-0.0.6/mockserver_client.egg-info/SOURCES.txt +0 -30
- mockserver-client-0.0.6/mockserver_client.egg-info/not-zip-safe +0 -1
- mockserver-client-0.0.6/mockserver_client.egg-info/pbr.json +0 -1
- mockserver-client-0.0.6/mockserver_client.egg-info/requires.txt +0 -1
- mockserver-client-0.0.6/requirements.txt +0 -1
- mockserver-client-0.0.6/setup.cfg +0 -29
- mockserver-client-0.0.6/setup.py +0 -6
- mockserver-client-0.0.6/test/__init__.py +0 -23
- mockserver-client-0.0.6/test/test_basic_expectations.py +0 -57
- mockserver-client-0.0.6/test/test_basic_responses.py +0 -65
- mockserver-client-0.0.6/test/test_basic_stubbing.py +0 -124
- mockserver-client-0.0.6/test/test_delays.py +0 -38
- mockserver-client-0.0.6/test/test_form_requests.py +0 -20
- mockserver-client-0.0.6/test/test_json_requests.py +0 -34
- mockserver-client-0.0.6/test/test_json_response.py +0 -26
- mockserver-client-0.0.6/test-constraints.txt +0 -12
- mockserver-client-0.0.6/test-requirements.txt +0 -1
- mockserver-client-0.0.6/test-runner.sh +0 -32
- mockserver-client-0.0.6/tox.ini +0 -28
- {mockserver-client-0.0.6 → mockserver_client-6.0.0}/mockserver_client.egg-info/dependency_links.txt +0 -0
- {mockserver-client-0.0.6 → mockserver_client-6.0.0}/mockserver_client.egg-info/top_level.txt +0 -0
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: mockserver-client
|
|
3
|
+
Version: 6.0.0
|
|
4
|
+
Summary: Official Python client for MockServer — create expectations, verify requests, and register dynamic callbacks via WebSocket
|
|
5
|
+
Author-email: James 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-monorepo
|
|
9
|
+
Project-URL: Documentation, https://www.mock-server.com/mock_server/getting_started.html
|
|
10
|
+
Project-URL: Changelog, https://www.mock-server.com/mock_server/changelog.html
|
|
11
|
+
Project-URL: Bug Tracker, https://github.com/mock-server/mockserver-monorepo/issues
|
|
12
|
+
Keywords: MockServer,HTTP Mock,Proxy,Testing,API,WebSocket,Callback,MCP,AI
|
|
13
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Topic :: Software Development :: Testing
|
|
22
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
23
|
+
Requires-Python: >=3.9
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
Requires-Dist: websockets>=15.0.1
|
|
26
|
+
Provides-Extra: dev
|
|
27
|
+
Requires-Dist: pytest>=8.4.2; extra == "dev"
|
|
28
|
+
Requires-Dist: pytest-asyncio>=1.2.0; extra == "dev"
|
|
29
|
+
Requires-Dist: pytest-cov>=7.1.0; extra == "dev"
|
|
30
|
+
|
|
31
|
+
# MockServer Python Client
|
|
32
|
+
|
|
33
|
+
Python client for [MockServer](https://www.mock-server.com) with full WebSocket callback support.
|
|
34
|
+
|
|
35
|
+
## Features
|
|
36
|
+
|
|
37
|
+
- **Full REST API**: Create expectations, verify requests, clear/reset, retrieve recorded data
|
|
38
|
+
- **Response Callbacks**: Register Python functions that dynamically generate responses via WebSocket
|
|
39
|
+
- **Forward Callbacks**: Modify requests before they are forwarded to the real server
|
|
40
|
+
- **Forward+Response Callbacks**: Modify both the forwarded request and the response
|
|
41
|
+
- **Fluent API**: `client.when(request).respond(callback)` — mirrors the Java client
|
|
42
|
+
- **Async + Sync**: Native asyncio API with a synchronous wrapper for non-async code
|
|
43
|
+
- **Minimal Dependencies**: Only `websockets` (for callback support)
|
|
44
|
+
|
|
45
|
+
## Installation
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
pip install mockserver-client
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Quick Start
|
|
52
|
+
|
|
53
|
+
### Synchronous API
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
from mockserver import MockServerClient, HttpRequest, HttpResponse
|
|
57
|
+
|
|
58
|
+
client = MockServerClient("localhost", 1080)
|
|
59
|
+
|
|
60
|
+
# Static expectation
|
|
61
|
+
client.when(
|
|
62
|
+
HttpRequest.request("/api/users").with_method("GET")
|
|
63
|
+
).respond(
|
|
64
|
+
HttpResponse.response('{"users": []}', status_code=200)
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
# Verify
|
|
68
|
+
client.verify(
|
|
69
|
+
HttpRequest.request("/api/users").with_method("GET"),
|
|
70
|
+
VerificationTimes.at_least(1)
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
# Clean up
|
|
74
|
+
client.reset()
|
|
75
|
+
client.close()
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Context Manager
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
with MockServerClient("localhost", 1080) as client:
|
|
82
|
+
client.when(
|
|
83
|
+
HttpRequest.request("/hello")
|
|
84
|
+
).respond(
|
|
85
|
+
HttpResponse.response("world")
|
|
86
|
+
)
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Async API
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
import asyncio
|
|
93
|
+
from mockserver import AsyncMockServerClient, HttpRequest, HttpResponse
|
|
94
|
+
|
|
95
|
+
async def main():
|
|
96
|
+
async with AsyncMockServerClient("localhost", 1080) as client:
|
|
97
|
+
await client.when(
|
|
98
|
+
HttpRequest.request("/api/data")
|
|
99
|
+
).respond(
|
|
100
|
+
HttpResponse.response('{"key": "value"}')
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
asyncio.run(main())
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## Response Callbacks
|
|
107
|
+
|
|
108
|
+
Register a Python function that generates responses dynamically when matching requests arrive:
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
from mockserver import MockServerClient, HttpRequest, HttpResponse
|
|
112
|
+
|
|
113
|
+
def handle_request(request):
|
|
114
|
+
if request.method == "POST":
|
|
115
|
+
return HttpResponse.response("created", status_code=201)
|
|
116
|
+
return HttpResponse.not_found_response()
|
|
117
|
+
|
|
118
|
+
client = MockServerClient("localhost", 1080)
|
|
119
|
+
client.mock_with_callback(
|
|
120
|
+
HttpRequest.request("/api/callback"),
|
|
121
|
+
handle_request
|
|
122
|
+
)
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
Or with the fluent API:
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
client.when(
|
|
129
|
+
HttpRequest.request("/api/callback")
|
|
130
|
+
).respond(handle_request)
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## Forward Callbacks
|
|
134
|
+
|
|
135
|
+
Modify requests before they are forwarded to the real server:
|
|
136
|
+
|
|
137
|
+
```python
|
|
138
|
+
def modify_request(request):
|
|
139
|
+
return request.with_header("X-Forwarded", "true").with_path("/modified" + request.path)
|
|
140
|
+
|
|
141
|
+
client.mock_with_forward_callback(
|
|
142
|
+
HttpRequest.request("/proxy/.*"),
|
|
143
|
+
modify_request
|
|
144
|
+
)
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## Forward+Response Callbacks
|
|
148
|
+
|
|
149
|
+
Modify both the forwarded request and the response:
|
|
150
|
+
|
|
151
|
+
```python
|
|
152
|
+
def modify_request(request):
|
|
153
|
+
return request.with_header("X-Proxied", "true")
|
|
154
|
+
|
|
155
|
+
def modify_response(request, response):
|
|
156
|
+
return response.with_header("X-Modified", "true")
|
|
157
|
+
|
|
158
|
+
client.mock_with_forward_callback(
|
|
159
|
+
HttpRequest.request("/proxy/.*"),
|
|
160
|
+
modify_request,
|
|
161
|
+
modify_response
|
|
162
|
+
)
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## Verification
|
|
166
|
+
|
|
167
|
+
```python
|
|
168
|
+
from mockserver import VerificationTimes
|
|
169
|
+
|
|
170
|
+
# Verify a request was received at least once
|
|
171
|
+
client.verify(
|
|
172
|
+
HttpRequest.request("/api/users").with_method("GET"),
|
|
173
|
+
VerificationTimes.at_least(1)
|
|
174
|
+
)
|
|
175
|
+
|
|
176
|
+
# Verify exact count
|
|
177
|
+
client.verify(
|
|
178
|
+
HttpRequest.request("/api/users"),
|
|
179
|
+
VerificationTimes.exactly(3)
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
# Verify request sequence (order matters)
|
|
183
|
+
client.verify_sequence(
|
|
184
|
+
HttpRequest.request("/first"),
|
|
185
|
+
HttpRequest.request("/second"),
|
|
186
|
+
HttpRequest.request("/third"),
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
# Verify no interactions
|
|
190
|
+
client.verify_zero_interactions()
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
## Retrieval
|
|
194
|
+
|
|
195
|
+
```python
|
|
196
|
+
# Get recorded requests
|
|
197
|
+
requests = client.retrieve_recorded_requests(
|
|
198
|
+
HttpRequest.request("/api/.*")
|
|
199
|
+
)
|
|
200
|
+
|
|
201
|
+
# Get active expectations
|
|
202
|
+
expectations = client.retrieve_active_expectations()
|
|
203
|
+
|
|
204
|
+
# Get log messages
|
|
205
|
+
logs = client.retrieve_log_messages()
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
## Control
|
|
209
|
+
|
|
210
|
+
```python
|
|
211
|
+
# Clear specific expectations
|
|
212
|
+
client.clear(HttpRequest.request("/api/users"))
|
|
213
|
+
|
|
214
|
+
# Clear by type
|
|
215
|
+
client.clear(HttpRequest.request("/api/users"), clear_type="LOG")
|
|
216
|
+
|
|
217
|
+
# Reset everything
|
|
218
|
+
client.reset()
|
|
219
|
+
|
|
220
|
+
# Bind additional ports
|
|
221
|
+
client.bind(1081, 1082)
|
|
222
|
+
|
|
223
|
+
# Check if running
|
|
224
|
+
if client.has_started():
|
|
225
|
+
print("MockServer is running")
|
|
226
|
+
|
|
227
|
+
# Stop
|
|
228
|
+
client.stop()
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
## TLS Support
|
|
232
|
+
|
|
233
|
+
```python
|
|
234
|
+
# Uses system trust store (default — verifies certificates)
|
|
235
|
+
client = MockServerClient("localhost", 1080, secure=True)
|
|
236
|
+
|
|
237
|
+
# Custom CA certificate
|
|
238
|
+
client = MockServerClient(
|
|
239
|
+
"localhost", 1080,
|
|
240
|
+
secure=True,
|
|
241
|
+
ca_cert_path="/path/to/ca.pem"
|
|
242
|
+
)
|
|
243
|
+
|
|
244
|
+
# Disable certificate verification (testing only — NOT recommended for production)
|
|
245
|
+
client = MockServerClient(
|
|
246
|
+
"localhost", 1080,
|
|
247
|
+
secure=True,
|
|
248
|
+
tls_verify=False
|
|
249
|
+
)
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
## Domain Model
|
|
253
|
+
|
|
254
|
+
All domain model classes support builder-style chaining:
|
|
255
|
+
|
|
256
|
+
```python
|
|
257
|
+
request = (
|
|
258
|
+
HttpRequest.request("/api/users")
|
|
259
|
+
.with_method("POST")
|
|
260
|
+
.with_header("Content-Type", "application/json")
|
|
261
|
+
.with_header("Authorization", "Bearer token")
|
|
262
|
+
.with_body('{"name": "test"}')
|
|
263
|
+
.with_query_param("page", "1")
|
|
264
|
+
.with_secure(True)
|
|
265
|
+
)
|
|
266
|
+
|
|
267
|
+
response = (
|
|
268
|
+
HttpResponse.response()
|
|
269
|
+
.with_status_code(201)
|
|
270
|
+
.with_header("Location", "/api/users/1")
|
|
271
|
+
.with_body('{"id": 1, "name": "test"}')
|
|
272
|
+
.with_delay(Delay(time_unit="SECONDS", value=1))
|
|
273
|
+
)
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
## Requirements
|
|
277
|
+
|
|
278
|
+
- Python 3.9+
|
|
279
|
+
- `websockets` >= 12.0 (for callback support)
|
|
280
|
+
|
|
281
|
+
## License
|
|
282
|
+
|
|
283
|
+
Apache 2.0
|
|
284
|
+
|
|
285
|
+
## AI Assistant Integration
|
|
286
|
+
|
|
287
|
+
MockServer includes a built-in [MCP](https://modelcontextprotocol.io) (Model Context Protocol) server that enables AI coding assistants to create expectations, verify requests, and debug HTTP traffic programmatically.
|
|
288
|
+
|
|
289
|
+
- **MCP Endpoint:** `http://localhost:1080/mockserver/mcp`
|
|
290
|
+
- **AI Documentation:** [llms.txt](https://www.mock-server.com/llms.txt)
|
|
291
|
+
- **Setup Guide:** [AI Integration](https://www.mock-server.com/mock_server/ai_mcp_setup.html)
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
# MockServer Python Client
|
|
2
|
+
|
|
3
|
+
Python client for [MockServer](https://www.mock-server.com) with full WebSocket callback support.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Full REST API**: Create expectations, verify requests, clear/reset, retrieve recorded data
|
|
8
|
+
- **Response Callbacks**: Register Python functions that dynamically generate responses via WebSocket
|
|
9
|
+
- **Forward Callbacks**: Modify requests before they are forwarded to the real server
|
|
10
|
+
- **Forward+Response Callbacks**: Modify both the forwarded request and the response
|
|
11
|
+
- **Fluent API**: `client.when(request).respond(callback)` — mirrors the Java client
|
|
12
|
+
- **Async + Sync**: Native asyncio API with a synchronous wrapper for non-async code
|
|
13
|
+
- **Minimal Dependencies**: Only `websockets` (for callback support)
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pip install mockserver-client
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Quick Start
|
|
22
|
+
|
|
23
|
+
### Synchronous API
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
from mockserver import MockServerClient, HttpRequest, HttpResponse
|
|
27
|
+
|
|
28
|
+
client = MockServerClient("localhost", 1080)
|
|
29
|
+
|
|
30
|
+
# Static expectation
|
|
31
|
+
client.when(
|
|
32
|
+
HttpRequest.request("/api/users").with_method("GET")
|
|
33
|
+
).respond(
|
|
34
|
+
HttpResponse.response('{"users": []}', status_code=200)
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
# Verify
|
|
38
|
+
client.verify(
|
|
39
|
+
HttpRequest.request("/api/users").with_method("GET"),
|
|
40
|
+
VerificationTimes.at_least(1)
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
# Clean up
|
|
44
|
+
client.reset()
|
|
45
|
+
client.close()
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Context Manager
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
with MockServerClient("localhost", 1080) as client:
|
|
52
|
+
client.when(
|
|
53
|
+
HttpRequest.request("/hello")
|
|
54
|
+
).respond(
|
|
55
|
+
HttpResponse.response("world")
|
|
56
|
+
)
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Async API
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
import asyncio
|
|
63
|
+
from mockserver import AsyncMockServerClient, HttpRequest, HttpResponse
|
|
64
|
+
|
|
65
|
+
async def main():
|
|
66
|
+
async with AsyncMockServerClient("localhost", 1080) as client:
|
|
67
|
+
await client.when(
|
|
68
|
+
HttpRequest.request("/api/data")
|
|
69
|
+
).respond(
|
|
70
|
+
HttpResponse.response('{"key": "value"}')
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
asyncio.run(main())
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Response Callbacks
|
|
77
|
+
|
|
78
|
+
Register a Python function that generates responses dynamically when matching requests arrive:
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
from mockserver import MockServerClient, HttpRequest, HttpResponse
|
|
82
|
+
|
|
83
|
+
def handle_request(request):
|
|
84
|
+
if request.method == "POST":
|
|
85
|
+
return HttpResponse.response("created", status_code=201)
|
|
86
|
+
return HttpResponse.not_found_response()
|
|
87
|
+
|
|
88
|
+
client = MockServerClient("localhost", 1080)
|
|
89
|
+
client.mock_with_callback(
|
|
90
|
+
HttpRequest.request("/api/callback"),
|
|
91
|
+
handle_request
|
|
92
|
+
)
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Or with the fluent API:
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
client.when(
|
|
99
|
+
HttpRequest.request("/api/callback")
|
|
100
|
+
).respond(handle_request)
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Forward Callbacks
|
|
104
|
+
|
|
105
|
+
Modify requests before they are forwarded to the real server:
|
|
106
|
+
|
|
107
|
+
```python
|
|
108
|
+
def modify_request(request):
|
|
109
|
+
return request.with_header("X-Forwarded", "true").with_path("/modified" + request.path)
|
|
110
|
+
|
|
111
|
+
client.mock_with_forward_callback(
|
|
112
|
+
HttpRequest.request("/proxy/.*"),
|
|
113
|
+
modify_request
|
|
114
|
+
)
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
## Forward+Response Callbacks
|
|
118
|
+
|
|
119
|
+
Modify both the forwarded request and the response:
|
|
120
|
+
|
|
121
|
+
```python
|
|
122
|
+
def modify_request(request):
|
|
123
|
+
return request.with_header("X-Proxied", "true")
|
|
124
|
+
|
|
125
|
+
def modify_response(request, response):
|
|
126
|
+
return response.with_header("X-Modified", "true")
|
|
127
|
+
|
|
128
|
+
client.mock_with_forward_callback(
|
|
129
|
+
HttpRequest.request("/proxy/.*"),
|
|
130
|
+
modify_request,
|
|
131
|
+
modify_response
|
|
132
|
+
)
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
## Verification
|
|
136
|
+
|
|
137
|
+
```python
|
|
138
|
+
from mockserver import VerificationTimes
|
|
139
|
+
|
|
140
|
+
# Verify a request was received at least once
|
|
141
|
+
client.verify(
|
|
142
|
+
HttpRequest.request("/api/users").with_method("GET"),
|
|
143
|
+
VerificationTimes.at_least(1)
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
# Verify exact count
|
|
147
|
+
client.verify(
|
|
148
|
+
HttpRequest.request("/api/users"),
|
|
149
|
+
VerificationTimes.exactly(3)
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
# Verify request sequence (order matters)
|
|
153
|
+
client.verify_sequence(
|
|
154
|
+
HttpRequest.request("/first"),
|
|
155
|
+
HttpRequest.request("/second"),
|
|
156
|
+
HttpRequest.request("/third"),
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
# Verify no interactions
|
|
160
|
+
client.verify_zero_interactions()
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## Retrieval
|
|
164
|
+
|
|
165
|
+
```python
|
|
166
|
+
# Get recorded requests
|
|
167
|
+
requests = client.retrieve_recorded_requests(
|
|
168
|
+
HttpRequest.request("/api/.*")
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
# Get active expectations
|
|
172
|
+
expectations = client.retrieve_active_expectations()
|
|
173
|
+
|
|
174
|
+
# Get log messages
|
|
175
|
+
logs = client.retrieve_log_messages()
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
## Control
|
|
179
|
+
|
|
180
|
+
```python
|
|
181
|
+
# Clear specific expectations
|
|
182
|
+
client.clear(HttpRequest.request("/api/users"))
|
|
183
|
+
|
|
184
|
+
# Clear by type
|
|
185
|
+
client.clear(HttpRequest.request("/api/users"), clear_type="LOG")
|
|
186
|
+
|
|
187
|
+
# Reset everything
|
|
188
|
+
client.reset()
|
|
189
|
+
|
|
190
|
+
# Bind additional ports
|
|
191
|
+
client.bind(1081, 1082)
|
|
192
|
+
|
|
193
|
+
# Check if running
|
|
194
|
+
if client.has_started():
|
|
195
|
+
print("MockServer is running")
|
|
196
|
+
|
|
197
|
+
# Stop
|
|
198
|
+
client.stop()
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
## TLS Support
|
|
202
|
+
|
|
203
|
+
```python
|
|
204
|
+
# Uses system trust store (default — verifies certificates)
|
|
205
|
+
client = MockServerClient("localhost", 1080, secure=True)
|
|
206
|
+
|
|
207
|
+
# Custom CA certificate
|
|
208
|
+
client = MockServerClient(
|
|
209
|
+
"localhost", 1080,
|
|
210
|
+
secure=True,
|
|
211
|
+
ca_cert_path="/path/to/ca.pem"
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
# Disable certificate verification (testing only — NOT recommended for production)
|
|
215
|
+
client = MockServerClient(
|
|
216
|
+
"localhost", 1080,
|
|
217
|
+
secure=True,
|
|
218
|
+
tls_verify=False
|
|
219
|
+
)
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
## Domain Model
|
|
223
|
+
|
|
224
|
+
All domain model classes support builder-style chaining:
|
|
225
|
+
|
|
226
|
+
```python
|
|
227
|
+
request = (
|
|
228
|
+
HttpRequest.request("/api/users")
|
|
229
|
+
.with_method("POST")
|
|
230
|
+
.with_header("Content-Type", "application/json")
|
|
231
|
+
.with_header("Authorization", "Bearer token")
|
|
232
|
+
.with_body('{"name": "test"}')
|
|
233
|
+
.with_query_param("page", "1")
|
|
234
|
+
.with_secure(True)
|
|
235
|
+
)
|
|
236
|
+
|
|
237
|
+
response = (
|
|
238
|
+
HttpResponse.response()
|
|
239
|
+
.with_status_code(201)
|
|
240
|
+
.with_header("Location", "/api/users/1")
|
|
241
|
+
.with_body('{"id": 1, "name": "test"}')
|
|
242
|
+
.with_delay(Delay(time_unit="SECONDS", value=1))
|
|
243
|
+
)
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
## Requirements
|
|
247
|
+
|
|
248
|
+
- Python 3.9+
|
|
249
|
+
- `websockets` >= 12.0 (for callback support)
|
|
250
|
+
|
|
251
|
+
## License
|
|
252
|
+
|
|
253
|
+
Apache 2.0
|
|
254
|
+
|
|
255
|
+
## AI Assistant Integration
|
|
256
|
+
|
|
257
|
+
MockServer includes a built-in [MCP](https://modelcontextprotocol.io) (Model Context Protocol) server that enables AI coding assistants to create expectations, verify requests, and debug HTTP traffic programmatically.
|
|
258
|
+
|
|
259
|
+
- **MCP Endpoint:** `http://localhost:1080/mockserver/mcp`
|
|
260
|
+
- **AI Documentation:** [llms.txt](https://www.mock-server.com/llms.txt)
|
|
261
|
+
- **Setup Guide:** [AI Integration](https://www.mock-server.com/mock_server/ai_mcp_setup.html)
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
from mockserver.models import (
|
|
2
|
+
AfterAction,
|
|
3
|
+
Body,
|
|
4
|
+
ConnectionOptions,
|
|
5
|
+
Delay,
|
|
6
|
+
DelayDistribution,
|
|
7
|
+
Expectation,
|
|
8
|
+
ExpectationId,
|
|
9
|
+
HttpClassCallback,
|
|
10
|
+
HttpError,
|
|
11
|
+
HttpForward,
|
|
12
|
+
HttpObjectCallback,
|
|
13
|
+
HttpOverrideForwardedRequest,
|
|
14
|
+
HttpRequest,
|
|
15
|
+
HttpRequestAndHttpResponse,
|
|
16
|
+
HttpResponse,
|
|
17
|
+
HttpSseResponse,
|
|
18
|
+
HttpTemplate,
|
|
19
|
+
HttpWebSocketResponse,
|
|
20
|
+
JsonRpcBody,
|
|
21
|
+
KeyToMultiValue,
|
|
22
|
+
OpenAPIDefinition,
|
|
23
|
+
OpenAPIExpectation,
|
|
24
|
+
Ports,
|
|
25
|
+
RequestDefinition,
|
|
26
|
+
SocketAddress,
|
|
27
|
+
SseEvent,
|
|
28
|
+
TimeToLive,
|
|
29
|
+
Times,
|
|
30
|
+
Verification,
|
|
31
|
+
VerificationSequence,
|
|
32
|
+
VerificationTimes,
|
|
33
|
+
WebSocketMessage,
|
|
34
|
+
)
|
|
35
|
+
from mockserver.exceptions import (
|
|
36
|
+
MockServerCallbackError,
|
|
37
|
+
MockServerConnectionError,
|
|
38
|
+
MockServerError,
|
|
39
|
+
MockServerVerificationError,
|
|
40
|
+
MockServerWebSocketError,
|
|
41
|
+
)
|
|
42
|
+
from mockserver.client import MockServerClient
|
|
43
|
+
from mockserver.async_client import AsyncMockServerClient
|
|
44
|
+
|
|
45
|
+
__all__ = [
|
|
46
|
+
"AfterAction",
|
|
47
|
+
"AsyncMockServerClient",
|
|
48
|
+
"Body",
|
|
49
|
+
"ConnectionOptions",
|
|
50
|
+
"Delay",
|
|
51
|
+
"DelayDistribution",
|
|
52
|
+
"Expectation",
|
|
53
|
+
"ExpectationId",
|
|
54
|
+
"HttpClassCallback",
|
|
55
|
+
"HttpError",
|
|
56
|
+
"HttpForward",
|
|
57
|
+
"HttpObjectCallback",
|
|
58
|
+
"HttpOverrideForwardedRequest",
|
|
59
|
+
"HttpRequest",
|
|
60
|
+
"HttpRequestAndHttpResponse",
|
|
61
|
+
"HttpResponse",
|
|
62
|
+
"HttpSseResponse",
|
|
63
|
+
"HttpTemplate",
|
|
64
|
+
"HttpWebSocketResponse",
|
|
65
|
+
"JsonRpcBody",
|
|
66
|
+
"KeyToMultiValue",
|
|
67
|
+
"MockServerCallbackError",
|
|
68
|
+
"MockServerClient",
|
|
69
|
+
"MockServerConnectionError",
|
|
70
|
+
"MockServerError",
|
|
71
|
+
"MockServerVerificationError",
|
|
72
|
+
"MockServerWebSocketError",
|
|
73
|
+
"OpenAPIDefinition",
|
|
74
|
+
"OpenAPIExpectation",
|
|
75
|
+
"Ports",
|
|
76
|
+
"RequestDefinition",
|
|
77
|
+
"SocketAddress",
|
|
78
|
+
"SseEvent",
|
|
79
|
+
"TimeToLive",
|
|
80
|
+
"Times",
|
|
81
|
+
"Verification",
|
|
82
|
+
"VerificationSequence",
|
|
83
|
+
"VerificationTimes",
|
|
84
|
+
"WebSocketMessage",
|
|
85
|
+
]
|