diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java index 98f9b4fc82..4a6f90cf49 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java @@ -57,6 +57,7 @@ import java.net.URLEncoder; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -189,12 +190,19 @@ public class MsHTTPSamplerProxy extends MsTestElement { if (this.body != null) { List bodyParams = this.body.getBodyParams(sampler, this.getId()); if (StringUtils.isNotEmpty(this.body.getType()) && "Form Data".equals(this.body.getType())) { - sampler.setDoMultipart(true); + AtomicBoolean kvIsEmpty = new AtomicBoolean(true); this.body.getKvs().forEach(files -> { if (StringUtils.isNotEmpty(files.getName()) && "file".equals(files.getType()) && CollectionUtils.isNotEmpty(files.getFiles())) { sampler.setDoBrowserCompatibleMultipart(true); } + if (StringUtils.isNotEmpty(files.getName())) { + kvIsEmpty.set(false); + } }); + //值不为空时才会设置doMultiPart + if (!kvIsEmpty.get()) { + sampler.setDoMultipart(true); + } } if (CollectionUtils.isNotEmpty(bodyParams)) { Arguments arguments = httpArguments(bodyParams); @@ -684,28 +692,28 @@ public class MsHTTPSamplerProxy extends MsTestElement { list.stream(). filter(KeyValue::isValid). filter(KeyValue::isEnable).forEach(keyValue -> { - try { - String value = StringUtils.isNotEmpty(keyValue.getValue()) && keyValue.getValue().startsWith("@") ? ScriptEngineUtils.buildFunctionCallString(keyValue.getValue()) : keyValue.getValue(); - HTTPArgument httpArgument = new HTTPArgument(keyValue.getName(), value); - if (keyValue.getValue() == null) { - httpArgument.setValue(""); - } - httpArgument.setAlwaysEncoded(keyValue.isUrlEncode()); - if (StringUtils.isNotBlank(keyValue.getContentType())) { - httpArgument.setContentType(keyValue.getContentType()); - } - if(StringUtils.equalsIgnoreCase(this.method,"get")){ - if(StringUtils.isNotEmpty(httpArgument.getValue())){ - arguments.addArgument(httpArgument); - } - }else { - arguments.addArgument(httpArgument); - } - } catch (Exception e) { + try { + String value = StringUtils.isNotEmpty(keyValue.getValue()) && keyValue.getValue().startsWith("@") ? ScriptEngineUtils.buildFunctionCallString(keyValue.getValue()) : keyValue.getValue(); + HTTPArgument httpArgument = new HTTPArgument(keyValue.getName(), value); + if (keyValue.getValue() == null) { + httpArgument.setValue(""); + } + httpArgument.setAlwaysEncoded(keyValue.isUrlEncode()); + if (StringUtils.isNotBlank(keyValue.getContentType())) { + httpArgument.setContentType(keyValue.getContentType()); + } + if (StringUtils.equalsIgnoreCase(this.method, "get")) { + if (StringUtils.isNotEmpty(httpArgument.getValue())) { + arguments.addArgument(httpArgument); + } + } else { + arguments.addArgument(httpArgument); + } + } catch (Exception e) { - } - } - ); + } + } + ); return arguments; } diff --git a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java index bd1ae2a574..16ebff0aba 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java @@ -1645,18 +1645,9 @@ public class ApiDefinitionService { return extApiDefinitionMapper.selectEffectiveIdByProjectId(projectId); } -// public List selectByProjectIdAndMethodAndUrl(String projectId, String method,String url) { -// ApiDefinitionExample example = new ApiDefinitionExample(); -// ApiDefinitionExample.Criteria criteria = example.createCriteria().andMethodEqualTo(method).andProjectIdEqualTo(projectId); -// if(StringUtils.isNotEmpty(url)){ -// criteria.andPathEqualTo(url); -// } -// return apiDefinitionMapper.selectByExample(example); -// } + public List preparedUrl(String projectId, String method, String baseUrlSuffix) { - public List preparedUrl(String projectId, String method, String urlSuffix) { - - if (StringUtils.isEmpty(urlSuffix)) { + if (StringUtils.isEmpty(baseUrlSuffix)) { return new ArrayList<>(); } else { ApiDefinitionExample example = new ApiDefinitionExample(); @@ -1665,6 +1656,7 @@ public class ApiDefinitionService { List apiIdList = new ArrayList<>(); boolean urlSuffixEndEmpty = false; + String urlSuffix = baseUrlSuffix; if (urlSuffix.endsWith("/")) { urlSuffixEndEmpty = true; urlSuffix = urlSuffix + "testMock"; @@ -1674,29 +1666,33 @@ public class ApiDefinitionService { urlParams[urlParams.length - 1] = ""; } for (ApiDefinition api : apiList) { - String path = api.getPath(); - if (StringUtils.isEmpty(path)) { - continue; - } - if (path.startsWith("/")) { - path = path.substring(1); - } - if (StringUtils.isNotEmpty(path)) { - String[] pathArr = path.split("/"); - if (pathArr.length == urlParams.length) { - boolean isFetch = true; - for (int i = 0; i < urlParams.length; i++) { - String pathItem = pathArr[i]; - if (!(pathItem.startsWith("{") && pathItem.endsWith("}"))) { - if (!StringUtils.equals(pathArr[i], urlParams[i])) { - isFetch = false; - break; + if(StringUtils.equalsAny(api.getPath(),baseUrlSuffix,"/"+baseUrlSuffix)){ + apiIdList.add(api.getId()); + }else { + String path = api.getPath(); + if (StringUtils.isEmpty(path)) { + continue; + } + if (path.startsWith("/")) { + path = path.substring(1); + } + if (StringUtils.isNotEmpty(path)) { + String[] pathArr = path.split("/"); + if (pathArr.length == urlParams.length) { + boolean isFetch = true; + for (int i = 0; i < urlParams.length; i++) { + String pathItem = pathArr[i]; + if (!(pathItem.startsWith("{") && pathItem.endsWith("}"))) { + if (!StringUtils.equals(pathArr[i], urlParams[i])) { + isFetch = false; + break; + } } - } - } - if (isFetch) { - apiIdList.add(api.getId()); + } + if (isFetch) { + apiIdList.add(api.getId()); + } } } } diff --git a/backend/src/main/java/io/metersphere/api/service/MockConfigService.java b/backend/src/main/java/io/metersphere/api/service/MockConfigService.java index 7170888425..2b1027b991 100644 --- a/backend/src/main/java/io/metersphere/api/service/MockConfigService.java +++ b/backend/src/main/java/io/metersphere/api/service/MockConfigService.java @@ -393,8 +393,10 @@ public class MockConfigService { if (StringUtils.equalsAnyIgnoreCase(type, "Form Data", "WWW_FORM") && expectBodyObject.containsKey("kvs")) { JSONArray kvsArr = expectBodyObject.getJSONArray("kvs"); List mockConfigRequestParams = MockApiUtils.getParamsByJSONArray(kvsArr); - if (!MockApiUtils.checkParamsCompliance(jsonArray, mockConfigRequestParams, StringUtils.equals(paramsFilterType, "And"))) { - return false; + if(CollectionUtils.isNotEmpty(mockConfigRequestParams)){ + if (!MockApiUtils.checkParamsCompliance(jsonArray, mockConfigRequestParams, StringUtils.equals(paramsFilterType, "And"))) { + return false; + } } } else { JSON mockExpectJsonArray = MockApiUtils.getExpectBodyParams(expectBodyObject);