Fix the empty value matching bug in GatewayParamParser of API gateway adapter common module
Signed-off-by: Eric Zhao <sczyh16@gmail.com>
This commit is contained in:
parent
e50be35a9d
commit
fb31bb8d4d
|
|
@ -103,7 +103,7 @@ public class GatewayParamParser<T> {
|
||||||
private String parseClientIp(/*@Valid*/ GatewayParamFlowItem item, T request) {
|
private String parseClientIp(/*@Valid*/ GatewayParamFlowItem item, T request) {
|
||||||
String clientIp = requestItemParser.getRemoteAddress(request);
|
String clientIp = requestItemParser.getRemoteAddress(request);
|
||||||
String pattern = item.getPattern();
|
String pattern = item.getPattern();
|
||||||
if (pattern == null) {
|
if (StringUtil.isEmpty(pattern)) {
|
||||||
return clientIp;
|
return clientIp;
|
||||||
}
|
}
|
||||||
return parseWithMatchStrategyInternal(item.getMatchStrategy(), clientIp, pattern);
|
return parseWithMatchStrategyInternal(item.getMatchStrategy(), clientIp, pattern);
|
||||||
|
|
@ -114,7 +114,7 @@ public class GatewayParamParser<T> {
|
||||||
String pattern = item.getPattern();
|
String pattern = item.getPattern();
|
||||||
// TODO: what if the header has multiple values?
|
// TODO: what if the header has multiple values?
|
||||||
String headerValue = requestItemParser.getHeader(request, headerKey);
|
String headerValue = requestItemParser.getHeader(request, headerKey);
|
||||||
if (pattern == null) {
|
if (StringUtil.isEmpty(pattern)) {
|
||||||
return headerValue;
|
return headerValue;
|
||||||
}
|
}
|
||||||
// Match value according to regex pattern or exact mode.
|
// Match value according to regex pattern or exact mode.
|
||||||
|
|
@ -124,7 +124,7 @@ public class GatewayParamParser<T> {
|
||||||
private String parseHost(/*@Valid*/ GatewayParamFlowItem item, T request) {
|
private String parseHost(/*@Valid*/ GatewayParamFlowItem item, T request) {
|
||||||
String pattern = item.getPattern();
|
String pattern = item.getPattern();
|
||||||
String host = requestItemParser.getHeader(request, "Host");
|
String host = requestItemParser.getHeader(request, "Host");
|
||||||
if (pattern == null) {
|
if (StringUtil.isEmpty(pattern)) {
|
||||||
return host;
|
return host;
|
||||||
}
|
}
|
||||||
// Match value according to regex pattern or exact mode.
|
// Match value according to regex pattern or exact mode.
|
||||||
|
|
@ -135,7 +135,7 @@ public class GatewayParamParser<T> {
|
||||||
String paramName = item.getFieldName();
|
String paramName = item.getFieldName();
|
||||||
String pattern = item.getPattern();
|
String pattern = item.getPattern();
|
||||||
String param = requestItemParser.getUrlParam(request, paramName);
|
String param = requestItemParser.getUrlParam(request, paramName);
|
||||||
if (pattern == null) {
|
if (StringUtil.isEmpty(pattern)) {
|
||||||
return param;
|
return param;
|
||||||
}
|
}
|
||||||
// Match value according to regex pattern or exact mode.
|
// Match value according to regex pattern or exact mode.
|
||||||
|
|
@ -146,7 +146,7 @@ public class GatewayParamParser<T> {
|
||||||
String cookieName = item.getFieldName();
|
String cookieName = item.getFieldName();
|
||||||
String pattern = item.getPattern();
|
String pattern = item.getPattern();
|
||||||
String param = requestItemParser.getCookieValue(request, cookieName);
|
String param = requestItemParser.getCookieValue(request, cookieName);
|
||||||
if (pattern == null) {
|
if (StringUtil.isEmpty(pattern)) {
|
||||||
return param;
|
return param;
|
||||||
}
|
}
|
||||||
// Match value according to regex pattern or exact mode.
|
// Match value according to regex pattern or exact mode.
|
||||||
|
|
|
||||||
|
|
@ -186,6 +186,35 @@ public class GatewayParamParserTest {
|
||||||
assertThat(params[apiRule1.getParamItem().getIndex()]).isEqualTo(expectedUrlParamValue2);
|
assertThat(params[apiRule1.getParamItem().getIndex()]).isEqualTo(expectedUrlParamValue2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testParseParametersWithEmptyItemPattern() {
|
||||||
|
RequestItemParser<Object> itemParser = mock(RequestItemParser.class);
|
||||||
|
GatewayParamParser<Object> paramParser = new GatewayParamParser<>(itemParser);
|
||||||
|
// Create a fake request.
|
||||||
|
Object request = new Object();
|
||||||
|
// Prepare gateway rules.
|
||||||
|
Set<GatewayFlowRule> rules = new HashSet<>();
|
||||||
|
final String routeId = "my_test_route_DS(*H";
|
||||||
|
final String headerName = "X-Sentinel-Flag";
|
||||||
|
GatewayFlowRule routeRule1 = new GatewayFlowRule(routeId)
|
||||||
|
.setCount(10)
|
||||||
|
.setIntervalSec(2)
|
||||||
|
.setParamItem(new GatewayParamFlowItem()
|
||||||
|
.setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_HEADER)
|
||||||
|
.setFieldName(headerName)
|
||||||
|
.setPattern("")
|
||||||
|
.setMatchStrategy(SentinelGatewayConstants.PARAM_MATCH_STRATEGY_EXACT)
|
||||||
|
);
|
||||||
|
rules.add(routeRule1);
|
||||||
|
GatewayRuleManager.loadRules(rules);
|
||||||
|
|
||||||
|
mockSingleHeader(itemParser, headerName, "Sent1nel");
|
||||||
|
Object[] params = paramParser.parseParameterFor(routeId, request, routeIdPredicate);
|
||||||
|
assertThat(params.length).isEqualTo(1);
|
||||||
|
// Empty pattern should not take effect.
|
||||||
|
assertThat(params[routeRule1.getParamItem().getIndex()]).isEqualTo("Sent1nel");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testParseParametersWithItemPatternMatching() {
|
public void testParseParametersWithItemPatternMatching() {
|
||||||
RequestItemParser<Object> itemParser = mock(RequestItemParser.class);
|
RequestItemParser<Object> itemParser = mock(RequestItemParser.class);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue