Search in sources :

Example 26 with WebApplicationContextRunner

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

the class WebMvcEndpointExposureIntegrationTests method singleWebEndpointCanBeExposed.

@Test
void singleWebEndpointCanBeExposed() {
    WebApplicationContextRunner contextRunner = this.contextRunner.withPropertyValues("management.endpoints.web.exposure.include=beans");
    contextRunner.run((context) -> {
        WebTestClient client = createClient(context);
        assertThat(isExposed(client, HttpMethod.GET, "beans")).isTrue();
        assertThat(isExposed(client, HttpMethod.GET, "conditions")).isFalse();
        assertThat(isExposed(client, HttpMethod.GET, "configprops")).isFalse();
        assertThat(isExposed(client, HttpMethod.GET, "custommvc")).isFalse();
        assertThat(isExposed(client, HttpMethod.GET, "customservlet")).isFalse();
        assertThat(isExposed(client, HttpMethod.GET, "env")).isFalse();
        assertThat(isExposed(client, HttpMethod.GET, "health")).isFalse();
        assertThat(isExposed(client, HttpMethod.GET, "info")).isFalse();
        assertThat(isExposed(client, HttpMethod.GET, "mappings")).isFalse();
        assertThat(isExposed(client, HttpMethod.POST, "shutdown")).isFalse();
        assertThat(isExposed(client, HttpMethod.GET, "threaddump")).isFalse();
        assertThat(isExposed(client, HttpMethod.GET, "httptrace")).isFalse();
    });
}
Also used : WebApplicationContextRunner(org.springframework.boot.test.context.runner.WebApplicationContextRunner) WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) Test(org.junit.jupiter.api.Test)

Example 27 with WebApplicationContextRunner

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

the class WebMvcEndpointExposureIntegrationTests method webEndpointsCanBeExposed.

@Test
void webEndpointsCanBeExposed() {
    WebApplicationContextRunner contextRunner = this.contextRunner.withPropertyValues("management.endpoints.web.exposure.include=*");
    contextRunner.run((context) -> {
        WebTestClient client = createClient(context);
        assertThat(isExposed(client, HttpMethod.GET, "beans")).isTrue();
        assertThat(isExposed(client, HttpMethod.GET, "conditions")).isTrue();
        assertThat(isExposed(client, HttpMethod.GET, "configprops")).isTrue();
        assertThat(isExposed(client, HttpMethod.GET, "custommvc")).isTrue();
        assertThat(isExposed(client, HttpMethod.GET, "customservlet")).isTrue();
        assertThat(isExposed(client, HttpMethod.GET, "env")).isTrue();
        assertThat(isExposed(client, HttpMethod.GET, "health")).isTrue();
        assertThat(isExposed(client, HttpMethod.GET, "info")).isTrue();
        assertThat(isExposed(client, HttpMethod.GET, "mappings")).isTrue();
        assertThat(isExposed(client, HttpMethod.POST, "shutdown")).isFalse();
        assertThat(isExposed(client, HttpMethod.GET, "threaddump")).isTrue();
        assertThat(isExposed(client, HttpMethod.GET, "httptrace")).isTrue();
    });
}
Also used : WebApplicationContextRunner(org.springframework.boot.test.context.runner.WebApplicationContextRunner) WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) Test(org.junit.jupiter.api.Test)

Example 28 with WebApplicationContextRunner

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

the class HttpMessageConvertersAutoConfigurationTests method whenEncodingCharsetIsConfiguredThenStringMessageConverterUsesSpecificCharset.

@Test
void whenEncodingCharsetIsConfiguredThenStringMessageConverterUsesSpecificCharset() {
    new WebApplicationContextRunner().withConfiguration(AutoConfigurations.of(HttpMessageConvertersAutoConfiguration.class)).withPropertyValues("server.servlet.encoding.charset=UTF-16").run((context) -> {
        assertThat(context).hasSingleBean(StringHttpMessageConverter.class);
        assertThat(context.getBean(StringHttpMessageConverter.class).getDefaultCharset()).isEqualTo(StandardCharsets.UTF_16);
    });
}
Also used : ReactiveWebApplicationContextRunner(org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner) WebApplicationContextRunner(org.springframework.boot.test.context.runner.WebApplicationContextRunner) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) Test(org.junit.jupiter.api.Test)

Example 29 with WebApplicationContextRunner

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

the class ServletWebServerFactoryAutoConfigurationTests method tomcatProtocolHandlerCustomizerRegisteredAsBeanAndViaFactoryIsOnlyCalledOnce.

@Test
void tomcatProtocolHandlerCustomizerRegisteredAsBeanAndViaFactoryIsOnlyCalledOnce() {
    WebApplicationContextRunner runner = new WebApplicationContextRunner(AnnotationConfigServletWebServerApplicationContext::new).withConfiguration(AutoConfigurations.of(ServletWebServerFactoryAutoConfiguration.class)).withUserConfiguration(DoubleRegistrationTomcatProtocolHandlerCustomizerConfiguration.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)

Example 30 with WebApplicationContextRunner

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

the class ServletWebServerFactoryAutoConfigurationTests method tomcatContextCustomizerRegisteredAsBeanAndViaFactoryIsOnlyCalledOnce.

@Test
void tomcatContextCustomizerRegisteredAsBeanAndViaFactoryIsOnlyCalledOnce() {
    WebApplicationContextRunner runner = new WebApplicationContextRunner(AnnotationConfigServletWebServerApplicationContext::new).withConfiguration(AutoConfigurations.of(ServletWebServerFactoryAutoConfiguration.class)).withUserConfiguration(DoubleRegistrationTomcatContextCustomizerConfiguration.class).withPropertyValues("server.port: 0");
    runner.run((context) -> {
        TomcatServletWebServerFactory factory = context.getBean(TomcatServletWebServerFactory.class);
        TomcatContextCustomizer customizer = context.getBean("contextCustomizer", TomcatContextCustomizer.class);
        assertThat(factory.getTomcatContextCustomizers()).contains(customizer);
        then(customizer).should().customize(any(Context.class));
    });
}
Also used : TomcatContextCustomizer(org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer) AnnotationConfigServletWebServerApplicationContext(org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext) AssertableWebApplicationContext(org.springframework.boot.test.context.assertj.AssertableWebApplicationContext) Context(org.apache.catalina.Context) ApplicationContext(org.springframework.context.ApplicationContext) ServletContext(jakarta.servlet.ServletContext) 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