|
45 | 45 |
|
46 | 46 | import static org.junit.jupiter.api.Assertions.assertEquals; |
47 | 47 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| 48 | +import static org.mockito.ArgumentMatchers.any; |
| 49 | +import static org.mockito.ArgumentMatchers.argThat; |
48 | 50 | import static org.mockito.Mockito.mock; |
49 | 51 | import static org.mockito.Mockito.spy; |
50 | 52 | import static org.mockito.Mockito.verify; |
@@ -202,6 +204,73 @@ public void executeSelectorManyMatch() { |
202 | 204 | verify(testShenyuPlugin).doExecute(exchange, shenyuPluginChain, selectorData, ruleData); |
203 | 205 | } |
204 | 206 |
|
| 207 | + @Test |
| 208 | + public void selectorCacheShouldNotShadowDynamicCandidate() { |
| 209 | + ConditionData uriCondition = new ConditionData(); |
| 210 | + uriCondition.setOperator("match"); |
| 211 | + uriCondition.setParamName("/"); |
| 212 | + uriCondition.setParamType("uri"); |
| 213 | + uriCondition.setParamValue("/http/**"); |
| 214 | + ConditionData headerCondition = new ConditionData(); |
| 215 | + headerCondition.setOperator("="); |
| 216 | + headerCondition.setParamName("X-Tenant"); |
| 217 | + headerCondition.setParamType("header"); |
| 218 | + headerCondition.setParamValue("a"); |
| 219 | + SelectorData genericSelector = SelectorData.builder() |
| 220 | + .id("generic").pluginName("SHENYU").enabled(true).continued(false).logged(false) |
| 221 | + .matchMode(0).sort(2).conditionList(Collections.singletonList(uriCondition)) |
| 222 | + .type(SelectorTypeEnum.CUSTOM_FLOW.getCode()).build(); |
| 223 | + SelectorData tenantSelector = SelectorData.builder() |
| 224 | + .id("tenant").pluginName("SHENYU").enabled(true).continued(false).logged(false) |
| 225 | + .matchMode(0).sort(1).conditionList(List.of(uriCondition, headerCondition)) |
| 226 | + .type(SelectorTypeEnum.CUSTOM_FLOW.getCode()).build(); |
| 227 | + BaseDataCache.getInstance().cachePluginData(pluginData); |
| 228 | + BaseDataCache.getInstance().cacheSelectData(genericSelector); |
| 229 | + BaseDataCache.getInstance().cacheSelectData(tenantSelector); |
| 230 | + |
| 231 | + ServerWebExchange ordinaryExchange = exchangeWithTenant("b"); |
| 232 | + StepVerifier.create(testShenyuPlugin.execute(ordinaryExchange, shenyuPluginChain)).verifyComplete(); |
| 233 | + ServerWebExchange tenantExchange = exchangeWithTenant("a"); |
| 234 | + StepVerifier.create(testShenyuPlugin.execute(tenantExchange, shenyuPluginChain)).verifyComplete(); |
| 235 | + |
| 236 | + verify(testShenyuPlugin).doExecute(any(), any(), argThat(selector -> "tenant".equals(selector.getId())), any()); |
| 237 | + } |
| 238 | + |
| 239 | + @Test |
| 240 | + public void ruleCacheShouldNotShadowDynamicCandidate() { |
| 241 | + ConditionData uriCondition = new ConditionData(); |
| 242 | + uriCondition.setOperator("match"); |
| 243 | + uriCondition.setParamName("/"); |
| 244 | + uriCondition.setParamType("uri"); |
| 245 | + uriCondition.setParamValue("/http/**"); |
| 246 | + ConditionData headerCondition = new ConditionData(); |
| 247 | + headerCondition.setOperator("="); |
| 248 | + headerCondition.setParamName("X-Tenant"); |
| 249 | + headerCondition.setParamType("header"); |
| 250 | + headerCondition.setParamValue("a"); |
| 251 | + this.selectorData.setContinued(true); |
| 252 | + this.selectorData.setMatchMode(0); |
| 253 | + this.selectorData.setLogged(false); |
| 254 | + this.selectorData.setConditionList(Collections.singletonList(uriCondition)); |
| 255 | + RuleData genericRule = RuleData.builder() |
| 256 | + .id("generic").pluginName("SHENYU").selectorId("1").enabled(true).logged(false) |
| 257 | + .matchMode(0).sort(2).conditionDataList(Collections.singletonList(uriCondition)).build(); |
| 258 | + RuleData tenantRule = RuleData.builder() |
| 259 | + .id("tenant").pluginName("SHENYU").selectorId("1").enabled(true).logged(false) |
| 260 | + .matchMode(0).sort(1).conditionDataList(List.of(uriCondition, headerCondition)).build(); |
| 261 | + BaseDataCache.getInstance().cachePluginData(pluginData); |
| 262 | + BaseDataCache.getInstance().cacheSelectData(selectorData); |
| 263 | + BaseDataCache.getInstance().cacheRuleData(genericRule); |
| 264 | + BaseDataCache.getInstance().cacheRuleData(tenantRule); |
| 265 | + |
| 266 | + ServerWebExchange ordinaryExchange = exchangeWithTenant("b"); |
| 267 | + StepVerifier.create(testShenyuPlugin.execute(ordinaryExchange, shenyuPluginChain)).verifyComplete(); |
| 268 | + ServerWebExchange tenantExchange = exchangeWithTenant("a"); |
| 269 | + StepVerifier.create(testShenyuPlugin.execute(tenantExchange, shenyuPluginChain)).verifyComplete(); |
| 270 | + |
| 271 | + verify(testShenyuPlugin).doExecute(any(), any(), any(), argThat(rule -> "tenant".equals(rule.getId()))); |
| 272 | + } |
| 273 | + |
205 | 274 | @Test |
206 | 275 | public void executeRuleManyMatch() { |
207 | 276 | List<ConditionData> conditionDataList = Collections.singletonList(conditionData); |
@@ -382,6 +451,13 @@ private void mockShenyuConfig() { |
382 | 451 | SpringBeanUtils.getInstance().setApplicationContext(context); |
383 | 452 | } |
384 | 453 |
|
| 454 | + private ServerWebExchange exchangeWithTenant(final String tenant) { |
| 455 | + ServerWebExchange result = MockServerWebExchange.from(MockServerHttpRequest.get("/http/SHENYU/SHENYU") |
| 456 | + .header("X-Tenant", tenant).build()); |
| 457 | + result.getAttributes().put(Constants.CONTEXT, mock(ShenyuContext.class)); |
| 458 | + return result; |
| 459 | + } |
| 460 | + |
385 | 461 | static class TestShenyuPlugin extends AbstractShenyuPlugin { |
386 | 462 |
|
387 | 463 | @Override |
|
0 commit comments