多协议模拟API服务器

Mocking Bird 是一个支持7种主要API协议的全功能模拟服务器,用于开发、测试和作为多协议API工具的参考实现。

  • by emadomedher
  • Go
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 emadomedher/mocking-bird

使用说明

A fully-featured mock API server supporting 7 protocols out of the box

Quick Start • Protocols • CRUD Testing • Examples



什么是模仿鸟?

反舌鸟 是一个独立的模拟API服务器,它能说出所有主要的API协议。将其用于开发、测试,或作为多协议API工具的参考实现。

✨ 特性

  • 🎯 支持7种协议:OpenAPI、Swagger、SOAP/WSDL、GraphQL、OData、gRPC、JSON-RPC
  • 🔄 完整的CRUD操作:为所有类似REST的API创建、读取、更新、删除
  • 💾 内存持久性:数据在服务器运行时保持不变(不需要数据库)
  • 🔐 多种身份验证类型:承载令牌、基本身份验证、API密钥
  • 📝 综合规格:每个协议都包括用于自动发现的规范端点
  • 🧪 包含测试套件:对所有端点进行自动化CRUD测试

📦 支持的协议

协议 资源 描述 身份验证 端口/路径
OpenAPI 3.0 宠物 带CRUD操作的宠物店 无 :9999/openapi
Swagger 2.0 恐龙 带CRUD的恐龙目录 持有者: dino-token :9999/swagger
WSDL/SOAP 植物 带CRUD的植物数据库 载体: mock-token :9999/wdsl/soap
图查询语言 汽车 带CRUD的汽车库存 基本: graphql-user:graphql-pass :9999/graphql
OData V4 电影 支持查询的电影数据库 无 :9999/odata
gRPC 服装 服装项目(4类) 无 :50051-50054
JSON-RPC 计算器 数学运算 无 :9999/jsonrpc

🚀 快速开始

安装

git clone git@github.com:emadomedher/mocking-bird.git
cd mocking-bird
go build -o mockingbird .

跑

./mockingbird

服务器启动于 http://localhost:9999 (端口上的gRPC 50051-50054)

测试

./test-crud.sh

对所有API运行自动CRUD测试,并验证内存中的持久性。


📚 API文档

OpenAPI(宠物)

规格: http://localhost:9999/openapi/openapi.json

端点:

GET    /openapi/pets           # List pets
POST   /openapi/pets           # Create pet
GET    /openapi/pets/{id}      # Get pet by ID
PUT    /openapi/pets/{id}      # Update pet
DELETE /openapi/pets/{id}      # Delete pet

示例:

curl -X POST http://localhost:9999/openapi/pets \
  -H "Content-Type: application/json" \
  -d '{"name":"Fluffy"}'

Swagger 2.0(恐龙)

规格: http://localhost:9999/swagger/swagger.json

端点:

GET    /swagger/dinosaurs           # List dinosaurs
POST   /swagger/dinosaurs           # Create dinosaur
GET    /swagger/dinosaurs/{id}      # Get dinosaur by ID
PUT    /swagger/dinosaurs/{id}      # Update dinosaur
DELETE /swagger/dinosaurs/{id}      # Delete dinosaur

认证: Authorization: Bearer dino-token

示例:

curl http://localhost:9999/swagger/dinosaurs \
  -H "Authorization: Bearer dino-token"

WSDL/SOAP(植物)

WSDL规范: http://localhost:9999/wdsl/wsdl

运营: ListPlants, GetPlant, CreatePlant, UpdatePlant, DeletePlant

认证: Authorization: Bearer mock-token

示例:

curl -X POST http://localhost:9999/wdsl/soap \
  -H "Authorization: Bearer mock-token" \
  -H "Content-Type: text/xml" \
  -d '<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <ListPlants xmlns="http://example.com/plants"/>
  </soap:Body>
</soap:Envelope>'

GraphQL(汽车)

模式(SDL): http://localhost:9999/graphql/schema

端点: POST http://localhost:9999/graphql

认证:基本身份验证(graphql-user:graphql-pass)

查询:

query {
  listCars(limit: 10) {
    id
    name
  }
}

query {
  getCar(id: "1") {
    id
    name
  }
}

突变:

mutation {
  createCar(name: "Tesla") {
    id
    name
  }
}

mutation {
  updateCar(id: "1", name: "Tesla Model S") {
    id
    name
  }
}

mutation {
  deleteCar(id: "1")
}

示例:

curl -X POST http://localhost:9999/graphql \
  -u graphql-user:graphql-pass \
  -H "Content-Type: application/json" \
  -d '{"query":"query{listCars(limit:5){id name}}"}'

OData v4(电影)

元数据: http://localhost:9999/odata/$metadata

端点:

