ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

moco框架的使用

2020-05-07 18:51:17  阅读:280  来源: 互联网

标签:请求 框架 get method json 参数 使用 post moco


一、moco简介

moco框架是github上的一个开源项目,可模拟http,https,Socket协议。

mock测试就是在测试过程中,对于某些不容易构造  或者  不容易获取的对象  又或者  开发还未完成开发的功能实现,用一个虚拟的对象来创建以便测试的测试方法。

二、moco框架的下载及启动

下载:https://github.com/dreamhead/moco

这里我的moco目录结构是在同一文件夹下,这样的:

启动:java -jar moco-runner-1.0.0-standalone.jar http -p 2222 -c get_data.json (启动协议为http的mock服务,服务端口2222)

三、模拟GET请求

以下是模拟get请求已经写好的要模拟数据,包括请求uri,method,参数,响应内容

 1 [
 2   {
 3     "description":"不带参数的get请求",
 4     "request":{
 5       "uri":"/withGetDemo",
 6       "method":"get"
 7     },
 8     "response":{
 9       "text":"这是不带参数的get请求"
10     }
11   },
12   {
13     "description":"带参数的get请求,p1,p2分别的参数1,参数2,名称可随便起,个数也可随便加",
14     "request":{
15       "uri":"/wihtGetDemobyParam",
16       "method":"get",
17       "queries":{
18         "p1":"hh",
19         "p2":"good"
20       }
21     },
22     "response":{
23       "text":"这是带参数的get请求"
24     }
25   }
26 ]

这里我用浏览器模拟第一个get请求,得到的响应如下:

 

这里我用postman模拟第二个带有请求参数的get请求,得到的响应如下:

 

 四、模拟POST请求

以下为post请求的几种请求方式:

1、不带参数的post请求

2、带参数的post请求

3、带cookie的post请求

4、带header的post请求

模拟的返回响应数据如下

 1 [
 2   {
 3     "description":"不带参数的post请求",
 4     "request":{
 5       "uri":"/postDemo",
 6       "method":"post"
 7     },
 8     "response":{
 9       "text":"这是不带参数的post请求"
10     }
11   },
12   {
13     "description":"带参数的post请求",
14     "request":{
15       "uri":"/postDemoWithParam",
16       "method":"post",
17       "forms":{
18         "param1":"one",
19         "param2":"two"
20       }
21     },
22     "response":{
23       "json":{"name":"han","age":30,"address":"beijing daxing"}
24     }
25   },
26   {
27     "description":"带cookie的Post请求",
28     "request":{
29         "uri":"/postDemoWithCookies",
30         "method":"post",
31         "cookies":{
32             "login":"true"
33         },
34         "json":{
35             "name":"hi",
36             "age":"3"
37         }
38     },
39     "response":{
40         "status":"200",
41         "json":{
42             "name":"success",
43             "status":"1"
44         }
45     }
46   },
47 
48   {
49     "description":"带header的post请求",
50     "request":{
51         "uri":"/withHeader",
52         "method":"post",
53         "headers":{
54             "content-type":"application/json"
55         },
56         "json":{
57             "name":"xiaoming",
58             "age":"18"
59         }
60     },
61     "response":{
62         "json":{
63             "message":"success",
64             "status":"1"
65         }
66     }
67   }
68 ]

 

标签:请求,框架,get,method,json,参数,使用,post,moco
来源: https://www.cnblogs.com/hanxiaobei/p/12844874.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有