赞
踩
{
"request":{
"url":"/api/add",
"method":"POST"
},
"response":{
"status":200,
"body":"post body"
}
}
{
"request" : {
"urlPattern" : "/postzpp3",
"method" : "POST"
},
"response" : {
"status" : 200,
"bodyFileName" : "jsonreturn3.json",
"headers" : {
"Content-Type" : "text/json; charset=utf-8",
"Content-Encoding" : "gzip",
"Transfer-Encoding" : "chunked",
"Server" : "Jetty(6.1.26)"
}
}
}

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

{
"request" : {
"urlPattern" : "/mockHelloWorldImplServiceSoapBinding",
"method" : "POST",
"bodyPatterns": [
{ "matches": ".*sayHi.*" }
]
},
"response" : {
"status" : 200,
"bodyFileName" : "body-mockHelloWorldImplServiceSoapBinding-111.json",
"headers" : {
"Content-Type" : "text/xml; charset=utf-8",
"Content-Encoding" : "gzip",
"Transfer-Encoding" : "chunked",
"Server" : "Jetty(6.1.26)"
}
}
}

body-mockHelloWorldImplServiceSoapBinding-111.json: 内容kdlsdls;;sd
</pre>结果:</div><div><img src="https://img-blog.csdn.net/20160113124251728?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /></div><h4>(3)post请求(非json参数,返回json)</h4><div><pre code_snippet_id="1555411" snippet_file_name="blog_20160113_7_9944238" name="code" class="plain">{
"request" : {
"urlPattern" : "/mockpostjsonreturn",
"method" : "POST",
"bodyPatterns": [
{ "matches": ".*sayHi.*" }
]
},
"response" : {
"status" : 200,
"bodyFileName" : "jsonreturn.json",
"headers" : {
"Content-Type" : "text/json; charset=utf-8",
"Content-Encoding" : "gzip",
"Transfer-Encoding" : "chunked",
"Server" : "Jetty(6.1.26)"
}
}
}

