mirror of https://github.com/halo-dev/halo
refactor: register method for reverse proxy registry (#2567)
#### What type of PR is this? /kind improvement /area core /milestone 2.0 #### What this PR does / why we need it: 对反向代理规则注册增加重复注册检查 场景: ReverseProxyRouterFunctionRegistry 中有一个 pluginIdReverseProxyMap 记录了 插件名称和插件的 ReverseProxy 名称对应关系 当重复 fake-plugin -> test-reverse-proxy 时,pluginIdReverseProxyMap 的 value 不会去重,因此需要增加重复注册检查 #### Which issue(s) this PR fixes: Fixes # #### Special notes for your reviewer: how to test it? 本 PR 不需要测试,已经对上述场景添加了单元测试 /cc @halo-dev/sig-halo #### Does this PR introduce a user-facing change? ```release-note None ```pull/2582/head
parent
6cce2202a1
commit
2505c7fe4a
|
@ -47,7 +47,12 @@ public class ReverseProxyRouterFunctionRegistry {
|
|||
final String proxyName = reverseProxy.getMetadata().getName();
|
||||
long stamp = lock.writeLock();
|
||||
try {
|
||||
List<String> reverseProxyNames = pluginIdReverseProxyMap.get(pluginId);
|
||||
if (reverseProxyNames != null && reverseProxyNames.contains(proxyName)) {
|
||||
return Mono.empty();
|
||||
}
|
||||
pluginIdReverseProxyMap.add(pluginId, proxyName);
|
||||
|
||||
// Obtain plugin application context
|
||||
PluginApplicationContext pluginApplicationContext =
|
||||
ExtensionContextRegistry.getInstance().getByPluginId(pluginId);
|
||||
|
@ -62,6 +67,14 @@ public class ReverseProxyRouterFunctionRegistry {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Only for test.
|
||||
*/
|
||||
protected int reverseProxySize(String pluginId) {
|
||||
List<String> names = pluginIdReverseProxyMap.get(pluginId);
|
||||
return names == null ? 0 : names.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove reverse proxy router function by plugin id.
|
||||
*
|
||||
|
|
|
@ -0,0 +1,77 @@
|
|||
package run.halo.app.plugin.resources;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.web.reactive.function.server.RouterFunction;
|
||||
import org.springframework.web.reactive.function.server.ServerResponse;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
import run.halo.app.core.extension.ReverseProxy;
|
||||
import run.halo.app.extension.Metadata;
|
||||
import run.halo.app.plugin.ExtensionContextRegistry;
|
||||
import run.halo.app.plugin.PluginApplicationContext;
|
||||
|
||||
/**
|
||||
* Tests for {@link ReverseProxyRouterFunctionRegistry}.
|
||||
*
|
||||
* @author guqing
|
||||
* @since 2.0.0
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class ReverseProxyRouterFunctionRegistryTest {
|
||||
|
||||
@InjectMocks
|
||||
private ReverseProxyRouterFunctionRegistry registry;
|
||||
|
||||
@Mock
|
||||
private ReverseProxyRouterFunctionFactory reverseProxyRouterFunctionFactory;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
ExtensionContextRegistry instance = ExtensionContextRegistry.getInstance();
|
||||
instance.register("fake-plugin", Mockito.mock(PluginApplicationContext.class));
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
ExtensionContextRegistry.getInstance().remove("fake-plugin");
|
||||
}
|
||||
|
||||
@Test
|
||||
void register() {
|
||||
ReverseProxy mock = Mockito.mock(ReverseProxy.class);
|
||||
Metadata metadata = new Metadata();
|
||||
metadata.setName("test-reverse-proxy");
|
||||
when(mock.getMetadata()).thenReturn(metadata);
|
||||
RouterFunction<ServerResponse> routerFunction = request -> Mono.empty();
|
||||
|
||||
when(reverseProxyRouterFunctionFactory.create(any(), any()))
|
||||
.thenReturn(Mono.just(routerFunction));
|
||||
registry.register("fake-plugin", mock)
|
||||
.as(StepVerifier::create)
|
||||
.verifyComplete();
|
||||
|
||||
assertThat(registry.reverseProxySize("fake-plugin")).isEqualTo(1);
|
||||
|
||||
// repeat register a same reverse proxy
|
||||
registry.register("fake-plugin", mock)
|
||||
.as(StepVerifier::create)
|
||||
.verifyComplete();
|
||||
|
||||
assertThat(registry.reverseProxySize("fake-plugin")).isEqualTo(1);
|
||||
|
||||
verify(reverseProxyRouterFunctionFactory, times(1)).create(any(), any());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue