提交 021bdf60 authored 作者: Matrix's avatar Matrix

fix(用例执行器): 用例执行器现在可以同时使用path和body了

上级 38411ad4
...@@ -276,28 +276,31 @@ public class HttpClientActuator implements Actuator { ...@@ -276,28 +276,31 @@ public class HttpClientActuator implements Actuator {
public String initUrlString(HttpRequestDetail httpRequestDetail,Long envId, Long projectId) { public String initUrlString(HttpRequestDetail httpRequestDetail,Long envId, Long projectId) {
String url = httpRequestDetail.getUrl(); String url = httpRequestDetail.getUrl();
try { try {
if (httpRequestDetail.getRequestType() == HttpRequestType.QUERY) { for (RequestParam param : httpRequestDetail.getRequestParams()) {
if (httpRequestDetail.getMethod().equals(HttpMethod.GET)) { if (param.getRequestType() == HttpRequestType.QUERY) {
URIBuilder uriBuilder; if (httpRequestDetail.getMethod().equals(HttpMethod.GET)) {
uriBuilder = new URIBuilder(url); URIBuilder uriBuilder;
for (RequestBody requestBody : httpRequestDetail.getRequestBodies()) { uriBuilder = new URIBuilder(url);
switch (requestBody.getType()) { for (RequestBody requestBody : httpRequestDetail.getRequestBodies()) {
case TEXT: switch (requestBody.getType()) {
uriBuilder.setParameter(requestBody.getKey(), requestBody.getValue()); case TEXT:
break; uriBuilder.setParameter(requestBody.getKey(), requestBody.getValue());
case FILE: break;
throw new NullPointerException("QUERY请求不能传入文件流"); case FILE:
default: throw new NullPointerException("QUERY请求不能传入文件流");
default:
}
} }
url = uriBuilder.build().toString();
} }
url = uriBuilder.build().toString(); }else if(param.getRequestType() == HttpRequestType.PATH){
List<RequestBody> requestBodies = param.getRequestBodies();
Map<String, String> requestMap =
requestBodies.stream().collect(Collectors.toMap(RequestBody::getKey, RequestBody::getValue));
url = completeExpressionUtil.completePathVariable(url,requestMap,envId,projectId);
} }
}else if(httpRequestDetail.getRequestType() == HttpRequestType.PATH){
List<RequestBody> requestBodies = httpRequestDetail.getRequestBodies();
Map<String, String> requestMap =
requestBodies.stream().collect(Collectors.toMap(RequestBody::getKey, RequestBody::getValue));
url = completeExpressionUtil.completePathVariable(url,requestMap,envId,projectId);
} }
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
throw new HttpRequestException(String.format("URL格式不正确,不正确的URL为: %s",url)); throw new HttpRequestException(String.format("URL格式不正确,不正确的URL为: %s",url));
} }
......
...@@ -32,9 +32,11 @@ public class HttpRequestDetail { ...@@ -32,9 +32,11 @@ public class HttpRequestDetail {
@ApiModelProperty("键值对入参") @ApiModelProperty("键值对入参")
private List<RequestBody> requestBodies = new ArrayList<>(); private List<RequestBody> requestBodies = new ArrayList<>();
@ApiModelProperty("复合参数组")
private List<RequestParam> requestParams = new ArrayList<>();
/** /**
* 增加键值对入参 * 增加键值对入参
*/ */
private Boolean addRequestBody(RequestBody requestBody) { private Boolean addRequestBody(RequestBody requestBody) {
return requestBodies.add(requestBody); return requestBodies.add(requestBody);
......
package org.matrix.actuators.httpclient;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.List;
/**
* RequestParam. 用于复合参数
*
* @author Matrix <xhyrzldf@gmail.com>
* @since 2022/4/24 at 4:48 PM
* Suffering is the most powerful teacher of life.
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class RequestParam {
@ApiModelProperty("请求类型")
private HttpRequestType requestType = HttpRequestType.NONE;
@ApiModelProperty("键值对入参")
private List<RequestBody> requestBodies = new ArrayList<>();
}
...@@ -66,7 +66,7 @@ public class SqlExpActuator implements Actuator { ...@@ -66,7 +66,7 @@ public class SqlExpActuator implements Actuator {
private final ITestDataService dataService; private final ITestDataService dataService;
/** /**
* 解析并运行SQL语句,可以包含别的SQL变量,例如 'select * from user where user.id = ${user_id} ' * 解析并运行SQL语句,可以包含别的SQL变量,例如 'select * from user where user.id = ${user_id}'
* *
* @param sqlDetail sql的poolId与sql语句的对象 * @param sqlDetail sql的poolId与sql语句的对象
* @param envId 环境id * @param envId 环境id
......
...@@ -6,22 +6,20 @@ import com.alibaba.fastjson.JSONObject; ...@@ -6,22 +6,20 @@ import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.junit.jupiter.api.Test; 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.*;
import org.matrix.actuators.httpclient.HttpRequestDetail;
import org.matrix.actuators.httpclient.HttpResponseDetail;
import org.matrix.config.HttpRequestConfig; import org.matrix.config.HttpRequestConfig;
import org.matrix.database.entity.ExecutionRecord; import org.matrix.database.entity.ExecutionRecord;
import org.matrix.database.service.IExecutionRecordService; import org.matrix.database.service.IExecutionRecordService;
import org.matrix.exception.GlobalException; import org.matrix.exception.GlobalException;
import org.matrix.socket.enums.TestExecuteType; import org.matrix.socket.enums.TestExecuteType;
import org.matrix.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
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;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.testng.collections.Lists;
import java.util.List; import java.util.*;
import java.util.Map;
import java.util.Optional;
import static java.util.stream.Collectors.groupingBy; import static java.util.stream.Collectors.groupingBy;
...@@ -41,10 +39,16 @@ class HttpClientActuatorTest { ...@@ -41,10 +39,16 @@ class HttpClientActuatorTest {
JSONObject stringValue = new JSONObject(); JSONObject stringValue = new JSONObject();
stringValue.put("account","hxh"); stringValue.put("account","hxh");
stringValue.put("password","hxh1234567"); stringValue.put("password","hxh1234567");
jsonObject.put("url","http://127.0.0.1:8765/user/login"); jsonObject.put("url","http://127.0.0.1:8765/user/{id}");
jsonObject.put("method","POST"); jsonObject.put("method","POST");
jsonObject.put("requestType","JSON"); jsonObject.put("requestType","JSON");
jsonObject.put("stringValue",JSONObject.toJSON(stringValue)); jsonObject.put("stringValue",JSONObject.toJSON(stringValue));
JSONArray array = new JSONArray();
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("requestType", HttpRequestType.PATH);
jsonObject1.put("requestBodies", new RequestBody("id", MultiPartRequestBodyType.TEXT, "login"));
array.add(jsonObject1);
jsonObject.put("requestParams", array);
// String json = "{\n" + // String json = "{\n" +
...@@ -58,7 +62,7 @@ class HttpClientActuatorTest { ...@@ -58,7 +62,7 @@ class HttpClientActuatorTest {
// "}"; // "}";
HttpRequestDetail httpRequestDetail1 = JSON.parseObject(JSONObject.toJSONString(jsonObject), HttpRequestDetail.class); HttpRequestDetail httpRequestDetail1 = JSON.parseObject(JSONObject.toJSONString(jsonObject), HttpRequestDetail.class);
HttpRequestDetail httpRequestDetail = JSON.parseObject(JSONObject.toJSONString(jsonObject), HttpRequestDetail.class); HttpRequestDetail httpRequestDetail = JSON.parseObject(JSONObject.toJSONString(jsonObject), HttpRequestDetail.class);
HttpResponseDetail httpResponseDetail = httpClientActuator.sendHttpRequest(httpRequestDetail,1l,1l); HttpResponseDetail httpResponseDetail = httpClientActuator.sendHttpRequest(httpRequestDetail,10l,1l);
System.out.println(httpResponseDetail); System.out.println(httpResponseDetail);
} }
@Test @Test
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论