• Mock接口平台Moco学习


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

    Moco源码和jar下载地址: git  jar  下载moco-runner-0.12.0-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!"
       }
      }
     

     

    关注vx公众号了解更多内容

     

  • 相关阅读:
    web前端性能优化
    JavaScript事件类型
    十分钟入门less(翻译自:Learn lESS in 10 Minutes(or less))
    JavaScript中的事件对象
    JavaScript中捕获/阻止捕获、冒泡/阻止冒泡
    探究JavaScript中的五种事件处理程序
    2D游戏中的碰撞检测:圆形与矩形碰撞检测(Javascrip版)
    碰撞检测算法:点和矩形碰撞、点和圆形碰撞、矩形碰撞、圆形碰撞
    如何停止requestAnimationFrame方法启动的动画
    Ajax方式上传文件
  • 原文地址:https://www.cnblogs.com/louiezhou/p/12320990.html
Copyright © 2020-2023  润新知