Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected Mono<Void> doExecute(final ServerWebExchange exchange, final ShenyuPlu
&& ThreadLocalRandom.current().nextInt(100) < percentage) {
rewriteUri = rewriteHandle.getReplace().contains("{") && rewriteHandle.getRegex().contains("{")
? PathMatchUtils.replaceAll(rewriteHandle.getReplace(), rewriteHandle.getRegex().substring(rewriteHandle.getRegex().indexOf("{")),
rewriteUri.substring(rewriteHandle.getRegex().indexOf("{")))
rewriteUri.substring(rewriteHandle.getRegex().indexOf("{")))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the code is same?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, the tab change here is just from resolving a merge conflict between my branch and master. My actual code changes are in the first commit (f4a3193) of this PR.It is likely that the same changes were already merged into master, so they no longer appear in the diff.

: rewriteUri.replaceAll(rewriteHandle.getRegex(), rewriteHandle.getReplace());
Map<String, Object> attributes = exchange.getAttributes();
if (Optional.ofNullable(rewriteHandle.getRewriteMetaData()).orElse(false)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,32 @@ public void shouldReturnNewURIForRewritePlugin() {
assertEquals("/shenyu/rewrite", exchange.getAttributes().get(Constants.REWRITE_URI));
}

@Test
public void shouldRewritePathVariableWithCompleteValue() {
RuleData data = new RuleData();
data.setHandle("{\"regex\":\"/shenyu/{id}\",\"replace\":\"/shenyu/{id}\"}");
RewriteHandle rewriteHandle = GsonUtils.getGson().fromJson(data.getHandle(), RewriteHandle.class);
RewritePluginDataHandler.CACHED_HANDLE.get().cachedHandle(CacheKeyUtils.INST.getKey(data), rewriteHandle);
ServerWebExchange rewriteExchange = MockServerWebExchange.from(MockServerHttpRequest.get("/shenyu/123").build());
rewriteExchange.getAttributes().put(Constants.CONTEXT, new ShenyuContext());
when(chain.execute(rewriteExchange)).thenReturn(Mono.empty());
SelectorData selectorData = mock(SelectorData.class);
StepVerifier.create(rewritePlugin.doExecute(rewriteExchange, chain, selectorData, data)).expectSubscription().verifyComplete();
assertEquals("/shenyu/123", rewriteExchange.getAttributes().get(Constants.REWRITE_URI));
}

@Test
public void shouldNotThrowWhenOnlyReplaceContainsPlaceholder() {
RuleData data = new RuleData();
data.setHandle("{\"regex\":\"/shenyu/.*\",\"replace\":\"/new/{id}\"}");
RewriteHandle rewriteHandle = GsonUtils.getGson().fromJson(data.getHandle(), RewriteHandle.class);
RewritePluginDataHandler.CACHED_HANDLE.get().cachedHandle(CacheKeyUtils.INST.getKey(data), rewriteHandle);
when(chain.execute(exchange)).thenReturn(Mono.empty());
SelectorData selectorData = mock(SelectorData.class);
StepVerifier.create(rewritePlugin.doExecute(exchange, chain, selectorData, data)).expectSubscription().verifyComplete();
assertEquals("/new/{id}", exchange.getAttributes().get(Constants.REWRITE_URI));
}

@Test
public void testSkip() {
final boolean result = rewritePlugin.skip(exchange);
Expand Down
Loading