|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.shenyu.client.core.register; |
| 19 | + |
| 20 | +import org.apache.shenyu.common.enums.RpcTypeEnum; |
| 21 | +import org.apache.shenyu.common.exception.ShenyuException; |
| 22 | +import org.apache.shenyu.register.common.config.ShenyuClientConfig; |
| 23 | +import org.junit.jupiter.api.Assertions; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.mockito.Mockito; |
| 26 | +import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
| 27 | +import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
| 28 | +import org.springframework.boot.web.context.WebServerApplicationContext; |
| 29 | +import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; |
| 30 | +import org.springframework.boot.web.server.WebServer; |
| 31 | +import org.springframework.context.ApplicationContext; |
| 32 | +import org.springframework.core.env.Environment; |
| 33 | + |
| 34 | +import java.util.Properties; |
| 35 | + |
| 36 | +/** |
| 37 | + * Test for {@link ClientRegisterConfigImpl} port resolution (issue #8426). |
| 38 | + */ |
| 39 | +public class ClientRegisterConfigImplTest { |
| 40 | + |
| 41 | + private static final int DISCOVERED_PORT = 27987; |
| 42 | + |
| 43 | + @Test |
| 44 | + public void configuredPositivePortIsAuthoritative() { |
| 45 | + final ClientRegisterConfigImpl config = new ClientRegisterConfigImpl( |
| 46 | + clientConfig(props("port", "18443")), RpcTypeEnum.HTTP, |
| 47 | + Mockito.mock(ApplicationContext.class), Mockito.mock(Environment.class)); |
| 48 | + Assertions.assertEquals(18443, config.getPort()); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void zeroConfiguredPortResolvesActuallyBoundServerPort() { |
| 53 | + //issue #8426: an explicitly configured 0 means random-port binding and |
| 54 | + //must not be registered verbatim; the bound port wins instead |
| 55 | + final ApplicationContext context = webServerContext(DISCOVERED_PORT); |
| 56 | + final ClientRegisterConfigImpl config = new ClientRegisterConfigImpl( |
| 57 | + clientConfig(props("port", "0")), RpcTypeEnum.HTTP, context, Mockito.mock(Environment.class)); |
| 58 | + Assertions.assertEquals(DISCOVERED_PORT, config.getPort()); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void absentPortResolvesActuallyBoundServerPort() { |
| 63 | + final ApplicationContext context = webServerContext(DISCOVERED_PORT); |
| 64 | + final ClientRegisterConfigImpl config = new ClientRegisterConfigImpl( |
| 65 | + clientConfig(new Properties()), RpcTypeEnum.HTTP, context, Mockito.mock(Environment.class)); |
| 66 | + Assertions.assertEquals(DISCOVERED_PORT, config.getPort()); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void zeroResolutionWithoutRunningServerFailsLoudly() { |
| 71 | + //a factory configured for a random port but no owned running server |
| 72 | + //(context is not a web-server context): registration must fail loudly |
| 73 | + //instead of silently resolving the non-positive configured value |
| 74 | + final ApplicationContext context = Mockito.mock(ApplicationContext.class); |
| 75 | + final DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); |
| 76 | + beanFactory.registerBeanDefinition("servletWebServerFactory", BeanDefinitionBuilder |
| 77 | + .rootBeanDefinition(TomcatServletWebServerFactory.class) |
| 78 | + .addConstructorArgValue(0) |
| 79 | + .getBeanDefinition()); |
| 80 | + Mockito.when(context.getAutowireCapableBeanFactory()).thenReturn(beanFactory); |
| 81 | + final ClientRegisterConfigImpl config = new ClientRegisterConfigImpl( |
| 82 | + clientConfig(props("port", "0")), RpcTypeEnum.HTTP, context, Mockito.mock(Environment.class)); |
| 83 | + Assertions.assertThrows(ShenyuException.class, config::getPort); |
| 84 | + } |
| 85 | + |
| 86 | + private static ApplicationContext webServerContext(final int port) { |
| 87 | + final WebServer server = webServer(port); |
| 88 | + final WebServerApplicationContext context = Mockito.mock(WebServerApplicationContext.class); |
| 89 | + Mockito.when(context.getWebServer()).thenReturn(server); |
| 90 | + return context; |
| 91 | + } |
| 92 | + |
| 93 | + private static WebServer webServer(final int port) { |
| 94 | + final WebServer webServer = Mockito.mock(WebServer.class); |
| 95 | + Mockito.when(webServer.getPort()).thenReturn(port); |
| 96 | + return webServer; |
| 97 | + } |
| 98 | + |
| 99 | + private static Properties props(final String key, final String value) { |
| 100 | + final Properties properties = new Properties(); |
| 101 | + properties.setProperty(key, value); |
| 102 | + return properties; |
| 103 | + } |
| 104 | + |
| 105 | + private static ShenyuClientConfig clientConfig(final Properties props) { |
| 106 | + final ShenyuClientConfig config = new ShenyuClientConfig(); |
| 107 | + final ShenyuClientConfig.ClientPropertiesConfig clientProperties = new ShenyuClientConfig.ClientPropertiesConfig(); |
| 108 | + clientProperties.setProps(props); |
| 109 | + config.getClient().put(RpcTypeEnum.HTTP.getName(), clientProperties); |
| 110 | + return config; |
| 111 | + } |
| 112 | +} |
0 commit comments