@Test public void testPickMatchingApiDefinitions() { 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); Set<ApiDefinition> apiDefinitions = new HashSet<>(); String apiName1 = "some_customized_api"; ApiDefinition api1 = new ApiDefinition(apiName1) .setPredicateItems(Collections.singleton( new ApiPathPredicateItem().setPattern("/product/**") .setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX) )); String apiName2 = "another_customized_api"; ApiDefinition api2 = new ApiDefinition(apiName2) .setPredicateItems(new HashSet<ApiPredicateItem>() {{ add(new ApiPathPredicateItem().setPattern("/something")); add(new ApiPathPredicateItem().setPattern("/other/**") .setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX)); }}); apiDefinitions.add(api1); apiDefinitions.add(api2); GatewayApiDefinitionManager.loadApiDefinitions(apiDefinitions); SentinelGatewayFilter filter = new SentinelGatewayFilter(); when(requestPath.value()).thenReturn("/product/123"); Set<String> matchingApis = filter.pickMatchingApiDefinitions(exchange); assertThat(matchingApis.size()).isEqualTo(1); assertThat(matchingApis.contains(apiName1)).isTrue(); when(requestPath.value()).thenReturn("/products"); assertThat(filter.pickMatchingApiDefinitions(exchange).size()).isZero(); when(requestPath.value()).thenReturn("/something"); matchingApis = filter.pickMatchingApiDefinitions(exchange); assertThat(matchingApis.size()).isEqualTo(1); assertThat(matchingApis.contains(apiName2)).isTrue(); when(requestPath.value()).thenReturn("/other/foo/3"); matchingApis = filter.pickMatchingApiDefinitions(exchange); assertThat(matchingApis.size()).isEqualTo(1); assertThat(matchingApis.contains(apiName2)).isTrue(); }
Set<String> pickMatchingApiDefinitions(ServerWebExchange exchange) { return GatewayApiMatcherManager.getApiMatcherMap().values() .stream() .filter(m -> m.test(exchange)) .map(WebExchangeApiMatcher::getApiName) .collect(Collectors.toSet()); }
SentinelGatewayFilter implements GatewayFilter, GlobalFilter { Set<String> pickMatchingApiDefinitions(ServerWebExchange exchange) { return GatewayApiMatcherManager.getApiMatcherMap().values() .stream() .filter(m -> m.test(exchange)) .map(WebExchangeApiMatcher::getApiName) .collect(Collectors.toSet()); } }
SentinelGatewayFilter implements GatewayFilter, GlobalFilter { Set<String> pickMatchingApiDefinitions(ServerWebExchange exchange) { return GatewayApiMatcherManager.getApiMatcherMap().values() .stream() .filter(m -> m.test(exchange)) .map(WebExchangeApiMatcher::getApiName) .collect(Collectors.toSet()); }  }
SentinelGatewayFilter implements GatewayFilter, GlobalFilter { Set<String> pickMatchingApiDefinitions(ServerWebExchange exchange) { return GatewayApiMatcherManager.getApiMatcherMap().values() .stream() .filter(m -> m.test(exchange)) .map(WebExchangeApiMatcher::getApiName) .collect(Collectors.toSet()); }  @Override Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain); }
SentinelGatewayFilter implements GatewayFilter, GlobalFilter { Set<String> pickMatchingApiDefinitions(ServerWebExchange exchange) { return GatewayApiMatcherManager.getApiMatcherMap().values() .stream() .filter(m -> m.test(exchange)) .map(WebExchangeApiMatcher::getApiName) .collect(Collectors.toSet()); }  @Override Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain);  }