Skip to content

Commit

Permalink
[Testing] Refactor mock server to support distributed configuration l…
Browse files Browse the repository at this point in the history
…oading (#37695) (#37713)

* 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.
  • Loading branch information
swan-amazon authored Feb 21, 2025
1 parent f654bba commit 2b4717e
Show file tree
Hide file tree
Showing 22 changed files with 1,379 additions and 253 deletions.
5 changes: 5 additions & 0 deletions .github/.wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ CurrentHue
CurrentLevel
CurrentSaturation
customAcl
customizable
customizations
cvfJ
cxx
Expand Down Expand Up @@ -1039,6 +1040,7 @@ otatesting
otaURL
OTBR
otcli
OU
outform
outgoingCommands
overridable
Expand Down Expand Up @@ -1150,6 +1152,7 @@ PyObject
pypi
PyRun
pytest
PYTHONPATH
QEMU
Qorvo
QPG
Expand Down Expand Up @@ -1326,6 +1329,7 @@ SRP
SRV
SSBL
SSID
SSL
startoffset
StartScan
startsWith
Expand Down Expand Up @@ -1490,6 +1494,7 @@ unfocus
Unicast
UniFlash
UnitLocalization
unittest
unpair
unprovisioned
Unsecure
Expand Down
29 changes: 27 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,37 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Mock Server Tests",
"type": "debugpy",
"request": "launch",
"module": "unittest",
"args": [
"${workspaceFolder}/integrations/mock_server/tests/test_mock_server.py"
],
"env": {
"PYTHONPATH": "${workspaceFolder}/integrations/mock_server/src:${PYTHONPATH}"
},
"console": "integratedTerminal",
"cwd": "${workspaceFolder}"
},
{
"name": "Python Debugger: test_dcl_server",
"type": "debugpy",
"request": "launch",
"program": "/workspace/connectedhomeip/examples/chip-tool/commands/dcl/test_dcl_server.py",
"args": [],
"program": "${workspaceFolder}/integrations/mock_server/src/main.py",
"args": [
"--port",
"8443",
"--config",
"${workspaceFolder}/integrations/mock_server/configurations/server_config.json",
"--routing-config-dir",
"${workspaceFolder}/integrations/mock_server/configurations/fake_distributed_compliance_ledger",
"--cert",
"${workspaceFolder}/server.crt",
"--key",
"${workspaceFolder}/server.key"
],
"console": "integratedTerminal"
},
{
Expand Down
251 changes: 0 additions & 251 deletions examples/chip-tool/commands/dcl/test_dcl_server.py

This file was deleted.

56 changes: 56 additions & 0 deletions integrations/mock_server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Mock HTTP/HTTPS Server

## Overview

This project provides a configurable mock HTTP/HTTPS server designed for API
testing, dynamic response generation, and automated request handling. It
supports static responses, dynamic custom response handlers, query parameter
matching, request body validation (including regex), and both HTTP and HTTPS
protocols.

## Setup

```bash
openssl req -x509 -newkey rsa:2048 -keyout server.key -out server.crt -days 365 -nodes -subj "/C=US/ST= /L= /O= /OU= /CN=localhost"
```

## Features

### Server Functionality

- Secure HTTPS with TLS support
- CLI-driven execution with customizable options (port, configuration files,
SSL options)
- Handles GET, POST, PUT, and DELETE requests
- Concurrent request handling via threading

### Route Matching

- Exact path and wildcard (\*) path matching
- Query parameter validation
- Priority-based route matching

### Configuration

- Main server configuration file
- Separate routing configuration directory
- JSON-based configuration format
- Dynamic route loading

### Security & Logging

- TLS encryption (HTTPS only)
- Structured logging with DEBUG level
- Graceful error handling for invalid routes

## Running Tests

### Test Execution

You can run the tests using one of these methods:

1. Using Python unittest with PYTHONPATH:

```bash
PYTHONPATH=$PYTHONPATH:/workspace/connectedhomeip/integrations/mock_server/src python3 -m unittest integrations/mock_server/tests/test_mock_server.py
```
Loading

0 comments on commit 2b4717e

Please sign in to comment.