Merge pull request #12148 from hkhan/JAVA-8151-update-spring-webclient
Java 8151 update spring webclient
This commit is contained in:
		
						commit
						e825abd158
					
				| @ -1,24 +1,14 @@ | ||||
| package com.baeldung.reactive.webclient; | ||||
| 
 | ||||
| import lombok.AllArgsConstructor; | ||||
| import lombok.Data; | ||||
| import lombok.NoArgsConstructor; | ||||
| 
 | ||||
| @Data | ||||
| @NoArgsConstructor | ||||
| @AllArgsConstructor | ||||
| public class Foo { | ||||
| 
 | ||||
|     private String name; | ||||
| 
 | ||||
|     public Foo() { | ||||
|         super(); | ||||
|     } | ||||
| 
 | ||||
|     public Foo(String name) { | ||||
|         super(); | ||||
|         this.name = name; | ||||
|     } | ||||
| 
 | ||||
|     public String getName() { | ||||
|         return name; | ||||
|     } | ||||
| 
 | ||||
|     public void setName(String name) { | ||||
|         this.name = name; | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  | ||||
| @ -15,11 +15,10 @@ public class WebClientApplication { | ||||
|     } | ||||
| 
 | ||||
|     @Bean | ||||
|     public SecurityWebFilterChain functionalValidationsSpringSecurityFilterChain(ServerHttpSecurity http) { | ||||
|         http.authorizeExchange() | ||||
|                 .anyExchange() | ||||
|                 .permitAll(); | ||||
|         http.csrf().disable(); | ||||
|     public SecurityWebFilterChain filterChain(ServerHttpSecurity http) { | ||||
|         http.csrf().disable() | ||||
|           .authorizeExchange() | ||||
|           .anyExchange().permitAll(); | ||||
|         return http.build(); | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -45,13 +45,13 @@ import static org.assertj.core.api.Assertions.assertThat; | ||||
| @SpringBootTest(classes = WebClientApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT) | ||||
| public class WebClientIntegrationTest { | ||||
| 
 | ||||
|     @LocalServerPort | ||||
|     private int port; | ||||
| 
 | ||||
|     private static final String BODY_VALUE = "bodyValue"; | ||||
|     private static final ParameterizedTypeReference<Map<String, String>> MAP_RESPONSE_REF = new ParameterizedTypeReference<Map<String, String>>() { | ||||
|     }; | ||||
| 
 | ||||
|     @LocalServerPort | ||||
|     private int port; | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenDifferentWebClientCreationMethods_whenUsed_thenObtainExpectedResponse() { | ||||
|         // WebClient creation | ||||
| @ -106,9 +106,11 @@ public class WebClientIntegrationTest { | ||||
|     public void givenDifferentUriSpecifications_whenUsed_thenObtainExpectedResponse() { | ||||
|         // uri specification | ||||
|         RequestBodySpec bodySpecUsingString = createDefaultPostRequest().uri("/resource"); | ||||
|         RequestBodySpec bodySpecUsingUriBuilder = createDefaultPostRequest().uri(uriBuilder -> uriBuilder.pathSegment("resource") | ||||
|         RequestBodySpec bodySpecUsingUriBuilder = createDefaultPostRequest().uri( | ||||
|           uriBuilder -> uriBuilder.pathSegment("resource") | ||||
|             .build()); | ||||
|         RequestBodySpec bodySpecusingURI = createDefaultPostRequest().uri(URI.create("http://localhost:" + port + "/resource")); | ||||
|         RequestBodySpec bodySpecusingURI = createDefaultPostRequest().uri( | ||||
|           URI.create("http://localhost:" + port + "/resource")); | ||||
|         RequestBodySpec bodySpecOverridenBaseUri = createDefaultPostRequest().uri(URI.create("/resource")); | ||||
|         RequestBodySpec bodySpecOverridenBaseUri2 = WebClient.builder() | ||||
|           .baseUrl("http://localhost:" + port) | ||||
| @ -140,8 +142,10 @@ public class WebClientIntegrationTest { | ||||
|     @Test | ||||
|     public void givenDifferentBodySpecifications_whenUsed_thenObtainExpectedResponse() { | ||||
|         // request body specifications | ||||
|         RequestHeadersSpec<?> headersSpecPost1 = createDefaultPostResourceRequest().body(BodyInserters.fromPublisher(Mono.just(BODY_VALUE), String.class)); | ||||
|         RequestHeadersSpec<?> headersSpecPost2 = createDefaultPostResourceRequest().body(BodyInserters.fromValue(BODY_VALUE)); | ||||
|         RequestHeadersSpec<?> headersSpecPost1 = createDefaultPostResourceRequest().body( | ||||
|           BodyInserters.fromPublisher(Mono.just(BODY_VALUE), String.class)); | ||||
|         RequestHeadersSpec<?> headersSpecPost2 = createDefaultPostResourceRequest().body( | ||||
|           BodyInserters.fromValue(BODY_VALUE)); | ||||
|         RequestHeadersSpec<?> headersSpecPost3 = createDefaultPostResourceRequest().bodyValue(BODY_VALUE); | ||||
|         RequestHeadersSpec<?> headersSpecFooPost = createDefaultPostRequest().uri("/resource-foo") | ||||
|           .body(Mono.just(new Foo("fooName")), Foo.class); | ||||
| @ -152,7 +156,8 @@ public class WebClientIntegrationTest { | ||||
|         LinkedMultiValueMap<String, String> map = new LinkedMultiValueMap<>(); | ||||
|         map.add("key1", "multipartValue1"); | ||||
|         map.add("key2", "multipartValue2"); | ||||
|         BodyInserter<MultiValueMap<String, Object>, ClientHttpRequest> inserterMultipart = BodyInserters.fromMultipartData(map); | ||||
|         BodyInserter<MultiValueMap<String, Object>, ClientHttpRequest> inserterMultipart = BodyInserters.fromMultipartData( | ||||
|           map); | ||||
|         RequestHeadersSpec<?> headersSpecInserterMultipart = createDefaultPostRequest().uri("/resource-multipart") | ||||
|           .body(inserterMultipart); | ||||
| 
 | ||||
| @ -177,7 +182,8 @@ public class WebClientIntegrationTest { | ||||
|           .expectError(CodecException.class) | ||||
|           .verify(); | ||||
|         // assert response for request with no body | ||||
|         Mono<Map<String, String>> responsePostWithNoBody = createDefaultPostResourceRequest().exchangeToMono(responseHandler -> { | ||||
|         Mono<Map<String, String>> responsePostWithNoBody = createDefaultPostResourceRequest().exchangeToMono( | ||||
|           responseHandler -> { | ||||
|               assertThat(responseHandler.statusCode()).isEqualTo(HttpStatus.BAD_REQUEST); | ||||
|               return responseHandler.bodyToMono(MAP_RESPONSE_REF); | ||||
|           }); | ||||
| @ -190,7 +196,8 @@ public class WebClientIntegrationTest { | ||||
|     @Test | ||||
|     public void givenPostSpecifications_whenHeadersAdded_thenObtainExpectedResponse() { | ||||
|         // request header specification | ||||
|         RequestHeadersSpec<?> headersSpecInserterStringWithHeaders = createDefaultPostResourceRequestResponse().header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) | ||||
|         RequestHeadersSpec<?> headersSpecInserterStringWithHeaders = createDefaultPostResourceRequestResponse().header( | ||||
|             HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) | ||||
|           .accept(MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML) | ||||
|           .acceptCharset(StandardCharsets.UTF_8) | ||||
|           .ifNoneMatch("*") | ||||
| @ -209,8 +216,7 @@ public class WebClientIntegrationTest { | ||||
|         Mono<String> responsePostString2 = createDefaultPostResourceRequestResponse().exchangeToMono(response -> { | ||||
|             if (response.statusCode() == HttpStatus.OK) { | ||||
|                 return response.bodyToMono(String.class); | ||||
|             } else if (response.statusCode() | ||||
|                 .is4xxClientError()) { | ||||
|             } else if (response.statusCode().is4xxClientError()) { | ||||
|                 return Mono.just("Error response"); | ||||
|             } else { | ||||
|                 return response.createException() | ||||
| @ -220,8 +226,7 @@ public class WebClientIntegrationTest { | ||||
|         Mono<String> responsePostNoBody = createDefaultPostResourceRequest().exchangeToMono(response -> { | ||||
|             if (response.statusCode() == HttpStatus.OK) { | ||||
|                 return response.bodyToMono(String.class); | ||||
|             } else if (response.statusCode() | ||||
|                 .is4xxClientError()) { | ||||
|             } else if (response.statusCode().is4xxClientError()) { | ||||
|                 return Mono.just("Error response"); | ||||
|             } else { | ||||
|                 return response.createException() | ||||
| @ -268,8 +273,8 @@ public class WebClientIntegrationTest { | ||||
| 
 | ||||
|         StepVerifier.create(neverendingMonoBodyRequest.retrieve() | ||||
|             .bodyToMono(String.class)) | ||||
|             .expectErrorMatches(ex -> WebClientRequestException.class.isAssignableFrom(ex.getClass()) && ReadTimeoutException.class.isAssignableFrom(ex.getCause() | ||||
|                 .getClass())) | ||||
|           .expectErrorMatches(ex -> WebClientRequestException.class.isAssignableFrom(ex.getClass()) | ||||
|             && ReadTimeoutException.class.isAssignableFrom(ex.getCause().getClass())) | ||||
|           .verify(); | ||||
|     } | ||||
| 
 | ||||
|  | ||||
| @ -1,22 +1,22 @@ | ||||
| package com.baeldung.reactive.webclient; | ||||
| 
 | ||||
| import org.junit.Before; | ||||
| import org.junit.Test; | ||||
| import org.junit.runner.RunWith; | ||||
| import org.junit.jupiter.api.BeforeEach; | ||||
| import org.junit.jupiter.api.Test; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.boot.test.context.SpringBootTest; | ||||
| import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; | ||||
| import org.springframework.boot.web.server.LocalServerPort; | ||||
| import org.springframework.test.annotation.DirtiesContext; | ||||
| import org.springframework.test.context.junit4.SpringRunner; | ||||
| import org.springframework.test.web.reactive.server.WebTestClient; | ||||
| 
 | ||||
| @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS) | ||||
| @RunWith(SpringRunner.class) | ||||
| @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = WebClientApplication.class) | ||||
| import static org.springframework.test.annotation.DirtiesContext.ClassMode.BEFORE_CLASS; | ||||
| 
 | ||||
| @DirtiesContext(classMode = BEFORE_CLASS) | ||||
| @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = WebClientApplication.class) | ||||
| public class WebControllerIntegrationTest { | ||||
| 
 | ||||
|     @LocalServerPort | ||||
|     int randomServerPort; | ||||
|     private int randomServerPort; | ||||
| 
 | ||||
|     @Autowired | ||||
|     private WebTestClient testClient; | ||||
| @ -24,30 +24,26 @@ public class WebControllerIntegrationTest { | ||||
|     @Autowired | ||||
|     private WebController webController; | ||||
| 
 | ||||
|     @Before | ||||
|     public void setup() { | ||||
|     @BeforeEach | ||||
|     void setup() { | ||||
|         webController.setServerPort(randomServerPort); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenEndpointWithBlockingClientIsCalled_thenThreeTweetsAreReceived() { | ||||
|     void whenEndpointWithBlockingClientIsCalled_thenThreeTweetsAreReceived() { | ||||
|         testClient.get() | ||||
|           .uri("/tweets-blocking") | ||||
|           .exchange() | ||||
|           .expectStatus() | ||||
|           .isOk() | ||||
|           .expectBodyList(Tweet.class) | ||||
|           .hasSize(3); | ||||
|           .expectStatus().isOk() | ||||
|           .expectBodyList(Tweet.class).hasSize(3); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void whenEndpointWithNonBlockingClientIsCalled_thenThreeTweetsAreReceived() { | ||||
|     void whenEndpointWithNonBlockingClientIsCalled_thenThreeTweetsAreReceived() { | ||||
|         testClient.get() | ||||
|           .uri("/tweets-non-blocking") | ||||
|           .exchange() | ||||
|           .expectStatus() | ||||
|           .isOk() | ||||
|           .expectBodyList(Tweet.class) | ||||
|           .hasSize(3); | ||||
|           .expectStatus().isOk() | ||||
|           .expectBodyList(Tweet.class).hasSize(3); | ||||
|     } | ||||
| } | ||||
| @ -3,6 +3,7 @@ package com.baeldung.reactive.webclient; | ||||
| import org.junit.jupiter.api.Test; | ||||
| import org.springframework.beans.factory.annotation.Autowired; | ||||
| import org.springframework.boot.test.context.SpringBootTest; | ||||
| import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; | ||||
| import org.springframework.boot.web.server.LocalServerPort; | ||||
| import org.springframework.context.ApplicationContext; | ||||
| import org.springframework.security.test.context.support.WithMockUser; | ||||
| @ -14,7 +15,7 @@ import org.springframework.web.reactive.function.server.ServerResponse; | ||||
| import org.springframework.web.server.WebHandler; | ||||
| import reactor.core.publisher.Mono; | ||||
| 
 | ||||
| @SpringBootTest(classes = WebClientApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||||
| @SpringBootTest(classes = WebClientApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT) | ||||
| public class WebTestClientIntegrationTest { | ||||
| 
 | ||||
|     @LocalServerPort | ||||
| @ -26,73 +27,61 @@ public class WebTestClientIntegrationTest { | ||||
|     @Autowired | ||||
|     private WebClientController controller; | ||||
| 
 | ||||
|     private final RouterFunction ROUTER_FUNCTION = RouterFunctions.route(RequestPredicates.GET("/resource"), request -> ServerResponse.ok() | ||||
|         .build()); | ||||
|     private final WebHandler WEB_HANDLER = exchange -> Mono.empty(); | ||||
| 
 | ||||
|     @Test | ||||
|     public void testWebTestClientWithServerWebHandler() { | ||||
|         WebTestClient.bindToWebHandler(WEB_HANDLER) | ||||
|             .build(); | ||||
|     public void whenBindToWebHandler_thenRequestProcessed() { | ||||
|         WebHandler webHandler = exchange -> Mono.empty(); | ||||
| 
 | ||||
|         WebTestClient.bindToWebHandler(webHandler) | ||||
|           .build() | ||||
|           .get() | ||||
|           .exchange() | ||||
|           .expectBody().isEmpty(); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void testWebTestClientWithRouterFunction() { | ||||
|         WebTestClient.bindToRouterFunction(ROUTER_FUNCTION) | ||||
|     public void whenBindToRouter_thenRequestProcessed() { | ||||
|         RouterFunction<ServerResponse> routerFunction = RouterFunctions.route( | ||||
|           RequestPredicates.GET("/resource"), | ||||
|           request -> ServerResponse.ok().build() | ||||
|         ); | ||||
| 
 | ||||
|         WebTestClient.bindToRouterFunction(routerFunction) | ||||
|           .build() | ||||
|             .get() | ||||
|             .uri("/resource") | ||||
|           .get().uri("/resource") | ||||
|           .exchange() | ||||
|             .expectStatus() | ||||
|             .isOk() | ||||
|             .expectBody() | ||||
|             .isEmpty(); | ||||
|           .expectStatus().isOk() | ||||
|           .expectBody().isEmpty(); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     @WithMockUser | ||||
|     public void testWebTestClientWithServerURL() { | ||||
|     public void whenBindToServer_thenRequestProcessed() { | ||||
|         WebTestClient.bindToServer() | ||||
|             .baseUrl("http://localhost:" + port) | ||||
|             .build() | ||||
|             .get() | ||||
|             .uri("/resource") | ||||
|           .baseUrl("http://localhost:" + port).build() | ||||
|           .get().uri("/resource") | ||||
|           .exchange() | ||||
|             .expectStatus() | ||||
|             .isOk() | ||||
|             .expectBody() | ||||
|             .jsonPath("field") | ||||
|             .isEqualTo("value"); | ||||
|         ; | ||||
|           .expectStatus().isOk() | ||||
|           .expectBody().jsonPath("field").isEqualTo("value"); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     @WithMockUser | ||||
|     public void testWebTestClientWithApplicationContext() { | ||||
|     public void whenBindToApplicationContext_thenRequestProcessed() { | ||||
|         WebTestClient.bindToApplicationContext(context) | ||||
|           .build() | ||||
|             .get() | ||||
|             .uri("/resource") | ||||
|           .get().uri("/resource") | ||||
|           .exchange() | ||||
|             .expectStatus() | ||||
|             .isOk() | ||||
|             .expectBody() | ||||
|             .jsonPath("field") | ||||
|             .isEqualTo("value"); | ||||
|           .expectStatus().isOk() | ||||
|           .expectBody().jsonPath("field").isEqualTo("value"); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void testWebTestClientWithController() { | ||||
|     public void whenBindToController_thenRequestProcessed() { | ||||
|         WebTestClient.bindToController(controller) | ||||
|           .build() | ||||
|             .get() | ||||
|             .uri("/resource") | ||||
|           .get().uri("/resource") | ||||
|           .exchange() | ||||
|             .expectStatus() | ||||
|             .isOk() | ||||
|             .expectBody() | ||||
|             .jsonPath("field") | ||||
|             .isEqualTo("value"); | ||||
|           .expectStatus().isOk() | ||||
|           .expectBody().jsonPath("field").isEqualTo("value"); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user