Search in sources :

Example 61 with WebTestClient

use of org.springframework.test.web.reactive.server.WebTestClient in project spring-boot by spring-projects.

the class WebMvcEndpointExposureIntegrationTests method createClient.

private WebTestClient createClient(AssertableWebApplicationContext context) {
    int port = context.getSourceApplicationContext(ServletWebServerApplicationContext.class).getWebServer().getPort();
    ExchangeStrategies exchangeStrategies = ExchangeStrategies.builder().codecs((configurer) -> configurer.defaultCodecs().maxInMemorySize(-1)).build();
    return WebTestClient.bindToServer().baseUrl("http://localhost:" + port).exchangeStrategies(exchangeStrategies).responseTimeout(Duration.ofMinutes(5)).build();
}
Also used : InMemoryAuditEventRepository(org.springframework.boot.actuate.audit.InMemoryAuditEventRepository) AnnotationConfigServletWebServerApplicationContext(org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) DispatcherServletAutoConfiguration(org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ServletException(jakarta.servlet.ServletException) JacksonAutoConfiguration(org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration) ServletWebServerFactoryAutoConfiguration(org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration) Supplier(java.util.function.Supplier) ServletEndpoint(org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpoint) EndpointServlet(org.springframework.boot.actuate.endpoint.web.EndpointServlet) WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) WebApplicationContextRunner(org.springframework.boot.test.context.runner.WebApplicationContextRunner) Duration(java.time.Duration) HealthContributorAutoConfiguration(org.springframework.boot.actuate.autoconfigure.health.HealthContributorAutoConfiguration) WebEndpointAutoConfiguration(org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration) InMemoryHttpTraceRepository(org.springframework.boot.actuate.trace.http.InMemoryHttpTraceRepository) GetMapping(org.springframework.web.bind.annotation.GetMapping) HttpMessageConvertersAutoConfiguration(org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration) AutoConfigurations(org.springframework.boot.autoconfigure.AutoConfigurations) ServletManagementContextAutoConfiguration(org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementContextAutoConfiguration) AssertableWebApplicationContext(org.springframework.boot.test.context.assertj.AssertableWebApplicationContext) HttpTraceAutoConfiguration(org.springframework.boot.actuate.autoconfigure.trace.http.HttpTraceAutoConfiguration) HttpMethod(org.springframework.http.HttpMethod) IOException(java.io.IOException) EndpointAutoConfiguration(org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration) HttpServlet(jakarta.servlet.http.HttpServlet) ManagementContextAutoConfiguration(org.springframework.boot.actuate.autoconfigure.web.server.ManagementContextAutoConfiguration) EntityExchangeResult(org.springframework.test.web.reactive.server.EntityExchangeResult) Test(org.junit.jupiter.api.Test) RestControllerEndpoint(org.springframework.boot.actuate.endpoint.web.annotation.RestControllerEndpoint) Configuration(org.springframework.context.annotation.Configuration) HttpStatus(org.springframework.http.HttpStatus) WebMvcAutoConfiguration(org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration) ExchangeStrategies(org.springframework.web.reactive.function.client.ExchangeStrategies) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) Bean(org.springframework.context.annotation.Bean) ServletWebServerApplicationContext(org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext) ExchangeStrategies(org.springframework.web.reactive.function.client.ExchangeStrategies) ServletEndpoint(org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpoint) RestControllerEndpoint(org.springframework.boot.actuate.endpoint.web.annotation.RestControllerEndpoint)

Example 62 with WebTestClient

use of org.springframework.test.web.reactive.server.WebTestClient in project spring-boot by spring-projects.

the class ControllerEndpointWebFluxIntegrationTests method endpointsCanBeAccessed.

