跳转到主要内容

mockserver&&模拟测试工具

源代码下载

源代码下载:https://iovhm.com/book/attachments/15

安装

官方帮助:https://www.mock-server.com/


version: "3"
services:
  mock-server:
    image: mockserver/mockserver:mockserver-5.15.0
    container_name: mockserver
    restart: always
    privileged: true
    ports:
      - "1080:1080"
    environment:
      - TZ=Asia/Shanghai
      - MOCKSERVER_INITIALIZATION_JSON_PATH=/config/expectations.json     #  系统启动时从配置文件加载期望
      - MOCKSERVER_PERSISTED_EXPECTATIONS_PATH=/config/expectations.json  #  持久化期望到配置文件
      - MOCKSERVER_PERSIST_EXPECTATIONS=true                              #  开启期望配置持久化,默认情况是存放在内存的
    volumes:  
      - ./config:/config      
  mock-dify:
    image: harbor.iovhm.com/public/mock-dify:1.0.0      
    container_name: mock-dify
    restart: always
    privileged: true
    ports:
      - "8080:8080"
    environment:  
      - TZ=Asia/Shanghai
      - DIFY_API_URL=https://youdify/v1/chat-messages
      - DIFY_API_KEY=Bearer your-api-key



打开dashboard查看工作情况

host:port/mockserver/dashboard

创建一个期望


curl --request PUT \
  --url https://smart-ops-mock.saas.vppark.cn/mockserver/expectation \
  --data '{
    "id":"/hello",
    "httpRequest": {
        "method": "GET",
        "path": "/hello"
    },
    "httpResponse": {
        "headers": {
            "Content-Type": [
                "application/json"
            ]
        },
        "statusCode": 200,
        "body": {
            "code": 200,
            "message": "ok",
            "data": {
                "list": {
                    "a": "b",
                    "c": "d"
                }
            }
        }
    }
}'

触发一个期望


curl --request GET \
  --url https://smart-ops-mock.saas.vppark.cn/hello 

删除一个期望


curl --request PUT \
  --url https://smart-ops-mock.saas.vppark.cn/mockserver/clear \
  --data '{
    "path": "/hello"
}'

创建一个期望,并转发到其他服务


curl --request PUT \
  --url https://smart-ops-mock.saas.vppark.cn/mockserver/expectation \
  --data '{
    "id": "/baidu",
    "httpRequest": {
        "method": "POST",
        "path": "/baidu"
    },
    "httpOverrideForwardedRequest": {
        "httpRequest": {
            "method": "GET",
            "path": "/",
            "headers": {
                "Host": [
                    "www.baidu.com"
                ]
            },
            "secure": true
        }
    }
}}'

已经增加的期望列表


curl --request PUT \
  --url 'https://smart-ops-mock.saas.vppark.cn/mockserver/retrieve?type=active_expectations' 

系统状态


curl --request PUT \
  --url https://smart-ops-mock.saas.vppark.cn/mockserver/status 

重置一切

既整个系统添加的内容全部没有了


curl --request PUT \
  --url https://smart-ops-mock.saas.vppark.cn/mockserver/reset 

转发到dify(deepseek)

转发到其他服务的时候,想要动态修下游的请求body,需要使用模板语,模板语法里面只能填写字符串,特殊字符需要转义,很不方便,编写了一个JS脚本来完成。

mockserver模板介绍

https://www.mock-server.com/mock_server/response_templates.html#velocity_templates https://velocity.apache.org/tools/3.1/tools-summary.html#JsonTool

// 直接转发,请求参数不支持动态替换
var request_static = {
    "id": "/api/visitor/list",
    "httpRequest": {
        "method": "GET",
        "path": "/api/visitor/list"
    },
    "httpOverrideForwardedRequest": {
        "requestOverride": {
            "method": "POST",
            "path": "/dify/v1/chat-messages",
            "headers": {
                "Host": [
                    "mock-dify:8080"
                ],
            },
            // "secure": true, // https访问下游
            "body": {
                "query": "数据结构为{\"personIds\": [\"d7228950bf29490484bc845dff756b95\"]},在原数据上增加开始时间和结束时间,将时间设置为明天,只输出纯数据内容,不添加任何解释、说明、格式标记、代码块或多余符号",
            }
        }
    }
}


// 模板转发,请求参数动态替换,但是template写法很恶心,可以使用下面的方法进行转换
// 模板变量参考:https://www.mock-server.com/mock_server/response_templates.html#velocity_templates
// 模板变量参考:https://velocity.apache.org/tools/3.1/tools-summary.html#JsonTool
var request_dynamic = {
    "id": "/api/visitor/list2",
    "httpRequest": {
        "method": "GET",
        "path": "/api/visitor/list2"
    },
    "httpForwardTemplate": {
        "templateType": "VELOCITY",
        "template": "#set($jsonBody=$json.parse($!request.body))\n{\"method\":\"POST\",\"path\":\"/v1/chat-messages\",\"secure\":true,\"headers\":{\"Host\":[\"api-ai-agent.vppark.cn\"],\"Authorization\":[\"Bearer app-rWhiquJ6eMwsdCUQkaVJf9Dx\"]},\"body\":{\"inputs\":{},\"query\":\"数据结构为{\\\"personIds\\\": [\\\"d7228950bf29490484bc845dff756b95\\\"]},在原数据上增加开始时间和结束时间,当前时间为$!now_iso_8601,将时间设置为明天,只输出纯数据内容,不添加任何解释、说明、格式标记、代码块或多余符号\",\"response_mode\":\"blocking\",\"user\":\"abc-123\",\"username\":\"abc-123\",\"ext_replace\": \"$!request.body\"}}"
    }
}

// 模板JSON写法
var dynamic_template = {
    "method": "POST",
    "path": "/dify/v1/chat-messages",
    "headers": {
        "Host": [
            "mock-dify:8080"
        ],
    },
    // "secure": true, // https访问下游
    "body": {
        "query": "数据结构为{\"code\":0,\"msg\":\"success\",\"data\":{\"successes\":[{\"clientId\":\"$!jsonBody.clientId\",\"personId\":\"$!jsonBody.personId\"}]}},获取请求参数中的clientId、personId如果personId为空自动生成19位的id不允许重复",
    }
}

// 转换为字符串
let formattedJson = JSON.stringify(dynamic_template);
formattedJson = formattedJson.replace(/\\/g, '\\\\')
formattedJson = formattedJson.replace(/"/g, '\\"')

console.log(formattedJson)