Search in sources :

Example 81 with WebTestClient

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

the class DefaultErrorWebExceptionHandlerIntegrationTests method htmlError.

@Test
void htmlError() {
    this.contextRunner.withPropertyValues("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("status: 500").contains("message: Expected!");
    });
}
Also used : WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) Test(org.junit.jupiter.api.Test)

Example 82 with WebTestClient

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

the class DefaultErrorWebExceptionHandlerIntegrationTests method escapeHtmlInDefaultErrorView.

@Test
void escapeHtmlInDefaultErrorView() {
    this.contextRunner.withPropertyValues("spring.mustache.prefix=classpath:/unknown/", "server.error.include-message=always").run((context) -> {
        WebTestClient client = getWebClient(context);
        String body = client.get().uri("/html").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()).doesNotContain("<script>").contains("&lt;script&gt;");
    });
}
Also used : WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) Test(org.junit.jupiter.api.Test)

Example 83 with WebTestClient

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

the class DefaultErrorWebExceptionHandlerIntegrationTests method invalidAcceptMediaType.

@Test
void invalidAcceptMediaType() {
    this.contextRunner.run((context) -> {
        WebTestClient client = getWebClient(context);
        client.get().uri("/notfound").header("Accept", "v=3.0").exchange().expectStatus().isEqualTo(HttpStatus.NOT_FOUND);
    });
}
Also used : WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) Test(org.junit.jupiter.api.Test)

Example 84 with WebTestClient

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

the class DefaultErrorWebExceptionHandlerIntegrationTests method defaultErrorAttributesSubclassWithoutDelegation.

@Test
void defaultErrorAttributesSubclassWithoutDelegation() {
    this.contextRunner.withUserConfiguration(CustomErrorAttributesWithoutDelegation.class).run((context) -> {
        WebTestClient client = getWebClient(context);
        client.get().uri("/badRequest").exchange().expectStatus().isBadRequest().expectBody().jsonPath("status").isEqualTo("400").jsonPath("timestamp").doesNotExist().jsonPath("error").isEqualTo("custom error").jsonPath("path").doesNotExist();
    });
}
Also used : WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) Test(org.junit.jupiter.api.Test)

Example 85 with WebTestClient

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

the class DefaultErrorWebExceptionHandlerIntegrationTests method alwaysIncludeMessage.

@Test
void alwaysIncludeMessage() {
    this.contextRunner.withPropertyValues("server.error.include-exception=true", "server.error.include-message=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("message").isNotEmpty().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