第二节 wireMock用json模拟HTTP请求

一,HTTP请求

1)GET接口

{
    "request": {
        "method": "GET",
        "url": "/api/mytest"
    },
    "response": {
        "status": 200,
        "body": "More content\n"
    }
}

2)POST接口

{
    "request": {
        "method": "POST",
        "url": "/api/products",


          "bodyPatterns": [
                 {"equalToJson" : "{ \"name\": \"new product\", \"creator\": \"tester\", \"createTime\": \"2015-09-07\" }",


 "jsonCompareMode": "LENIENT"}
         ]
    },
    "response": {
        "status": 200,
        "body": "Add successfully.",
         "headers":{
                   "x-token":"xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
         }
    }

}

3)DELETE接口

{
    "request": {
        "method": "DELETE",
        "url": "/api/products/delete" 
    },
    "response": {
        "status": 204,      
        "headers":{
                  "x-token":" xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
        }
    }
}

4)404错误

{
"priority": 10,
    "request": {
        "url": "/unknown.html",
        "method": "GET"
    },
    "response": {
        "status": 404,
        "headers": {
            "Content-Type": "text/html; charset=utf-8"
        }
    }
}

5)PUT请求:

{
    "request": {
        "method": "PUT",
        "url": "/api/products/1",
          "bodyPatterns": [
            {"equalToJson" : "{ \"id\": 1, \"name\": \"new product\", \"creator\": \"tester\", \"createTime\": \"2015-09-07\" }", "jsonCompareMode": "LENIENT"}
         ]
    },
    "response": {
        "status": 200,
        "body": "Update successfully.",
         "headers":{
                  "x-token":" xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
         }
    }
}

5)Query参数匹配:

{
    "request": {
        "method": "GET",
        "urlPath": "/api/products",
        "queryParameters": {
            "search": {
                "contains": "chin"
            }
        }
    },
    "response": {
            "status": 200,
             "headers":{ "Content-Type": "application/json"},
              "body": "{ \"id\": 7, \"name\": \"shan zai\", \"from\":\"China\" },{ \"id\": 7, \"name\": \"shan zai\", \"from\":\"China(RPC)\" }"
    }
}

6)故障模拟

{
    "request": {
        "method": "GET",
        "url": "/fault"
    },
    "response": {
        "fault": "MALFORMED_RESPONSE_CHUNK"
    }
}

拓展用法请看下一节《第三节 URL的匹配方法》

猜你喜欢

转载自blog.csdn.net/weixin_39527812/article/details/81544131
今日推荐