当前位置:网站首页>Mock service Moco series (III) - redirection, regular expression, delay, template, event, sub module design
Mock service Moco series (III) - redirection, regular expression, delay, template, event, sub module design
2022-07-25 17:50:00 【wangmcn】
Mock service moco series ( 3、 ... and )
Redirect 、 Regular expressions 、 Delay 、 Templates 、 event 、 Modular design
Catalog
- 1、 Redirect
- 2、 Regular expressions
- 3、 Delay
- 4、 Templates
- 5、 event
- 5.1、 The whole event
- 5.2、 Asynchronous events
- 6、 Modular design
1、 Redirect
moco Can help us simulate redirection , Redirect the request to a different one url, You can simulate some request interception 、 Failure of request verification, etc .
One 、 Redirect to Baidu
1、 establish 09Redirect.json The configuration file .
redirectTo: Redirect the request to the specified url On .
The contents are as follows :
[
{
"description":" Redirect to Baidu ",
"request":{
"uri":"/redirect"
},
"redirectTo":"https://www.baidu.com/"
}
]2、 Input start moco The service command .
java -jar moco-runner-0.12.0-standalone.jar http -p 8083 -c 09Redirect.json
3、 Browser access moco Service address .
Access address :http://localhost:8083/redirect
The access results show : visit http://localhost:8083/redirect Jump to https://www.baidu.com/ page .
Two 、 Redirect to custom web pages
1、 establish 10Redirect2.json The configuration file .
The configuration file has 2 Interface ( Redirecting Requests 、 Request redirected to ).
redirectTo: Redirect the request to the specified url On .
The contents are as follows :
[
{
"description":" Redirect to custom web pages ",
"request":{
"uri":"/redirect/topath"
},
"redirectTo":"/redirect/new"
},
{
"description":" Request redirected to ",
"request":{
"uri":"/redirect/new"
},
"response":{
"headers":{
"Content-Type":"text/html;charset=gbk"
},
"text":" Redirection succeeded "
}
}
]2、 Input start moco The service command .
java -jar moco-runner-0.12.0-standalone.jar http -p 8083 -c 10Redirect2.json
3、 Browser access moco Service address .
Access address :http://localhost:8083/redirect/topath
The access results show :
visit http://localhost:8083/redirect/topath Jump to http://localhost:8083/redirect/new page .
2、 Regular expressions
1、 establish 11Match.json The configuration file .
match: Regular expressions .
\\w Match the letter 、 Numbers 、 Underlined characters .
* Represents any number of characters .
\\w* Denotes any number of \\w
The contents are as follows :
[
{
"description":" Regular expressions ",
"request":{
"uri":{
"match": "/\\w*/demo"
}
},
"response":{
"text": "Moco Match"
}
}
]2、 Input start moco The service command .
java -jar moco-runner-0.12.0-standalone.jar http -p 8083 -c 11Match.json
3、 Browser access moco Service address .
Because the request address uses regular expressions , The following addresses can be accessed :
http://localhost:8083/123/demo
http://localhost:8083/abc/demo
http://localhost:8083/123abc/demo
The access results show :
moco By Java Regular expression implementation , You can see more details here .
https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
3、 Delay
Sometimes , We need a delay to simulate slow server-side operations .
1、 establish 12Latency.json The configuration file .
latency: Delay . For example, set delay 5 second :
"latency":{
"duration": 5,
"unit": "second"
}The contents are as follows :
[
{
"description":" Delay ",
"request":{
"uri":"/demo",
"method":"get"
},
"response":{
"latency":{
"duration": 5,
"unit": "second"
},
"text":"Moco Latency"
}
}
]2、 Input start moco The service command .
java -jar moco-runner-0.12.0-standalone.jar http -p 8083 -c 12Latency.json
3、 Browser access moco Service address .
Access address :http://localhost:8083/demo
The access results show : Delay 5 Seconds later , Return the response content .
4、 Templates
Sometimes , We need to customize our response based on some content , For example, the response should have the same header as the request . This can be achieved through templates : You can use... In templates req Get request information .
Method :
"template": "${req.version}"
"template": "${req.method}"
"template": "${req.content}"
"template": "${req.headers['foo']}"
"template": "${req.queries['foo']}"
"template": "${req.forms['foo']}"
"template": "${req.cookies['foo']}"
"template": "${req.json.foo}"1、 establish 13Template.json The configuration file .
template: Templates .
for example : Get the request parameter field value in the response content .
"template": "${req.queries['username']}"
The contents are as follows :
[
{
"description":" Templates ",
"request":{
"uri":"/demo",
"method":"get"
},
"response":{
"text": {
"template": "${req.queries['username']}"
}
}
}
]2、 Input start moco The service command .
java -jar moco-runner-0.12.0-standalone.jar http -p 8083 -c 13Template.json
3、 Browser access moco Service address .
Access address :http://localhost:8083/demo?username=admin
The access results show : Response content to get request parameters username Value (admin).
5、 event
5.1、 The whole event
Sometimes , When we request some specific interfaces , You may need to ask for another address , In order to complete the request .
The complete event will be triggered after your request is fully processed .
One 、Get request
1、 establish 14Complete.json The configuration file .
The contents are as follows :
[
{
"description":" The whole event (Get request )",
"request":{
"uri":"/event",
"method":"get"
},
"response": {
"text": "event"
},
"on": {
"complete": {
"get" : {
"url":"https://www.baidu.com/"
}
}
}
}
]2、 Input start moco The service command .
java -jar moco-runner-0.12.0-standalone.jar http -p 8083 -c 14Complete.json
3、 Browser access moco Service address .
Access address :http://localhost:8083/event
The access results show :
Two 、Post request
1、 establish 15Complete2.json The configuration file .
The contents are as follows :
[
{
"description":" The whole event (Post request )",
"request":{
"uri":"/event",
"method":"get"
},
"response":{
"text":"event"
},
"on":{
"complete":{
"post":{
"url":"https://www.baidu.com/",
"content": "content"
}
}
}
}
]2、 Input start moco The service command .
java -jar moco-runner-0.12.0-standalone.jar http -p 8083 -c 15Complete2.json
3、 Browser access moco Service address .
Access address :http://localhost:8083/event
The access results show :
5.2、 Asynchronous events
The previous requests are synchronized by default , This means that it will not be returned to the client until the server finishes processing . Of course, you can also make asynchronous requests , You can also specify the waiting time for this asynchronous request .
1、 establish 16Asynchronous.json The configuration file .
"async":"true" Start asynchronous request .
"latency":5000 Set the waiting time for this asynchronous request .
The contents are as follows :
[
{
"description":" Asynchronous events ",
"request":{
"uri":"/event",
"method":"get"
},
"response":{
"text":"event"
},
"on":{
"complete":{
"async":"true",
"latency":5000,
"post":{
"url":"https://www.baidu.com/",
"content":"content"
}
}
}
}
]2、 Input start moco The service command .
java -jar moco-runner-0.12.0-standalone.jar http -p 8083 -c 16Asynchronous.json
3、 Browser access moco Service address .
Access address :http://localhost:8083/event
The access results show :
6、 Modular design
In practice , Each business module has its own business ( A lot of requests ), If these different business modules are written in a configuration file , Obviously, it's not very easy to manage . Here we use the idea of sub module design to solve this problem .
1、 create profile .
As shown in the figure :
Global.json For global profiles ( Appoint A Business module 、B Business module ),moco Support the introduction of other configuration files in the global configuration file , In this way, the configuration file can be defined according to different businesses , Easy to manage .
A.json by A Business module configuration file .
B.json by B Business module configuration file .
Global.json The contents are as follows :
[
{ "context":"/a", "include":"A.json" },
{ "context":"/b", "include":"B.json" }
]A.json The contents are as follows :
[
{
"description":" modular A",
"request":{
"uri":"/demoa"
},
"response":{
"text":"A Module"
}
}
]B.json The contents are as follows :
[
{
"description":" modular B",
"request":{
"uri":"/demob"
},
"response":{
"text":"B Module"
}
}
]2、 Input start moco The service command .
java -jar moco-runner-0.12.0-standalone.jar http -p 8083 -g Global.json
Be careful , At this time, you need to pass the parameter -g To load the global configuration file , Instead of using -c.
3、 Browser access moco Service address .
(1) visit A Business module
Access address :http://localhost:8083/a/demoa
The access results show :
(2) visit B Business module
Access address :http://localhost:8083/b/demob
The access results show :
边栏推荐
猜你喜欢

