前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Mock接口平台Moco学习

Mock接口平台Moco学习

作者头像
yuanyuan
发布2019-09-02 15:38:50
1.1K0
发布2019-09-02 15:38:50
举报
文章被收录于专栏:小满小满

Mock就是模拟接口的。本文学习Mock的 Moco开源框架。

Moco源码和jar下载地址: git jar 下载moco-runner-xxxx-standalone.jar

  • moco的启动及第一个demo

Step1: 在项目中创建一个package:moco,并将下载的jar包放在该package下。

Step2:创建一个json文件,格式如下:

代码语言:javascript
复制
[
  {
   "description":"This is my first mock demo",
   "request":{
       "uri":"/demo"
   },
   "response":{
       "text":"This is response"
   }
  }
]

Step3:cmd进入到该package下,运行命令:java -jar ./moco-runner-0.12.0-standalone.jar http -p 8888 -c startup1.json

在命令行中出现,则命令运行成功

代码语言:javascript
复制
29 Apr 2019 14:31:54 [main] INFO  Server is started at 8888
29 Apr 2019 14:31:55 [main] INFO  Shutdown port is 52901

Step4:打开浏览器,输入 localhost:8888.在浏览器上就可以看到我们在json文件中定义的数据。

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

1. 不带参数的get实现:

代码语言:javascript
复制
[
  {
   "description":"This is Get request without paramter",
   "request":{
       "uri":"/getdemo",
       "method":"get"
   },
   "response":{
       "text":"This is response for Get request without paramter "
   }
  }
]

2.带参数的get实现:

代码语言:javascript
复制
{
   "description":"This is Get request with paramter",
   "request":{
       "uri":"/getwithparam",
       "method":"get",
       "queries":{
           "name":"zhangsan",
           "age":"18"
       }
   },
   "response":{
       "text":"This is response for Get request with paramter "
   }
  }

在浏览器中访问:http://localhost:8888/getwithparam?name=zhangsan&age=18 就可以返回定义的json数据。

  • Moco框架的http协议Post方法的Mock实现

1.不带参数的post请求

代码语言:javascript
复制
[
  {
   "description":"This is Post request",
   "request":{
       "uri":"/postdemo",
       "method":"post"
   },
   "response":{
       "text":"This is Post response"
   }
  }
]

注意的是post请求不能直接在浏览器中访问,这个时候我们就需要借助jmeter postman等测试工具进行post请求的测试了。具体方法这里不演示。

2.带参数的post请求实现

代码语言:javascript
复制
 { 
   "description":"This is Post request with paramter",
   "request":{
       "uri":"/postwithparam",
       "method":"post",
       "forms":{
           "name":"zhangsan",
           "age":"18"
       }
   },
   "response":{
       "text":"This is Post response with paramter"
   }
  }
  • Moco框架如何加入Cookies

1.带cookies信息的get请求

代码语言:javascript
复制
  {
   "description":"This is Get request with cookies",
   "request":{
       "uri":"/get/with/cookies",
       "method":"get",
       "cookies":{
          "login":"true"
       }
   },
   "response":{
       "text":"This is get response with cookies"
   }
  }

2.带cookies信息的post请求

代码语言:javascript
复制
 { 
   "description":"This is Post request with cookies",
   "request":{
       "uri":"/post/with/cookies",
       "method":"post",
       "cookies":{
          "login":"true"
       }
       "json":{
           "name":"zhangsan",
           "age":"18"
       }
   },
   "response":{
       "status":200,
       "json":{
          "zhangsan":"success",
           "status":"1"
       }
   }
  }
  • Moco框架如何加入Header

Header请求头信息的格式在get和post请求中是一致的。

代码语言:javascript
复制
 { 
   "description":"This is Post request with header",
   "request":{
       "uri":"/postwithheader",
       "method":"post",
       "headers":{
           "content-type":"application/json"
       },
       "json":{
           "name":"zhangsan",
           "age":"18"
       }
   },
   "response":{
       "text":"This is Post response with paramter"
   }
  }
  • Moco框架如何进行重定向

1.重定向到baidu

代码语言:javascript
复制
{
   "description":"redirect to www.baidu.com",
   "request":{
       "uri":"/redirect"
    },
   "redirectTo":"http://www.baidu.com"
  }

2.重定向到自己网站的某个地址

代码语言:javascript
复制
{
   "description":"redirect to my path",
   "request":{
       "uri":"/redirect2"
    },
   "redirectTo":"redirected"
  },

  {
   "description":"This is my path",
   "request":{
       "uri":"/redirected"
   },
   "response":{
       "text":"redirect success!"
   }
  }

如果喜欢作者的文章,请关注"写代码的猿"订阅号以便第一时间获得最新内容。本文版权归作者所有,欢迎转载.

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-04-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档