|
24 | 24 | import org.apache.shenyu.common.config.ShenyuConfig; |
25 | 25 | import org.apache.shenyu.common.utils.InitialSyncApplication; |
26 | 26 | import org.apache.shenyu.sync.data.api.SyncDataService; |
| 27 | +import org.apache.shenyu.web.controller.LocalAppAuthController; |
| 28 | +import org.apache.shenyu.web.controller.LocalMetadataController; |
| 29 | +import org.apache.shenyu.web.controller.LocalPluginController; |
27 | 30 | import org.apache.shenyu.web.filter.LocalDispatcherFilter; |
28 | 31 | import org.apache.shenyu.web.handler.ShenyuWebHandler; |
29 | 32 | import org.junit.jupiter.api.Test; |
|
36 | 39 | import org.springframework.boot.availability.ReadinessState; |
37 | 40 | import org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext; |
38 | 41 | import org.springframework.context.ConfigurableApplicationContext; |
| 42 | +import org.springframework.http.MediaType; |
39 | 43 | import org.springframework.web.reactive.function.client.WebClient; |
40 | 44 | import reactor.core.publisher.Mono; |
41 | 45 | import reactor.netty.DisposableServer; |
@@ -63,19 +67,20 @@ class FileModeIntegrationTest { |
63 | 67 | private boolean failApplication; |
64 | 68 |
|
65 | 69 | private ConfigurableApplicationContext start(final Path path, final String... additional) { |
66 | | - String[] args = new String[additional.length + 7]; |
| 70 | + boolean explicitPort = Arrays.stream(additional).anyMatch(argument -> argument.startsWith("--server.port=")); |
| 71 | + String[] args = new String[additional.length + (explicitPort ? 6 : 7)]; |
67 | 72 | args[0] = "--spring.config.location=classpath:bootstrap-defaults/application.yml"; |
68 | 73 | args[1] = "--shenyu.sync.file.enabled=true"; |
69 | 74 | args[2] = "--shenyu.sync.file.path=" + path; |
70 | | - args[3] = "--server.port=0"; |
71 | | - args[4] = "--spring.main.banner-mode=off"; |
72 | | - args[5] = "--shenyu.sync.websocket.initial-sync-readiness=true"; |
73 | | - args[6] = "--management.endpoint.health.probes.enabled=true"; |
74 | | - System.arraycopy(additional, 0, args, 7, additional.length); |
75 | | - ArrayList<String> arguments = new ArrayList<>(Arrays.asList(args)); |
76 | | - if (additional.length > 0) { |
77 | | - arguments.remove("--server.port=0"); |
| 75 | + int index = 3; |
| 76 | + if (!explicitPort) { |
| 77 | + args[index++] = "--server.port=0"; |
78 | 78 | } |
| 79 | + args[index++] = "--spring.main.banner-mode=off"; |
| 80 | + args[index++] = "--shenyu.sync.websocket.initial-sync-readiness=true"; |
| 81 | + args[index++] = "--management.endpoint.health.probes.enabled=true"; |
| 82 | + System.arraycopy(additional, 0, args, index, additional.length); |
| 83 | + ArrayList<String> arguments = new ArrayList<>(Arrays.asList(args)); |
79 | 84 | SpringApplication application = new SpringApplication(TestApplication.class); |
80 | 85 | if (failApplication) { |
81 | 86 | application.addInitializers(context -> context.getBeanFactory().addBeanPostProcessor(new BeanPostProcessor() { |
@@ -140,6 +145,28 @@ void forwardsAndRestartsWithoutAdminOrMutableLocalApis() throws IOException { |
140 | 145 | } |
141 | 146 | } |
142 | 147 |
|
| 148 | + @Test |
| 149 | + void broadHealthPrefixCannotReachDisabledLocalWriters() throws IOException { |
| 150 | + Path path = directory.resolve("gateway.yaml"); |
| 151 | + Files.writeString(path, FileConfigurationParserTest.document().toString()); |
| 152 | + try (ConfigurableApplicationContext context = start(path, "--shenyu.health.paths=/")) { |
| 153 | + int port = ((ReactiveWebServerApplicationContext) context).getWebServer().getPort(); |
| 154 | + WebClient client = WebClient.create("http://127.0.0.1:" + port); |
| 155 | + String result = client.post().uri("/shenyu/plugin/rule/saveOrUpdate") |
| 156 | + .contentType(MediaType.APPLICATION_JSON) |
| 157 | + .bodyValue("{\"id\":\"injected-rule\",\"selectorId\":\"http-api\",\"pluginName\":\"divide\"}") |
| 158 | + .exchangeToMono(response -> response.bodyToMono(String.class).defaultIfEmpty("") |
| 159 | + .map(body -> response.statusCode().value() + "|" + body)) |
| 160 | + .block(Duration.ofSeconds(10)); |
| 161 | + assertThat(result).startsWith("404|"); |
| 162 | + assertThat(BaseDataCache.getInstance().obtainRuleData("http-api")).hasSize(1); |
| 163 | + assertThat(context.getBeansOfType(LocalDispatcherFilter.class)).isEmpty(); |
| 164 | + assertThat(context.getBeansOfType(LocalPluginController.class)).isEmpty(); |
| 165 | + assertThat(context.getBeansOfType(LocalMetadataController.class)).isEmpty(); |
| 166 | + assertThat(context.getBeansOfType(LocalAppAuthController.class)).isEmpty(); |
| 167 | + } |
| 168 | + } |
| 169 | + |
143 | 170 | @Test |
144 | 171 | void invalidConfigurationCannotStartListening() throws IOException { |
145 | 172 | Path path = directory.resolve("invalid.yaml"); |
|
0 commit comments