• Mock接口平台Moco学习


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

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

    • moco的启动及第一个demo

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

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

    [
      {
       "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

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

    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实现:

    [
      {
       "description":"This is Get request without paramter",
       "request":{
           "uri":"/getdemo",
           "method":"get"
       },
       "response":{
           "text":"This is response for Get request without paramter "
       }
      }
    ]

    2.带参数的get实现:

    {
       "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请求

    [
      {
       "description":"This is Post request",
       "request":{
           "uri":"/postdemo",
           "method":"post"
       },
       "response":{
           "text":"This is Post response"
       }
      }
    ]

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

    2.带参数的post请求实现

     { 
       "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请求

      {
       "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请求

     { 
       "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请求中是一致的。

     { 
       "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

    {
       "description":"redirect to www.baidu.com",
       "request":{
           "uri":"/redirect"
        },
       "redirectTo":"http://www.baidu.com"
      }

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

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

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

  • 相关阅读:
    简易的sniffer程序
    ubuntu 13.10 Rhythmbox不能播放mp3 和中文乱码的问题
    5.单行函数,多行函数,字符函数,数字函数,日期函数,数据类型转换,数字和字符串转换,通用函数(case和decode)
    Java设计模式-单例模式
    WordPress的用户系统总结
    STM8S EEPROM 操作
    简化ui文件转换写法
    大型网站架构之应用服务器集群化
    大型网站架构之分布式缓存
    大型网站架构之JAVA中间件
  • 原文地址:https://www.cnblogs.com/yuan1225/p/10790821.html
Copyright © 2020-2023  润新知