|
|
@ -36,6 +36,7 @@ import org.junit.jupiter.params.ParameterizedTest;
|
|
|
|
import org.junit.jupiter.params.provider.Arguments;
|
|
|
|
import org.junit.jupiter.params.provider.Arguments;
|
|
|
|
import org.junit.jupiter.params.provider.MethodSource;
|
|
|
|
import org.junit.jupiter.params.provider.MethodSource;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.config.BeanDefinition;
|
|
|
|
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
|
|
|
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
|
|
|
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration;
|
|
|
|
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration;
|
|
|
|
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
|
|
|
|
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
|
|
|
@ -47,7 +48,9 @@ import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
|
|
|
|
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
|
|
|
|
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
|
|
|
|
import org.springframework.boot.web.server.WebServer;
|
|
|
|
import org.springframework.boot.web.server.WebServer;
|
|
|
|
import org.springframework.boot.web.server.WebServerFactoryCustomizerBeanPostProcessor;
|
|
|
|
import org.springframework.boot.web.server.WebServerFactoryCustomizerBeanPostProcessor;
|
|
|
|
|
|
|
|
import org.springframework.boot.web.servlet.AbstractFilterRegistrationBean;
|
|
|
|
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
|
|
|
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
|
|
|
|
|
|
|
import org.springframework.boot.web.servlet.ServletContextInitializer;
|
|
|
|
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
|
|
|
|
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
|
|
|
|
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
|
|
|
|
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
@ -104,34 +107,44 @@ class WebSocketServletAutoConfigurationTests {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
@SuppressWarnings("rawtypes")
|
|
|
|
@Servlet5ClassPathOverrides
|
|
|
|
void whenCustomUpgradeFilterRegistrationIsDefinedAutoConfiguredRegistrationOfJettyUpgradeFilterBacksOff() {
|
|
|
|
void jettyWebSocketUpgradeFilterIsAddedToServletContext() {
|
|
|
|
new WebApplicationContextRunner()
|
|
|
|
try (AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext(
|
|
|
|
.withConfiguration(AutoConfigurations.of(JettyConfiguration.class,
|
|
|
|
JettyConfiguration.class, WebSocketServletAutoConfiguration.JettyWebSocketConfiguration.class)) {
|
|
|
|
WebSocketServletAutoConfiguration.JettyWebSocketConfiguration.class))
|
|
|
|
assertThat(context.getServletContext().getFilterRegistration(WebSocketUpgradeFilter.class.getName()))
|
|
|
|
.withUserConfiguration(CustomUpgradeFilterRegistrationConfiguration.class)
|
|
|
|
.isNotNull();
|
|
|
|
.run((context) -> {
|
|
|
|
}
|
|
|
|
Map<String, FilterRegistrationBean> filterRegistrations = context
|
|
|
|
|
|
|
|
.getBeansOfType(FilterRegistrationBean.class);
|
|
|
|
|
|
|
|
assertThat(filterRegistrations).containsOnlyKeys("unauthorizedFilter",
|
|
|
|
|
|
|
|
"customUpgradeFilterRegistration");
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
@Test
|
|
|
|
@SuppressWarnings("rawtypes")
|
|
|
|
@SuppressWarnings("rawtypes")
|
|
|
|
void whenCustomUpgradeFilterIsDefinedAutoConfiguredRegistrationOfJettyUpgradeFilterBacksOff() {
|
|
|
|
void jettyWebSocketUpgradeFilterIsNotExposedAsABean() {
|
|
|
|
new WebApplicationContextRunner()
|
|
|
|
new WebApplicationContextRunner()
|
|
|
|
.withConfiguration(AutoConfigurations.of(JettyConfiguration.class,
|
|
|
|
.withConfiguration(AutoConfigurations.of(JettyConfiguration.class,
|
|
|
|
WebSocketServletAutoConfiguration.JettyWebSocketConfiguration.class))
|
|
|
|
WebSocketServletAutoConfiguration.JettyWebSocketConfiguration.class))
|
|
|
|
.withUserConfiguration(CustomUpgradeFilterConfiguration.class)
|
|
|
|
|
|
|
|
.run((context) -> {
|
|
|
|
.run((context) -> {
|
|
|
|
Map<String, FilterRegistrationBean> filterRegistrations = context
|
|
|
|
Map<String, Filter> filters = context.getBeansOfType(Filter.class);
|
|
|
|
.getBeansOfType(FilterRegistrationBean.class);
|
|
|
|
assertThat(filters.values()).noneMatch(WebSocketUpgradeFilter.class::isInstance);
|
|
|
|
assertThat(filterRegistrations).containsOnlyKeys("unauthorizedFilter");
|
|
|
|
Map<String, AbstractFilterRegistrationBean> filterRegistrations = context
|
|
|
|
|
|
|
|
.getBeansOfType(AbstractFilterRegistrationBean.class);
|
|
|
|
|
|
|
|
assertThat(filterRegistrations.values()).extracting(AbstractFilterRegistrationBean::getFilter)
|
|
|
|
|
|
|
|
.noneMatch(WebSocketUpgradeFilter.class::isInstance);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
|
|
@Servlet5ClassPathOverrides
|
|
|
|
|
|
|
|
void jettyWebSocketUpgradeFilterServletContextInitializerBacksOffWhenBeanWithSameNameIsDefined() {
|
|
|
|
|
|
|
|
try (AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext(
|
|
|
|
|
|
|
|
JettyConfiguration.class, CustomWebSocketUpgradeFilterServletContextInitializerConfiguration.class,
|
|
|
|
|
|
|
|
WebSocketServletAutoConfiguration.JettyWebSocketConfiguration.class)) {
|
|
|
|
|
|
|
|
BeanDefinition definition = context.getBeanFactory()
|
|
|
|
|
|
|
|
.getBeanDefinition("websocketUpgradeFilterServletContextInitializer");
|
|
|
|
|
|
|
|
assertThat(definition.getFactoryBeanName())
|
|
|
|
|
|
|
|
.contains("CustomWebSocketUpgradeFilterServletContextInitializerConfiguration");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Stream<Arguments> testConfiguration() {
|
|
|
|
static Stream<Arguments> testConfiguration() {
|
|
|
|
String response = "Tomcat";
|
|
|
|
String response = "Tomcat";
|
|
|
|
return Stream.of(
|
|
|
|
return Stream.of(
|
|
|
@ -196,23 +209,13 @@ class WebSocketServletAutoConfigurationTests {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration(proxyBeanMethods = false)
|
|
|
|
@Configuration(proxyBeanMethods = false)
|
|
|
|
static class CustomUpgradeFilterRegistrationConfiguration {
|
|
|
|
static class CustomWebSocketUpgradeFilterServletContextInitializerConfiguration {
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
@Bean
|
|
|
|
FilterRegistrationBean<WebSocketUpgradeFilter> customUpgradeFilterRegistration() {
|
|
|
|
ServletContextInitializer websocketUpgradeFilterServletContextInitializer() {
|
|
|
|
FilterRegistrationBean<WebSocketUpgradeFilter> registration = new FilterRegistrationBean<>(
|
|
|
|
return (servletContext) -> {
|
|
|
|
new WebSocketUpgradeFilter());
|
|
|
|
|
|
|
|
return registration;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
@Configuration(proxyBeanMethods = false)
|
|
|
|
|
|
|
|
static class CustomUpgradeFilterConfiguration {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
|
|
|
|
WebSocketUpgradeFilter customUpgradeFilter() {
|
|
|
|
|
|
|
|
return new WebSocketUpgradeFilter();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|