mock服務(wù)
使用前準(zhǔn)備
環(huán)境選擇必須是Mock環(huán)境
非mock環(huán)境 mock服務(wù)是不起作用的。
環(huán)境設(shè)置如下:
路徑:在小眼睛左邊
相對(duì)地址自定義
Mock響應(yīng)數(shù)據(jù)編輯
數(shù)據(jù)編輯框 & 預(yù)覽數(shù)據(jù)框
比如數(shù)據(jù)如下填寫(xiě):
{
"id": "87ef1Dbb-b4eB-549F-EbdF-C69F6369bADB",
"name": "李四",
"phone": "13718425555"
}
固定基本數(shù)據(jù)使用
{
code:1000,
msg:"sucess"
,data:{
"id": "87ef1Dbb-b4eB-549F-EbdF-C69F6369bADB",
"name": "李四",
"phone": "13718425555"
}
}
隨機(jī)動(dòng)態(tài)數(shù)據(jù)
{
"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": "成功"
}
實(shí)際產(chǎn)生的mock數(shù)據(jù)还蹲。剛開(kāi)始看到時(shí)候很驚艷一把哈爹耗!
RESTFUL邏輯動(dòng)態(tài)Mock數(shù)據(jù)響應(yīng)
{
"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服務(wù)的時(shí)候同時(shí)使用斷言
斷言定義如下:
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")');
生成代碼
路徑:接口界面–》生成代碼
生成代碼界面如下圖:
包含的開(kāi)發(fā)語(yǔ)言有:
cURL
Java
PHP
Go
Python
Node
R
Rust
Strest
Dart
MATLAB
Elixir
Ansible
Browser
JsonString
C#
列舉幾個(gè)常用的開(kāi)發(fā)語(yǔ)言
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)