前后端接口分离-wiremock
来源:互联网 发布:ido视频软件 下载 编辑:程序博客网 时间:2024/05/17 03:35
1. wiremock 使用方式
方式一: json配置
方式二:引入jar包
2. json配置使用wiremock
post请求
(1) post请求(无参数)
使用步骤:
下载wiremock-1.48-standalone.jar 包,按如下步骤启动端口:
step 1: mappings 文件夹中新建add.json,内容:
{"request":{"url":"/api/add","method":"POST"},"response":{"status":200,"body":"post body"}}
cd 到 上述jar包的目录
java -jar wiremock-1.48-standalone.jar
step 3: 发送post请求,验证是否mock成功(使用fiddler)
mock url : http://127.0.0.1:8080/api/add
不在意请求的参数时:post请求(urlPattern)
{ "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)" } }}
(2)post请求(含json参数)
{ "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" } }}
结果:
(2)post请求(非json参数)
{ "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="http://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内容:
{"code":"1001","msg":"invalid app_code"}
结果:
(4)post请求()
get 请求
(0)get请求(任意参数,json返回)
{ "request": { "method": "GET", "urlPattern": "/api/allparam.*" }, "response": { "status": 200, "bodyFileName": "mytest.json", "headers": { "Content-Type": "application/json", "Cache-Control": "max-age=86400" } }}
http://127.0.0.1:8080/api/allparam
http://127.0.0.1:8080/api/allparam?a=3
注意:如果将"urlPattern": "/api/allparam.*" 改成 "urlPattern": "/api/allparam", 则访问的url必须为:
http://127.0.0.1:8080/api/allparam
(1)get请求(url无参数,json返回)
step 1: mappings 文件夹中新建.json,内容:
{ "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": []}
浏览器中直接输入http://127.0.0.1:8080/api/mytest 此链接即可。
(2)get请求(url含参数,json返回)
{ "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" } }}
(3)get请求(url无参数,非json返回)
{ "request": { "method": "GET", "url": "/api/text" }, "response": { "status": 200, "body": "Hello world!", "headers": { "Content-Type": "text/plain" }}}
delay get请求(url无参数,json返回)
{ "request": { "method": "GET", "url": "/delayed" }, "response": { "status": 200, "bodyFileName": "mytest.json", "headers": { "Content-Type": "application/json", "Cache-Control": "max-age=86400" }, "fixedDelayMilliseconds": 2000 }}
3. wiremock进阶
wiremock 手册
http://wiremock.org/stubbing.html
wiremock源码
https://github.com/tomakehurst/wiremock
用法描述
/v1/vip/mediaorderstatusurlMatching("/v1/vip/mediaorderstatus.*")正则表达式,可用的url:
/v1/vip/mediaorderstatus.*
/v1/vip/mediaorderstatus
/v1/vip/mediaorderstatus?a=1urlMatching("/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
注: java中未找到
.withRequestBody(matching("<status>OK</status>"))
.withRequestBody(notMatching(".*ERROR.*"))
.willReturn(aResponse().withStatus(200)));put/post请求体匹配(内容匹配)
equalTo, matching, notMatching, containing1)
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 matching1)
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 bodyIn Java:
.withRequestBody(equalToXml("<thing>value</thing>"))
and in JSON:
"bodyPatterns" : [
{ "equalToXml" : "<thing>value</thing>" }
]XML body matching
XPath body matching
urlEqualTo("/v1/vip/mediaorderstatus")固定url
/v1/vip/mediaorderstatusurlMatching("/v1/vip/mediaorderstatus.*")正则表达式,可用的url:
/v1/vip/mediaorderstatus.*
/v1/vip/mediaorderstatus
/v1/vip/mediaorderstatus?a=1urlMatching("/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, containing1)
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 matching1)
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 bodyIn Java:
.withRequestBody(equalToXml("<thing>value</thing>"))
and in JSON:
"bodyPatterns" : [
{ "equalToXml" : "<thing>value</thing>" }
]XML body matching
XPath body matching
设置延迟
方法一: 全局性设置延迟
WireMock.setGlobalFixedDelay()
方法二: 按接口设置延迟( milliseconds)
stubFor(get(urlEqualTo("/delayed")).willReturn(
aResponse()
.withStatus(200)
.withFixedDelay(2000)));
aResponse()
.withStatus(200)
.withFixedDelay(2000)));
例子:
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}") ));
Bad responses
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)));
分别返回:
HTTP/1.1 200 OK
Transfer-Encoding: chunked
Server: Jetty(6.1.x)
lskdu018973t09sylgasjkfg1][]'./.sdlv
Transfer-Encoding: chunked
Server: Jetty(6.1.x)
lskdu018973t09sylgasjkfg1][]'./.sdlv
————————————————————————
HTTP/1.0 200 This buggy server did not return headers
lskdu018973t09sylgasjkfg1][]'./.sdlv
lskdu018973t09sylgasjkfg1][]'./.sdlv
————————————————————————
HTTP/1.1 504 Fiddler - Receive Failure
Date: Mon, 25 Jan 2016 08:45:18 GMT
Content-Type: text/html; charset=UTF-8
Connection: close
Cache-Control: no-cache, must-revalidate
Timestamp: 16:45:18.677
[Fiddler] ReadResponse() failed: The server did not return a complete response for this request. Server returned 0 bytes.
Date: Mon, 25 Jan 2016 08:45:18 GMT
Content-Type: text/html; charset=UTF-8
Connection: close
Cache-Control: no-cache, must-revalidate
Timestamp: 16:45:18.677
[Fiddler] ReadResponse() failed: The server did not return a complete response for this request. Server returned 0 bytes.
0 0
- 前后端接口分离-wiremock
- 前后端分离架构中的接口设计
- 可视化接口管理工具RAP,前后端分离,
- 前后端分离架构中的接口设计
- web前后端分离
- 前后端分离
- 前后端分离问题
- 前后端分离
- 前后端分离方案
- 前后端分离(一)
- 前后端分离(二)
- 前后端分离(三)
- 前后端分离实践
- 前后端完全分离
- 前后端分离案例
- 谈谈前后端分离
- 前后端分离开发
- 前后端分离势在必行!
- 命令行编译Android项目的工具类
- JVM学习笔记 -- 从一段几乎所有人代码都会犯错的代码开始
- unity 在菜单中添加 复制文件菜单项(简易版)
- 理解MySQL——索引
- SQLServer :判断是否启用即时文件初始化
- 前后端接口分离-wiremock
- Android通知之Notification的多种用法
- Eclipse中如何添加Junit jar包
- 关于重入函数的资料总结
- redis复制
- i2c总线时序
- 把Eclipse自带的浏览器修改为自己习惯的浏览器
- 是否应该立即将网站升级到Drupal 8?
- 【转】用gitlabCI快速搭建一个GitServer与CI