Search in sources :

Example 6 with GatewayParamFlowItem

use of com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem in project Sentinel by alibaba.

the class SpringCloudGatewayParamParserTest method testParseParametersWithItems.

@Test
public void testParseParametersWithItems() {
    // Mock a request.
    ServerWebExchange exchange = mock(ServerWebExchange.class);
    ServerHttpRequest request = mock(ServerHttpRequest.class);
    when(exchange.getRequest()).thenReturn(request);
    RequestPath requestPath = mock(RequestPath.class);
    when(request.getPath()).thenReturn(requestPath);
    // Prepare gateway rules.
    Set<GatewayFlowRule> rules = new HashSet<>();
    String routeId1 = "my_test_route_A";
    String api1 = "my_test_route_B";
    String headerName = "X-Sentinel-Flag";
    String paramName = "p";
    GatewayFlowRule routeRule1 = new GatewayFlowRule(routeId1).setCount(2).setIntervalSec(2).setBurst(2).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_CLIENT_IP));
    GatewayFlowRule routeRule2 = new GatewayFlowRule(routeId1).setCount(10).setIntervalSec(1).setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER).setMaxQueueingTimeoutMs(600).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_HEADER).setFieldName(headerName));
    GatewayFlowRule routeRule3 = new GatewayFlowRule(routeId1).setCount(20).setIntervalSec(1).setBurst(5).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM).setFieldName(paramName));
    GatewayFlowRule routeRule4 = new GatewayFlowRule(routeId1).setCount(120).setIntervalSec(10).setBurst(30).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_HOST));
    GatewayFlowRule apiRule1 = new GatewayFlowRule(api1).setResourceMode(SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME).setCount(5).setIntervalSec(1).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM).setFieldName(paramName));
    rules.add(routeRule1);
    rules.add(routeRule2);
    rules.add(routeRule3);
    rules.add(routeRule4);
    rules.add(apiRule1);
    GatewayRuleManager.loadRules(rules);
    String expectedHost = "hello.test.sentinel";
    String expectedAddress = "66.77.88.99";
    String expectedHeaderValue1 = "Sentinel";
    String expectedUrlParamValue1 = "17";
    mockClientHostAddress(request, expectedAddress);
    Map<String, String> expectedHeaders = new HashMap<String, String>() {

        {
            put(headerName, expectedHeaderValue1);
            put("Host", expectedHost);
        }
    };
    mockHeaders(request, expectedHeaders);
    mockSingleUrlParam(request, paramName, expectedUrlParamValue1);
    Object[] params = paramParser.parseParameterFor(routeId1, exchange, e -> e.getResourceMode() == 0);
    assertThat(params.length).isEqualTo(4);
    assertThat(params[routeRule1.getParamItem().getIndex()]).isEqualTo(expectedAddress);
    assertThat(params[routeRule2.getParamItem().getIndex()]).isEqualTo(expectedHeaderValue1);
    assertThat(params[routeRule3.getParamItem().getIndex()]).isEqualTo(expectedUrlParamValue1);
    assertThat(params[routeRule4.getParamItem().getIndex()]).isEqualTo(expectedHost);
    assertThat(paramParser.parseParameterFor(api1, exchange, e -> e.getResourceMode() == 0).length).isZero();
    String expectedUrlParamValue2 = "fs";
    mockSingleUrlParam(request, paramName, expectedUrlParamValue2);
    params = paramParser.parseParameterFor(api1, exchange, e -> e.getResourceMode() == 1);
    assertThat(params.length).isEqualTo(1);
    assertThat(params[apiRule1.getParamItem().getIndex()]).isEqualTo(expectedUrlParamValue2);
}
Also used : RequestPath(org.springframework.http.server.RequestPath) GatewayParamParser(com.alibaba.csp.sentinel.adapter.gateway.common.param.GatewayParamParser) GatewayParamFlowItem(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem) SentinelGatewayConstants(com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants) HttpHeaders(org.springframework.http.HttpHeaders) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Set(java.util.Set) MultiValueMap(org.springframework.util.MultiValueMap) HashMap(java.util.HashMap) Test(org.junit.Test) RequestPath(org.springframework.http.server.RequestPath) Mockito.when(org.mockito.Mockito.when) InetSocketAddress(java.net.InetSocketAddress) ServerWebExchange(org.springframework.web.server.ServerWebExchange) InetAddress(java.net.InetAddress) HashSet(java.util.HashSet) GatewayApiDefinitionManager(com.alibaba.csp.sentinel.adapter.gateway.common.api.GatewayApiDefinitionManager) RuleConstant(com.alibaba.csp.sentinel.slots.block.RuleConstant) GatewayFlowRule(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule) Map(java.util.Map) After(org.junit.After) GatewayRuleManager(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager) Before(org.junit.Before) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) Mockito.mock(org.mockito.Mockito.mock) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HashMap(java.util.HashMap) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) GatewayFlowRule(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule) GatewayParamFlowItem(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with GatewayParamFlowItem

use of com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem in project Sentinel by alibaba.

the class GatewayRuleConfig method initGatewayRules.

private void initGatewayRules() {
    Set<GatewayFlowRule> rules = new HashSet<>();
    rules.add(new GatewayFlowRule("images").setCount(10).setIntervalSec(1));
    rules.add(new GatewayFlowRule("images").setCount(2).setIntervalSec(2).setBurst(2).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_CLIENT_IP)));
    rules.add(new GatewayFlowRule("comments").setCount(3).setIntervalSec(1).setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER).setMaxQueueingTimeoutMs(6000).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_HEADER).setFieldName("X-Sentinel-Flag")));
    rules.add(new GatewayFlowRule("comments").setCount(1).setIntervalSec(1).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM).setFieldName("pa")));
    rules.add(new GatewayFlowRule("some_customized_api").setResourceMode(SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME).setCount(5).setIntervalSec(1).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM).setFieldName("pn")));
    GatewayRuleManager.loadRules(rules);
}
Also used : GatewayParamFlowItem(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem) GatewayFlowRule(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule) HashSet(java.util.HashSet)

