Query参数匹配:http://localhost:9999/api/products?search=china
{ "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)\" }" } } |
返回文件内容:http://localhost:9999/file/1
{
"request": {
"method": "GET",
"url": "/file/1"
},
"response": {
"status": 200,
"bodyFileName": "test.xml"(或:”xmlfiles/test.xml”)
}
}
(在__files文件夹下建好所调文件,路径为相对__files文件夹。)
模拟错误
{ "request" : { "url" : "/unknown.html", "method" : "GET" }, "response" : { "status" : 404, "headers" : { "Content-Type" : "text/html; charset=utf-8" } } } { "request": { "method": "GET", "url": "/fault" }, "response": { "fault": "MALFORMED_RESPONSE_CHUNK" } } |
录制HTTP请求及回放
Wiremock的录制过程是启动一个代理服务,截取HTTP请求和响应,在mappings文件夹中创建一json文件记录下请求地址和响应概要,在__files文件夹下创建一文件包含响应内容;当重启Standalone进程时,那些记录下的请求响应就会作为Mock Service生成。
启动录制服务:
java -jar wiremock-1.57-standalone.jar --proxy-all="http://localhost:7777" --record-mappings –verbose
默认的代理服务端口是8080,即之后发向http://localhost:7777的请求,可以用http://localhst:8080/来代理。
生成mapping文件和响应内容文件:
{ "request" : { "url" : "/test.aspx", "method" : "GET" }, "response" : { "status" : 200, "bodyFileName" : "body-test.aspx-WK0fD.json", "headers" : { "Cache-Control" : "private", "Content-Type" : "text/html; charset=utf-8", "Server" : "Microsoft-IIS/7.5", "X-AspNet-Version" : "2.0.50727", "X-Powered-By" : "ASP.NET", "Date" : "Tue, 08 Sep 2015 03:14:36 GMT", "Content-Length" : "61" } } } |
录制完重启服务,验证刚才录制的请求是否生效: