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

feat(base): 修复了httprequest的BUG

上级 55c57ba4
......@@ -2,6 +2,7 @@ package org.matrix.actuators.checkpoint;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
/**
......@@ -24,17 +25,17 @@ public class CheckPoint {
/**
* 包含检测
*/
private ContainCheckPoint containCheckPoints;
private String containCheckPoint;
/**
* 不包含检测
*/
private NoContainCheckPoint noContainCheckPoint;
private String noContainCheckPoint;
/**
* JsonPath检查点
*/
private List<JsonPathCheckPoint> jsonPathCheckPoints;
private JsonPathCheckPoint jsonPathCheckPoint;
......
......@@ -7,17 +7,13 @@ import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathNotFoundException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.matrix.actuators.Actuator;
import org.matrix.actuators.httpclient.HttpResponseDetail;
import org.matrix.actuators.sql.SqlExpActuator;
import org.matrix.actuators.util.CompleteExpressionUtil;
import org.matrix.exception.CheckPointException;
import org.matrix.util.SpringUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpStatus;
......@@ -25,10 +21,6 @@ import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
......@@ -57,7 +49,6 @@ public class CheckPointActuator implements Actuator {
}
public CheckPointResult httpCheck(HttpResponseDetail httpResponseDetail, CheckPoint checkPoint) {
CheckPointResult checkPointResult = new CheckPointResult();
//根据checkPoint里的细节数据开始检测
......@@ -70,24 +61,34 @@ public class CheckPointActuator implements Actuator {
checkPointResult.addCheckPointResultDetail(nullCheck(httpResponseDetail.getResponseBody()));
}
//包含检查点检测
checkPointResult.addCheckPointResultDetail(containCheck(checkPoint.getContainCheckPoints(), httpResponseDetail.getResponseBody()));
if (!StringUtils.isEmpty(checkPoint.getContainCheckPoint())){
checkPointResult.addCheckPointResultDetail(containCheck(checkPoint.getContainCheckPoint(), httpResponseDetail.getResponseBody()));
}
//不包含检查点检测
checkPointResult.addCheckPointResultDetail(noContainCheck(checkPoint.getNoContainCheckPoint(), httpResponseDetail.getResponseBody()));
if (!StringUtils.isEmpty(checkPoint.getNoContainCheckPoint())){
checkPointResult.addCheckPointResultDetail(noContainCheck(checkPoint.getNoContainCheckPoint(), httpResponseDetail.getResponseBody()));
}
//JsonPath检查点检测
if (checkPoint.getJsonPathCheckPoints().size() > 0) {
if (checkPoint.getJsonPathCheckPoint()!=null) {
Object jsonObject = Configuration.defaultConfiguration().jsonProvider().parse(httpResponseDetail.getResponseBody());
for (JsonPathCheckPoint jsonPathCheckPoint : checkPoint.getJsonPathCheckPoints()) {
checkPointResult.addCheckPointResultDetail(jsonPathCheck(jsonPathCheckPoint, jsonObject));
}
checkPointResult.addCheckPointResultDetail(jsonPathCheck(checkPoint.getJsonPathCheckPoint(), jsonObject));
}
return checkPointResult;
}
public CheckPointResultDetail jsonPathCheck(JsonPathCheckPoint jsonPathCheckPoint, Object jsonObject) {
String expression;
String expression = jsonPathCheckPoint.getExpression();
try {
expression = CompleteExpressionUtil.completeDynamicVariable(
expression,
envId,
projectId)
;
//todo 李迪凡 先将result中的动态变量都还原出来 (先后顺序不能乱)得先还原动态变量
expression = CompleteExpressionUtil.completeJsonPathExpression(jsonPathCheckPoint.getExpression(), jsonObject);
expression = CompleteExpressionUtil.completeJsonPathExpression(
expression,
jsonObject
);
} catch (IllegalArgumentException e) {
return new CheckPointResultDetail(
false,
......@@ -102,19 +103,19 @@ public class CheckPointActuator implements Actuator {
try {
ScriptEngine jsEngine = getScriptEngine();
Object eval = jsEngine.eval(expression);
if (eval instanceof Boolean){
if ((Boolean) eval){
if (eval instanceof Boolean) {
if ((Boolean) eval) {
return new CheckPointResultDetail(
true,
String.format("JsonPath检查点,检查通过,表达式为:%s", expression)
);
}else {
} else {
return new CheckPointResultDetail(
false,
String.format("JsonPath检查点,检查未通过,计算结果为false,错误的表达式为:%s", expression)
);
}
}else {
} else {
return new CheckPointResultDetail(
false,
String.format("JsonPath检查点,检查未通过,JsonPath的返回值不是一个布尔类型,错误的表达式为:%s", expression)
......@@ -132,12 +133,14 @@ public class CheckPointActuator implements Actuator {
}
public CheckPointResultDetail containCheck(ContainCheckPoint containCheckPoint, String responseBody) {
if (responseBody.contains(containCheckPoint.getValue())) {
public CheckPointResultDetail containCheck(String containCheckPoint, String responseBody) {
containCheckPoint = CompleteExpressionUtil.completeDynamicVariable(
containCheckPoint,
envId,
projectId)
;
String[] split = containCheckPoint.split(",");
if (responseBody.contains(containCheckPoint)) {
return new CheckPointResultDetail(
true,
"包含检查点,检查通过"
......@@ -145,23 +148,23 @@ public class CheckPointActuator implements Actuator {
} else {
return new CheckPointResultDetail(
false,
String.format("包含检查点,检查未通过,结果:%s 中,不包含值:%s", responseBody, containCheckPoint.getValue())
String.format("包含检查点,检查未通过,结果:%s 中,不包含值:%s", responseBody, containCheckPoint)
);
}
}
public CheckPointResultDetail noContainCheck(NoContainCheckPoint noContainCheckPoint, String responseBody) {
if (responseBody.contains(noContainCheckPoint.getValue())) {
return new CheckPointResultDetail(
false,
String.format("不包含检查点,检查未通过,结果:%s 中,包含值:%s", responseBody, noContainCheckPoint.getValue())
);
} else {
return new CheckPointResultDetail(
true,
"不包含检查点,检查通过"
);
}
public CheckPointResultDetail noContainCheck(String noContainCheckPoint, String responseBody) {
if (responseBody.contains(noContainCheckPoint)) {
return new CheckPointResultDetail(
false,
String.format("不包含检查点,检查未通过,结果:%s 中,包含值:%s", responseBody, noContainCheckPoint)
);
} else {
return new CheckPointResultDetail(
true,
"不包含检查点,检查通过"
);
}
}
......@@ -210,7 +213,4 @@ public class CheckPointActuator implements Actuator {
}
}
......@@ -25,4 +25,8 @@ public class CheckPointResult {
public Boolean addCheckPointResultDetail(CheckPointResultDetail checkPointResultDetail){
return checkPointResultDetails.add(checkPointResultDetail);
}
public Boolean addCheckPointResultDetail(List<CheckPointResultDetail> checkPointResultDetail){
return checkPointResultDetails.addAll(checkPointResultDetail);
}
}
......@@ -107,7 +107,7 @@ public class HttpClientActuator implements Actuator {
projectId)
);
}
if (StringUtils.isEmpty(httpRequestDetail.getStringValue())){
if (!StringUtils.isEmpty(httpRequestDetail.getStringValue())){
//todo 李迪凡 将httpRequestDetail的stringValue 动态变量补全
httpRequestDetail.setStringValue(
CompleteExpressionUtil.completeDynamicVariable(
......
......@@ -3,6 +3,7 @@ package org.matrix.actuators.usecase;
import com.alibaba.fastjson.JSON;
import org.matrix.actuators.Actuator;
import org.matrix.actuators.checkpoint.CheckPointActuator;
import org.matrix.actuators.checkpoint.JsonPathCheckPoint;
import org.matrix.actuators.httpclient.HttpClientActuator;
import org.matrix.config.HttpRequestConfig;
import org.matrix.database.entity.TestCase;
......@@ -27,7 +28,7 @@ public class CaseActuator implements Actuator {
private HttpClientActuator httpClientActuator;
private final String baseJsPath = "baseJs.js";
private final String baseJsPath = "syntaxCheck.js";
public CaseActuator(Long envId, Long projectId) {
this.projectId = projectId;
......@@ -58,7 +59,21 @@ public class CaseActuator implements Actuator {
}
private CheckPoint getCheckPointEntity(TestCase testCase) {
return new CheckPoint();
CheckPoint checkPoint = new CheckPoint();
if (testCase.getAbnormalCheckpoint()==0){
checkPoint.setUseExceptionCheck(false);
}else {
checkPoint.setUseExceptionCheck(true);
}
if (testCase.getNoEmptyCheckpoint()==0){
checkPoint.setUseNullCheck(false);
}else {
checkPoint.setUseNullCheck(true);
}
checkPoint.setContainCheckPoint(testCase.getContainCheckpoint());
checkPoint.setNoContainCheckPoint(testCase.getNoContainCheckpoint());
checkPoint.setJsonPathCheckPoint(JSON.parseObject(testCase.getJsonpathCheckpoint(), JsonPathCheckPoint.class));
return checkPoint;
}
......
......@@ -40,10 +40,10 @@ public class TestCase extends BaseEntity {
private String moveAferTest;
@ApiModelProperty("是否进行异常检验,0为否,1为是")
private Integer abnormalCheckpoint;
private Integer abnormalCheckpoint = 0;
@ApiModelProperty("是否进行非空检验,0为否,1为是")
private Integer noEmptyCheckpoint;
private Integer noEmptyCheckpoint = 0;
@ApiModelProperty("包含某字段检验(例如 张三,李四) 则对检查结果中是否包含张三或者李四")
private String containCheckpoint;
......
package org.matrix.actuators.sql;
import com.alibaba.fastjson.JSON;
import org.checkerframework.checker.units.qual.A;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.matrix.actuators.httpclient.HttpClientActuator;
import org.matrix.actuators.httpclient.HttpRequestDetail;
import org.matrix.actuators.httpclient.HttpResponseDetail;
import org.matrix.actuators.usecase.CaseActuator;
import org.matrix.actuators.usecase.TestCaseExecuteResult;
import org.matrix.config.HttpRequestConfig;
import org.matrix.database.entity.TestCase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class CaseActuatorTest {
@Test
public void test(){
CaseActuator httpClientActuator = new CaseActuator(
1l,
1l
);
String json = "{\n" +
" \"url\":\"http://127.0.0.1:13245/test/sayHelloJson\",\n" +
" \"method\":\"POST\",\n" +
" \"requestType\":\"JSON\",\n" +
" \"stringValue\":\"{\\\"name\\\":\\\"${componentName}[0]\\\"}\",\n" +
" \"headers\":[\n" +
" {\n" +
" \"name\":\"cookie\",\n" +
" \"value\":\"123456\"\n" +
" }\n" +
" ]\n" +
"}";
TestCase testCase = new TestCase();
testCase.setName("name");
testCase.setType(1);
testCase.setDetail(json);
testCase.setAbnormalCheckpoint(1);
testCase.setContainCheckpoint("xxxxxxx");
TestCaseExecuteResult testCaseExecuteResult = httpClientActuator.executeTestCase(testCase);
System.out.println(testCaseExecuteResult);
}
}
package org.matrix.actuators.sql;
import com.alibaba.fastjson.JSON;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.matrix.actuators.httpclient.HttpClientActuator;
import org.matrix.actuators.httpclient.HttpRequestDetail;
import org.matrix.actuators.httpclient.HttpResponseDetail;
import org.matrix.config.HttpRequestConfig;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
......@@ -12,17 +14,18 @@ import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest
public class HttpClientActuatorTest {
@Test
public void test(){
HttpClientActuator httpClientActuator = new HttpClientActuator(
new HttpRequestConfig(),
0l,
0l
1l,
1l
);
String json = "{\n" +
" \"url\":\"http://127.0.0.1:13245/test/sayHelloJson\",\n" +
" \"method\":\"POST\",\n" +
" \"requestType\":\"JSON\",\n" +
" \"stringValue\":\"{\\\"name\\\":\\\"李四\\\"}\",\n" +
" \"stringValue\":\"{\\\"name\\\":\\\"${componentName}[0]\\\"}\",\n" +
" \"headers\":[\n" +
" {\n" +
" \"name\":\"cookie\",\n" +
......@@ -31,7 +34,8 @@ public class HttpClientActuatorTest {
" ]\n" +
"}";
HttpRequestDetail httpRequestDetail = JSON.parseObject(json, HttpRequestDetail.class);
httpClientActuator.sendHttpRequest(httpRequestDetail);
HttpResponseDetail httpResponseDetail = httpClientActuator.sendHttpRequest(httpRequestDetail);
System.out.println(httpResponseDetail);
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论