zoukankan      html  css  js  c++  java
  • Mock接口框架的应用实战

    一.Mock平台功能介绍
    Mock平台可以帮助前端人员进行接口的模拟,本文介绍Moco框架
    二.Moco框架基本介绍
    Moco框架其实就是一个jar包,到 http://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/0.11.0 网站上去获得,下载moco-runner-0.11.0-standalone.jar

    三.Moco的启动及第一个demo

    1.创建文件夹MocoDemo

    2.将下载的moco-runner-standalone.jar放到MocoDemo文件夹下

    3.jar的启动命令

    java -jar ./moco-runner-0.11.0-standalone.jar http -p 8888 -c json配置文件路径
    http表示协议
    -p表示端口号
    -c表示json配置文件
    4.编写startup.json文件

     1 [
     2   {
     3     "description":"这是我们的第一个mock例子",
     4     "request":{
     5       "uri":"/demo"
     6     },
     7     "response":{
     8       "text":"第一个moco框架demo"
     9     }
    10   }
    11 ]

    5.运行jar:java -jar ./moco-runner-0.11.0-standalone.jar http -p 8888 -c startup.json后,访问localhost:8888/demo页面上会打印第一个moco框架demo

    四.Moco框架的http协议get方法Moco实现

    1.模拟不带参数的get方法实现,访问地址:http://localhost:8888/getDemo 

     1 [
     2   {
     3     "description":"模拟不带参数的get请求",
     4     "request":{
     5       "uri":"/getDemo",
     6       "method":"get"
     7     },
     8     "response":{
     9       "text":"这是一个没有参数的get请求"
    10     }
    11   }
    12 ]

    2.模拟带参数的get方法实现,访问地址:http://localhost:8888/getWithParam?name=huhansan&age=23

     1 [
     2   {
     3     "description":"模拟带参数的get请求",
     4     "request":{
     5       "uri":"/getWithParam",
     6       "method":"get",
     7       "queries":{
     8       "name":"huhansan",
     9       "age":"23"
    10       }
    11     },
    12     "response":{
    13       "text":"这是一个带参数的get请求"
    14     }
    15   }
    16 ]

    五.Moco框架的http协议post方法Moco实现

    1.模拟不带参数的post请求的实现:

     1 [
     2     {
     3         "description":"模拟不带参数的post请求",
     4         "request":{
     5         "uri":"/postDemo",
     6         "method":"post"
     7         },
     8         "response":{
     9         "text":"这是一个不带参数的post请求"
    10         }
    11     }
    12 ]

    2.模拟带参数的post请求,注意:携带参数的为forms,不是queries

    [
     2     {
     3         "description":"模拟带参数的post请求",
     4         "request":{
     5             "uri":"/postWithParam",
     6             "method":"post",
     7             "forms":{
     8                 "name":"huhansan",
     9                 "sex":"man"
    10             }
    11         },
    12         "response":{
    13         "text":"这是一个带参数post的请求"
    14         }
    15     }
    16 ]

    六.Moco框架的http协议带cookies信息的实现

    1.带cookies信息的get请求

     1 [
     2     {
     3         "description":"模拟带cookies的get请求",
     4         "request":{
     5             "uri":"/getWithCookies",
     6             "method":"get",
     7             "cookies":{
     8                 "login":"true"
     9             }
    10         },
    11         "response":{
    12         "text":"这是一个需要携带cookie才能访问的get请求"
    13         }
    14     }
    15 ]

    2.带cookies信息的post请求

     1 [
     2     {
     3         "description":"模拟带cookies的post请求",
     4         "request":{
     5             "uri":"/postWithCookies",
     6             "method":"post",
     7             "cookies":{
     8                 "login":"true"
     9             },
    10             "json":{
    11             "name":"huhansan",
    12             "age":"18"
    13             }
    14         },
    15         "response":{
    16         "status":200,
    17             "json":{
    18             "huhansan":"success",
    19             "status":"1"
    20             }
    21         }
    22     }
    23 ]

    七.带有headers信息的mock请求

     1 [
     2     {
     3         "description":"模拟带headers的post请求",
     4         "request":{
     5             "uri":"/postWithHeaders",
     6             "method":"post",
     7             "headers":{
     8                 "content-type":"application/json"
     9             },
    10             "json":{
    11             "name":"huhansan",
    12             "age":"18"
    13             }
    14         },
    15         "response":{
    16         "status":200,
    17             "json":{
    18             "huhansan":"success",
    19             "status":2
    20             }
    21         }
    22     }
    23 ]

    八.实现请求重定向

    1 [
    2     {
    3         "description":"重定向到百度",
    4         "request":{
    5             "uri":"/redirect"
    6         },
    7         "redirectTo":"http://www.baidu.com"
    8     }
    9 ]
  • 相关阅读:
    Linux报错:“/bin/bash^M: 坏的解释器
    搭建单向HTTPS
    Wamp Apache 启动失败检测方法
    Excel 日常操作
    apache https 双向认证
    android搭建
    我身为程序员踩过的坑
    windows 2008 安装 apache + mysql + php
    Svn
    工具软件类
  • 原文地址:https://www.cnblogs.com/heyuling/p/12069523.html
Copyright © 2011-2022 走看看