1、源码下载地址
http://search.maven.org/#search%7Cga%7C1%7Cstubby4j
#java jdk 1.7或以上版本
2、配置桩的匹配正则和响应消息
创建文件cfg.yaml(任意目录创建),内容如下:
----------------------
- request:
method: GET
url: /hello-world
response:
status: 200
headers:
content-type: application/json
body: Hello World!
- request:
url: /aaa/bbb/queryCst
method: POST
post:
(.*)6791169999(.*)
response:
status: 200
headers:
content-type: application/json
body: >
<?xml version="1.0" ?>
<response>
<resultCode>0</resultCode>
</response>
----------------------
3、在stubby4j-3.3.0.jar所在目录,启动桩。
----------------------
C:\Users\Suse\Desktop\stub\stubby4j>java -jar stubby4j-3.3.0.jar -d E:\yaml\cfg.yaml
Loaded: [GET] /hello-world
Loaded: [POST] /aaa/bbbb/queryCst
----------------------
启动后,默认的访问端口为8882.
按上面的配置,即为访问:http://127.0.0.1:8882/hello-world,就会响应“Hello World!”。
启动时,如果有端口冲突,可以在命令中指定:
java -jar stubby4j-3.3.0.jar -a 28889 -s 28880 -t 28887 -d E:\yaml\cfg.yaml
其中-s为服务端口。
更多细节,可以执行help获取。
----------------------
C:\Users\Suse\Desktop\stub\stubby4j>java -jar stubby4j-3.3.0.jar --help
usage:
java -jar stubby4j-x.x.xx.jar [-a <arg>] [-d <arg>] [-da] [-ds]
[-h] [-k <arg>] [-l <arg>] [-m] [-o] [-p <arg>] [-s <arg>] [-t
<arg>] [-v] [-w]
-a,--admin <arg> Port for admin portal. Defaults to 8889.
-d,--data <arg> Data file to pre-load endpoints. Valid YAML
1.1 expected.
-da,--disable_admin_portal Does not start Admin portal
-ds,--disable_ssl Does not enable SSL connections
-h,--help This help text.
-k,--keystore <arg> Keystore file for custom TLS. By default TLS
is enabled using internal keystore.
-l,--location <arg> Hostname at which to bind stubby.
-m,--mute Mute console output.
-o,--debug Dumps raw HTTP request to the console (if
console is not muted!).
-p,--password <arg> Password for the provided keystore file.
-s,--stubs <arg> Port for stub portal. Defaults to 8882.
-t,--tls <arg> Port for TLS connection. Defaults to 7443.
-v,--version Prints out to console stubby version.
-w,--watch Periodically scans for changes in last
modification date of the main YAML and
referenced external files (if any). The flag
can accept an optional arg value which is
the watch scan time in milliseconds. If
milliseconds is not provided, the watch
scans every 100ms. If last modification date
changed since the last scan period, the stub
configuration is reloaded
----------------------
二、高级篇
本篇主讲规则正则。
大家在学习这个前,请优先把yaml的语法搞清楚。
规则整体形式:
----------------------
- request:
method:...
headers: ...
url: ...
query:...
post: ...
response:
----------------------
1、request
1)method:
method: GET
method: [GET, POST,PUT,DELETE,HEAD] #字段不写,表示匹配全部方法
2)headers:
headers:
content-type: application/json
x-custom-header: "^this/is/\d/test"
3)url:
url: /
url: ^/hello-world[0-9]{2}$
url: ^($!world).*$ #非world开头
4)query:
query:
type_name: user
client_id: id
#需要特别注意,只支持在URL中(含?后面参数)的查询。
5)post:
method: [POST]
post: ziduan=123 #匹配消息体中的字段
post:
(.*)aaa=123&bbb=222(.*) #匹配消息体中的多个字段
2、Response
1)status: 200
2)headers:
headers:
content-type: application/json
3)body:
body: OK
body: '{"filed1":"value"}'
body: >
<?xml version="1.0" ?>
<response>
<resultCode>0</resultCode>
</response>
4)file:
file: resp1.json
#body和file只能存在一个。
3、动态响应
-------------------
- request:
method: [GET, POST,PUT,DELETE,HEAD]
url: ^/hello-world([0-9]{2})$
response:
status: 200
headers:
content-type: application/json
body: Hello World!<% url.1 %>
-------------------
通过前置的(),被响应<% url.1 %>引用。
以上配置的执行结果为:
-------------------
请求:
POST http://127.0.0.1:8882/hello-world22
响应:
Hello World!22
-------------------
#声明:以上分享参考了如下官方资料:
https://github.com/azagniotov/stubby4j/blob/2d5997299de6247717c2abc58cee51ff63016245/README.md
4、改善空间:
1)-d参数可以修改为目录,支持多个配置文件
2)日志的打印比较简单,可以再详细些
[17:41:32] -> POST [/aaa/bbb/queryCst]
[17:41:32] <- 200 [/aaa/bbb/queryCst] OK
后期有时间再修改
实用例:
vi rule.yaml
---------------------------------
- request:
url: ^/xxx/aaaa$
method: POST
post:
(.*)<version>(.*)</version>(.*)
response_ref: resp.200
vi resp.yaml
---------------------------------
resp.200:
response:
status: 200
headers:
content-type: application/xml
Server-Code: 0
body: >
<?xml version="1.0" encoding="utf-8"?>
<response>
<version><% post.2 %></version>
<status>200</status>
<message>succeed.</message>
</response>