Skip to content

Commit d095e16

Browse files
committed
[Testing] Refactor mock server to support distributed configuration loading (#37695)
* Refactor mock server to support distributed configuration loading Refactors the mock server implementation to support loading configuration from multiple JSON files, enabling flexible mocking of different services during Matter device commissioning validation. Key Changes: - Restructured configuration loading to support directory-based routing configs - Added dataclass-based type safety for configuration and route definitions - Updated path handling to use pathlib.Path for better cross-platform support - Modified server launch configuration to support routing config directory - Added configurations for mocking multiple services: * Distributed Compliance Ledger (DCL) * Product Terms & Conditions server Technical Improvements: - Introduced strongly typed Route and Configuration classes - Simplified route matching logic with dedicated matcher - Improved error handling for configuration loading - Updated unit tests to support new configuration structure The changes enable quick iteration of mock service responses during preproduction testing and PlugFest validation, particularly for testing new commissioning flows that rely on DCL-based configuration with indirect product server references. Test Configuration: - Added example configurations for VID:65521/65522, PID:32769 - Updated TC URL endpoints to use port 44538 - Included sample Terms & Conditions responses * server: Add validation checks for SSL certificate and key files Add input validation to verify that SSL certificate and key files exist and are regular files before attempting to create the SSL context. This provides clearer error messages to users when certificate files are missing or invalid, following the same validation pattern used for config files.
1 parent f654bba commit d095e16

22 files changed

+1379
-253
lines changed

.github/.wordlist.txt

+5
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ CurrentHue
326326
CurrentLevel
327327
CurrentSaturation
328328
customAcl
329+
customizable
329330
customizations
330331
cvfJ
331332
cxx
@@ -1039,6 +1040,7 @@ otatesting
10391040
otaURL
10401041
OTBR
10411042
otcli
1043+
OU
10421044
outform
10431045
outgoingCommands
10441046
overridable
@@ -1150,6 +1152,7 @@ PyObject
11501152
pypi
11511153
PyRun
11521154
pytest
1155+
PYTHONPATH
11531156
QEMU
11541157
Qorvo
11551158
QPG
@@ -1326,6 +1329,7 @@ SRP
13261329
SRV
13271330
SSBL
13281331
SSID
1332+
SSL
13291333
startoffset
13301334
StartScan
13311335
startsWith
@@ -1490,6 +1494,7 @@ unfocus
14901494
Unicast
14911495
UniFlash
14921496
UnitLocalization
1497+
unittest
14931498
unpair
14941499
unprovisioned
14951500
Unsecure

.vscode/launch.json

+27-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,37 @@
44
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
55
"version": "0.2.0",
66
"configurations": [
7+
{
8+
"name": "Python: Mock Server Tests",
9+
"type": "debugpy",
10+
"request": "launch",
11+
"module": "unittest",
12+
"args": [
13+
"${workspaceFolder}/integrations/mock_server/tests/test_mock_server.py"
14+
],
15+
"env": {
16+
"PYTHONPATH": "${workspaceFolder}/integrations/mock_server/src:${PYTHONPATH}"
17+
},
18+
"console": "integratedTerminal",
19+
"cwd": "${workspaceFolder}"
20+
},
721
{
822
"name": "Python Debugger: test_dcl_server",
923
"type": "debugpy",
1024
"request": "launch",
11-
"program": "/workspace/connectedhomeip/examples/chip-tool/commands/dcl/test_dcl_server.py",
12-
"args": [],
25+
"program": "${workspaceFolder}/integrations/mock_server/src/main.py",
26+
"args": [
27+
"--port",
28+
"8443",
29+
"--config",
30+
"${workspaceFolder}/integrations/mock_server/configurations/server_config.json",
31+
"--routing-config-dir",
32+
"${workspaceFolder}/integrations/mock_server/configurations/fake_distributed_compliance_ledger",
33+
"--cert",
34+
"${workspaceFolder}/server.crt",
35+
"--key",
36+
"${workspaceFolder}/server.key"
37+
],
1338
"console": "integratedTerminal"
1439
},
1540
{

examples/chip-tool/commands/dcl/test_dcl_server.py

-251
This file was deleted.

integrations/mock_server/README.md

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Mock HTTP/HTTPS Server
2+
3+
## Overview
4+
5+
This project provides a configurable mock HTTP/HTTPS server designed for API
6+
testing, dynamic response generation, and automated request handling. It
7+
supports static responses, dynamic custom response handlers, query parameter
8+
matching, request body validation (including regex), and both HTTP and HTTPS
9+
protocols.
10+
11+
## Setup
12+
13+
```bash
14+
openssl req -x509 -newkey rsa:2048 -keyout server.key -out server.crt -days 365 -nodes -subj "/C=US/ST= /L= /O= /OU= /CN=localhost"
15+
```
16+
17+
## Features
18+
19+
### Server Functionality
20+
21+
- Secure HTTPS with TLS support
22+
- CLI-driven execution with customizable options (port, configuration files,
23+
SSL options)
24+
- Handles GET, POST, PUT, and DELETE requests
25+
- Concurrent request handling via threading
26+
27+
### Route Matching
28+
29+
- Exact path and wildcard (\*) path matching
30+
- Query parameter validation
31+
- Priority-based route matching
32+
33+
### Configuration
34+
35+
- Main server configuration file
36+
- Separate routing configuration directory
37+
- JSON-based configuration format
38+
- Dynamic route loading
39+
40+
### Security & Logging
41+
42+
- TLS encryption (HTTPS only)
43+
- Structured logging with DEBUG level
44+
- Graceful error handling for invalid routes
45+
46+
## Running Tests
47+
48+
### Test Execution
49+
50+
You can run the tests using one of these methods:
51+
52+
1. Using Python unittest with PYTHONPATH:
53+
54+
```bash
55+
PYTHONPATH=$PYTHONPATH:/workspace/connectedhomeip/integrations/mock_server/src python3 -m unittest integrations/mock_server/tests/test_mock_server.py
56+
```

0 commit comments

Comments
 (0)