Search in sources :

Example 31 with WebApplicationContextRunner

use of org.springframework.boot.test.context.runner.WebApplicationContextRunner in project spring-boot by spring-projects.

the class ServletWebServerFactoryAutoConfigurationTests method undertowServletWebServerFactoryCustomizerIsAutoConfigured.

@Test
void undertowServletWebServerFactoryCustomizerIsAutoConfigured() {
    WebApplicationContextRunner runner = new WebApplicationContextRunner(AnnotationConfigServletWebServerApplicationContext::new).withClassLoader(new FilteredClassLoader(Tomcat.class, HttpServer.class, Server.class)).withConfiguration(AutoConfigurations.of(ServletWebServerFactoryAutoConfiguration.class)).withUserConfiguration(UndertowBuilderCustomizerConfiguration.class).withPropertyValues("server.port:0");
    runner.run((context) -> assertThat(context).hasSingleBean(UndertowServletWebServerFactoryCustomizer.class));
}
Also used : WebApplicationContextRunner(org.springframework.boot.test.context.runner.WebApplicationContextRunner) AnnotationConfigServletWebServerApplicationContext(org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext) FilteredClassLoader(org.springframework.boot.test.context.FilteredClassLoader) Test(org.junit.jupiter.api.Test)

Example 32 with WebApplicationContextRunner

use of org.springframework.boot.test.context.runner.WebApplicationContextRunner in project spring-boot by spring-projects.

the class ServletWebServerFactoryAutoConfigurationTests method undertowDeploymentInfoCustomizerRegisteredAsBeanAndViaFactoryIsOnlyCalledOnce.

@Test
void undertowDeploymentInfoCustomizerRegisteredAsBeanAndViaFactoryIsOnlyCalledOnce() {
    WebApplicationContextRunner runner = new WebApplicationContextRunner(AnnotationConfigServletWebServerApplicationContext::new).withClassLoader(new FilteredClassLoader(Tomcat.class, HttpServer.class, Server.class)).withConfiguration(AutoConfigurations.of(ServletWebServerFactoryAutoConfiguration.class)).withUserConfiguration(DoubleRegistrationUndertowDeploymentInfoCustomizerConfiguration.class).withPropertyValues("server.port: 0");
    runner.run((context) -> {
        UndertowServletWebServerFactory factory = context.getBean(UndertowServletWebServerFactory.class);
        UndertowDeploymentInfoCustomizer customizer = context.getBean("deploymentInfoCustomizer", UndertowDeploymentInfoCustomizer.class);
        assertThat(factory.getDeploymentInfoCustomizers()).contains(customizer);
        then(customizer).should().customize(any(DeploymentInfo.class));
    });
}
Also used : WebApplicationContextRunner(org.springframework.boot.test.context.runner.WebApplicationContextRunner) AnnotationConfigServletWebServerApplicationContext(org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext) UndertowServletWebServerFactory(org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory) DeploymentInfo(io.undertow.servlet.api.DeploymentInfo) UndertowDeploymentInfoCustomizer(org.springframework.boot.web.embedded.undertow.UndertowDeploymentInfoCustomizer) FilteredClassLoader(org.springframework.boot.test.context.FilteredClassLoader) Test(org.junit.jupiter.api.Test)

Example 33 with WebApplicationContextRunner

use of org.springframework.boot.test.context.runner.WebApplicationContextRunner in project spring-boot by spring-projects.

the class ServletWebServerFactoryAutoConfigurationTests method relativeRedirectsShouldNotBeEnabledWhenUsingTomcatContainerAndNotUsingRelativeRedirects.

@Test
void relativeRedirectsShouldNotBeEnabledWhenUsingTomcatContainerAndNotUsingRelativeRedirects() {
    WebApplicationContextRunner runner = new WebApplicationContextRunner(AnnotationConfigServletWebServerApplicationContext::new).withConfiguration(AutoConfigurations.of(ServletWebServerFactoryAutoConfiguration.class)).withPropertyValues("server.forward-headers-strategy=framework", "server.tomcat.use-relative-redirects=false", "server.port=0");
    runner.run((context) -> {
        Filter filter = context.getBean(FilterRegistrationBean.class).getFilter();
        assertThat(filter).isInstanceOf(ForwardedHeaderFilter.class);
        assertThat(filter).extracting("relativeRedirects").isEqualTo(false);
    });
}
Also used : WebApplicationContextRunner(org.springframework.boot.test.context.runner.WebApplicationContextRunner) Filter(jakarta.servlet.Filter) ForwardedHeaderFilter(org.springframework.web.filter.ForwardedHeaderFilter) AnnotationConfigServletWebServerApplicationContext(org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext) FilterRegistrationBean(org.springframework.boot.web.servlet.FilterRegistrationBean) Test(org.junit.jupiter.api.Test)

