当前位置:网站首页>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 :
边栏推荐
- Notes on Flickr's dataset
- 约瑟夫环问题
- 【Cadence Allegro PCB设计】error: Possible pin type conflict GND/VCC Power Connected to Output
- Is it safe to open a futures account online? How to apply for a low handling fee?
- Thesis reading_ Multi task learning_ MMoE
- [Hardware Engineer] Why do DC-DC isolated switching power modules use transformers?
- 世界各地的标志性建筑物
- 我也是醉了,Eureka 延迟注册还有这个坑!
- Unity 贝塞尔曲线的创建
- 【硬件工程师】关于信号电平驱动能力
猜你喜欢

自动化测试 PO设计模型

「数字安全」警惕 NFT的七大骗局

Beyond convnext, replknet | look 51 × 51 convolution kernel how to break ten thousand volumes!

如何看一本书

Stm32 paj7620u2 gesture recognition module (IIC communication) program source code explanation

OSPF综合实验

Three dimensional function display of gray image
P2P 之 UDP穿透NAT的原理与实现

STM32 PAJ7620U2手势识别模块(IIC通信)程序源码详解

一篇文章了解超声波加湿器
随机推荐
【硬件工程师】元器件选型都不会?
函数名指针和函数指针
[cadence Allegro PCB design] permanently modify the shortcut key (customized) ~ it is valid for personal test~
四六级
Idea 必备插件
[Hardware Engineer] about signal level driving capability
Is there a method in PostgreSQL that only compiles statements but does not execute them?
An article about ultrasonic humidifier
Hcip first day experiment
「数字安全」警惕 NFT的七大骗局
Go language context control function execution timeout return
PageHelper还能结合Lambda表达式实现简洁的分页封装
食品安全 | 八问八答带你重新认识小龙虾!这样吃才对!
go defer与recover简单笔记
简述Synchronized以及锁升级
After consulting about how to deal with DDL in Flink SQL client, how to add fields and jobs to the mapping table in Fink SQL?
Several implementations of PHP to solve concurrency problems
Type assertion of go interface variables
Dating activity records
Go channel simple notes