Skip to content

Commit 69f8244

Browse files
authored
fix(context-path): retain empty suffix when adding a prefix (#8477)
1 parent 9af4388 commit 69f8244

2 files changed

Lines changed: 65 additions & 1 deletion

File tree

‎shenyu-plugin/shenyu-plugin-context-path/src/main/java/org/apache/shenyu/plugin/context/path/ContextPathPlugin.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ private void buildRealURI(final ServerWebExchange exchange, final ShenyuContext
105105
}
106106
String addPrefix = handle.getAddPrefix();
107107
if (StringUtils.isNoneBlank(addPrefix)) {
108-
if (StringUtils.isNotBlank(realURI)) {
108+
// An exact context-path match leaves an empty suffix that must not restore the original path.
109+
if (StringUtils.isNotBlank(contextPath)) {
109110
realURI = addPrefix + realURI;
110111
} else {
111112
realURI = addPrefix + context.getPath();

‎shenyu-plugin/shenyu-plugin-context-path/src/test/java/org/apache/shenyu/plugin/context/path/ContextPathPluginTest.java‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717

1818
package org.apache.shenyu.plugin.context.path;
1919

20+
import org.apache.commons.lang3.StringUtils;
2021
import org.apache.shenyu.common.constant.Constants;
22+
import org.apache.shenyu.common.dto.MetaData;
2123
import org.apache.shenyu.common.dto.RuleData;
2224
import org.apache.shenyu.common.dto.SelectorData;
2325
import org.apache.shenyu.common.dto.convert.rule.impl.ContextMappingRuleHandle;
@@ -29,12 +31,15 @@
2931
import org.apache.shenyu.plugin.api.result.DefaultShenyuResult;
3032
import org.apache.shenyu.plugin.api.result.ShenyuResult;
3133
import org.apache.shenyu.plugin.api.utils.SpringBeanUtils;
34+
import org.apache.shenyu.plugin.base.cache.MetaDataCache;
3235
import org.apache.shenyu.plugin.base.utils.CacheKeyUtils;
3336
import org.junit.jupiter.api.Assertions;
3437
import org.junit.jupiter.api.BeforeEach;
3538
import org.junit.jupiter.api.Test;
3639
import org.junit.jupiter.params.ParameterizedTest;
40+
import org.junit.jupiter.params.provider.CsvSource;
3741
import org.junit.jupiter.params.provider.ValueSource;
42+
import org.mockito.MockedStatic;
3843
import org.springframework.context.ConfigurableApplicationContext;
3944
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
4045
import org.springframework.mock.web.server.MockServerWebExchange;
@@ -44,7 +49,9 @@
4449
import static org.apache.shenyu.plugin.context.path.handler.ContextPathPluginDataHandler.CACHED_HANDLE;
4550
import static org.junit.jupiter.api.Assertions.assertEquals;
4651
import static org.junit.jupiter.api.Assertions.assertFalse;
52+
import static org.junit.jupiter.api.Assertions.assertSame;
4753
import static org.mockito.Mockito.mock;
54+
import static org.mockito.Mockito.mockStatic;
4855
import static org.mockito.Mockito.verify;
4956
import static org.mockito.Mockito.when;
5057

@@ -105,6 +112,62 @@ public void executeTest() {
105112
Assertions.assertDoesNotThrow(() -> contextPathPlugin.doExecute(exchange, chain, selectorData, ruleData));
106113
}
107114

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+
108171
@ParameterizedTest
109172
@ValueSource(strings = {"/foo", "/api/order/longer"})
110173
public void testNonMatchingContextPathPreservesExchange(final String configuredPath) {

0 commit comments

Comments
 (0)