MockServer项目CURL命令使用指南:从基础到高级实践

MockServer项目CURL命令使用指南:从基础到高级实践

mockserver MockServer enables easy mocking of any system you integrate with via HTTP or HTTPS with clients written in Java, JavaScript and Ruby. MockServer also includes a proxy that introspects all proxied traffic including encrypted SSL traffic and supports Port Forwarding, Web Proxying (i.e. HTTP proxy), HTTPS Tunneling Proxying (using HTTP CONNECT) and SOCKS Proxying (i.e. dynamic port forwarding). mockserver 项目地址: https://gitcode.com/gh_mirrors/mo/mockserver

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接口使用方法,从基础配置到高级功能,包括:

  1. 基本期望设置与批量配置
  2. 回调功能与错误模拟
  3. 请求验证机制
  4. 请求转发配置
  5. 模板引擎支持
  6. OpenAPI集成
  7. 系统管理与维护

掌握这些命令可以帮助开发者高效地构建和维护测试环境,确保API开发的质量和可靠性。MockServer的灵活性和强大功能使其成为现代API开发测试中不可或缺的工具。

mockserver MockServer enables easy mocking of any system you integrate with via HTTP or HTTPS with clients written in Java, JavaScript and Ruby. MockServer also includes a proxy that introspects all proxied traffic including encrypted SSL traffic and supports Port Forwarding, Web Proxying (i.e. HTTP proxy), HTTPS Tunneling Proxying (using HTTP CONNECT) and SOCKS Proxying (i.e. dynamic port forwarding). mockserver 项目地址: https://gitcode.com/gh_mirrors/mo/mockserver

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霍美予Mabel

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值