@Test public void onDifferentPort() throws Exception { this.applicationContext.register(RootConfig.class, EndpointConfig.class, DifferentPortConfig.class, BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class); this.applicationContext.refresh(); assertContent("/controller", ports.get().server, "controlleroutput"); assertContent("/endpoint", ports.get().server, null); assertContent("/controller", ports.get().management, null); assertContent("/endpoint", ports.get().management, "endpointoutput"); assertContent("/error", ports.get().management, startsWith("{")); ApplicationContext managementContext = this.applicationContext .getBean(ManagementContextResolver.class).getApplicationContext(); List<?> interceptors = (List<?>) ReflectionTestUtils.getField( managementContext.getBean(EndpointHandlerMapping.class), "interceptors"); assertThat(interceptors).hasSize(1); }
@Test public void onDifferentPortWithSpecificContainer() throws Exception { this.applicationContext.register(SpecificContainerConfig.class, RootConfig.class, DifferentPortConfig.class, EndpointConfig.class, BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class); this.applicationContext.refresh(); assertContent("/controller", ports.get().server, "controlleroutput"); assertContent("/endpoint", ports.get().server, null); assertContent("/controller", ports.get().management, null); assertContent("/endpoint", ports.get().management, "endpointoutput"); assertContent("/error", ports.get().management, startsWith("{")); ApplicationContext managementContext = this.applicationContext .getBean(ManagementContextResolver.class).getApplicationContext(); List<?> interceptors = (List<?>) ReflectionTestUtils.getField( managementContext.getBean(EndpointHandlerMapping.class), "interceptors"); assertThat(interceptors).hasSize(1); EmbeddedServletContainerFactory parentContainerFactory = this.applicationContext .getBean(EmbeddedServletContainerFactory.class); EmbeddedServletContainerFactory managementContainerFactory = managementContext .getBean(EmbeddedServletContainerFactory.class); assertThat(parentContainerFactory) .isInstanceOf(SpecificEmbeddedServletContainerFactory.class); assertThat(managementContainerFactory) .isInstanceOf(SpecificEmbeddedServletContainerFactory.class); assertThat(managementContainerFactory).isNotSameAs(parentContainerFactory); }
@Test public void onRandomPort() throws Exception { this.applicationContext.register(RootConfig.class, EndpointConfig.class, RandomPortConfig.class, BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class); GrabManagementPort grabManagementPort = new GrabManagementPort( this.applicationContext); this.applicationContext.addApplicationListener(grabManagementPort); this.applicationContext.refresh(); int managementPort = grabManagementPort.getServletContainer().getPort(); assertThat(managementPort).isNotEqualTo(ports.get().server); assertContent("/controller", ports.get().server, "controlleroutput"); assertContent("/endpoint", ports.get().server, null); assertContent("/controller", managementPort, null); assertContent("/endpoint", managementPort, "endpointoutput"); }
@Test public void specificPortsViaPropertiesWithClash() throws Exception { int managementPort = ports.get().management; ServerSocket serverSocket = new ServerSocket(); serverSocket.bind(new InetSocketAddress(managementPort)); try { EnvironmentTestUtils.addEnvironment(this.applicationContext, "server.port:" + ports.get().server, "management.port:" + ports.get().management); this.applicationContext.register(RootConfig.class, EndpointConfig.class, BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class); this.thrown.expect(EmbeddedServletContainerException.class); this.applicationContext.refresh(); } finally { serverSocket.close(); } }
@Test public void portPropertiesOnDifferentPort() throws Exception { new ServerPortInfoApplicationContextInitializer() .initialize(this.applicationContext); this.applicationContext.register(RootConfig.class, DifferentPortConfig.class, BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class); this.applicationContext.refresh(); Integer localServerPort = this.applicationContext.getEnvironment() .getProperty("local.server.port", Integer.class); Integer localManagementPort = this.applicationContext.getEnvironment() .getProperty("local.management.port", Integer.class); assertThat(localServerPort).isNotNull(); assertThat(localManagementPort).isNotNull(); assertThat(localServerPort).isNotEqualTo(localManagementPort); assertThat(this.applicationContext.getBean(ServerPortConfig.class).getCount()) .isEqualTo(2); }
@Test public void managementSpecificSslUsingDifferentPort() throws Exception { EnvironmentTestUtils.addEnvironment(this.applicationContext, "management.ssl.enabled=true", "management.ssl.key-store=classpath:test.jks", "management.ssl.key-password=password"); this.applicationContext.register(RootConfig.class, EndpointConfig.class, DifferentPortConfig.class, BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class); this.applicationContext.refresh(); assertContent("/controller", ports.get().server, "controlleroutput"); assertContent("/endpoint", ports.get().server, null); assertHttpsContent("/controller", ports.get().management, null); assertHttpsContent("/endpoint", ports.get().management, "endpointoutput"); assertHttpsContent("/error", ports.get().management, startsWith("{")); ApplicationContext managementContext = this.applicationContext .getBean(ManagementContextResolver.class).getApplicationContext(); List<?> interceptors = (List<?>) ReflectionTestUtils.getField( managementContext.getBean(EndpointHandlerMapping.class), "interceptors"); assertThat(interceptors).hasSize(1); ManagementServerProperties managementServerProperties = this.applicationContext .getBean(ManagementServerProperties.class); assertThat(managementServerProperties.getSsl()).isNotNull(); assertThat(managementServerProperties.getSsl().isEnabled()).isTrue(); }
@Test public void managementServerCanDisableSslWhenUsingADifferentPort() throws Exception { EnvironmentTestUtils.addEnvironment(this.applicationContext, "server.ssl.enabled=true", "server.ssl.key-store=classpath:test.jks", "server.ssl.key-password=password", "management.ssl.enabled=false"); this.applicationContext.register(RootConfig.class, EndpointConfig.class, DifferentPortConfig.class, BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class); this.applicationContext.refresh(); assertHttpsContent("/controller", ports.get().server, "controlleroutput"); assertHttpsContent("/endpoint", ports.get().server, null); assertContent("/controller", ports.get().management, null); assertContent("/endpoint", ports.get().management, "endpointoutput"); assertContent("/error", ports.get().management, startsWith("{")); ApplicationContext managementContext = this.applicationContext .getBean(ManagementContextResolver.class).getApplicationContext(); List<?> interceptors = (List<?>) ReflectionTestUtils.getField( managementContext.getBean(EndpointHandlerMapping.class), "interceptors"); assertThat(interceptors).hasSize(1); ManagementServerProperties managementServerProperties = this.applicationContext .getBean(ManagementServerProperties.class); assertThat(managementServerProperties.getSsl()).isNotNull(); assertThat(managementServerProperties.getSsl().isEnabled()).isFalse(); }
@Test public void onDifferentPort() throws Exception { this.applicationContext.register(RootConfig.class, EndpointConfig.class, DifferentPortConfig.class, BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class); this.applicationContext.refresh(); assertContent("/controller", ports.get().server, "controlleroutput"); assertContent("/endpoint", ports.get().server, null); assertContent("/controller", ports.get().management, null); assertContent("/endpoint", ports.get().management, "endpointoutput"); assertContent("/error", ports.get().management, startsWith("{")); ApplicationContext managementContext = this.applicationContext .getBean(ManagementContextResolver.class).getApplicationContext(); List<?> interceptors = (List<?>) ReflectionTestUtils.getField( managementContext.getBean(EndpointHandlerMapping.class), "interceptors"); assertEquals(1, interceptors.size()); this.applicationContext.close(); assertAllClosed(); }
@Test public void onDifferentPortInServletContainer() throws Exception { this.applicationContext.register(RootConfig.class, EndpointConfig.class, DifferentPortConfig.class, BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class); ServletContext servletContext = mock(ServletContext.class); given(servletContext.getInitParameterNames()) .willReturn(new Vector<String>().elements()); given(servletContext.getAttributeNames()) .willReturn(new Vector<String>().elements()); this.applicationContext.setServletContext(servletContext); this.applicationContext.refresh(); assertContent("/controller", ports.get().management, null); assertContent("/endpoint", ports.get().management, null); this.applicationContext.close(); assertAllClosed(); }
@Test public void onRandomPort() throws Exception { this.applicationContext.register(RootConfig.class, EndpointConfig.class, RandomPortConfig.class, BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class); GrabManagementPort grabManagementPort = new GrabManagementPort( this.applicationContext); this.applicationContext.addApplicationListener(grabManagementPort); this.applicationContext.refresh(); int managementPort = grabManagementPort.getServletContainer().getPort(); assertThat(managementPort, not(equalTo(ports.get().server))); assertContent("/controller", ports.get().server, "controlleroutput"); assertContent("/endpoint", ports.get().server, null); assertContent("/controller", managementPort, null); assertContent("/endpoint", managementPort, "endpointoutput"); }
@Test public void specificPortsViaProperties() throws Exception { EnvironmentTestUtils.addEnvironment(this.applicationContext, "server.port:" + ports.get().server, "management.port:" + ports.get().management); this.applicationContext.register(RootConfig.class, EndpointConfig.class, BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class); this.applicationContext.refresh(); assertContent("/controller", ports.get().server, "controlleroutput"); assertContent("/endpoint", ports.get().server, null); assertContent("/controller", ports.get().management, null); assertContent("/endpoint", ports.get().management, "endpointoutput"); this.applicationContext.close(); assertAllClosed(); }
@Test public void specificPortsViaPropertiesWithClash() throws Exception { int managementPort = ports.get().management; ServerSocket serverSocket = new ServerSocket(); serverSocket.bind(new InetSocketAddress(managementPort)); try { EnvironmentTestUtils.addEnvironment(this.applicationContext, "server.port:" + ports.get().server, "management.port:" + ports.get().management); this.applicationContext.register(RootConfig.class, EndpointConfig.class, BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class); this.thrown.expect(EmbeddedServletContainerException.class); this.applicationContext.refresh(); this.applicationContext.close(); } finally { serverSocket.close(); assertAllClosed(); } }
@Test public void portPropertiesOnDifferentPort() throws Exception { new ServerPortInfoApplicationContextInitializer() .initialize(this.applicationContext); this.applicationContext.register(RootConfig.class, DifferentPortConfig.class, BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class); this.applicationContext.refresh(); Integer localServerPort = this.applicationContext.getEnvironment() .getProperty("local.server.port", Integer.class); Integer localManagementPort = this.applicationContext.getEnvironment() .getProperty("local.management.port", Integer.class); assertThat(localServerPort, notNullValue()); assertThat(localManagementPort, notNullValue()); assertThat(localServerPort, not(equalTo(localManagementPort))); assertThat(this.applicationContext.getBean(ServerPortConfig.class).getCount(), equalTo(2)); this.applicationContext.close(); assertAllClosed(); }
@Test public void onDifferentPortAndContext() throws Exception { this.applicationContext.register(RootConfig.class, EndpointConfig.class, DifferentPortConfig.class, BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class); management.setContextPath("/admin"); this.applicationContext.refresh(); assertContent("/controller", ports.get().server, "controlleroutput"); assertContent("/admin/endpoint", ports.get().management, "endpointoutput"); assertContent("/error", ports.get().management, startsWith("{")); }
@Test public void onDifferentPortAndMainContext() throws Exception { this.applicationContext.register(RootConfig.class, EndpointConfig.class, DifferentPortConfig.class, BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class); management.setContextPath("/admin"); server.setContextPath("/spring"); this.applicationContext.refresh(); assertContent("/spring/controller", ports.get().server, "controlleroutput"); assertContent("/admin/endpoint", ports.get().management, "endpointoutput"); assertContent("/error", ports.get().management, startsWith("{")); }
@Test public void onDifferentPortInServletContainer() throws Exception { this.applicationContext.register(RootConfig.class, EndpointConfig.class, DifferentPortConfig.class, BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class); ServletContext servletContext = mock(ServletContext.class); given(servletContext.getInitParameterNames()) .willReturn(new Vector<String>().elements()); given(servletContext.getAttributeNames()) .willReturn(new Vector<String>().elements()); this.applicationContext.setServletContext(servletContext); this.applicationContext.refresh(); assertContent("/controller", ports.get().management, null); assertContent("/endpoint", ports.get().management, null); }
@Test public void onDifferentPortWithPrimaryFailure() throws Exception { this.applicationContext.register(RootConfig.class, EndpointConfig.class, DifferentPortConfig.class, BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class); this.applicationContext.refresh(); ApplicationContext managementContext = this.applicationContext .getBean(ManagementContextResolver.class).getApplicationContext(); ApplicationFailedEvent event = mock(ApplicationFailedEvent.class); given(event.getApplicationContext()).willReturn(this.applicationContext); this.applicationContext.publishEvent(event); assertThat(((ConfigurableApplicationContext) managementContext).isActive()) .isFalse(); }
@Test public void specificPortsViaProperties() throws Exception { EnvironmentTestUtils.addEnvironment(this.applicationContext, "server.port:" + ports.get().server, "management.port:" + ports.get().management); this.applicationContext.register(RootConfig.class, EndpointConfig.class, BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class); this.applicationContext.refresh(); assertContent("/controller", ports.get().server, "controlleroutput"); assertContent("/endpoint", ports.get().server, null); assertContent("/controller", ports.get().management, null); assertContent("/endpoint", ports.get().management, "endpointoutput"); }
@Test public void managementSpecificSslUsingSamePortFails() throws Exception { EnvironmentTestUtils.addEnvironment(this.applicationContext, "management.ssl.enabled=true", "management.ssl.key-store=classpath:test.jks", "management.ssl.key-password=password"); this.applicationContext.register(RootConfig.class, EndpointConfig.class, BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class, ServerPortConfig.class); this.thrown.expect(IllegalStateException.class); this.thrown.expectMessage("Management-specific SSL cannot be configured as the " + "management server is not listening on a separate port"); this.applicationContext.refresh(); }
@Test public void onDifferentPortWithSpecificContainer() throws Exception { this.applicationContext.register(SpecificContainerConfig.class, RootConfig.class, DifferentPortConfig.class, EndpointConfig.class, BaseConfiguration.class, EndpointWebMvcAutoConfiguration.class, ErrorMvcAutoConfiguration.class); this.applicationContext.refresh(); assertContent("/controller", ports.get().server, "controlleroutput"); assertContent("/endpoint", ports.get().server, null); assertContent("/controller", ports.get().management, null); assertContent("/endpoint", ports.get().management, "endpointoutput"); assertContent("/error", ports.get().management, startsWith("{")); ApplicationContext managementContext = this.applicationContext .getBean(ManagementContextResolver.class).getApplicationContext(); List<?> interceptors = (List<?>) ReflectionTestUtils.getField( managementContext.getBean(EndpointHandlerMapping.class), "interceptors"); assertThat(interceptors).hasSize(1); EmbeddedServletContainerFactory parentContainerFactory = this.applicationContext .getBean(EmbeddedServletContainerFactory.class); EmbeddedServletContainerFactory managementContainerFactory = managementContext .getBean(EmbeddedServletContainerFactory.class); assertThat(parentContainerFactory) .isInstanceOf(SpecificEmbeddedServletContainerFactory.class); assertThat(managementContainerFactory) .isInstanceOf(SpecificEmbeddedServletContainerFactory.class); assertThat(managementContainerFactory).isNotSameAs(parentContainerFactory); this.applicationContext.close(); assertAllClosed(); }