Skip to content

Commit ce886c3

Browse files
committed
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
1 parent 5398152 commit ce886c3

22 files changed

+1179
-253
lines changed

.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

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
- Supports HTTP & HTTPS (with self-signed certificates)
22+
- CLI-driven execution with customizable options (port, configuration
23+
file, SSL options, etc.)
24+
- Handles GET, POST, PUT, and DELETE requests
25+
26+
- **Route Matching**
27+
28+
- Exact path and wildcard (\*) path matching
29+
- Query parameter validation
30+
- Request body matching with exact or regex-based rules
31+
32+
- **Static Response Handling**
33+
34+
- Static responses defined in `config.json`
35+
36+
- **Error Handling & Logging**
37+
- Graceful error responses for missing or invalid routes
38+
- Structured logging with configurable verbosity
39+
40+
## Running Tests
41+
42+
### Prerequisites
43+
44+
- Python virtual environment (located in `out/venv`)
45+
- Python unittest framework
46+
47+
### Test Execution
48+
49+
You can run the tests using one of these methods:
50+
51+
1. Using Python unittest with PYTHONPATH:
52+
53+
```bash
54+
PYTHONPATH=$PYTHONPATH:/workspace/connectedhomeip/integrations/mock_server/src python3 -m unittest integrations/mock_server/tests/test_mock_server.py
55+
```
56+
57+
f"/C=US/ST=/L=/O=/OU=/CN=localhost"

integrations/mock_server/__init__.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright (c) 2025 Project CHIP Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.

0 commit comments

Comments
 (0)