Mock接口框架的介绍与应用

Mock接口框架的介绍与应用

一、moco框架的基本介绍

mock用来模拟接口,这里mock用的是moco框架,moco框架是github上的一个开源项目,可模拟http,https,Socket协议。

二、moco框架的入门使用

从github上下载jar包,https://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/0.11.0/,选择带standalone的最大的一个jar包,点击下载
在这里插入图片描述
在需要的进行测试的项目中新建一个文件夹,将下载下来的jar包直接拷进文件夹

建立一个json文件(图例中json文件名为startup1),输入以下代码,保存
在这里插入图片描述

startup1.json文件

[
  {
    "description":"这是我们的第一个mock例子",
    "request":{
      "uri":"/demo"
    },
    "response":{
      "text":"第一个moco框架demo"
    }
  }
]

基本操作说明

进入json文件夹所在目录:

cd Chapter7

启动命令:

java -jar -Dfile.encoding=UTF-8 ./moco-runner-0.10.0-standalone.jar http -p 8080 -c json配置文件                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             cd Chapter7

如:

java -jar -Dfile.encoding=UTF-8 ./moco-runner-0.10.0-standalone.jar http -p 8080 -c startup1.json          
-Dfile.encoding=UTF-8:字符集
-p 8080:访问的端口号(8080-c startup1.json:访问的字符集

1、Moco框架的http协议get方法Mock实现

1.1 不带参数的get方法实现

startupGet.json文件

[
  {
    "description":"模拟一个没有参数的get请求",
    "request":{
      "uri":"/get/demo",
      "method":"get"
    },
    "response":{
      "text":"这是一个没有参数的get请求"
    }
  }
]

启动命令:java -jar -Dfile.encoding=UTF-8 ./moco-runner-0.10.0-standalone.jar http -p 8080 -c startupGet.json
启动成功后,直接用浏览器访问地址:http://localhost:8080/get/demo
在这里插入图片描述

1.2 带参数的get方法实现

startupGet.json文件

[
{
    "description":"模拟一个带参数的get请求",
    "request":{
      "uri":"/get/with/param",
      "method":"get",
      "queries":{
"name":"huhansan",
"age":"18"
}
},
"response":{
"text":"我胡汉三又回来啦!"
}
}
]

启动命令:java -jar -Dfile.encoding=UTF-8 ./moco-runner-0.10.0-standalone.jar http -p 8080 -c startupGet.json
启动成功后,直接用浏览器访问地址:http://localhost:8080/get/with/demo?name=huhansan&age=18
在这里插入图片描述

2 Moco框架的http协议post方法Mock实现

2.1 不带参数的post方法实现

startupPost.json文件

[
  {
    "description":"模拟一个post请求",
    "request":{
      "uri":"/post/demo",
      "method":"post"
    },
    "response":{
      "text":"这是我的第一个mock的post请求"
    }
  }
]

启动命令:java -jar -Dfile.encoding=UTF-8 ./moco-runner-0.10.0-standalone.jar http -p 8080 -c startupPost.json
启动成功后,请求地址:http://localhost:8080/post/demo
在postman或Jmeter中请求
在这里插入图片描述

2.2 带参数的post方法实现

特别注意:

get请求的传参使用"queries",post请求的传参使用"params"或"json"

startupPost.json文件,参数为params格式

[
{
    "description":"模拟一个带参数的post请求",
    "request":{
      "uri":"/post/with/param",
      "method":"post",
      "forms":{
        "name":"huhansan",
        "age":"18"
      }
    },
    "response":{
      "text":"这是我mock的带参数的post请求"
    }
  }
  ]

或者:

startupPost.json文件,参数为json格式

[
{
    "description":"模拟一个带参数的post请求",
    "request":{
      "uri":"/post/with/param",
      "method":"post",
      "json":{
        "name":"huhansan",
        "age":"18"
      }
    },
    "response":{
      "text":"这是我mock的带参数的post请求"
    }
  }
  ]

启动命令:java -jar -Dfile.encoding=UTF-8 ./moco-runner-0.10.0-standalone.jar http -p 8080 -c startupPost.json
启动成功后,请求地址:http://localhost:8080/post/with/param

在postman或Jmeter中请求

在这里插入图片描述
在这里插入图片描述
3 Mock框架中如何加入Cookies

3.1 带cookies信息的get请求

startupWithCookies1.json文件

[
  {
    "description":"这是一个带cookies信息的get请求",
    "request":{
      "uri":"/get/with/cookies",
      "method":"get",
      "cookies":{
        "login":"true"
      }
  },
    "response":{
      "text":"这是一个需要携带cookies信息才能访问的get请求"
    }
}
]

启动命令:java -jar -Dfile.encoding=UTF-8 ./moco-runner-0.10.0-standalone.jar http -p 8080 -c startupWithCookies1.json
启动成功后,请求地址:http://localhost:8080/get/with/cookies

在postman或Jmeter中请求
在这里插入图片描述
在这里插入图片描述
3.2 带cookies信息的post请求

startupWithCookies1.json文件

[
{
    "description":"这是一个带cookies信息的post请求",
    "request":{
      "uri":"/post/with/cookies",
      "method":"post",
      "cookies":{
        "login":"true"
      },
      "json":{
        "name":"huhansan",
        "age":"18"
      }
    },
    "response":{
      "status":200,
      "json":{
        "huhansan":"success",
        "status":"1"
      }
    }
  }
  ]

启动命令:java -jar -Dfile.encoding=UTF-8 ./moco-runner-0.10.0-standalone.jar http -p 8080 -c startupWithCookies1.json
启动成功后,请求地址:http://localhost:8080/post/with/cookies

在postman或Jmeter中请求
在这里插入图片描述
在这里插入图片描述

4 Mock框架中如何加入Header

4.1 带有headers信息的mock请求

startupWithHeader.json文件

[
  {
    "description":"这是一个带header信息的post请求",
    "request":{
      "uri":"/post/with/headers",
      "method":"post",
      "headers":{
        "content-type":"application/json"
      },
      "json":{
        "name":"wanglaosi",
        "sex":"woman"
      }
    },
    "response":{
      "json":{
        "wanglaosi":"success",
        "status":"1"
      }
    }
  }
]

启动命令:java -jar -Dfile.encoding=UTF-8 ./moco-runner-0.10.0-standalone.jar http -p 8080 -c startupWithHeader.json
启动成功后,请求地址:http://localhost:8080/post/with/headers

在postman或Jmeter中请求
在这里插入图片描述
5 Moco框架中如何进行重定向

5.1 实现请求重定向

startupWithRedirect.json文件

5.1.1 重定向到百度等网页

[
  {
    "description":"重定向到百度",
    "request":{
      "uri":"/redirect"
    },
    "redirectTo":"http://www.baidu.com"
  }
  ]

启动命令:java -jar -Dfile.encoding=UTF-8 ./moco-runner-0.10.0-standalone.jar http -p 8080 -c startupWithRedirect.json
启动成功后,请求地址:http://localhost:8080/redirect
5.1.2 重定向到自定义的网址

[
  {
    "description":"重定向到一个自己的网页上",
    "request":{
      "uri":"/redirect/to/path"
    },
    "redirectTo":"/redirect/new"
  },
  {
    "description":"这是被重定向到的请求地址",
    "request":{
      "uri":"/redirect/new"
    },
    "response":{
      "text":"重定向成功啦!"
  }
  }
]

启动命令:java -jar -Dfile.encoding=UTF-8 ./moco-runner-0.10.0-standalone.jar http -p 8080 -c startupWithRedirect.json
启动成功后,请求地址:http://localhost:8080/redirect/to/path
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值