提交 22e81adc authored 作者: 黄夏豪's avatar 黄夏豪

refactor(base): 补充了异常情况的处理

上级 047c7faf
......@@ -5,6 +5,7 @@ import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.springframework.http.HttpStatus;
@Data
@AllArgsConstructor
......@@ -18,7 +19,7 @@ public class HttpResponseDetail {
private String responseBody = "error request";
@ApiModelProperty("响应状态,例如:200、401、500")
private Integer statusCode = 500;
private HttpStatus statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
@ApiModelProperty("响应时间,单位为 ms ")
private Long responseTime = 0l;
......
package org.matrix.exception;
/**
* @author huangxiahao
*/
public class CheckPointException extends RuntimeException {
public CheckPointException(String message){
super(message);
}
}
package org.matrix.exception;
/**
* @author huangxiahao
*/
public class HttpRequestException extends RuntimeException {
public HttpRequestException(String message){
super(message);
}
}
......@@ -13,6 +13,7 @@ import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.matrix.entity.checkPoint.*;
import org.matrix.entity.httpRequest.HttpResponseDetail;
import org.matrix.exception.CheckPointException;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpStatus;
......@@ -28,7 +29,7 @@ import java.util.regex.Pattern;
* 数据检测点工具
* todo 打印LOG
*
* @author Administrator
* @author huangxiahao
*/
public class CheckPointUtil {
......@@ -39,8 +40,7 @@ public class CheckPointUtil {
try {
this.baseJs = IOUtils.toString(cpr.getInputStream(), StandardCharsets.UTF_8);
} catch (IOException e) {
//todo 抛出异常 初始JS加载失败
e.printStackTrace();
throw new CheckPointException("初始JS加载失败");
}
}
......@@ -59,7 +59,6 @@ public class CheckPointUtil {
for (ContainCheckPoint containCheckPoint : checkPoint.getContainCheckPoints()) {
checkPointResult.addCheckPointResultDetail(containCheck(containCheckPoint, httpResponseDetail.getResponseBody()));
}
//todo 数据库参数检测
//JsonPath检查点检测
if (checkPoint.getJsonPathCheckPoints().size() > 0) {
Object jsonObject = Configuration.defaultConfiguration().jsonProvider().parse(httpResponseDetail.getResponseBody());
......@@ -106,13 +105,8 @@ public class CheckPointUtil {
);
}
} catch (ScriptException e) {
//todo 抛出异常初始化js引擎失败
e.printStackTrace();
throw new CheckPointException("初始化js引擎失败");
}
return new CheckPointResultDetail(
false,
"JsonPath检查点,检查未通过,空的表达式"
);
}
public ScriptEngine getScriptEngine() throws ScriptException {
......@@ -136,39 +130,6 @@ public class CheckPointUtil {
return result;
}
public static void main(String[] args) {
String json = "{\n" +
" \"school\": [\n" +
" {\n" +
" \"className\": \"一班\",\n" +
" \"student\":[\n" +
" \"一班的张三\",\"一班的李四\",\"一班的王五\",\"一班的赵六\",\"一班的田七\"\n" +
" ]\n" +
" },\n" +
" {\n" +
" \"className\": \"二班\",\n" +
" \"student\":[\n" +
" \"二班的张三\",\"二班的李四\",\"二班的王五\",\"二班的赵六\",\"二班的田七\"\n" +
" ]\n" +
" },\n" +
" {\n" +
" \"className\": \"三班\",\n" +
" \"student\":[\n" +
" \"三班的张三\",\"三班的李四\",\"三班的王五\",\"三班的赵六\",\"三班的田七\"\n" +
" ]\n" +
" }\n" +
" ]\n" +
"}";
Object jsonObject = Configuration.defaultConfiguration().jsonProvider().parse(json);
CheckPointUtil checkPointUtil = new CheckPointUtil("baseJS.js");
CheckPointResultDetail checkPointResultDetail = checkPointUtil.jsonPathCheck(new JsonPathCheckPoint(
"contains({$..className},\"一班\")"
),
jsonObject);
System.out.println(checkPointResultDetail);
}
public CheckPointResultDetail containCheck(ContainCheckPoint containCheckPoint, String responseBody) {
if (containCheckPoint.getIsContain()) {
if (responseBody.contains(containCheckPoint.getValue())) {
......@@ -222,14 +183,14 @@ public class CheckPointUtil {
} else {
return new CheckPointResultDetail(
true,
"异常检查点,检查通过"
"不为空检查点,检查通过"
);
}
}
public CheckPointResultDetail exceptionCheck(HttpResponseDetail httpResponseDetail) {
if (httpResponseDetail.getStatusCode() == HttpStatus.OK.value()) {
if (httpResponseDetail.getStatusCode().value() == HttpStatus.OK.value()) {
return new CheckPointResultDetail(
true,
"异常检查点,检查通过"
......
......@@ -19,7 +19,9 @@ import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.matrix.entity.httpRequest.*;
import org.matrix.exception.HttpRequestException;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import java.io.File;
import java.io.FileInputStream;
......@@ -32,12 +34,10 @@ import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* HttpClient调用封装
*
* todo 打印LOG
* @author HuangXiahao
**/
public class HttpClientUtil {
......@@ -98,12 +98,11 @@ public class HttpClientUtil {
return new HttpResponseDetail(
response,
EntityUtils.toString(response.getEntity(), "UTF-8"),
response.getStatusLine().getStatusCode(),
HttpStatus.valueOf(response.getStatusLine().getStatusCode()),
endTime.getTime() - startTime.getTime()
);
} catch (IOException e) {
e.printStackTrace();
return null;
throw new HttpRequestException(String.format("解析返回值失败,本次请求详细参数如下: %s ",httpRequestDetail));
} finally {
//关闭请求request
closeRequest(requestBase);
......@@ -202,28 +201,25 @@ public class HttpClientUtil {
public String initUrlString(HttpRequestDetail httpRequestDetail) {
String url = httpRequestDetail.getUrl();
try {
switch (httpRequestDetail.getRequestType()) {
case QUERY:
if (httpRequestDetail.getMethod().equals(HttpMethod.GET)) {
URIBuilder uriBuilder ;
uriBuilder = new URIBuilder(url);
for (RequestBody requestBody : httpRequestDetail.getRequestBodies()) {
switch (requestBody.getType()) {
case TEXT:
uriBuilder.setParameter(requestBody.getKey(), requestBody.getValue());
break;
case FILE:
throw new NullPointerException("QUERY请求不能传入文件流");
default:
}
if (httpRequestDetail.getRequestType() == HttpRequestType.QUERY) {
if (httpRequestDetail.getMethod().equals(HttpMethod.GET)) {
URIBuilder uriBuilder;
uriBuilder = new URIBuilder(url);
for (RequestBody requestBody : httpRequestDetail.getRequestBodies()) {
switch (requestBody.getType()) {
case TEXT:
uriBuilder.setParameter(requestBody.getKey(), requestBody.getValue());
break;
case FILE:
throw new NullPointerException("QUERY请求不能传入文件流");
default:
}
url = uriBuilder.build().toString();
}
break;
default:
url = uriBuilder.build().toString();
}
}
} catch (URISyntaxException e) {
e.printStackTrace();
throw new HttpRequestException(String.format("URL格式不正确,不正确的URL为: %s",url));
}
return url;
}
......@@ -269,6 +265,7 @@ public class HttpClientUtil {
response.close();
} catch (IOException e) {
e.printStackTrace();
throw new HttpRequestException(String.format("关闭返回流失败"));
}
}
}
......
package org.matrix;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Unit test for simple App.
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论