Moco框架基本介绍

基本操作:

java -jar ./moco-runner-0.11.0-standalone.jar [http/https/socket] -p 8888 -c json配置文件

demo 1

[
  {
        "description": "这是我的第一个mock例子",
        "request" :{
              "text":"/demo"
          } ,
          "response":{
                "text": "第一个moco框架案例"
          }
  }
]

Moco框架的http get实现

[
  {
      "description": "模拟一个没有参数的get请求",
      "request":{
            "uri": "/getdemo",
            "method": "get"
        },
        "response":{
             "text":"这是一个没有参数的get请求"
        }
   },
   {
      "description": "模拟一个带有参数的get请求",
      "request":{
            "uri": "/getwithparam",
            "method": "get",
            "queries":{
                      "name" :"小李",
                      "age":"18"
               }
        },
        "response":{
             "text":"这是一个带有参数的get请求"
        }
    }
]

Moco框架http post请求

[
    {
          "description":"模拟一个post请求",
          "request":{
                "uri":"/postdemo",
                "method":"post"
            },
            "response":{
                "text":"这是我的第一个post请求"
            }
     },
      {
          "description":"模拟一个带参数的post请求",
          "request":{
                "uri":"/postwithparam",
                "method":"post",
                "forms":{
                      "name":"小李",
                       "age":"18"
                  }
            },
            "response":{
                "text":"这是我的第一个post请求"
            }
     }
]

Moco框架加入cookies

[
    {
        "description":"这是一个带cookies信息的个头请求",
        "request":{
              "uri":"/get/with/cookies",
              "method":"get",
              "cookies":{
                    "login":"true"
                }
          },
          "response":{
                "text":"这是一个需要携带cookies信息才能访问的get请求"
            }
      }
]

Moco框架带信息的post请求

[
       {
        "description":"这是一个带cookies信息的个头请求",
        "request":{
              "uri":"/post/with/cookies",
              "method":"post",
              "cookies":{
                    "login":"true"
                },
                "json":{
                    "name":"小张",
                    "age":"18"
                }
          },
          "response":{
              "status": 200,
              "json":{
                  "detail":"success",
                   "status": "1"
                  }
            }
      }
 ]

Moco框架添加header请求

  [
      {
          "description":"这是一个带header信息的post请求",
          "request":{
                 "uri":"/post/with/headers",
                  "method":"post",
                  "headers":{
                        "content-type":"application/json"
                    },
                    "json":{
                           "name":"小王",
                            "age":16
                    }
            },
            "response":{
                  "json":{
                        "wang":"success",
                        "status":1
                    }
              }
       }
  ]

Moco框架处理重定向

[
  {
         "description":"重定向",
          "request":{
              "uri":"redirect"
          },
          "redirectTo":"http://www.baidu.com"
  },
    {
         "description":"重定向到自己指定页面",
          "request":{
              "uri":"/redirect/topath"
          },
          "redirectTo":"/redirect/new"
    },
     {
         "description":"这是被重定向到的请求",
          "request":{
              "uri":"/redirect/new"
          },
          "response":{
              "text":"重定向成功了"
          }
    }
 ]
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、什么是Moco:Moco 是一个搭建模拟服务器的工具,其支持 API 和独立运行两种方式,前者通常是在 jun...
    零下的雨阅读 9,945评论 6 6
  • 1.简介 mock用来模拟接口的,本次学习mock用的是moco框架,moco框架是github上的一个开源项目,...
    飞翼_U阅读 8,790评论 1 1
  • Mockito简介什么是mock?在软件开发的世界之外, "mock"一词是指模仿或者效仿。 因此可以将“mock...
    燕京博士阅读 9,023评论 0 6
  • 网络 理论模型,分为七层物理层数据链路层传输层会话层表示层应用层 实际应用,分为四层链路层网络层传输层应用层 IP...
    FlyingLittlePG阅读 4,288评论 0 0
  • 紫煊: 我亲爱的宝贝! 昨天我们休养生息,在床上看书、做字卡游戏、聊天……这样的安排很好。幸亏我们决定...
    窚煊阅读 1,183评论 0 2