终于把基础的请求讲完了,感觉还算顺利,希望读者们也能从中得到一些对自己有帮助的东西。会者不难难者不会,每次二十多分钟很快能把 Demo 讲完,如果想要掌握接口测试,还要多写代码,多实践。
相信一万行代码的理论!
视频专题:
本次分享如何处理header
和cookie
,一般来讲,如果是自动化测试项目,这些数据的处理都是通过测试框架完成的,很少手动测试。这里演示的 Demo,比较适合的就是比较独立的测试任务场景。
gitee 地址:https://gitee.com/fanapi/tester
package com.fun;
import com.alibaba.fastjson.JSONObject;
import com.fun.config.HttpClientConstant;
import com.fun.frame.httpclient.FanLibrary;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.HttpPost;
public class AR extends FanLibrary {
public static String APIKEY;
public static void main(String[] args) {
printHeader();
developerLogin();
// registerUser();
testOver();
}
public static void registerUser() {
if (StringUtils.isEmpty(APIKEY)) developerLogin();
String url = "https://api.apiopen.top/registerUser";
JSONObject param = new JSONObject();
param.put("apikey", APIKEY);
param.put("name", "FunTester0021");
param.put("passwd", "123456");
param.put("nikeName", "FunTester");
param.put("headerImg", "http://pic.automancloud.com/sick-jvm-heap-1.png");
param.put("phone", "13100001111");
param.put("email", "Fhaohaizi@163.com");
param.put("vipGrade", "3");
param.put("autograph", "abc");
param.put("remarks", "这是测试用户!");
HttpPost httpPost = getHttpPost(url, param);
JSONObject response = getHttpResponse(httpPost);
output(response);
}
public static void register() {
String url = "https://api.apiopen.top/developerRegister";
JSONObject param = new JSONObject();
param.put("name", "FunTester");
param.put("passwd", "FunTester");
param.put("email", "Fhaohaizi@163.com");
HttpPost httpPost = getHttpPost(url, param);
JSONObject response = getHttpResponse(httpPost);
output(response);
}
public static void developerLogin() {
String url = "https://api.apiopen.top/developerLogin";
JSONObject params = new JSONObject();
params.put("name", "funtester");
params.put("passwd", "funtester");
HttpPost httpPost = getHttpPost(url, params);
httpPost.addHeader(HttpClientConstant.X_Requested_KWith);
httpPost.addHeader(getHeader("name", "FunTester"));
// JSONObject cookie = new JSONObject();
// cookie.put("name", "FunTester");
// cookie.put("pwd", "FunTester");
// cookie.put("age", "22");
// Header cookies = getCookies(cookie);
String dd = "_zap=3f36e41c-ea6e-4436-892e-3509be0a60be; _xsrf=0LCDSGzBzIxS6kWPEmf94J8KtpfmFti1; ISSW=1; d_c0=\"AADZkMsmABGPTqeAzeeGiltFGWN_rrYKq6s=|1584847946\"; _ga=GA1.2.910125274.1584847946; _gid=GA1.2.455967142.1584847946; capsion_ticket=\"2|1:0|10:1584847947|14:capsion_ticket|44:M2IwNmRhNmQ1MGViNGQ3Y2E4MDU0OTYwZjQ5ZDU5NDA=|33ef00769fdd6874a735867c43b2a0d0265c1ec8f9e106c2eb0d4805c64e4c8f\"; z_c0=\"2|1:0|10:1584847951|4:z_c0|92:Mi4xbDBITUFRQUFBQUFBQU5tUXl5WUFFU1lBQUFCZ0FsVk5UeXBrWHdCbFMxOGNwajI0QXgzMldCYTNqaGd3NmpkTlh3|915961266a0495f8ad137d24f81a0fe1020b8712019d6d8d355e4f9d65159868\"; tshl=; tst=r; q_c1=f36a39fa82744e4992cd32c16194783d|1584879682000|1584879682000; Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49=1584847946,1584945282; _gat_gtag_UA_149949619_1=1; Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49=1584953227; KLBRSID=9d75f80756f65c61b0a50d80b4ca9b13|1584953228|1584953191";
// httpPost.addHeader(cookies);
httpPost.addHeader(HttpClientConstant.COOKIE, dd);
JSONObject response = getHttpResponse(httpPost);
output(response);
if (response.getIntValue("code") == 200) {
APIKEY = response.getJSONObject("result").getString("apikey");
} else {
fail();
}
}
}