网关mock调整

This commit is contained in:
akun 2024-05-21 17:39:43 +08:00
parent 074ff9d67b
commit ac952deb8a
3 changed files with 26 additions and 23 deletions

View File

@ -1,12 +1,10 @@
package com.sf.vertx.handle;
import com.sf.vertx.api.pojo.AppConfig;
import com.sf.vertx.api.pojo.MockResponse;
import com.sf.vertx.constans.SacErrorCode;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.HttpException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
@Slf4j
public class ApiMockHandlerImpl implements ApiMockHandler {

View File

@ -257,6 +257,7 @@ public class AppConfigHandler {
public static MockResponse mock(String key, RoutingContext rc) {
if (APICODE_CONFIG_MAP.get(key) != null && APICODE_CONFIG_MAP.get(key).getMockDefaultHttpStatus() != null){
// 如果是sac服务query和body参数都从body中取
Integer httpStatus = APICODE_CONFIG_MAP.get(key).getMockDefaultHttpStatus();
String mockResponse = APICODE_CONFIG_MAP.get(key).getMockDefaultResponse();
List<MockExpectation> mockExpectations = APICODE_CONFIG_MAP.get(key).getMockExpectations();
@ -278,7 +279,7 @@ public class AppConfigHandler {
String parameterPosition = matchCondition.getParameterPosition();
String parameterKey = matchCondition.getParameterKey();
List<String> parameterValue = matchCondition.getParameterValue();
String requestParameterValue = getRequestParameterValue(parameterPosition,parameterKey,rc.request(),jsonBody);
String requestParameterValue = getRequestParameterValue(AppConfigHandler.isServiceTypeSac(key),parameterPosition,parameterKey,rc.request(),jsonBody);
if (!MatchType.IS_NULL.equals(matchType) && !MatchType.NOT_NULL.equals(matchType)){
// 需要值而没有设置值直接匹配失败
if (CollectionUtil.isEmpty(parameterValue) || requestParameterValue == null) {
@ -352,9 +353,12 @@ public class AppConfigHandler {
return null;
}
private static String getRequestParameterValue(String parameterPosition, String parameterKey, HttpServerRequest request,JsonObject jsonBody) {
private static String getRequestParameterValue(Boolean isServiceTypeSac,String parameterPosition, String parameterKey, HttpServerRequest request,JsonObject jsonBody) {
switch (parameterPosition){
case "query":
if (isServiceTypeSac){
return jsonBody.getString(parameterKey);
}
return request.getParam(parameterKey);
case "header":
return request.getHeader(parameterKey);

View File

@ -1,5 +1,6 @@
package com.sf.vertx.handle;
import io.vertx.core.buffer.Buffer;
import org.apache.commons.lang3.StringUtils;
import com.sf.vertx.constans.SacErrorCode;
@ -20,7 +21,7 @@ public class RestfulFailureHandlerImpl implements RestfulFailureHandler {
Integer sc = SacErrorCode.DEFAULT_ERROR_CODE;
try {
Throwable failure = frc.failure();
log.info("failure:{}", failure);
log.info("failure:", failure);
if (failure instanceof HttpException) {
HttpException httpException = (HttpException) failure;
sc = httpException.getStatusCode();
@ -49,7 +50,7 @@ public class RestfulFailureHandlerImpl implements RestfulFailureHandler {
frc.response().setChunked(true).setStatusCode(statusCode).putHeader("Content-Type", "application/json")
.putHeader(AppConfigHandler.sacResponseHeaderKey(), String.valueOf(sc))
.putHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(errorJson.size())).end(errorJson.toBuffer());
.end(errorJson.toBuffer());
return;
}