@Test
void endpointsCanBeAccessed() {
    TestSecurityContextHolder.getContext().setAuthentication(new TestingAuthenticationToken("user", "N/A", "ROLE_ACTUATOR"));
    this.context = new AnnotationConfigReactiveWebApplicationContext();
    this.context.register(DefaultConfiguration.class, ExampleController.class);
    TestPropertyValues.of("management.endpoints.web.exposure.include=*").applyTo(this.context);
    this.context.refresh();
    WebTestClient webClient = WebTestClient.bindToApplicationContext(this.context).build();
    webClient.get().uri("/actuator/example").exchange().expectStatus().isOk();
}
Also used : WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) AnnotationConfigReactiveWebApplicationContext(org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 63 with WebTestClient

use of org.springframework.test.web.reactive.server.WebTestClient in project spring-boot by spring-projects.

the class WebFluxEndpointIntegrationTests method linksAreProvidedToAllEndpointTypes.

@Test
void linksAreProvidedToAllEndpointTypes() {
    this.contextRunner.withPropertyValues("management.endpoints.web.exposure.include:*").run((context) -> {
        WebTestClient client = createWebTestClient(context);
        client.get().uri("/actuator").exchange().expectStatus().isOk().expectBody().jsonPath("_links.beans").isNotEmpty().jsonPath("_links.restcontroller").isNotEmpty().jsonPath("_links.controller").isNotEmpty();
    });
}
Also used : WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) Test(org.junit.jupiter.api.Test)

Example 64 with WebTestClient

use of org.springframework.test.web.reactive.server.WebTestClient in project spring-boot by spring-projects.

the class DefaultErrorWebExceptionHandlerIntegrationTests method bindingResultErrorIncludeMessageAndErrors.

@Test
void bindingResultErrorIncludeMessageAndErrors() {
    this.contextRunner.withPropertyValues("server.error.include-message=on-param", "server.error.include-binding-errors=on-param").run((context) -> {
        WebTestClient client = getWebClient(context);
        client.post().uri("/bind?message=true&errors=true").contentType(MediaType.APPLICATION_JSON).bodyValue("{}").exchange().expectStatus().isBadRequest().expectBody().jsonPath("status").isEqualTo("400").jsonPath("error").isEqualTo(HttpStatus.BAD_REQUEST.getReasonPhrase()).jsonPath("path").isEqualTo(("/bind")).jsonPath("exception").doesNotExist().jsonPath("errors").isArray().jsonPath("message").isNotEmpty().jsonPath("requestId").isEqualTo(this.logIdFilter.getLogId());
    });
}
Also used : WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) Test(org.junit.jupiter.api.Test)

Example 65 with WebTestClient

use of org.springframework.test.web.reactive.server.WebTestClient in project spring-boot by spring-projects.

the class DefaultErrorWebExceptionHandlerIntegrationTests method bindingResultError.

@Test
void bindingResultError() {
    this.contextRunner.run((context) -> {
        WebTestClient client = getWebClient(context);
        client.post().uri("/bind").contentType(MediaType.APPLICATION_JSON).bodyValue("{}").exchange().expectStatus().isBadRequest().expectBody().jsonPath("status").isEqualTo("400").jsonPath("error").isEqualTo(HttpStatus.BAD_REQUEST.getReasonPhrase()).jsonPath("path").isEqualTo(("/bind")).jsonPath("exception").doesNotExist().jsonPath("errors").doesNotExist().jsonPath("message").doesNotExist().jsonPath("requestId").isEqualTo(this.logIdFilter.getLogId());
    });
}
Also used : WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) Test(org.junit.jupiter.api.Test)

Aggregations

WebTestClient (org.springframework.test.web.reactive.server.WebTestClient)165 Test (org.junit.jupiter.api.Test)159 SecurityWebFilterChain (org.springframework.security.web.server.SecurityWebFilterChain)44 WebTestClientBuilder (org.springframework.security.test.web.reactive.server.WebTestClientBuilder)34 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)31 Authentication (org.springframework.security.core.Authentication)28 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)27 WebDriver (org.openqa.selenium.WebDriver)26 Mono (reactor.core.publisher.Mono)25 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)23 ServerSecurityContextRepository (org.springframework.security.web.server.context.ServerSecurityContextRepository)22 ReactiveAuthenticationManager (org.springframework.security.authentication.ReactiveAuthenticationManager)21 WebFilterChainProxy (org.springframework.security.web.server.WebFilterChainProxy)21 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)20 BDDMockito.given (org.mockito.BDDMockito.given)20 Mockito.verify (org.mockito.Mockito.verify)20 GetMapping (org.springframework.web.bind.annotation.GetMapping)19 WebFilter (org.springframework.web.server.WebFilter)18 SecurityContext (org.springframework.security.core.context.SecurityContext)17 RestController (org.springframework.web.bind.annotation.RestController)17