GET    /odata/Movies              # List movies with query options
POST   /odata/Movies              # Create movie
GET    /odata/Movies(1)           # Get movie by ID
PUT    /odata/Movies(1)           # Update movie
DELETE /odata/Movies(1)           # Delete movie

查询选项: $filter, $orderby, $top, $skip, $select, $expand

示例:

# List movies with filtering
curl "http://localhost:9999/odata/Movies?\$filter=Year gt 2000&\$orderby=Rating desc"

# Create a movie
curl -X POST http://localhost:9999/odata/Movies \
  -H "Content-Type: application/json" \
  -d '{"title":"Inception","year":2010,"genre":"Sci-Fi","rating":8.8}'

gRPC(衣服)

原型文件: clothes.proto

服务: ClothesService

类别和港口:

  • 帽子 - localhost:50051
  • 鞋子 - localhost:50052
  • 裤子 - localhost:50053
  • 衬衫 - localhost:50054

方法: ListClothes(ListClothesRequest) returns (ListClothesResponse)

示例 (使用 grpcurl):

grpcurl -plaintext localhost:50051 list
grpcurl -plaintext localhost:50051 ClothesService/ListClothes

JSON-RPC(计算器)

OpenRPC规范: http://localhost:9999/jsonrpc/openrpc.json

方法: add, subtract, multiply, divide, rpc.discover

示例:

curl -X POST http://localhost:9999/jsonrpc \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","id":1,"method":"add","params":{"a":5,"b":3}}'

# Response: {"jsonrpc":"2.0","id":1,"result":8}

🧪 CRUD测试

运行综合测试套件以验证所有CRUD操作:

./test-crud.sh

它测试什么:

  • ✅ 为所有API创建操作
  • ✅ 读取操作(按ID列出和获取)
  • ✅ 使用数据验证更新操作
  • ✅ 通过适当的清理删除操作
  • ✅ 跨请求的内存持久性

示例输出:

🧪 Testing CRUD operations for all Mock APIs
==============================================

Testing Pets (OpenAPI)
✓ Created pet with ID: 4
✓ Read pet successfully
✓ Updated pet successfully
✓ Deleted pet successfully

Testing Cars (GraphQL)
✓ Created car with ID: 3
✓ Read car successfully
✓ Updated car successfully
✓ Deleted car successfully

✅ All CRUD tests completed!

🎯 用例

本地开发

在开发过程中用Mocking Bird替换外部API依赖项。无网络呼叫,即时响应。

集成测试

在不设置实际服务的情况下,根据所有支持的协议测试API客户端。

CI/CD管道

在Docker中运行Mocking Bird,以便在构建管道中进行自动化测试。

协议学习

用作参考实现,以了解不同的API协议是如何工作的。

多协议工具

在构建需要支持多种API格式的工具(如 天际线MCP).


🐳 码头工人

FROM golang:1.21-alpine AS builder
WORKDIR /app
COPY . .
RUN go build -o mockingbird .

FROM alpine:latest
COPY --from=builder /app/mockingbird /usr/local/bin/
COPY --from=builder /app/clothes.proto /app/
EXPOSE 9999 50051 50052 50053 50054
CMD ["mockingbird"]

构建并运行:

docker build -t mockingbird .
docker run -p 9999:9999 -p 50051-50054:50051-50054 mockingbird

🛠️ 配置

环境变量:

# Dinosaur auth token
export DINOSAURS_SWAGGER2_TOKEN="custom-dino-token"

# GraphQL credentials
export GRAPHQL_USERNAME="admin"
export GRAPHQL_PASSWORD="secret"

# gRPC base port (default: 50051)
export GRPC_BASE_PORT=60051

📖 种子数据

每个API都预先提供了样本数据:

API 种子数据
宠物 15只宠物(Max、Whiskers、Rocky、Bella、Luna等)
恐龙 15种恐龙(霸王龙、三角龙、迅猛龙等)
植物 15种植物(孟翅目、蛇类植物、Fiddle Leaf Fig等)
汽车 15辆车(特斯拉Model 3、丰田凯美瑞、福特F-150等)
电影 20部电影(《黑客帝国》、《盗梦空间》、《教父》等)
服装 每类3件,4类(共12件)

数据在服务器进程的生命周期内一直存在于内存中。


🤝 贡献

欢迎投稿!思想:

  • 添加更多协议(例如Thrift、Avro)
  • 添加持久性选项(Redis、PostgreSQL)
  • 添加WebSocket支持
  • 添加速率限制示例
  • 提高测试覆盖率

📄 许可证

MIT许可证-请参阅 许可证 详情


🔗 相关项目

  • 天际线MCP -将任何API转化为用于人工智能代理的MCP工具
  • 使用Mocking Bird作为多协议支持的测试夹具

Built with Go. Mock everything.