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

feat(base): 修复了httprequest的BUG

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