【三:Mock服务的使用】

目录

1、工具包

1、:服务包的下载

moco-runner-0.11.0-standalone.jar 下载

2、:运行命令

java -jar ./moco-runner-0.11.0-standalone.jar http -p 8888 -c startup1.json

2、mock的demo

json 复制代码
[
  {
    "description":"这是我们的第一个mock例子",
    "request":{
      "uri":"/demo"
    },
    "response":{
      "text":"第一个moco框架demo"
    }
  }
]

1、get请求

json 复制代码
[
  {
    "description":"模拟一个没有参数get请求",
    "request":{
      "uri":"/getdemo",
      "method":"get"
    },
    "response":{
      "text":"这是一个没有参数的get请求"
    }

  },
  {
    "description":"这是一个带参数get请求",
    "request":{
      "uri":"/getwithparam",
      "method":"get",
      "queries":{
        "name":"huhansan",
        "sex":"20"
      }
    },
    "response":{
      "text":"我胡汉三又回来啦!!!!"
    }

  }
]

2、post请求

json 复制代码
[
  {
    "description":"模拟一个post请求",
    "request":{
      "uri":"/postdemo",
      "method":"post"
    },
    "response":{
      "text":"这是我的第一个mock的post请求"
    }
  },
  {
    "description":"这是一个带参数的post请求",
    "request":{
      "uri":"/postwithparam",
      "method":"post",
      "forms":{
        "name":"huhansan",
        "sex":"man"
      }
    },
    "response":{
      "text":"我胡汉三带着参数来啦!!!"

    }
  }
]

3、带cookies的请求

json 复制代码
[
  {
    "description":"这是一个会返回cookies信息的get请求",
    "request":{
      "uri":"/getCookies",
      "method":"get"
    },
    "response":{
      "cookies":{
        "login":"true"
      },
      "text":"恭喜你获得cookies信息成功"
    }
  },
  
  
  {
    "description":"这是一个带cookies信息的get请求",
    "request":{
      "uri":"/get/with/cookies",
      "method":"get",
      "cookies":{
        "login":"true"
      }
    },
    "response":{
      "text":"这是一个需要携带cookies信息才能访问的get请求"
    }
  },
  
  
  {
    "description":"这是一个带cookies信息的post请求",
    "request":{
      "uri":"/post/with/cookies",
      "method":"post",
      "cookies":{
        "login":"true"
      },
      "json":{
        "name":"huhansan",
        "age":"18"
      }
    },
    "response":{
      "status":200,
      "json":{
        "huhansan":"success",
        "status":"1"
      }
    }
  }

]

4、带请求头的请求

json 复制代码
[
  {
    "description":"这是一个带header信息的post请求",
    "request":{
      "uri":"/post/with/headers",
      "method":"post",
      "headers":{
        "content-type":"application/json"
      },
      "json":{
        "name":"wanglaosi",
        "sex":"woman"
      }
    },
    "response":{
      "json":{
        "wanglaosi":"success",
        "status":"1"
      }
    }

  }
]

5、请求重定向

json 复制代码
[
  {
    "description":"重定向到百度",
    "request":{
      "uri":"/redirect"
    },
    "redirectTo":"http://www.baidu.com"
  },
  {
    "description":"重定向到一个自己的网页上",
    "request":{
      "uri":"/redirect/topath"
    },
    "redirectTo":"/redirect/new"
  },
  {
    "description":"这是被重定向到的请求",
    "request":{
      "uri":"/redirect/new"
    },
    "response":{
      "text":"重定向成功啦"
    }
  }

]
相关推荐
2601_962106645 小时前
【Java Set综合实战】商品管理系统 + 图书收藏系统|Product、Book自定义类全套代码
java
RuoyiOffice5 小时前
SpringBoot+Vue3 凭证生命周期设计:审核、过账、冲销与反过账怎样保持账目一致
java·spring boot·事务·springboot3·财务系统·会计凭证·ruoyi office
2601_962106646 小时前
HashMap入门|工号-姓名键值存储、两种遍历方式、根据key/value查
java
杨凯凡6 小时前
Spring Framework 7 API 版本管理实战:请求头路由、兼容迁移与废弃策略
java·rest api·springframework·spring boot 4·api版本管理
T_Wizard7 小时前
同一个问题写了 4 遍:官方 SDK / HTTP / Spring AI / LangChain4j,到底该选哪个
java·ai编程
hai_android7 小时前
AudioTrack 深度解析:从原理到实战
android·java·kotlin
斯维赤7 小时前
限流、熔断、降级,你真的分得清吗?
java·后端·架构
aiks_yuan10 小时前
Java CPU 100% 怎么排查:JDK 11 起 jstack 自带线程 CPU 时间,不用再 top -H 换算十六进制(JDK 8、11、17 实测)
java
Flynn Liang11 小时前
AiServices 源码级拆解:一个接口如何变成 RAG Agent
java·langchain·源码分析·rag·langchain4j·aiservices
Flynn Liang11 小时前
LangChain4j 1.23 架构全景:Java 大模型应用的 6 层抽象
java·langchain·大模型·rag·ai agent·langchain4j