Skip to content

Commit 9ad9d2c

Browse files
authored
[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 26a7b1c commit 9ad9d2c

22 files changed

+1379
-253
lines changed

.github/.wordlist.txt

+5
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ CurrentHue
333333
CurrentLevel
334334
CurrentSaturation
335335
customAcl
336+
customizable
336337
customizations
337338
cvfJ
338339
cxx
@@ -1060,6 +1061,7 @@ otatesting
10601061
otaURL
10611062
OTBR
10621063
otcli
1064+
OU
10631065
outform
10641066
outgoingCommands
10651067
overridable
@@ -1171,6 +1173,7 @@ PyObject
11711173
pypi
11721174
PyRun
11731175
pytest
1176+
PYTHONPATH
11741177
QEMU
11751178
Qorvo
11761179
QPG
@@ -1349,6 +1352,7 @@ SRP
13491352
SRV
13501353
SSBL
13511354
SSID
1355+
SSL
13521356
startoffset
13531357
StartScan
13541358
startsWith
@@ -1516,6 +1520,7 @@ unfocus
15161520
Unicast
15171521
UniFlash
15181522
UnitLocalization
1523+
unittest
15191524
unpair
15201525
unprovisioned
15211526
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)