Search in sources :

Example 71 with WebTestClient

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

the class RSocketWebSocketNettyRouteProviderTests method webEndpointsShouldWork.

@Test
void webEndpointsShouldWork() {
    new ReactiveWebApplicationContextRunner(AnnotationConfigReactiveWebServerApplicationContext::new).withConfiguration(AutoConfigurations.of(HttpHandlerAutoConfiguration.class, WebFluxAutoConfiguration.class, ErrorWebFluxAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class, JacksonAutoConfiguration.class, CodecsAutoConfiguration.class, RSocketStrategiesAutoConfiguration.class, RSocketServerAutoConfiguration.class, RSocketMessagingAutoConfiguration.class, RSocketRequesterAutoConfiguration.class)).withUserConfiguration(WebConfiguration.class).withPropertyValues("spring.rsocket.server.transport=websocket", "spring.rsocket.server.mapping-path=/rsocket").run((context) -> {
        ReactiveWebServerApplicationContext serverContext = (ReactiveWebServerApplicationContext) context.getSourceApplicationContext();
        RSocketRequester requester = createRSocketRequester(context, serverContext.getWebServer());
        TestProtocol rsocketResponse = requester.route("websocket").data(new TestProtocol("rsocket")).retrieveMono(TestProtocol.class).block(Duration.ofSeconds(3));
        assertThat(rsocketResponse.getName()).isEqualTo("rsocket");
        WebTestClient client = createWebTestClient(serverContext.getWebServer());
        client.get().uri("/protocol").exchange().expectStatus().isOk().expectBody().jsonPath("name", "http");
    });
}
Also used : RSocketRequester(org.springframework.messaging.rsocket.RSocketRequester) WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) AnnotationConfigReactiveWebServerApplicationContext(org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext) AnnotationConfigReactiveWebServerApplicationContext(org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext) ReactiveWebServerApplicationContext(org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext) ReactiveWebApplicationContextRunner(org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner) Test(org.junit.jupiter.api.Test)

Example 72 with WebTestClient

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

the class WebTestClientAutoConfigurationTests method shouldApplySpringSecurityConfigurer.

@Test
@SuppressWarnings("unchecked")
void shouldApplySpringSecurityConfigurer() {
    this.contextRunner.withUserConfiguration(BaseConfiguration.class).run((context) -> {
        WebTestClient webTestClient = context.getBean(WebTestClient.class);
        WebTestClient.Builder builder = (WebTestClient.Builder) ReflectionTestUtils.getField(webTestClient, "builder");
        WebHttpHandlerBuilder httpHandlerBuilder = (WebHttpHandlerBuilder) ReflectionTestUtils.getField(builder, "httpHandlerBuilder");
        List<WebFilter> filters = (List<WebFilter>) ReflectionTestUtils.getField(httpHandlerBuilder, "filters");
        assertThat(filters.get(0).getClass().getName()).isEqualTo("org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers$MutatorFilter");
    });
}
Also used : WebFilter(org.springframework.web.server.WebFilter) WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) WebHttpHandlerBuilder(org.springframework.web.server.adapter.WebHttpHandlerBuilder) WebHttpHandlerBuilder(org.springframework.web.server.adapter.WebHttpHandlerBuilder) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 73 with WebTestClient

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

the class WebTestClientAutoConfigurationTests method shouldCustomizeTimeout.

@Test
void shouldCustomizeTimeout() {
    this.contextRunner.withUserConfiguration(BaseConfiguration.class).withPropertyValues("spring.test.webtestclient.timeout=15m").run((context) -> {
        WebTestClient webTestClient = context.getBean(WebTestClient.class);
        assertThat(webTestClient).hasFieldOrPropertyWithValue("responseTimeout", Duration.ofMinutes(15));
    });
}
Also used : WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) Test(org.junit.jupiter.api.Test)

Example 74 with WebTestClient

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

the class DefaultErrorWebExceptionHandlerIntegrationTests method testExceptionWithNullMessage.

@Test
void testExceptionWithNullMessage() {
    this.contextRunner.withPropertyValues("spring.mustache.prefix=classpath:/unknown/").run((context) -> {
        WebTestClient client = getWebClient(context);
        String body = client.get().uri("/notfound").accept(MediaType.TEXT_HTML).exchange().expectStatus().isNotFound().expectHeader().contentType(TEXT_HTML_UTF8).expectBody(String.class).returnResult().getResponseBody();
        assertThat(body).contains("Whitelabel Error Page").contains(this.logIdFilter.getLogId()).contains("type=Not Found, status=404");
    });
}
Also used : WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) Test(org.junit.jupiter.api.Test)

Example 75 with WebTestClient

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

the class DefaultErrorWebExceptionHandlerIntegrationTests method whitelabelDisabled.

@Test
void whitelabelDisabled() {
    this.contextRunner.withPropertyValues("server.error.whitelabel.enabled=false", "spring.mustache.prefix=classpath:/unknown/").run((context) -> {
        WebTestClient client = getWebClient(context);
        client.get().uri("/notfound").accept(MediaType.TEXT_HTML).exchange().expectStatus().isNotFound().expectBody().isEmpty();
    });
}
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