|
| 1 | +/* |
| 2 | + * Copyright 2012-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.autoconfigure.websocket.reactive; |
| 18 | + |
| 19 | +import java.util.function.Function; |
| 20 | +import java.util.stream.Stream; |
| 21 | + |
| 22 | +import jakarta.servlet.ServletContext; |
| 23 | +import jakarta.websocket.server.ServerContainer; |
| 24 | +import org.apache.catalina.Container; |
| 25 | +import org.apache.catalina.Context; |
| 26 | +import org.apache.catalina.startup.Tomcat; |
| 27 | +import org.eclipse.jetty.servlet.ServletContextHandler; |
| 28 | +import org.junit.jupiter.params.ParameterizedTest; |
| 29 | +import org.junit.jupiter.params.provider.Arguments; |
| 30 | +import org.junit.jupiter.params.provider.MethodSource; |
| 31 | + |
| 32 | +import org.springframework.boot.testsupport.classpath.ForkedClassPath; |
| 33 | +import org.springframework.boot.testsupport.web.servlet.DirtiesUrlFactories; |
| 34 | +import org.springframework.boot.testsupport.web.servlet.Servlet5ClassPathOverrides; |
| 35 | +import org.springframework.boot.web.embedded.jetty.JettyReactiveWebServerFactory; |
| 36 | +import org.springframework.boot.web.embedded.jetty.JettyWebServer; |
| 37 | +import org.springframework.boot.web.embedded.tomcat.TomcatReactiveWebServerFactory; |
| 38 | +import org.springframework.boot.web.embedded.tomcat.TomcatWebServer; |
| 39 | +import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext; |
| 40 | +import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory; |
| 41 | +import org.springframework.boot.web.server.WebServerFactoryCustomizerBeanPostProcessor; |
| 42 | +import org.springframework.context.annotation.Bean; |
| 43 | +import org.springframework.context.annotation.Configuration; |
| 44 | +import org.springframework.http.server.reactive.HttpHandler; |
| 45 | + |
| 46 | +import static org.assertj.core.api.Assertions.assertThat; |
| 47 | + |
| 48 | +/** |
| 49 | + * Tests for {@link WebSocketReactiveAutoConfiguration}. |
| 50 | + * |
| 51 | + * @author Andy Wilkinson |
| 52 | + */ |
| 53 | +@DirtiesUrlFactories |
| 54 | +class WebSocketReactiveAutoConfigurationTests { |
| 55 | + |
| 56 | + @ParameterizedTest(name = "{0}") |
| 57 | + @MethodSource("testConfiguration") |
| 58 | + @ForkedClassPath |
| 59 | + void serverContainerIsAvailableFromTheServletContext(String server, |
| 60 | + Function<AnnotationConfigReactiveWebServerApplicationContext, ServletContext> servletContextAccessor, |
| 61 | + Class<?>... configuration) { |
| 62 | + try (AnnotationConfigReactiveWebServerApplicationContext context = new AnnotationConfigReactiveWebServerApplicationContext( |
| 63 | + configuration)) { |
| 64 | + Object serverContainer = servletContextAccessor.apply(context) |
| 65 | + .getAttribute("jakarta.websocket.server.ServerContainer"); |
| 66 | + assertThat(serverContainer).isInstanceOf(ServerContainer.class); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + static Stream<Arguments> testConfiguration() { |
| 71 | + return Stream.of(Arguments.of("Jetty", |
| 72 | + (Function<AnnotationConfigReactiveWebServerApplicationContext, ServletContext>) WebSocketReactiveAutoConfigurationTests::getJettyServletContext, |
| 73 | + new Class<?>[] { JettyConfiguration.class, |
| 74 | + WebSocketReactiveAutoConfiguration.JettyWebSocketConfiguration.class }), |
| 75 | + Arguments.of("Tomcat", |
| 76 | + (Function<AnnotationConfigReactiveWebServerApplicationContext, ServletContext>) WebSocketReactiveAutoConfigurationTests::getTomcatServletContext, |
| 77 | + new Class<?>[] { TomcatConfiguration.class, |
| 78 | + WebSocketReactiveAutoConfiguration.TomcatWebSocketConfiguration.class })); |
| 79 | + } |
| 80 | + |
| 81 | + private static ServletContext getJettyServletContext(AnnotationConfigReactiveWebServerApplicationContext context) { |
| 82 | + return ((ServletContextHandler) ((JettyWebServer) context.getWebServer()).getServer().getHandler()) |
| 83 | + .getServletContext(); |
| 84 | + } |
| 85 | + |
| 86 | + private static ServletContext getTomcatServletContext(AnnotationConfigReactiveWebServerApplicationContext context) { |
| 87 | + return findContext(((TomcatWebServer) context.getWebServer()).getTomcat()).getServletContext(); |
| 88 | + } |
| 89 | + |
| 90 | + private static Context findContext(Tomcat tomcat) { |
| 91 | + for (Container child : tomcat.getHost().findChildren()) { |
| 92 | + if (child instanceof Context context) { |
| 93 | + return context; |
| 94 | + } |
| 95 | + } |
| 96 | + throw new IllegalStateException("The host does not contain a Context"); |
| 97 | + } |
| 98 | + |
| 99 | + @Configuration(proxyBeanMethods = false) |
| 100 | + static class CommonConfiguration { |
| 101 | + |
| 102 | + @Bean |
| 103 | + static WebServerFactoryCustomizerBeanPostProcessor webServerFactoryCustomizerBeanPostProcessor() { |
| 104 | + return new WebServerFactoryCustomizerBeanPostProcessor(); |
| 105 | + } |
| 106 | + |
| 107 | + @Bean |
| 108 | + HttpHandler echoHandler() { |
| 109 | + return (request, response) -> response.writeWith(request.getBody()); |
| 110 | + } |
| 111 | + |
| 112 | + } |
| 113 | + |
| 114 | + @Configuration(proxyBeanMethods = false) |
| 115 | + static class TomcatConfiguration extends CommonConfiguration { |
| 116 | + |
| 117 | + @Bean |
| 118 | + ReactiveWebServerFactory webServerFactory() { |
| 119 | + TomcatReactiveWebServerFactory factory = new TomcatReactiveWebServerFactory(); |
| 120 | + factory.setPort(0); |
| 121 | + return factory; |
| 122 | + } |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | + @Servlet5ClassPathOverrides |
| 127 | + @Configuration(proxyBeanMethods = false) |
| 128 | + static class JettyConfiguration extends CommonConfiguration { |
| 129 | + |
| 130 | + @Bean |
| 131 | + ReactiveWebServerFactory webServerFactory() { |
| 132 | + JettyReactiveWebServerFactory factory = new JettyReactiveWebServerFactory(); |
| 133 | + factory.setPort(0); |
| 134 | + return factory; |
| 135 | + } |
| 136 | + |
| 137 | + } |
| 138 | + |
| 139 | +} |
0 commit comments