Polish "Apply server customizer beans automatically"

Closes gh-16584
This commit is contained in:
Madhura Bhave
2019-04-22 15:16:12 -07:00
parent 8ff78ed4c3
commit 23cf8565b1
2 changed files with 123 additions and 30 deletions

View File

@@ -16,16 +16,17 @@
package org.springframework.boot.autoconfigure.web.reactive;
import org.apache.catalina.startup.Tomcat;
import org.eclipse.jetty.server.Server;
import org.junit.Test;
import org.mockito.Mockito;
import reactor.netty.http.server.HttpServer;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.boot.web.embedded.jetty.JettyReactiveWebServerFactory;
import org.springframework.boot.web.embedded.jetty.JettyServerCustomizer;
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatProtocolHandlerCustomizer;
@@ -33,13 +34,11 @@ import org.springframework.boot.web.embedded.tomcat.TomcatReactiveWebServerFacto
import org.springframework.boot.web.embedded.undertow.UndertowBuilderCustomizer;
import org.springframework.boot.web.embedded.undertow.UndertowDeploymentInfoCustomizer;
import org.springframework.boot.web.embedded.undertow.UndertowReactiveWebServerFactory;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext;
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext;
import org.springframework.boot.web.reactive.server.ConfigurableReactiveWebServerFactory;
import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
import org.springframework.context.ApplicationContextException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -161,44 +160,55 @@ public class ReactiveWebServerFactoryAutoConfigurationTests {
@Test
public void jettyServerCustomizerBeanIsAddedToFactory() {
ReactiveWebApplicationContextRunner runner = new ReactiveWebApplicationContextRunner(
new ReactiveWebApplicationContextRunner(
AnnotationConfigReactiveWebApplicationContext::new)
.withConfiguration(AutoConfigurations
.of(ReactiveWebServerFactoryAutoConfiguration.class))
.withUserConfiguration(JettyServerCustomizer.class);
runner.run((context) -> {
JettyReactiveWebServerFactory factory = context
.getBean(JettyReactiveWebServerFactory.class);
assertThat(factory.getServerCustomizers()).hasSize(1);
});
.withClassLoader(
new FilteredClassLoader(Tomcat.class, HttpServer.class))
.withUserConfiguration(JettyServerCustomizerConfiguration.class,
HttpHandlerConfiguration.class)
.run((context) -> {
JettyReactiveWebServerFactory factory = context
.getBean(JettyReactiveWebServerFactory.class);
assertThat(factory.getServerCustomizers()).hasSize(1);
});
}
@Test
public void undertowDeploymentInfoCustomizerBeanIsAddedToFactory() {
ReactiveWebApplicationContextRunner runner = new ReactiveWebApplicationContextRunner(
new ReactiveWebApplicationContextRunner(
AnnotationConfigReactiveWebApplicationContext::new)
.withConfiguration(AutoConfigurations
.of(ReactiveWebServerFactoryAutoConfiguration.class))
.withUserConfiguration(UndertowDeploymentInfoCustomizer.class);
runner.run((context) -> {
UndertowReactiveWebServerFactory factory = context
.getBean(UndertowReactiveWebServerFactory.class);
assertThat(factory.getDeploymentInfoCustomizers()).hasSize(1);
});
.withClassLoader(new FilteredClassLoader(Tomcat.class,
HttpServer.class, Server.class))
.withUserConfiguration(
UndertowDeploymentInfoCustomizerConfiguration.class,
HttpHandlerConfiguration.class)
.run((context) -> {
UndertowReactiveWebServerFactory factory = context
.getBean(UndertowReactiveWebServerFactory.class);
assertThat(factory.getDeploymentInfoCustomizers()).hasSize(1);
});
}
@Test
public void undertowBuilderCustomizerBeanIsAddedToFactory() {
ReactiveWebApplicationContextRunner runner = new ReactiveWebApplicationContextRunner(
new ReactiveWebApplicationContextRunner(
AnnotationConfigReactiveWebApplicationContext::new)
.withConfiguration(AutoConfigurations
.of(ReactiveWebServerFactoryAutoConfiguration.class))
.withUserConfiguration(UndertowBuilderCustomizer.class);
runner.run((context) -> {
UndertowReactiveWebServerFactory factory = context
.getBean(UndertowReactiveWebServerFactory.class);
assertThat(factory.getBuilderCustomizers()).hasSize(1);
});
.withClassLoader(new FilteredClassLoader(Tomcat.class,
HttpServer.class, Server.class))
.withUserConfiguration(
UndertowBuilderCustomizerConfiguration.class,
HttpHandlerConfiguration.class)
.run((context) -> {
UndertowReactiveWebServerFactory factory = context
.getBean(UndertowReactiveWebServerFactory.class);
assertThat(factory.getBuilderCustomizers()).hasSize(1);
});
}
@Test
@@ -300,6 +310,42 @@ public class ReactiveWebServerFactoryAutoConfigurationTests {
}
@Configuration(proxyBeanMethods = false)
static class JettyServerCustomizerConfiguration {
@Bean
public JettyServerCustomizer protocolHandlerCustomizer() {
return (server) -> {
};
}
}
@Configuration(proxyBeanMethods = false)
static class UndertowBuilderCustomizerConfiguration {
@Bean
public UndertowBuilderCustomizer protocolHandlerCustomizer() {
return (builder) -> {
};
}
}
@Configuration(proxyBeanMethods = false)
static class UndertowDeploymentInfoCustomizerConfiguration {
@Bean
public UndertowDeploymentInfoCustomizer protocolHandlerCustomizer() {
return (deploymentInfo) -> {
};
}
}
@Configuration(proxyBeanMethods = false)
static class ForwardedHeaderTransformerConfiguration {

View File

@@ -22,12 +22,16 @@ import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.catalina.startup.Tomcat;
import org.eclipse.jetty.server.Server;
import org.junit.Test;
import reactor.netty.http.server.HttpServer;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext;
import org.springframework.boot.test.context.runner.ContextConsumer;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
@@ -39,7 +43,6 @@ import org.springframework.boot.web.embedded.tomcat.TomcatProtocolHandlerCustomi
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.embedded.undertow.UndertowBuilderCustomizer;
import org.springframework.boot.web.embedded.undertow.UndertowDeploymentInfoCustomizer;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServer;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
@@ -157,9 +160,11 @@ public class ServletWebServerFactoryAutoConfigurationTests {
public void jettyServerCustomizerBeanIsAddedToFactory() {
WebApplicationContextRunner runner = new WebApplicationContextRunner(
AnnotationConfigServletWebServerApplicationContext::new)
.withClassLoader(
new FilteredClassLoader(Tomcat.class, HttpServer.class))
.withConfiguration(AutoConfigurations
.of(ServletWebServerFactoryAutoConfiguration.class))
.withUserConfiguration(JettyServerCustomizer.class);
.withUserConfiguration(JettyServerCustomizerConfiguration.class);
runner.run((context) -> {
JettyServletWebServerFactory factory = context
.getBean(JettyServletWebServerFactory.class);
@@ -171,9 +176,12 @@ public class ServletWebServerFactoryAutoConfigurationTests {
public void undertowDeploymentInfoCustomizerBeanIsAddedToFactory() {
WebApplicationContextRunner runner = new WebApplicationContextRunner(
AnnotationConfigServletWebServerApplicationContext::new)
.withClassLoader(new FilteredClassLoader(Tomcat.class,
HttpServer.class, Server.class))
.withConfiguration(AutoConfigurations
.of(ServletWebServerFactoryAutoConfiguration.class))
.withUserConfiguration(UndertowDeploymentInfoCustomizer.class);
.withUserConfiguration(
UndertowDeploymentInfoCustomizerConfiguration.class);
runner.run((context) -> {
UndertowServletWebServerFactory factory = context
.getBean(UndertowServletWebServerFactory.class);
@@ -185,9 +193,12 @@ public class ServletWebServerFactoryAutoConfigurationTests {
public void undertowBuilderCustomizerBeanIsAddedToFactory() {
WebApplicationContextRunner runner = new WebApplicationContextRunner(
AnnotationConfigServletWebServerApplicationContext::new)
.withClassLoader(new FilteredClassLoader(Tomcat.class,
HttpServer.class, Server.class))
.withConfiguration(AutoConfigurations
.of(ServletWebServerFactoryAutoConfiguration.class))
.withUserConfiguration(UndertowBuilderCustomizer.class);
.withUserConfiguration(
UndertowBuilderCustomizerConfiguration.class);
runner.run((context) -> {
UndertowServletWebServerFactory factory = context
.getBean(UndertowServletWebServerFactory.class);
@@ -413,6 +424,42 @@ public class ServletWebServerFactoryAutoConfigurationTests {
}
@Configuration(proxyBeanMethods = false)
static class JettyServerCustomizerConfiguration {
@Bean
public JettyServerCustomizer protocolHandlerCustomizer() {
return (server) -> {
};
}
}
@Configuration(proxyBeanMethods = false)
static class UndertowBuilderCustomizerConfiguration {
@Bean
public UndertowBuilderCustomizer protocolHandlerCustomizer() {
return (builder) -> {
};
}
}
@Configuration(proxyBeanMethods = false)
static class UndertowDeploymentInfoCustomizerConfiguration {
@Bean
public UndertowDeploymentInfoCustomizer protocolHandlerCustomizer() {
return (deploymentInfo) -> {
};
}
}
@Configuration(proxyBeanMethods = false)
static class ForwardedHeaderFilterConfiguration {