Search in sources :

Example 91 with WebTestClient

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

the class ViewResolutionTests method jsonOnly.

@Test
void jsonOnly() {
    WebTestClient testClient = MockMvcWebTestClient.bindToController(new PersonController()).singleView(new MappingJackson2JsonView()).build();
    testClient.get().uri("/person/Corea").exchange().expectStatus().isOk().expectHeader().contentTypeCompatibleWith(MediaType.APPLICATION_JSON).expectBody().jsonPath("$.person.name", "Corea");
}
Also used : WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) MockMvcWebTestClient(org.springframework.test.web.servlet.client.MockMvcWebTestClient) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView) Test(org.junit.jupiter.api.Test)

Example 92 with WebTestClient

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

the class ViewResolutionTests method contentNegotiation.

@Test
void contentNegotiation() throws Exception {
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    marshaller.setClassesToBeBound(Person.class);
    List<View> viewList = new ArrayList<>();
    viewList.add(new MappingJackson2JsonView());
    viewList.add(new MarshallingView(marshaller));
    ContentNegotiationManager manager = new ContentNegotiationManager(new HeaderContentNegotiationStrategy(), new FixedContentNegotiationStrategy(MediaType.TEXT_HTML));
    ContentNegotiatingViewResolver cnViewResolver = new ContentNegotiatingViewResolver();
    cnViewResolver.setDefaultViews(viewList);
    cnViewResolver.setContentNegotiationManager(manager);
    cnViewResolver.afterPropertiesSet();
    WebTestClient testClient = MockMvcWebTestClient.bindToController(new PersonController()).viewResolvers(cnViewResolver, new InternalResourceViewResolver()).build();
    EntityExchangeResult<Void> result = testClient.get().uri("/person/Corea").exchange().expectStatus().isOk().expectBody().isEmpty();
    // Further assertions on the server response
    MockMvcWebTestClient.resultActionsFor(result).andExpect(model().size(1)).andExpect(model().attributeExists("person")).andExpect(forwardedUrl("person/show"));
    testClient.get().uri("/person/Corea").accept(MediaType.APPLICATION_JSON).exchange().expectStatus().isOk().expectHeader().contentTypeCompatibleWith(MediaType.APPLICATION_JSON).expectBody().jsonPath("$.person.name", "Corea");
    testClient.get().uri("/person/Corea").accept(MediaType.APPLICATION_XML).exchange().expectStatus().isOk().expectHeader().contentType(MediaType.APPLICATION_XML).expectBody().xpath("/person/name/text()").isEqualTo("Corea");
}
Also used : WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) MockMvcWebTestClient(org.springframework.test.web.servlet.client.MockMvcWebTestClient) ArrayList(java.util.ArrayList) Jaxb2Marshaller(org.springframework.oxm.jaxb.Jaxb2Marshaller) FixedContentNegotiationStrategy(org.springframework.web.accept.FixedContentNegotiationStrategy) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView) View(org.springframework.web.servlet.View) MarshallingView(org.springframework.web.servlet.view.xml.MarshallingView) MappingJackson2JsonView(org.springframework.web.servlet.view.json.MappingJackson2JsonView) ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) MarshallingView(org.springframework.web.servlet.view.xml.MarshallingView) ContentNegotiatingViewResolver(org.springframework.web.servlet.view.ContentNegotiatingViewResolver) HeaderContentNegotiationStrategy(org.springframework.web.accept.HeaderContentNegotiationStrategy) InternalResourceViewResolver(org.springframework.web.servlet.view.InternalResourceViewResolver) Test(org.junit.jupiter.api.Test)

Example 93 with WebTestClient

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

the class IntegrationGraphControllerTests method testIntegrationGraphGet.

@Test
public void testIntegrationGraphGet() {
    WebTestClient webTestClient = WebTestClient.bindToApplicationContext(this.wac).build();
    webTestClient.get().uri("/testIntegration").accept(MediaType.APPLICATION_JSON_UTF8).exchange().expectStatus().isOk().expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8).expectBody().jsonPath("$.nodes..name").isArray().jsonPath("$.contentDescriptor.name").isEqualTo("testApplication").jsonPath("$.links").exists();
}
Also used : WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) Test(org.junit.Test)

Example 94 with WebTestClient

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

the class AbstractEndpointRequestIntegrationTests method toEndpointShouldMatch.

@Test
void toEndpointShouldMatch() {
    getContextRunner().run((context) -> {
        WebTestClient webTestClient = getWebTestClient(context);
        webTestClient.get().uri("/actuator/e1").exchange().expectStatus().isOk();
    });
}
Also used : WebTestClient(org.springframework.test.web.reactive.server.WebTestClient) Test(org.junit.jupiter.api.Test)

Example 95 with WebTestClient

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

the class MvcEndpointRequestIntegrationTests method toAnyEndpointWhenServletPathSetShouldMatch.

@Test
void toAnyEndpointWhenServletPathSetShouldMatch() {
    getContextRunner().withPropertyValues("spring.mvc.servlet.path=/admin", "spring.security.user.password=password").run((context) -> {
        WebTestClient webTestClient = getWebTestClient(context);
        webTestClient.get().uri("/admin/actuator/e2").exchange().expectStatus().isUnauthorized();
        webTestClient.get().uri("/admin/actuator/e2").header("Authorization", getBasicAuth()).exchange().expectStatus().isOk();
    });
}
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