Skip to content

Commit

Permalink
Refactor mock server to support distributed configuration loading
Browse files Browse the repository at this point in the history
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
  • Loading branch information
swan-amazon committed Feb 20, 2025
1 parent 5398152 commit de5555a
Show file tree
Hide file tree
Showing 22 changed files with 1,219 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 @@ -333,6 +333,7 @@ CurrentHue
CurrentLevel
CurrentSaturation
customAcl
customizable
customizations
cvfJ
cxx
Expand Down Expand Up @@ -1060,6 +1061,7 @@ otatesting
otaURL
OTBR
otcli
OU
outform
outgoingCommands
overridable
Expand Down Expand Up @@ -1171,6 +1173,7 @@ PyObject
pypi
PyRun
pytest
PYTHONPATH
QEMU
Qorvo
QPG
Expand Down Expand Up @@ -1349,6 +1352,7 @@ SRP
SRV
SSBL
SSID
SSL
startoffset
StartScan
startsWith
Expand Down Expand Up @@ -1516,6 +1520,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.

55 changes: 55 additions & 0 deletions integrations/mock_server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# 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**

- Supports HTTP & HTTPS (with self-signed certificates)
- CLI-driven execution with customizable options (port, configuration
file, SSL options, etc.)
- Handles GET, POST, PUT, and DELETE requests

- **Route Matching**

- Exact path and wildcard (\*) path matching
- Query parameter validation
- Request body matching with exact or regex-based rules

- **Static Response Handling**

- Static responses defined in `config.json`

- **Error Handling & Logging**
- Graceful error responses for missing or invalid routes
- Structured logging with configurable verbosity

## Running Tests

### Prerequisites

- Python virtual environment (located in `out/venv`)
- Python unittest framework

### 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 de5555a

Please sign in to comment.