Example 8 with GatewayParamFlowItem

use of com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem in project Sentinel by alibaba.

the class GatewayRuleConfig method initGatewayRules.

private void initGatewayRules() {
    Set<GatewayFlowRule> rules = new HashSet<>();
    rules.add(new GatewayFlowRule("aliyun-product-route").setCount(10).setIntervalSec(1));
    rules.add(new GatewayFlowRule("aliyun-product-route").setCount(2).setIntervalSec(2).setBurst(2).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_CLIENT_IP)));
    rules.add(new GatewayFlowRule("another-route-httpbin").setCount(10).setIntervalSec(1).setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER).setMaxQueueingTimeoutMs(600).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_HEADER).setFieldName("X-Sentinel-Flag")));
    rules.add(new GatewayFlowRule("another-route-httpbin").setCount(1).setIntervalSec(1).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM).setFieldName("pa")));
    rules.add(new GatewayFlowRule("some_customized_api").setResourceMode(SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME).setCount(5).setIntervalSec(1).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM).setFieldName("pn")));
    GatewayRuleManager.loadRules(rules);
}
Also used : GatewayParamFlowItem(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem) GatewayFlowRule(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule) HashSet(java.util.HashSet)

Example 9 with GatewayParamFlowItem

use of com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem in project Sentinel by alibaba.

the class GatewayConfiguration method initGatewayRules.

private void initGatewayRules() {
    Set<GatewayFlowRule> rules = new HashSet<>();
    rules.add(new GatewayFlowRule("aliyun_route").setCount(10).setIntervalSec(1));
    rules.add(new GatewayFlowRule("aliyun_route").setCount(2).setIntervalSec(2).setBurst(2).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_CLIENT_IP)));
    rules.add(new GatewayFlowRule("httpbin_route").setCount(10).setIntervalSec(1).setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER).setMaxQueueingTimeoutMs(600).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_HEADER).setFieldName("X-Sentinel-Flag")));
    rules.add(new GatewayFlowRule("httpbin_route").setCount(1).setIntervalSec(1).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM).setFieldName("pa")));
    rules.add(new GatewayFlowRule("httpbin_route").setCount(2).setIntervalSec(30).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM).setFieldName("type").setPattern("warn").setMatchStrategy(SentinelGatewayConstants.PARAM_MATCH_STRATEGY_CONTAINS)));
    rules.add(new GatewayFlowRule("some_customized_api").setResourceMode(SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME).setCount(5).setIntervalSec(1).setParamItem(new GatewayParamFlowItem().setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM).setFieldName("pn")));
    GatewayRuleManager.loadRules(rules);
}
Also used : GatewayParamFlowItem(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem) GatewayFlowRule(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule) HashSet(java.util.HashSet)

Example 10 with GatewayParamFlowItem

use of com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem in project Sentinel by alibaba.

the class GatewayParamParserTest method testParseParametersWithEmptyItemPattern.

@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");
}
Also used : GatewayParamFlowItem(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem) GatewayFlowRule(com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

GatewayParamFlowItem (com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayParamFlowItem)20 GatewayFlowRule (com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule)15 HashSet (java.util.HashSet)9 Test (org.junit.Test)4 HashMap (java.util.HashMap)2 SentinelGatewayConstants (com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants)1 GatewayApiDefinitionManager (com.alibaba.csp.sentinel.adapter.gateway.common.api.GatewayApiDefinitionManager)1 GatewayParamParser (com.alibaba.csp.sentinel.adapter.gateway.common.param.GatewayParamParser)1 GatewayRuleManager (com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager)1 RuleConstant (com.alibaba.csp.sentinel.slots.block.RuleConstant)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 Map (java.util.Map)1 Set (java.util.Set)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 After (org.junit.After)1 Before (org.junit.Before)1 Mockito.mock (org.mockito.Mockito.mock)1 Mockito.when (org.mockito.Mockito.when)1 HttpHeaders (org.springframework.http.HttpHeaders)1