• Moco框架


    About

     

    mock除了用在单元测试过程中,还有一个用途,当前端开发在开发页面的时候,需要服务端提供API接口。
    此时服务端没开发完成,或者说没搭建测试环境,这个时候前端开发会自己mock一个api服务端,自己给自己提供调用接口的返回数据。
    moco框架用途就是开发在开发的过程中,需要依赖一部分的接口,但是对方没有提供或者环境等等情况。
    moco框架支持API和独立运行两种方式,API的方式通常运行在Junit等java的测试框架中,而独立的运行方式是通过jar包开启服务,可以模拟HTTP,HTTPS,socket协议。这里介绍标准用法。
    更多使用方法参考:https://github.com/dreamhead/moco/blob/master/moco-doc/usage.md
    另外,jar包的运行依赖于java虚拟机,这里我们还需要安装java的JDK:

    jar包下载

    M:testmoco-runner
        ├─moco-runner-1.0.0-standalone.jar
        └─configure.json

    如何运行:

    M:testsmoco-runner>java -jar moco-runner-0.12.0-standalone.jar start -p 8888 -c config.json

    访问:

    http://127.0.0.1:8888/login

    快速上手

     

    再次强调:

    • 配置文件修改后,就要重新运行jar包。
    • 配置文件为json类型,对格式的要求比较高,要注意点!

    开搞!
    get请求

    configure.json

    [
      {
        "description": "一个简单的get请求",
        "request": {
          "method": "get",
          "uri": "/login"
        },
        "response": {
          "text": "我是login get method",
          "headers":{
            "Content-Type":"text/html;charset=gbk"
          }
        }
      },
      {
        "description": "带参数的get请求,p1和p2是两个参数",
        "request": {
          "method": "get",
          "uri": "/reg",
          "queries": {
            "p1": "v1",
            "p2": "v2"
          }
        },
        "response": {
          "text": "带参数的get请求",
          "headers":{
            "Content-Type":"text/html;charset=gbk"
          }
        }
      },
      {
        "description": "get请求返回json类型数据",
        "request": {
          "method": "get",
          "uri": "/login_json"
        },
        "response": {
          "json": {
            "key":"value",
            "请求方式是get":"相应结果为json类型"
          },
          "headers": {
            "Content-Type": "application/json;charset=gbk"
          }
        }
      }
    ]

    各个参数无需多言吧,简单明了。在响应体加入headers指定Content-Type编码格式为gbk是为了防止中文乱码问题。
    另外可以根据响应文本内容指定application/json或者text/html

    浏览器地址栏输入:

    http://127.0.0.1:8888/login
    http://127.0.0.1:8888/reg?p1=v1&p2=v2
    http://127.0.0.1:8888/login_json

    post请求
    configure.json

    [
      {
        "description": "post请求,请求参数为json格式,响应格式为json",
        "request": {
          "method": "post",
          "uri": "/post_json",
          "json": {
            "login_status": "successful"
          }
        },
        "response": {
          "json": {
            "login": "ok"
          },
          "headers": {
            "Content-Type": "application/json;charset=gbk"
          }
        }
      },
      {
        "description": "post请求,请求及响应都为json,并且请求带cookies",
        "request": {
          "method": "post",
          "uri": "/post_cookie",
          "json": {
            "login_status": "successful"
          },
          "cookies":{
            "user_id":"xsdaqawea"
          }
        },
        "response": {
          "json": {
            "login": "ok"
          },
          "headers": {
            "Content-Type": "application/json;charset=gbk"
          }
        }
      },
      {
        "description": "post请求,请求及响应都为json,并且请求带cookies和headers",
        "request": {
          "method": "post",
          "uri": "/post_cookie",
          "json": {
            "login_status": "successful"
          },
          "cookies": {
            "user_id": "xsdaqawea"
          },
          "headers":{
            "Content-Type":"application/json"
          }
        },
        "response": {
          "json": {
            "login": "ok"
          },
          "headers": {
            "Content-Type": "application/json;charset=gbk"
          }
        }
      }
    ]

    发送请求及结果:

    import requests
    
    res1 = requests.post(url='http://127.0.0.1:8888/post_json', json={"login_status": "successful"})
    
    print(res1.json())  # {'login': 'ok'}
    
    
    res2 = requests.post(url='http://127.0.0.1:8888/post_cookie', cookies={"user_id": "xsdaqawea"}, json={"login_status": "successful"})
    
    print(res2.json())  # {'login': 'ok'}

    请求重定向
    confiure.json

    [
      {
        "description":"重定向到指定网站",
        "request":{
          "method":"get",
          "uri":"/login_redirect"
        },
        "redirectTo":"https://www.baidu.com"
      }
    ]

    发送请求及结果:

    http://127.0.0.1:8888/login_json
  • 相关阅读:
    IOI 1996 网络协议
    lougu P2344奶牛抗议
    Poj3764 The XOR-longest Path
    A Simple Problem with Integers (线段树)
    NOIP2011 选择客栈
    20181029 T3 乐谱分段
    20181029 T2 寻宝游戏
    20181029 T1 教科书般的亵渎
    NOIP2011聪明的质监员
    浅谈AC自动机
  • 原文地址:https://www.cnblogs.com/zhang-da/p/12296104.html
Copyright © 2020-2023  润新知