MockServer项目CURL命令使用指南:从基础到高级实践
MockServer是一个强大的HTTP模拟工具,可以帮助开发者轻松创建测试环境。本文将全面介绍如何使用CURL命令与MockServer进行交互,涵盖从基础配置到高级功能的完整流程。
一、基础配置与期望设置
1.1 基本期望配置
最基本的MockServer配置是设置请求与响应的映射关系。以下示例展示了如何配置一个GET请求的期望响应:
curl -X PUT 'localhost:1080/mockserver/expectation' \
-d '{
"httpRequest": {
"method": "GET",
"path": "/view/cart",
"queryStringParameters": {
"cartId": ["055CA455-1DF7-45BB-8535-4F83E7266092"]
},
"cookies": {
"session": "4930456C-C718-476F-971F-CB8E047AB349"
}
},
"httpResponse": {
"body": "some_response_body"
}
}'
这个配置会匹配带有特定查询参数和Cookie的GET请求,并返回预设的响应体。
1.2 批量添加期望
MockServer支持一次性添加多个期望配置,这在初始化测试环境时非常有用:
curl -X PUT 'localhost:1080/mockserver/expectation' \
-d '[
{
"httpRequest": {"path": "/somePathOne"},
"httpResponse": {"statusCode": 200, "body": {"value": "one"}}
},
{
"httpRequest": {"path": "/somePathTwo"},
"httpResponse": {"statusCode": 200, "body": {"value": "two"}}
},
{
"httpRequest": {"path": "/somePathThree"},
"httpResponse": {"statusCode": 200, "body": {"value": "three"}}
}
]'
二、高级功能配置
2.1 回调功能
MockServer支持通过回调类动态生成响应,这在需要复杂逻辑时特别有用:
curl -X PUT 'localhost:1080/mockserver/expectation' \
-d '{
"httpRequest": {"path": "/some.*"},
"httpClassCallback": {
"callbackClass": "org.mockserver.examples.mockserver.CallbackActionExamples$TestExpectationCallback"
}
}'
2.2 错误模拟
测试异常场景时,可以配置MockServer返回错误:
# 随机字节错误
curl -X PUT 'localhost:1080/mockserver/expectation' \
-d '{
"httpRequest": {"path": "/some/path"},
"httpError": {
"dropConnection": true,
"responseBytes": "eQqmdjEEoaXnCvcK6lOAIZeU+Pn+womxmg=="
}
}'
# 直接断开连接
curl -X PUT 'localhost:1080/mockserver/expectation' \
-d '{
"httpRequest": {"path": "/some/path"},
"httpError": {"dropConnection": true}
}'
三、请求验证机制
3.1 基本验证
验证特定请求是否被调用:
curl -X PUT 'localhost:1080/mockserver/verify' \
-d '{
"httpRequest": {"path": "/some/path"},
"times": {"atLeast": 2, "atMost": 2}
}'
3.2 序列验证
验证请求是否按特定顺序被调用:
curl -X PUT 'localhost:1080/mockserver/verifySequence' \
-d '[
{"path": "/some/path/one"},
{"path": "/some/path/two"},
{"path": "/some/path/three"}
]'
四、请求转发配置
4.1 基本转发
将请求转发到其他服务:
# HTTP转发
curl -X PUT 'localhost:1080/mockserver/expectation' \
-d '{
"httpRequest": {"path": "/some/path"},
"httpForward": {
"host": "mock-server.com",
"port": 80,
"scheme": "HTTP"
}
}'
# HTTPS转发
curl -X PUT 'localhost:1080/mockserver/expectation' \
-d '{
"httpRequest": {"path": "/some/path"},
"httpForward": {
"host": "mock-server.com",
"port": 443,
"scheme": "HTTPS"
}
}'
4.2 高级转发配置
转发时可以修改请求和响应:
curl -X PUT "http://localhost:1080/mockserver/expectation" -d '{
"httpRequest": {"path": "/some/path"},
"httpOverrideForwardedRequest": {
"requestOverride": {
"headers": {"Host": ["target.host.com"]},
"body": "some_overridden_body"
},
"responseOverride": {
"body": "some_overridden_response_body"
}
}
}'
五、模板引擎支持
MockServer支持使用JavaScript和Velocity模板动态生成转发配置:
5.1 JavaScript模板
curl -X PUT 'localhost:1080/mockserver/expectation' \
-d '{
"httpRequest": {"path": "/some/path"},
"httpForwardTemplate": {
"template": "return {'path':\"/somePath\",'queryStringParameters':{'userId':request.queryStringParameters&&request.queryStringParameters['userId']},'headers':{'Host':[\"localhost:1081\"]},'body':{'name':'value'}};",
"templateType": "JAVASCRIPT"
}
}'
5.2 Velocity模板
curl -X PUT 'localhost:1080/mockserver/expectation' \
-d '{
"httpRequest": {"path": "/some/path"},
"httpForwardTemplate": {
"template": "{'path':\"/somePath\",'queryStringParameters':{'userId':[\"$!request.queryStringParameters['userId'][0]\"]},'cookies':{'SessionId':\"$!request.cookies['SessionId']\"},'headers':{'Host':[\"localhost:1081\"]},'body':\"{'name':'value'}\"}",
"templateType": "VELOCITY"
}
}'
六、OpenAPI集成
MockServer可以直接加载OpenAPI规范来创建期望:
# 从HTTP URL加载
curl -X PUT 'localhost:1080/mockserver/expectation' \
-d '{
"specUrlOrPayload": "https://example.com/openapi_spec.json"
}'
# 针对特定操作配置
curl -X PUT 'localhost:1080/mockserver/expectation' \
-d '{
"httpRequest": {
"specUrlOrPayload": "org/mockserver/openapi/openapi_petstore_example.json",
"operationId": "showPetById"
},
"httpResponse": {
"body": "some_response_body"
}
}'
七、管理与维护
7.1 清除配置
# 清除特定路径的配置
curl -X PUT "http://localhost:1080/mockserver/clear" -d '{
"path": "/some/path"
}'
# 重置所有配置
curl -X PUT "http://localhost:1080/mockserver/reset"
7.2 检索记录
# 检索记录的请求
curl -X PUT "http://localhost:1080/mockserver/retrieve?type=REQUESTS&format=JSON" -d '{
"path": "/some/path",
"method": "POST"
}'
# 检索日志消息
curl -X PUT "http://localhost:1080/mockserver/retrieve?type=LOGS" -d '{
"path": "/some/path",
"method": "POST"
}'
总结
本文全面介绍了MockServer的CURL接口使用方法,从基础配置到高级功能,包括:
- 基本期望设置与批量配置
- 回调功能与错误模拟
- 请求验证机制
- 请求转发配置
- 模板引擎支持
- OpenAPI集成
- 系统管理与维护
掌握这些命令可以帮助开发者高效地构建和维护测试环境,确保API开发的质量和可靠性。MockServer的灵活性和强大功能使其成为现代API开发测试中不可或缺的工具。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考