目录
前言
描述
封装RestAssuredUtils
public class RestAssuredUtils {
public static Logger log = LoggerFactory.getLogger(RestAssuredUtils.class);
public static String webApiHost = strWebApiHost;
public static String webApiPort = strWebApiPort;
public static String openApiHost = strOpenApiHost;
public static String openApiPort = strOpenApiPort
public static RequestSpecification requestSpe;
//openAPI 发送doPost请求
public static Response doPost(String url, String body, String user){
Map<String, Object> headers = null;
//构造一个RequestSpecification
RestAssured.baseURI= openApiHost;
if(openApiPort!=""){
RestAssured.port = Integer.valueOf(openApiPort).intValue();
}
RestAssured.basePath = url;
requestSpe = given();
if(user != null){
String apiKey = accountMap.get(user + "API_KEY");
String secretKey = accountMap.get(user + "SECRET_KEY");
String passPhrase = accountMap.get(user + "PASSPHRASE");
//签名所需要的参数
String queryString = "";
String path = url;
String unixTime = OpenUtils.getUnixTime();
//处理签名
String sign = OpenUtils.sign("POST", unixTime, path, queryString, body, secretKey);
//构造header, 包含签名信息
headers = OpenUtils.buildV3Headers(apiKey, sign, unixTime, passPhrase);
}
//通过requestSpe发送请求
Response rs = requestSpe.
contentType("application/json; charset=UTF-8").
headers(headers).
body(body).
when().
post("");
if(rs.getStatusCode() != 200)
log.info("返回报文: " + rs.asString());
return rs;
}
public static Response doPost(String url, Map<String, Object> body, String user){
Map<String, Object> headers = null;
//构造一个RequestSpecification
RestAssured.baseURI= openApiHost;
if(openApiPort!=""){
RestAssured.port = Integer.valueOf(openApiPort).intValue();
}
RestAssured.basePath = url;
requestSpe = given();
if(user != null){
String apiKey = accountMap.get(user + "API_KEY");
String secretKey = accountMap.get(user + "SECRET_KEY");
String passPhrase = accountMap.get(user + "PASSPHRASE");
//签名所需要的参数
String queryString = "";
String path = url;
String unixTime = OpenUtils.getUnixTime();
//处理签名, 这里需要处理Map中的元素
String sign = OpenUtils.sign("POST", unixTime, path, queryString, JSON.toJSONString(body), secretKey);
//构造header, 包含签名信息
headers = OpenUtils.buildV3Headers(apiKey, sign, unixTime, passPhrase);
}
//通过requestSpe发送请求
Response rs = requestSpe.
contentType("application/json; charset=UTF-8").
headers(headers).
body(body).
when().
post("");
if(rs.getStatusCode() != 200)
log.info("Not get 200 for: "+rs.asString());
return rs;
}
/**
* doGet 通用方法 body 为String 类型
* @param url
* @param user
* @param body: get方法没有body时候,赋值为"";
* @return
*/
public static Response doGet(String url, String user, String body) {
Map<String, Object> headers = null;
//构造一个RequestSpecification
RestAssured.baseURI= openApiHost;
if(openApiPort!=""){
RestAssured.port = Integer.valueOf(openApiPort).intValue();
}
RestAssured.basePath = url;
requestSpe = given();
if (user != null) {
String apiKey = accountMap.get(user + "API_KEY");
String secretKey = accountMap.get(user + "SECRET_KEY");
String passPhrase = accountMap.get(user + "PASSPHRASE");
//签名所需要的参数
String queryString = "";
String path = url;
String unixTime = OpenUtils.getUnixTime();
//处理签名
String sign = OpenUtils.sign("GET", unixTime, path, queryString, body, secretKey);
//构造header, 包含签名信息
headers = OpenUtils.buildV3Headers(apiKey, sign, unixTime, passPhrase);
}
//通过requestSpe发送请求
Response rs = requestSpe.
contentType("application/json; charset=UTF-8").
headers(headers).
body(body).
when().
get();
if (rs.getStatusCode() != 200)
log.info("Not get 200 for: " + rs.getBody().prettyPrint());
return rs;
}
/**
* body为map类型
* @param url
* @param user
* @param body
* @return
*/
public static Response doGet(String url, String user, Map<String, ?> body) {
Map<String, Object> headers = null;
//构造一个RequestSpecification
RestAssured.baseURI= openApiHost;
if(openApiPort!=""){
RestAssured.port = Integer.valueOf(openApiPort).intValue();
}
RestAssured.basePath = url;
requestSpe = given();
if (user != null) {
String apiKey = accountMap.get(user + "API_KEY");
String secretKey = accountMap.get(user + "SECRET_KEY");
String passPhrase = accountMap.get(user + "PASSPHRASE");
//签名所需要的参数
String queryString = "";
String path = url;
String unixTime = OpenUtils.getUnixTime();
//处理签名
String sign = OpenUtils.sign("GET", unixTime, path, queryString, JSON.toJSONString(body), secretKey);
//构造header, 包含签名信息
headers = OpenUtils.buildV3Headers(apiKey, sign, unixTime, passPhrase);
}
//通过requestSpe发送请求
Response rs = requestSpe.
contentType("application/json; charset=UTF-8").
headers(headers).
body(body).
when().
get();
if (rs.getStatusCode() != 200)
log.info("Not get 200 for: " + rs.getBody().prettyPrint());
return rs;
}
/**
* 处理get接口包含多个参数的情况,需要构造queryString
* @param url
* @param user
* @param input
* @return
*/
public static Response doGetParam(String url, String user, Map<String, ?> input) {
Map<String, Object> headers = new HashMap<>();
//构造一个RequestSpecification
RestAssured.baseURI= openApiHost;
if(openApiPort!=""){
RestAssured.port = Integer.valueOf(openApiPort).intValue();
}
//签名所需要的参数
String queryString = "";
String path = url;
String unixTime = OpenUtils.getUnixTime();
//遍历Map来生成url 和 queryString,才能进行正确的签名
StringBuilder paramsBuilder = new StringBuilder();
for(String key:input.keySet()){
paramsBuilder.append(key).append("=").append(input.get(key));
paramsBuilder.append("&");
}
if(paramsBuilder.length() != 0){
paramsBuilder.deleteCharAt(paramsBuilder.length()-1);
}
queryString = paramsBuilder.toString();
url = url+"?"+queryString;
RestAssured.basePath = url;
requestSpe = given();
if(user != null){
String apiKey = accountMap.get(user + "API_KEY");
String secretKey = accountMap.get(user + "SECRET_KEY");
String passPhrase = accountMap.get(user + "PASSPHRASE");
//处理签名
String sign = OpenUtils.sign("GET", unixTime, path, queryString, "", secretKey);
//构造header, 包含签名信息
headers = OpenUtils.buildV3Headers(apiKey, sign, unixTime, passPhrase);
}
//通过requestSpe发送请求
Response rs = requestSpe.
contentType("application/json; charset=UTF-8").
headers(headers).
params(input).
when().
get();
if (rs.getStatusCode() != 200)
log.info("Not get 200 for: " + rs.asString());
return rs;
}
}
总结
- 非常方便的处理 jsonString,Map<String, Object>类型的输入
版权所有,本文为原创文章,转载请注明出处