Automated test Po design model

2022/7/23

Food safety | eight questions and eight answers take you to know crayfish again! This is the right way to eat!

PageHelper还能结合Lambda表达式实现简洁的分页封装
![[Hardware Engineer] can't select components?](/img/bd/fdf62b85c082f7e51bf44737f1f787.png)
[Hardware Engineer] can't select components?

OSPF --- open shortest priority path protocol

食品安全 | 八问八答带你重新认识小龙虾!这样吃才对!

WPF 实现用户头像选择器

面试官:说说 log.Fatal 和 panic 的区别

Redis source code and design analysis -- 17. Redis event processing
随机推荐
Function name pointer and function pointer
栈的顺序存储结构,链式存储结构及实现
UFT(QTP)-总结点与自动化测试框架
Step by step introduction of sqlsugar based development framework (13) -- package the upload component based on elementplus, which is convenient for the project
多项式相加
Dating activity records
Trooper
What is an IP SSL certificate and how to apply for it?
简述Synchronized以及锁升级
MySQL数据库常用命令
Nineteen year old summary
I want to manage money. I don't understand. Is there a principal guaranteed financial product?
MySQL数据库中去重与连接查询的方法
An article about ultrasonic humidifier
EDI 对接CommerceHub OrderStream
Postman快速上手
直击考点:PMP考试中常见敏捷知识点汇总
PageHelper can also be combined with lambda expressions to achieve concise paging encapsulation
咨询下flink sql-client怎么处理DDL后,fink sql里面映射表加字段以及JOb?
WPF 实现用户头像选择器