一、URL的匹配方式
1)绝对匹配带参数:http://localhost:9999/your/url?and=query
{
"request": {
"method": "GET",
"url": "/your/url?and=query"
},
"response": {
"status": 200,
"body": "/your/url?and=query\n"
}
}
2)绝对匹配不带参数:http://localhost:9999/your/urlPath
{
"request": {
"method": "GET",
"urlPath": "/your/urlPath"
},
"response": {
"status": 200,
"body": " urlPath: /your/urlPath"
}
}
3)带参数,正则匹配:http://localhost:9999/your/urlPattern/jhkj?and=query
{
"request": {
"method": "GET",
"urlPattern": "/your/urlPattern/([a-z]*)\\?and=query"
},
"response": {
"status": 200,
"body": "/your/urlPattern/([a-z]*)\\?and=query"
}
}
4)不带参数,正则匹配:http://localhost:9999/your/urlPathPattern/sdfdsf
{
"request": {
"method": "GET",
"urlPathPattern": "/your/urlPathPattern/([a-z]*)"
},
"response": {
"status": 200,
"body": "/your/urlPathPattern/([a-z]*)"
}
}
5)单独传参数:http://localhost:9999/api/products/queryParameters/NoRegular?search=chin
{
"priority": 1,
"request": {
"method": "GET",
"urlPath": "/api/products/queryParameters/NoRegular",
"queryParameters": {
"search": {
"contains": "chin"
}
}
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"body": "{ \"id\": 7, \"name\": \"shan zai\", \"from\":\"China\" }"
}
}
6)单独传参数,正则参数:http://localhost:9999/api/products/queryParameters/Regular?search_term=aswiremocka
{
"request": {
"method": "GET",
"url": "/your/queryParameters",
"queryParameters" : {
"search_term" : {
"matches" : "^(.*)wiremock([A-Za-z]+)$"
}
}
},
"response": {
"status": 200,
"body": "dsfsdsdfsdf"
}
}
二、POST的传参方式
1)Body方式1:
接口:http://localhost:9999/api/products/imput/body/JSON
Body:{"total_results": 4}
返回:/api/products/imput/body/JSON.
示例:
{
"request": {
"method": "POST",
"url": "/api/products/imput/body/JSON",
"bodyPatterns" : [ {
"equalToJson" : { "total_results": 4 }
} ]
},
"response": {
"status": 200,
"body": "/api/products/imput/body/JSON.",
"headers":{
"x-token":"xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
}
}
2)Body方式2:
接口:http://localhost:9999/api/products/imput/body/JSON/String
Body:{"total_results": 4}
返回:/api/products/imput/body/JSON/String.
示例:
{
"request": {
"method": "POST",
"url": "/api/products/imput/body/JSON/String",
"bodyPatterns" : [ {
"equalToJson" : "{ \"total_results\": 4 }"
} ]
},
"response": {
"status": 200,
"body": "/api/products/imput/body/JSON/String.",
"headers":{
"x-token":"xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
}
}
3)Body方式3:
接口:http://localhost:9999/api/products/imput/body/JSON/include/key
Body正确输入:{ "name": { "thing": "RequiredThing" } }
Body错误输入:{ "things": { "name": "RequiredThing" } }
返回:/api/products/imput/body/JSON/include/key.
示例:
{
"request": {
"method": "POST",
"url": "/api/products/imput/body/JSON/include/key",
"bodyPatterns" : [ {
"matchesJsonPath" : "$.name"
} ]
},
"response": {
"status": 200,
"body": "/api/products/imput/body/JSON/include/key.",
"headers":{
"x-token":"xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
}
}
4)Body方式4:
接口:http://localhost:9999/api/products/imput/body/JSON/include/key/value
Body正确输入:
1:{ "things": { "name": "RequiredThing" } }
2:{ "things": [ { "name": "RequiredThing" }, { "name": "Wiremock" } ] }
Body错误输入:
1: {"price": 15 }
2:{ "things": { "name": "Wiremock" } }
返回:/api/products/imput/body/JSON/include/key/value.
示例:
{
"request": {
"method": "POST",
"url": "/api/products/imput/body/JSON/include/key/value",
"bodyPatterns" : [ {
"matchesJsonPath" : "$.things[?(@.name == 'RequiredThing')]"
} ]
},
"response": {
"status": 200,
"body": "/api/products/imput/body/JSON/include/key/value.",
"headers":{
"x-token":"xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
}
}
5)Body方式5:
接口:http://localhost:9999/api/products/imput/body/JSON/include/key/value_Regular
Body正确输入:
1:{ "things": { "name": "RequiredThing" } }
2:{ "things": [ { "name": "Required" }, { "name": "Wiremock" } ] }
Body错误输入:
1:{ "price": 15 }
2:{ "things": { "name": "Wiremock" } }
2:{ "things": [ { "name": "Thing" }, { "name": "Wiremock" } ] }
返回:/api/products/imput/body/JSON/include/key/value_Regular.
示例:
{
"request": {
"method": "POST",
"url": "/api/products/imput/body/JSON/include/key/value_Regular",
"bodyPatterns" : [ {
"matchesJsonPath" : "$.things[?(@.name =~ /Required.*/i)]"
} ]
},
"response": {
"status": 200,
"body": "/api/products/imput/body/JSON/include/key/value_Regular.",
"headers":{
"x-token":"xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
}
}
6)Body方式6:
接口:http://localhost:9999/api/products/imput/body/JSON/query_size
Body正确输入:
1:{ "things": [ { "name": "RequiredThing" }, { "name": "Wiremock" } ] }
Body错误输入:
1:{ "things": [ { "name": "RequiredThing" } ] }
返回:/api/products/imput/body/JSON/query_size.
示例:
{
"request": {
"method": "POST",
"url": "/api/products/imput/body/JSON/query_size",
"bodyPatterns" : [ {
"matchesJsonPath" : "$[?(@.things.size() == 2)]"
} ]
},
"response": {
"status": 200,
"body": "/api/products/imput/body/JSON/query_size.",
"headers":{
"x-token":"xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
}
}
7)Body方式7:
接口:http://localhost:9999/api/products/imput/body/expression/include_key
Body正确输入:
1:{"todoItem":{"wash": 42,"washw": 42}}
Body错误输入:
1:{"todoItem":{"washw": 42}}
返回:/api/products/imput/body/expression/include_key.
示例:
{
"request": {
"method": "POST",
"url": "/api/products/imput/body/expression/include_key",
"bodyPatterns" : [ {
"matchesJsonPath" : {
"expression": "$..todoItem",
"contains": "wash"
}
} ]
},
"response": {
"status": 200,
"body": "/api/products/imput/body/expression/include_key.",
"headers":{
"x-token":"xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
}
}
8)Body方式8:
接口:http://localhost:9999/api/products/imput/body/expression/include_key_value
Body正确输入:
{
"outer": {
"inner": 42
}
}
返回:/api/products/imput/body/expression/include_key_value
示例:
{
"request": {
"method": "POST",
"url": "/api/products/imput/body/JSON/include/key/value",
"bodyPatterns" : [ {
"matchesJsonPath" : "$.things[?(@.name == 'RequiredThing')]"
} ]
},
"response": {
"status": 200,
"body": "/api/products/imput/body/JSON/include/key/value.",
"headers":{
"x-token":"xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
}
}
9)Body方式9:
接口:http://localhost:9999/api/products/imput/body/equalToXml
Body正确输入xml:
返回:/api/products/imput/body/equalToXml.
示例:
{
"request": {
"method": "POST",
"url": "/api/products/imput/body/equalToXml",
"bodyPatterns" : [ {
"equalToXml" : "Hello "
} ]
},
"response": {
"status": 200,
"body": "/api/products/imput/body/equalToXml.",
"headers":{
"x-token":"xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
}
}
三、header
1)request中的header
接口:http://localhost:9999/your/headers/key_value?and=query
header传入Key:value:
Content-Type:application/json
name:lifuqing
authCookies:sdasdasdasdsadassdcvdwfwdfsdffdsfsdcfcxcx
返回:/your/headers/key_value?and=query
示例:
{
"request": {
"method": "GET",
"url": "/your/headers/key_value?and=query",
"headers":{
"Content-Type":{"equalTo": "application/json" },
"name":{"equalTo": "lifuqing"},
"authCookies":{"equalTo": "sdasdasdasdsadassdcvdwfwdfsdffdsfsdcfcxcx"}
}
},
"response": {
"status": 200,
"body": "/your/headers/key_value?and=query"
}
}
2)reponse中的header
接口:http://localhost:9999/api/products/return/header
返回:Update successfully.
使用抓包工具查看返回的header信息
示例:
{
"request": {
"method": "GET",
"url": "/api/products/return/header"
},
"response": {
"status": 200,
"body": "Update successfully.",
"headers": {
"Cache-Control":" no-cache, no-store, max-age=0, must-revalidate",
"Date": "Tue, 10 Jul 2018 08:55:44 GMT",
"x-token": " xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
}
}
四、返回值
1)返回字符串,文本
接口:http://localhost:9999/return/body/json/sting
返回:Hello world!
示例:
{
"request": {
"method": "GET",
"url": "/return/body/sting"
},
"response": {
"status": 200,
"body": "Hello world!"
}
}
2)返回文件
返回xml文件:
接口:http://localhost:9999/return/body/bodyFileName/xml
返回:返回myfile.xml文件的内容
示例:
__files:文件夹下放置一个myfile.xml文件,内容如下:
mappings:文件下mock接口:
{
"request": {
"method": "GET",
"url": "/return/body/bodyFileName/xml"
},
"response": {
"status": 200,
"bodyFileName": "myfile.xml"
}
}
效果展示:成功返回了xml文件中的内容
返回json文件:
接口:http://localhost:9999/return/body/bodyFileName/Json
返回:返回GET.JSONl文件的内容
示例:
__files:文件夹下放置一个GET.JSONl文件,内容如下:
{
"request": {
"method": "GET",
"url": "/api/mytest"
},
"response": {
"status": 200,
"body": "More content\n"
}
}
mappings:文件下mock接口:
{
"request": {
"method": "GET",
"url": "/return/body/bodyFileName/Json"
},
"response": {
"status": 200,
"bodyFileName": "GET.json"
}
}
效果展示:
参考:http://wiremock.org/docs/request-matching/