|
17 | 17 |
|
18 | 18 | package org.apache.shenyu.plugin.context.path; |
19 | 19 |
|
| 20 | +import org.apache.commons.lang3.StringUtils; |
20 | 21 | import org.apache.shenyu.common.constant.Constants; |
| 22 | +import org.apache.shenyu.common.dto.MetaData; |
21 | 23 | import org.apache.shenyu.common.dto.RuleData; |
22 | 24 | import org.apache.shenyu.common.dto.SelectorData; |
23 | 25 | import org.apache.shenyu.common.dto.convert.rule.impl.ContextMappingRuleHandle; |
|
29 | 31 | import org.apache.shenyu.plugin.api.result.DefaultShenyuResult; |
30 | 32 | import org.apache.shenyu.plugin.api.result.ShenyuResult; |
31 | 33 | import org.apache.shenyu.plugin.api.utils.SpringBeanUtils; |
| 34 | +import org.apache.shenyu.plugin.base.cache.MetaDataCache; |
32 | 35 | import org.apache.shenyu.plugin.base.utils.CacheKeyUtils; |
33 | 36 | import org.junit.jupiter.api.Assertions; |
34 | 37 | import org.junit.jupiter.api.BeforeEach; |
35 | 38 | import org.junit.jupiter.api.Test; |
36 | 39 | import org.junit.jupiter.params.ParameterizedTest; |
| 40 | +import org.junit.jupiter.params.provider.CsvSource; |
37 | 41 | import org.junit.jupiter.params.provider.ValueSource; |
| 42 | +import org.mockito.MockedStatic; |
38 | 43 | import org.springframework.context.ConfigurableApplicationContext; |
39 | 44 | import org.springframework.mock.http.server.reactive.MockServerHttpRequest; |
40 | 45 | import org.springframework.mock.web.server.MockServerWebExchange; |
|
44 | 49 | import static org.apache.shenyu.plugin.context.path.handler.ContextPathPluginDataHandler.CACHED_HANDLE; |
45 | 50 | import static org.junit.jupiter.api.Assertions.assertEquals; |
46 | 51 | import static org.junit.jupiter.api.Assertions.assertFalse; |
| 52 | +import static org.junit.jupiter.api.Assertions.assertSame; |
47 | 53 | import static org.mockito.Mockito.mock; |
| 54 | +import static org.mockito.Mockito.mockStatic; |
48 | 55 | import static org.mockito.Mockito.verify; |
49 | 56 | import static org.mockito.Mockito.when; |
50 | 57 |
|
@@ -105,6 +112,62 @@ public void executeTest() { |
105 | 112 | Assertions.assertDoesNotThrow(() -> contextPathPlugin.doExecute(exchange, chain, selectorData, ruleData)); |
106 | 113 | } |
107 | 114 |
|
| 115 | + @ParameterizedTest |
| 116 | + @CsvSource({ |
| 117 | + "/api, /api, /v2, /v2", |
| 118 | + "/api/, /api/, /v2, /v2", |
| 119 | + "/api, /api, /v2/, /v2/", |
| 120 | + "/api/, /api, /v2, /v2/", |
| 121 | + "/api/orders, /api, /v2, /v2/orders", |
| 122 | + "/api/a%2Fb, /api, /v2, /v2/a%2Fb", |
| 123 | + "/api/orders, , /v2, /v2/api/orders", |
| 124 | + "/api/orders, '', /v2, /v2/api/orders" |
| 125 | + }) |
| 126 | + public void testAddPrefixAfterRemovingContextPath(final String path, final String contextPath, |
| 127 | + final String addPrefix, final String expectedPath) { |
| 128 | + shenyuContext.setPath(path); |
| 129 | + ContextMappingRuleHandle handle = new ContextMappingRuleHandle(); |
| 130 | + handle.setContextPath(contextPath); |
| 131 | + handle.setAddPrefix(addPrefix); |
| 132 | + when(ruleData.getId()).thenReturn("addPrefix"); |
| 133 | + CACHED_HANDLE.get().cachedHandle(CacheKeyUtils.INST.getKey(ruleData), handle); |
| 134 | + |
| 135 | + contextPathPlugin.doExecute(exchange, chain, selectorData, ruleData).block(); |
| 136 | + |
| 137 | + assertEquals(expectedPath, shenyuContext.getRealUrl()); |
| 138 | + assertEquals(StringUtils.isBlank(contextPath) ? null : contextPath, exchange.getAttribute(Constants.CONTEXT_PATH)); |
| 139 | + verify(chain).execute(exchange); |
| 140 | + } |
| 141 | + |
| 142 | + @ParameterizedTest |
| 143 | + @ValueSource(booleans = {true, false}) |
| 144 | + public void testExactContextPathWithPrefixAndMetadataRewrite(final boolean metadataExists) { |
| 145 | + shenyuContext.setPath("/api"); |
| 146 | + MetaData original = new MetaData(); |
| 147 | + MetaData rewritten = new MetaData(); |
| 148 | + exchange.getAttributes().put(Constants.META_DATA, original); |
| 149 | + ContextMappingRuleHandle handle = new ContextMappingRuleHandle(); |
| 150 | + handle.setContextPath("/api"); |
| 151 | + handle.setAddPrefix("/v2"); |
| 152 | + handle.setRewriteContextPath("/rewritten"); |
| 153 | + when(ruleData.getId()).thenReturn("addPrefixWithMetadata"); |
| 154 | + CACHED_HANDLE.get().cachedHandle(CacheKeyUtils.INST.getKey(ruleData), handle); |
| 155 | + MetaDataCache cache = mock(MetaDataCache.class); |
| 156 | + try (MockedStatic<MetaDataCache> mocked = mockStatic(MetaDataCache.class)) { |
| 157 | + mocked.when(MetaDataCache::getInstance).thenReturn(cache); |
| 158 | + when(cache.obtain("/rewritten")).thenReturn(metadataExists ? rewritten : null); |
| 159 | + |
| 160 | + contextPathPlugin.doExecute(exchange, chain, selectorData, ruleData).block(); |
| 161 | + |
| 162 | + assertEquals("/v2", shenyuContext.getRealUrl()); |
| 163 | + assertEquals("/rewritten", exchange.getAttribute(Constants.REWRITE_CONTEXT_PATH)); |
| 164 | + assertSame(original, exchange.getAttribute(Constants.OLD_CONTEXT_PATH_META_DATA)); |
| 165 | + assertSame(metadataExists ? rewritten : original, exchange.getAttribute(Constants.META_DATA)); |
| 166 | + verify(cache).obtain("/rewritten"); |
| 167 | + verify(chain).execute(exchange); |
| 168 | + } |
| 169 | + } |
| 170 | + |
108 | 171 | @ParameterizedTest |
109 | 172 | @ValueSource(strings = {"/foo", "/api/order/longer"}) |
110 | 173 | public void testNonMatchingContextPathPreservesExchange(final String configuredPath) { |
|
0 commit comments