Example 34 with WebApplicationContextRunner

use of org.springframework.boot.test.context.runner.WebApplicationContextRunner in project spring-boot by spring-projects.

the class ServletWebServerFactoryAutoConfigurationTests method tomcatConnectorCustomizerRegisteredAsBeanAndViaFactoryIsOnlyCalledOnce.

@Test
void tomcatConnectorCustomizerRegisteredAsBeanAndViaFactoryIsOnlyCalledOnce() {
    WebApplicationContextRunner runner = new WebApplicationContextRunner(AnnotationConfigServletWebServerApplicationContext::new).withConfiguration(AutoConfigurations.of(ServletWebServerFactoryAutoConfiguration.class)).withUserConfiguration(DoubleRegistrationTomcatConnectorCustomizerConfiguration.class).withPropertyValues("server.port: 0");
    runner.run((context) -> {
        TomcatServletWebServerFactory factory = context.getBean(TomcatServletWebServerFactory.class);
        TomcatConnectorCustomizer customizer = context.getBean("connectorCustomizer", TomcatConnectorCustomizer.class);
        assertThat(factory.getTomcatConnectorCustomizers()).contains(customizer);
        then(customizer).should().customize(any(Connector.class));
    });
}
Also used : Connector(org.apache.catalina.connector.Connector) WebApplicationContextRunner(org.springframework.boot.test.context.runner.WebApplicationContextRunner) TomcatServletWebServerFactory(org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory) TomcatConnectorCustomizer(org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer) Test(org.junit.jupiter.api.Test)

Example 35 with WebApplicationContextRunner

use of org.springframework.boot.test.context.runner.WebApplicationContextRunner in project spring-boot by spring-projects.

the class ServletWebServerFactoryAutoConfigurationTests method tomcatProtocolHandlerCustomizerBeanIsAddedToFactory.

@Test
void tomcatProtocolHandlerCustomizerBeanIsAddedToFactory() {
    WebApplicationContextRunner runner = new WebApplicationContextRunner(AnnotationConfigServletWebServerApplicationContext::new).withConfiguration(AutoConfigurations.of(ServletWebServerFactoryAutoConfiguration.class)).withUserConfiguration(TomcatProtocolHandlerCustomizerConfiguration.class).withPropertyValues("server.port: 0");
    runner.run((context) -> {
        TomcatServletWebServerFactory factory = context.getBean(TomcatServletWebServerFactory.class);
        TomcatProtocolHandlerCustomizer<?> customizer = context.getBean("protocolHandlerCustomizer", TomcatProtocolHandlerCustomizer.class);
        assertThat(factory.getTomcatProtocolHandlerCustomizers()).contains(customizer);
        then(customizer).should().customize(any());
    });
}
Also used : WebApplicationContextRunner(org.springframework.boot.test.context.runner.WebApplicationContextRunner) TomcatServletWebServerFactory(org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory) Test(org.junit.jupiter.api.Test)

Aggregations

WebApplicationContextRunner (org.springframework.boot.test.context.runner.WebApplicationContextRunner)36 Test (org.junit.jupiter.api.Test)34 AnnotationConfigServletWebServerApplicationContext (org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext)14 ReactiveWebApplicationContextRunner (org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner)12 FilteredClassLoader (org.springframework.boot.test.context.FilteredClassLoader)8 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)6 TomcatServletWebServerFactory (org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory)6 ServletWebServerFactoryAutoConfiguration (org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration)5 UndertowServletWebServerFactory (org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory)4 Filter (jakarta.servlet.Filter)3 List (java.util.List)3 EndpointAutoConfiguration (org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration)3 WebEndpointAutoConfiguration (org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration)3 ContextMappings (org.springframework.boot.actuate.web.mappings.MappingsEndpoint.ContextMappings)3 FilterRegistrationBean (org.springframework.boot.web.servlet.FilterRegistrationBean)3 WebTestClient (org.springframework.test.web.reactive.server.WebTestClient)3 ConfigurableWebApplicationContext (org.springframework.web.context.ConfigurableWebApplicationContext)3 ServletContext (jakarta.servlet.ServletContext)2 Context (org.apache.catalina.Context)2 Connector (org.apache.catalina.connector.Connector)2