(1)运行mockserver
docker pull mockserver/mockserver:latest
services:
mockserver:
image: mockserver/mockserver:latest
ports:
- "1081:1080"
networks:
- other
networks:
other: {}
PS E:\docker\other> docker-compose up mockserver -d
[+] Running 1/1
- Container other-mockserver-1 Started 2.5s
PS E:\docker\other>
(2)配置模拟请求与响应
请求地址
http://127.0.0.1:1081/mockserver/expectation
请求方式
PUT
请求体
{
"httpRequest": {
"method": "GET",
"path": "/api/user"
},
"httpResponse": {
"statusCode": 200,
"body": {
"id": 1,
"name": "John Doe"
},
"headers": {
"Content-Type": [
"application/json"
]
}
}
}
响应体
[
{
"httpRequest": {
"method": "GET",
"path": "/api/user"
},
"httpResponse": {
"statusCode": 200,
"headers": {
"Content-Type": [
"application/json"
]
},
"body": {
"id": 1,
"name": "John Doe"
}
},
"id": "fa55ccc3-60f5-4cd1-94c8-3e312add4319",
"priority": 0,
"timeToLive": {
"unlimited": true
},
"times": {
"unlimited": true
}
}
]
(3)测试
请求地址
http://127.0.0.1:1081/api/user
请求方式
GET
响应体
{
"id": 1,
"name": "John Doe"
}
(4)重置所有配置
请求地址
http://127.0.0.1:1081/mockserver/reset
请求方式
PUT