jsonreturn.json内容:
{
"request": {
"method": "GET",
"urlPattern": "/api/allparam.*"
},
"response": {
"status": 200,
"bodyFileName": "mytest.json",
"headers": {
"Content-Type": "application/json",
"Cache-Control": "max-age=86400"
}
}
}
{
"request": {
"method": "GET",
"url": "/api/mytest"
},
"response": {
"status": 200,
"bodyFileName": "mytest.json",
"headers": {
"Content-Type": "application/json",
"Cache-Control": "max-age=86400"
}
}
}
step 2: __files文件夹下新建mytest.json内容:
{"retcode": "200","retmsg": "ok","retsign": "12333","rettime": "333333 ","totalcount": "0","data": []}
{
"request": {
"method": "GET",
"urlPattern": "/api/param\\?Action=d&name=.*"
},
"response": {
"status": 200,
"bodyFileName": "mytest.json",
"headers": {
"Content-Type": "application/json",
"Cache-Control": "max-age=86400"
}
}
}
{
"request": {
"method": "GET",
"url": "/api/text"
},
"response": {
"status": 200,
"body": "Hello world!",
"headers": {
"Content-Type": "text/plain"
}
}
}
{
"request": {
"method": "GET",
"url": "/delayed"
},
"response": {
"status": 200,
"bodyFileName": "mytest.json",
"headers": {
"Content-Type": "application/json",
"Cache-Control": "max-age=86400"
},
"fixedDelayMilliseconds": 2000
}
}
用法 | 描述 |
urlEqualTo("/v1/vip/mediaorderstatus") | 固定url /v1/vip/mediaorderstatus |
urlMatching("/v1/vip/mediaorderstatus.*") | 正则表达式,可用的url: /v1/vip/mediaorderstatus.* /v1/vip/mediaorderstatus /v1/vip/mediaorderstatus?a=1 |
urlMatching("/v1/vip/mediaorderstatus/[0-9]+") 注:不可以使用: /v1/vip/mediaorderstatus?a=1.* | 可用的url: /v1/vip/mediaorderstatus/12 /v1/vip/mediaorderstatus/1 /v1/vip/mediaorderstatus/11232344 不可用的url: /v1/vip/mediaorderstatus/ |
{ "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://www.cnblogs.com/tanglang/p/4791198.html | Query参数匹配http://localhost:9999/api/products?search=china注: java中未找到 urlPathMatching |
stubFor(post(urlEqualTo("/with/body")) .withRequestBody(matching("<status>OK</status>")) .withRequestBody(notMatching(".*ERROR.*")) .willReturn(aResponse().withStatus(200))); | put/post请求体匹配(内容匹配) equalTo, matching, notMatching, containing |
1) stubFor(post(urlEqualTo("/with/json/body")) .withRequestBody(equalToJson("{ \"houseNumber\": 4, \"postcode\": \"N1 1ZZ\" }")) .willReturn(aResponse().withStatus(200))); 或者: .withRequestBody(equalToJson("{ \"houseNumber\": 4, \"postcode\": \"N1 1ZZ\" }", LENIENT)) 2) import static com.github.tomakehurst.wiremock.client.WireMock.matchingJsonPath; JSONPath expressions : stubFor(post(urlEqualTo("/with/json/body")) .withRequestBody(matchingJsonPath("$.status")) .withRequestBody(matchingJsonPath("$.things[$(@.name == 'RequiredThing')]")) .willReturn(aResponse().withStatus(201))); | json 匹配 |
1) stubFor(get(urlEqualTo("/whatever")) .willReturn(aResponse() .withStatus(200) .withHeader("Content-Type", "application/json") .withHeader("Cache-Control", "no-cache"))); 2) Request header matching(不是十分必要) stubFor(post(urlEqualTo("/with/headers")) .withHeader("Content-Type", equalTo("text/xml")) .withHeader("Accept", matching("text/.*")) .withHeader("etag", notMatching("abcd.*")) .withHeader("X-Custom-Header", containing("2134")) .willReturn(aResponse().withStatus(200))); | response headers Request header matching |
1) stubFor(get(urlEqualTo("/body")) .willReturn(aResponse() .withBody("Literal text to put in the body"))); 2)来自文件 stubFor(get(urlEqualTo("/body-file")) .willReturn(aResponse() .withBodyFile("path/to/myfile.xml"))); | response body |
In Java: .withRequestBody(equalToXml("<thing>value</thing>")) and in JSON: "bodyPatterns" : [ { "equalToXml" : "<thing>value</thing>" } ] | XML body matching XPath body matching |
- stubFor(get(urlMatching("/v1/vip/mediaorderstatus/[0-9]+"))
- .willReturn(aResponse()
- .withHeader("content-type", "application/json")
- .withHeader("Access-Control-Allow-Origin", "*")
- .withStatus(200)
- .withFixedDelay(5000)// 延迟5s
- .withBody("{\"message\":\"Mini Cooper car response body\", \"success\":true}")
- ));
- stubFor(get(urlMatching("/v1/vip/mediaorderstatus2/[0-9]+"))
- .willReturn(aResponse()
- .withHeader("content-type", "application/json")
- .withHeader("Access-Control-Allow-Origin", "*")
- .withStatus(200)
- .withFixedDelay(0)// 延迟0s
- .withBody("{\"message\":\"Mini Cooper car response body\", \"success\":true}")
- ));

- stubFor(get(urlEqualTo("/fault1"))
- .willReturn(aResponse().withFault(Fault.MALFORMED_RESPONSE_CHUNK)));
- stubFor(get(urlEqualTo("/fault2"))
- .willReturn(aResponse().withFault(Fault.RANDOM_DATA_THEN_CLOSE)));
- stubFor(get(urlEqualTo("/fault3"))
- .willReturn(aResponse().withFault(Fault.EMPTY_RESPONSE)));
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。