Lightweight HTTP mock server — directory structure as URL paths
Project description
mockpath
轻量级 HTTP Mock 服务器。通过 YAML 配置 + JSON 文件定义模拟接口,目录结构即 URL 路径。
特性
- 目录结构自动映射为 URL 路径
- YAML 定义接口配置,JSON 定义请求/响应体
- 支持 query 参数匹配(子集匹配,请求中的额外参数不影响匹配)
- 支持请求体匹配(深度相等比较)
- 支持内联响应、文件引用响应、约定命名响应三种方式
--reload模式自动监听文件变更并热重载- 未知路径返回 404,方法不匹配返回 405
安装
pip install mockpath
使用
mockpath [-p PORT] [-d DIR] [--reload]
| 参数 | 说明 | 默认值 |
|---|---|---|
-p, --port |
监听端口 | 8000 |
-d, --dir |
配置目录 | ./api |
--reload |
监听文件变更,自动重载配置 | 关闭 |
文件命名规则
api/ # 配置根目录(通过 -d 指定)
v1/
users/
list.get.yaml # GET /v1/users/list 的配置
list.get.resp.json # 默认响应
list.get.resp.1.json # 匹配规则 #1 的响应
profile.post.yaml # POST /v1/users/profile 的配置
profile.post.resp.json # 默认响应
profile.post.req.1.json # 匹配规则 #1 的请求体
profile.post.resp.1.json # 匹配规则 #1 的响应
命名模式:<端点名>.<HTTP方法>.yaml
YAML 配置格式
基础接口(无匹配规则)
# profile.get.yaml
status: 200
响应体来自 profile.get.resp.json。
Query 参数匹配
# list.get.yaml
status: 200
matches:
- params:
page: "1"
limit: "10"
- params:
page: "2"
response: # 内联响应
users: []
total: 0
匹配逻辑:请求的 query 参数是配置参数的超集即可匹配(子集匹配)。
请求体匹配(POST/PUT/PATCH)
# profile.post.yaml
status: 201
matches:
- request: # 内联请求体
name: "Bob"
response_file: resp.1.json
- request_file: req.2.json # 引用外部请求体文件
response:
id: 4
name: "Alice"
- status: 200 # 约定命名:请求体来自 profile.post.req.3.json
请求体的三种指定方式
每条匹配规则的请求体按以下优先级确定:
request— YAML 中的内联 JSON 请求体request_file— 引用外部文件(相对于 YAML 文件的路径)- 约定命名 — 自动查找
<端点名>.<方法>.req.N.json
响应的三种指定方式
每条匹配规则的响应按以下优先级确定:
response— YAML 中的内联 JSON 响应response_file— 引用外部文件(相对于 YAML 文件的路径)- 约定命名 — 自动查找
<端点名>.<方法>.resp.N.json
匹配流程
- 按
matches列表顺序逐一尝试 - 第一个匹配成功的规则生效(first match wins)
- 无匹配 → 返回默认响应
示例
# 启动服务器
mockpath -p 3000
# Query 参数匹配
curl "http://localhost:3000/v1/users/list?page=1"
# → [{"id": 1, "name": "Alice"}]
curl "http://localhost:3000/v1/users/list?page=2"
# → {"users": [], "total": 0}
# 请求体匹配
curl -X POST http://localhost:3000/v1/users/profile \
-H "Content-Type: application/json" \
-d '{"name": "Bob"}'
# → {"id": 3, "name": "Bob"}
# 无匹配时返回默认响应
curl "http://localhost:3000/v1/users/list"
# → [{"id": 1, "name": "Alice"}]
依赖
- Python >= 3.10
- PyYAML
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
mockpath-0.1.0.tar.gz
(12.0 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mockpath-0.1.0.tar.gz.
File metadata
- Download URL: mockpath-0.1.0.tar.gz
- Upload date:
- Size: 12.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b27d223c9c4c3e4044166b029b961dd2e222fcbd7c2578861707591d797c16de
|
|
| MD5 |
c1dae492ad0c7dd03915bb81a3e09b4b
|
|
| BLAKE2b-256 |
dbd44e02f4cfa4471273f4b96018de2cbf0c57cb55a3485a0a942ef147274e0b
|
File details
Details for the file mockpath-0.1.0-py3-none-any.whl.
File metadata
- Download URL: mockpath-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
80164bc258dcedf785f48ba8b61cd9f1e30a3f1f2587b0d3e38e316fabe2e84b
|
|
| MD5 |
ad342ac94bd075858bb9e0982b85587f
|
|
| BLAKE2b-256 |
d04a5265fc87694096682ad87f2a324ed204230d49f0b212f88baa3ba7ee09db
|