Search in sources :

Example 76 with WebTestClient

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

the class DefaultErrorWebExceptionHandlerIntegrationTests method exactStatusTemplateErrorPage.

@Test
void exactStatusTemplateErrorPage() {
    this.contextRunner.withPropertyValues("server.error.whitelabel.enabled=false", "spring.mustache.prefix=" + getErrorTemplatesLocation()).run((context) -> {
        WebTestClient client = getWebClient(context);
        String body = client.get().uri("/notfound").accept(MediaType.TEXT_HTML).exchange().expectStatus().isNotFound().expectBody(String.class).returnResult().getResponseBody();
        assertThat(body).contains("404 page");
    });
}
Also used : WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) Test(org.junit.jupiter.api.Test)

Example 77 with WebTestClient

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

the class DefaultErrorWebExceptionHandlerIntegrationTests method includeStackTraceOnParam.

@Test
void includeStackTraceOnParam() {
    this.contextRunner.withPropertyValues("server.error.include-exception=true", "server.error.include-stacktrace=on-param").run((context) -> {
        WebTestClient client = getWebClient(context);
        client.get().uri("/?trace=true").exchange().expectStatus().isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR).expectBody().jsonPath("status").isEqualTo("500").jsonPath("error").isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()).jsonPath("exception").isEqualTo(IllegalStateException.class.getName()).jsonPath("trace").exists().jsonPath("requestId").isEqualTo(this.logIdFilter.getLogId());
    });
}
Also used : WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) Test(org.junit.jupiter.api.Test)

Example 78 with WebTestClient

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

the class DefaultErrorWebExceptionHandlerIntegrationTests method alwaysIncludeStackTrace.

@Test
void alwaysIncludeStackTrace() {
    this.contextRunner.withPropertyValues("server.error.include-exception=true", "server.error.include-stacktrace=always").run((context) -> {
        WebTestClient client = getWebClient(context);
        client.get().uri("/?trace=false").exchange().expectStatus().isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR).expectBody().jsonPath("status").isEqualTo("500").jsonPath("error").isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()).jsonPath("exception").isEqualTo(IllegalStateException.class.getName()).jsonPath("trace").exists().jsonPath("requestId").isEqualTo(this.logIdFilter.getLogId());
    });
}
Also used : WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) Test(org.junit.jupiter.api.Test)

Example 79 with WebTestClient

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

the class DefaultErrorWebExceptionHandlerIntegrationTests method jsonError.

@Test
void jsonError(CapturedOutput output) {
    this.contextRunner.run((context) -> {
        WebTestClient client = getWebClient(context);
        client.get().uri("/").exchange().expectStatus().isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR).expectBody().jsonPath("status").isEqualTo("500").jsonPath("error").isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()).jsonPath("path").isEqualTo(("/")).jsonPath("message").doesNotExist().jsonPath("exception").doesNotExist().jsonPath("trace").doesNotExist().jsonPath("requestId").isEqualTo(this.logIdFilter.getLogId());
        assertThat(output).contains("500 Server Error for HTTP GET \"/\"").contains("java.lang.IllegalStateException: Expected!");
    });
}
Also used : WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) Test(org.junit.jupiter.api.Test)

Example 80 with WebTestClient

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

the class DefaultErrorWebExceptionHandlerIntegrationTests method defaultErrorView.

@Test
void defaultErrorView() {
    this.contextRunner.withPropertyValues("spring.mustache.prefix=classpath:/unknown/", "server.error.include-stacktrace=always", "server.error.include-message=always").run((context) -> {
        WebTestClient client = getWebClient(context);
        String body = client.get().uri("/").accept(MediaType.TEXT_HTML).exchange().expectStatus().isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR).expectHeader().contentType(TEXT_HTML_UTF8).expectBody(String.class).returnResult().getResponseBody();
        assertThat(body).contains("Whitelabel Error Page").contains(this.logIdFilter.getLogId()).contains("<div>Expected!</div>").contains("<div style='white-space:pre-wrap;'>java.lang.IllegalStateException");
    });
}
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