网关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,31 +1,29 @@
package com.sf.vertx.handle; package com.sf.vertx.handle;
import com.sf.vertx.api.pojo.AppConfig;
import com.sf.vertx.api.pojo.MockResponse; import com.sf.vertx.api.pojo.MockResponse;
import com.sf.vertx.constans.SacErrorCode; import com.sf.vertx.constans.SacErrorCode;
import io.vertx.ext.web.RoutingContext; import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.HttpException; import io.vertx.ext.web.handler.HttpException;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
@Slf4j @Slf4j
public class ApiMockHandlerImpl implements ApiMockHandler { public class ApiMockHandlerImpl implements ApiMockHandler {
@Override @Override
public void handle(RoutingContext rc) { public void handle(RoutingContext rc) {
try { try {
String cacheKey = AppConfigHandler.getApiCodeConfigCacheKey(rc); String cacheKey = AppConfigHandler.getApiCodeConfigCacheKey(rc);
// mock // mock
MockResponse mockResponse = AppConfigHandler.mock(cacheKey,rc); MockResponse mockResponse = AppConfigHandler.mock(cacheKey, rc);
if (mockResponse != null) { if (mockResponse != null) {
rc.fail(new MockException(mockResponse.getHttpStatus(), mockResponse.getMockResponse())); rc.fail(new MockException(mockResponse.getHttpStatus(), mockResponse.getMockResponse()));
return; return;
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
rc.fail(new HttpException(SacErrorCode.DEFAULT_ERROR_CODE)); rc.fail(new HttpException(SacErrorCode.DEFAULT_ERROR_CODE));
return; return;
} }
rc.next(); rc.next();
} }
} }

View File

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

View File

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