mock服务
使用前准备
环境选择必须是Mock环境
非mock环境 mock服务是不起作用的。
环境设置如下:
路径:在小眼睛左边
相对地址自定义
Mock响应数据编辑
数据编辑框 & 预览数据框
比如数据如下填写:
{
"id": "87ef1Dbb-b4eB-549F-EbdF-C69F6369bADB",
"name": "李四",
"phone": "13718425555"
}
固定基本数据使用
{
code:1000,
msg:"sucess"
,data:{
"id": "87ef1Dbb-b4eB-549F-EbdF-C69F6369bADB",
"name": "李四",
"phone": "13718425555"
}
}
随机动态数据
{
"code": "0",
"data": {
"list|10": [{
"name": "@name",
"age": "@integer(2)"
}],
"url": "https://echo.apipost.cn"
},
"desc": "成功"
}
{
"code": "0",
"data": {
"list": [
{
"name": "Susan Williams",
"age": 4202047804417013
},
{
"name": "Cynthia Martinez",
"age": 7836460292479682
},
{
"name": "Joseph Rodriguez",
"age": 7904378098119320
},
{
"name": "Susan Perez",
"age": 2839392309833828
},
{
"name": "Scott White",
"age": 8529217801206224
},
{
"name": "David Allen",
"age": 3507736869454853
},
{
"name": "Jennifer Brown",
"age": 186237858137008
},
{
"name": "Margaret Anderson",
"age": 7679312547559736
},
{
"name": "Brenda Lee",
"age": 105486912822656
},
{
"name": "Brian Jackson",
"age": 1856742232963432
}
],
"url": "https://echo.apipost.cn"
},
"desc": "成功"
}
实际产生的mock数据。刚开始看到时候很惊艳一把哈!
RESTFUL逻辑动态Mock数据响应
{
"code": "0000",
"data": {
"verifySuccess": function() {
let body = _req.body;
console.log(body);
return body.phone === '13718425555' && body.pwd === '123456';
},
"userInfo": function() {
let body = _req.body;
if (body.phone === '13718425555' && body.pwd === '123456') {
return Mock.mock({
"id": "87ef1Dbb-b4eB-549F-EbdF-C69F6369bADB",
"name": "李四",
"phone": "13718425555"
});
} else {
return null;
}
},
},
"desc": "成功"
}
使用Mock服务的时候同时使用断言
断言定义如下:
apt.assert('response.raw.responseTime>=50');
apt.assert('response.raw.type=="json"');
apt.assert('response.json.hasOwnProperty("code")');
apt.assert('response.json.hasOwnProperty("msg")');
apt.assert('response.json.hasOwnProperty("data")');
生成代码
路径:接口界面–》生成代码
生成代码界面如下图:
包含的开发语言有:
cURL
Java
PHP
Go
Python
Node
R
Rust
Strest
Dart
MATLAB
Elixir
Ansible
Browser
JsonString
C#
列举几个常用的开发语言
c#
// Generated by ApiPost: https://www.apipost.cn/
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("Get"), "https://console-mock.apipost.cn/app/mock/project/e506875e-055f-452a-c781-e00e05f2f9f9/getUserInfo"))
{
request.Headers.TryAddWithoutValidation("User-Agent", "Apipost client Runtime/+https://www.apipost.cn/");
request.Headers.TryAddWithoutValidation("Content-Type", "application/json");
var response = await httpClient.SendAsync(request);
}
}
Java
// Generated by ApiPost: https://www.apipost.cn/
Request request = Request.Get("https://console-mock.apipost.cn/app/mock/project/e506875e-055f-452a-c781-e00e05f2f9f9/getUserInfo");
String body = "'";
request.bodyString(body,ContentType.APPLICATION_JSON);
request.setHeader("User-Agent", "Apipost client Runtime/+https://www.apipost.cn/");
request.setHeader("Content-Type", "application/json");
HttpResponse httpResponse = request.execute().returnResponse();
System.out.println(httpResponse.getStatusLine());
if (httpResponse.getEntity() != null) {
String html = EntityUtils.toString(httpResponse.getEntity());
System.out.println(html);
}
Python:
import requests
headers = {
'User-Agent': 'Apipost client Runtime/+https://www.apipost.cn/',
'Content-Type': 'application/json',
}
response = requests.post('https://console-mock.apipost.cn/app/mock/project/e506875e-055f-452a-c781-e00e05f2f9f9/getUserInfo', headers=headers)