实现思路
(一)将pos、get、patch等接口的调用封装成一个httpUtil工具类
(二)用例设计在excel中,解析excel文件,返回一个二维数据,作为数据提供者
(三)testng的test方法写单个接口批量测试,引用@dataProvide注解,达到批量测试的目的。
httpUtil工具类
package com.lemon.apiAuto.Util;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.alibaba.fastjson.JSON;
import com.lemon.apiAuto.constants.Constants;
public class HttpUtil {
/**
* 无参数的get请求
*
* @param uri 接口地址
*/
public static String doGet(String uri) {
try {
// * 1、创建request连接.
HttpGet get = new HttpGet(uri);
// 设置请求头
get.setHeader(Constants.HEADER_MEDIA_TYPE_NAME, Constants.HEADER_MEDIA_TYPE_VALUE);
// 创建一个客户端
HttpClient client = HttpClients.createDefault();
// 发送请求,并接受响应
HttpResponse response = client.execute(get);
// 获取接口响应封装到一个方法中,方便调用,返回body
return getResult(response);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 带参数的get请求,参数为map
*
* @param uri 接口地址
* @param map 接口参数
*/
public static String doGet(String uri, Map<String, String> mapParams) {
try {
// 在uri上拼接参数2.那么一个完整的url地址,基本格式如下:
// https://host:port/path?xxx=aaa&ooo=bbb
Set<String> keySet = mapParams.keySet();
int i = 0;
for (String key : keySet) {
if (i == 0) {
uri = uri + "?" + key + "=" + mapParams.get(key);
} else {
uri = uri + "&" + key + "=" + mapParams.get(key);
}
i++;
}
// 创建一个get请求
HttpGet get = new HttpGet(uri);
// 设置请求头
get.addHeader(Constants.HEADER_MEDIA_TYPE_NAME, Constants.HEADER_MEDIA_TYPE_VALUE);
get.addHeader(Constants.CONTENT_TYPE, Constants.CONTENT_TYPE_FORM);
// 创建一个客户端
HttpClient client = HttpClients.createDefault();
// 客户端发送请求,接受响应
HttpResponse response = client.execute(get);
return getResult(response);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* get请求,参数为 key=value&key=value 格式
*
* @param uri
* @param params
*/
public static String doGet(String uri, String params) {
uri = uri + "?" + params;
return doGet(uri);
}
/**
* post提交数据方式:application/x-www-form-urlencoded
*
* @param uri
* @param params 为json格式
* @param isAuthorization
*/
public static String doFormPost(String uri, String params, boolean isAuthorization) {
try {
// 创建post请求
HttpPost post = new HttpPost(uri);
// 添加请求头
post.addHeader(Constants.HEADER_MEDIA_TYPE_NAME, Constants.HEADER_MEDIA_TYPE_VALUE);
post.addHeader(Constants.CONTENT_TYPE, Constants.CONTENT_TYPE_FORM);
// 如果需要鉴权,添加请求头
if (isAuthorization == true) {
AuthorizationUtils.setTokenInRequest(post);
}
// 将json字符串转换成key-value的格式
if (params.contains("{")) {
params = json2keyValue(params);
System.out.println(params);
}
// System.out.println(params);
// 设置body
post.setEntity(new StringEntity(params));
// 创建客户端
HttpClient client = HttpClients.createDefault();
// 发送请求
HttpResponse response = client.execute(post);
return getResult(response);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* json字符串转换成key-value
*
* @param params
* @return
*/
public static String json2keyValue(String params) {
String result = "";
try {
// fson字符串转换成map,再组装
Map<Object, Object> map = JSON.parseObject(params, Map.class);
Set<Object> keySet = map.keySet();
for (Object key : keySet) {
String value = (String) map.get(key);
if (result.length() > 0) {
result += "&";
}
result += key + "=" + value;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* post提交数据方式:application/json
*
* @param uri
* @param params application/json
* @param isAuthorization 是否需要鉴权
*/
public static String doPost(String uri, String params, boolean isAuthorization) {
try {
// 创建post请求,用父类接受子类
HttpPost post = new HttpPost(uri);
// 添加请求头
post.addHeader(Constants.HEADER_MEDIA_TYPE_NAME, Constants.HEADER_MEDIA_TYPE_VALUE);
post.addHeader(Constants.CONTENT_TYPE, Constants.CONTENT_TYPE_JSON);
if (isAuthorization == true) {
AuthorizationUtils.setTokenInRequest(post);
}
// 设置body
post.setEntity(new StringEntity(params, "utf-8"));
// 创建客户端
HttpClient client = HttpClients.createDefault();
// 发送请求
HttpResponse response = client.execute(post);
return getResult(response);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 默认不需要鉴权
* @param uri 接口地址
* @param params 接口参数
* @return
*/
public static String doPost(String uri, String params) {
return doPost(uri, params, false);
}
/**
* patch请求
*
* @param uri 接口地址
* @param params 接口参数
* @param isAuthorization 是否需要鉴权
* @return
*/
public static String doPatch(String uri, String params, boolean isAuthorization) {
// 创建patch请求
HttpPatch patch = new HttpPatch(uri);
// 设置header
patch.setHeader(Constants.HEADER_MEDIA_TYPE_NAME, Constants.HEADER_MEDIA_TYPE_VALUE);
patch.setHeader(Constants.CONTENT_TYPE, Constants.CONTENT_TYPE_JSON);
// 如果需要鉴权
if (isAuthorization == true) {
// 请求头添加token
AuthorizationUtils.setTokenInRequest(patch);
}
// 设置参数
patch.setEntity(new StringEntity(params, "utf-8"));
// 创建客户端
HttpClient client = HttpClients.createDefault();
try {
HttpResponse response = client.execute(patch);
return getResult(response);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 获取响应结果
*
* @param response
* @return
* @throws IOException
*/
public static String getResult(HttpResponse response) throws IOException {
// 获取状态码
int code = response.getStatusLine().getStatusCode();
System.out.println(code);
// 获取body
HttpEntity entity = response.getEntity();
String body = EntityUtils.toString(entity);
System.out.println(body);
// 获取头信息
Header[] allHeaders = response.getAllHeaders();
String headers = Arrays.toString(allHeaders);
System.out.println(headers);
// 返回body
return body;
}
/**
* 调用接口请求,默认是否鉴权为 false ,不需要鉴权
*
* @param uri 接口地址
* @param type 请求方式
* @param params 参数
* @param contentType 参数请求类型
* @return
*/
public static String call(String uri, String type, String params, String contentType) {
return call(uri, type, params, contentType, false);
}
/**
* 调用接口请求
*
* @param uri 接口地址
* @param type 接口类型
* @param params 参数
* @param contentType 参数请求类型
*/
public static String call(String uri, String type, String params, String contentType, boolean isAuthorization) {
try {
// 如果请求方式为get
if ("get".equalsIgnoreCase(type)) {
// 判断参数是否为空,选择不同的请求方法
if (params != null && params.length() > 0) {
return doGet(uri, params);
} else {
return doGet(uri);
}
} else if ("post".equalsIgnoreCase(type)) {
if ("json".equals(contentType)) {
// 调用json格式的post'请求
return doPost(uri, params, isAuthorization);
} else if ("form".equals(contentType)) {
// 调用form格式的post'请求
return doFormPost(uri, params, isAuthorization);
} else {
System.out.println("content-type 不正确,无法调用接口请求");
}
} else if ("PATCH".equalsIgnoreCase(type)) {
// 调用patch请求
return doPatch(uri, params, isAuthorization);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}