首页 > 其他分享 >mock实现

mock实现

时间:2022-11-27 15:44:13浏览次数:27  
标签:description 登录 实现 request json login response mock

环境搭建
下载安装:依赖JDK环境,执行电脑安装好JDK,建议安装JDK1.8版本
Server-Moco下载地址:https://repo1.maven.org/maven2/com/github/dreamhead/moco-runner,选择相应版本,本次下载最新版1.3.0,下载下来就是moco-runner-1.3.0-standalone.jar包

Mock单个请求
  • 创建一个json 文件,名为 login.json,内容如下:

[
  {
    "description": "登录",
    "request": {
      "uri": "/login"
        },
    "response": {
      "text":"登录成功"
        }

  }
]

  • 启动 Mock 服务

java -jar moco-runner-1.3.0-standalone.jar http -p 8082 -c login.json

 

 

 在Postman 发送请求:http://localhost:8082/login(这就mock好啦)

 

 

 

Mock多个请求
  • 在创建一个json 文件,名为 home.json,内容如下:

[
  {
    "description": "首页",
    "request": {
      "uri": "/home"
        },
      "response": {
        "text":"进入首页"
        }

  }
]

  • 创建一个请求管理配置文件,名为 config.json,内容如下:

[
  {"include": "login.json"},
  {"include": "home.json"}
]

  • 启动 Mock 服务
  • java -jar moco-runner-1.3.0-standalone.jar http -p 8082 -g config.json

 

 

 在Postman 发送请求:http://localhost:8082/login 和 http://localhost:8082/home

 

 

 常见参数配置

  • 请求方式,通过method参数定义

[
  {
    "description": "登录",
    "request": {
      "uri": "/login",
    "method": "post"
        },
    "response": {
    "text": "登录成功"
          }
  }
]

  • 请求参数,通过queries参数定义

[
  {
    "description": "登录",
    "request": {
      "uri": "/login",
      “method": "post",
      "queries": {
        "username": "king",
        "pwd": 123456
            }
        },
    "response": {
      "text": "登录成功"
          }
  }
]

  • 请求头,通过headers参数定义

[
  {
    "description": "登录",
    "request": {
      "uri": "/login",
      "method": "post",
      "headers": {
        "appId": "4275CCE0-CE16-4BCC-9AF5-31CA133AC9AD"
            }
          },
    "response": {
      "text": "登录成功"
          }
  }
]

  • 表单请求体,通过forms参数定义

[
  {
    "description": "登录",
    "request": {
      "uri": "/login",
      "method": "post",
      "forms": {
        "username": "king",
        "password": 123456
            }
        },
    "response": {
      "text": "登录成功"
          }
  }
]

  • JSON请求体,通过json参数定义

[
  {
    "description": "登录",
    "request": {
      "uri": "/login",
      "method": "post",
      "headers": {
        "Content-Type": "application/json"
            },
      "json": {
        "username": "king",
        "password": 123456
            }
        },
    "response": {
      "text": "登录成功"
          }
  }
]

  • HTTP响应状态码,通过status参数定义

[
  {
    "description": "登录",
    "request": {
    "uri": "/login",
    "method": "post"
        },
    "response": {
      "status": 500,
      "text": "服务器出现异常!"
          }
  }
]

  • JSON响应数据,通过json参数定义

[
  {
    "description": "登录",
    "request": {
      "uri": "/login",
      "method": "post"
          },
    "response": {
      "headers": {
        "Content-Type": "application/json;charset=UTF-8"
            },
      "json": {
        "code": "0",
        "msg": "操作成功",
        "data": {
          "userID": 3175896456,
          "token": "17e6ad42cff592-0dfc3f6fa02aee-c343365-1fa400-17e6ad42d00891"
            }
          }
        }
  }
]

 

标签:description,登录,实现,request,json,login,response,mock
From: https://www.cnblogs.com/ailie/p/16928397.html

相关文章