Adapt to API change in Spring Framework 5.3.0 snapshots

See gh-23534
pull/23540/head
Stephane Nicoll 4 years ago
parent 6254ad634e
commit 366fec33d0

@ -67,6 +67,7 @@ import org.springframework.security.web.server.WebFilterChainProxy;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.reactive.function.client.WebClient;
import static org.assertj.core.api.Assertions.assertThat;
@ -269,7 +270,8 @@ class ReactiveCloudFoundryActuatorAutoConfigurationTests {
"cloudFoundrySecurityService");
WebClient webClient = (WebClient) ReflectionTestUtils.getField(interceptorSecurityService,
"webClient");
webClient.get().uri("https://self-signed.badssl.com/").exchange().block(Duration.ofSeconds(30));
webClient.get().uri("https://self-signed.badssl.com/").exchangeToMono(ClientResponse::releaseBody)
.block(Duration.ofSeconds(30));
});
}
@ -285,8 +287,9 @@ class ReactiveCloudFoundryActuatorAutoConfigurationTests {
"cloudFoundrySecurityService");
WebClient webClient = (WebClient) ReflectionTestUtils.getField(interceptorSecurityService,
"webClient");
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> webClient.get()
.uri("https://self-signed.badssl.com/").exchange().block(Duration.ofSeconds(30)))
assertThatExceptionOfType(RuntimeException.class)
.isThrownBy(() -> webClient.get().uri("https://self-signed.badssl.com/")
.exchangeToMono(ClientResponse::releaseBody).block(Duration.ofSeconds(30)))
.withCauseInstanceOf(SSLException.class);
});
}

@ -38,6 +38,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.reactive.ClientHttpConnector;
import org.springframework.mock.http.client.reactive.MockClientHttpResponse;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.reactive.function.client.WebClient;
import static org.assertj.core.api.Assertions.assertThat;
@ -111,7 +112,8 @@ class WebClientMetricsConfigurationTests {
WebClient webClient = mockWebClient(context.getBean(WebClient.Builder.class));
MeterRegistry registry = context.getBean(MeterRegistry.class);
for (int i = 0; i < 3; i++) {
webClient.get().uri("https://example.org/projects/" + i).exchange().block(Duration.ofSeconds(30));
webClient.get().uri("https://example.org/projects/" + i).exchangeToMono(ClientResponse::releaseBody)
.block(Duration.ofSeconds(30));
}
return registry;
}
@ -119,8 +121,8 @@ class WebClientMetricsConfigurationTests {
private void validateWebClient(WebClient.Builder builder, MeterRegistry registry) {
WebClient webClient = mockWebClient(builder);
assertThat(registry.find("http.client.requests").meter()).isNull();
webClient.get().uri("https://example.org/projects/{project}", "spring-boot").exchange()
.block(Duration.ofSeconds(30));
webClient.get().uri("https://example.org/projects/{project}", "spring-boot")
.exchangeToMono(ClientResponse::releaseBody).block(Duration.ofSeconds(30));
assertThat(registry.find("http.client.requests").tags("uri", "/projects/{project}").meter()).isNotNull();
}

@ -19,11 +19,13 @@ package org.springframework.boot.actuate.autoconfigure.web.servlet;
import java.util.Collections;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;
import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration;
@ -40,6 +42,7 @@ import org.springframework.boot.test.context.runner.ContextConsumer;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@ -74,9 +77,8 @@ class WebMvcEndpointChildContextConfigurationIntegrationTests {
@Test // gh-17938
void errorEndpointIsUsedWithEndpoint() {
this.runner.run(withWebTestClient((client) -> {
ClientResponse response = client.get().uri("actuator/fail").accept(MediaType.APPLICATION_JSON).exchange()
.block();
Map<String, ?> body = getResponseBody(response);
Map<String, ?> body = client.get().uri("actuator/fail").accept(MediaType.APPLICATION_JSON)
.exchangeToMono(toResponseBody()).block();
assertThat(body).hasEntrySatisfying("exception",
(value) -> assertThat(value).asString().contains("IllegalStateException"));
assertThat(body).hasEntrySatisfying("message",
@ -88,9 +90,8 @@ class WebMvcEndpointChildContextConfigurationIntegrationTests {
void errorPageAndErrorControllerIncludeDetails() {
this.runner.withPropertyValues("server.error.include-stacktrace=always", "server.error.include-message=always")
.run(withWebTestClient((client) -> {
ClientResponse response = client.get().uri("actuator/fail").accept(MediaType.APPLICATION_JSON)
.exchange().block();
Map<String, ?> body = getResponseBody(response);
Map<String, ?> body = client.get().uri("actuator/fail").accept(MediaType.APPLICATION_JSON)
.exchangeToMono(toResponseBody()).block();
assertThat(body).hasEntrySatisfying("message",
(value) -> assertThat(value).asString().contains("Epic Fail"));
assertThat(body).hasEntrySatisfying("trace", (value) -> assertThat(value).asString()
@ -101,9 +102,8 @@ class WebMvcEndpointChildContextConfigurationIntegrationTests {
@Test
void errorEndpointIsUsedWithRestControllerEndpoint() {
this.runner.run(withWebTestClient((client) -> {
ClientResponse response = client.get().uri("actuator/failController").accept(MediaType.APPLICATION_JSON)
.exchange().block();
Map<String, ?> body = getResponseBody(response);
Map<String, ?> body = client.get().uri("actuator/failController").accept(MediaType.APPLICATION_JSON)
.exchangeToMono(toResponseBody()).block();
assertThat(body).hasEntrySatisfying("exception",
(value) -> assertThat(value).asString().contains("IllegalStateException"));
assertThat(body).hasEntrySatisfying("message",
@ -114,10 +114,9 @@ class WebMvcEndpointChildContextConfigurationIntegrationTests {
@Test
void errorEndpointIsUsedWithRestControllerEndpointOnBindingError() {
this.runner.run(withWebTestClient((client) -> {
ClientResponse response = client.post().uri("actuator/failController")
.bodyValue(Collections.singletonMap("content", "")).accept(MediaType.APPLICATION_JSON).exchange()
.block();
Map<String, ?> body = getResponseBody(response);
Map<String, ?> body = client.post().uri("actuator/failController")
.bodyValue(Collections.singletonMap("content", "")).accept(MediaType.APPLICATION_JSON)
.exchangeToMono(toResponseBody()).block();
assertThat(body).hasEntrySatisfying("exception",
(value) -> assertThat(value).asString().contains("MethodArgumentNotValidException"));
assertThat(body).hasEntrySatisfying("message",
@ -134,9 +133,9 @@ class WebMvcEndpointChildContextConfigurationIntegrationTests {
};
}
@SuppressWarnings("unchecked")
private Map<String, ?> getResponseBody(ClientResponse response) {
return response.bodyToMono(Map.class).block();
private Function<ClientResponse, ? extends Mono<Map<String, ?>>> toResponseBody() {
return ((clientResponse) -> clientResponse.bodyToMono(new ParameterizedTypeReference<Map<String, ?>>() {
}));
}
@Endpoint(id = "fail")

@ -56,6 +56,7 @@ public class ElasticsearchReactiveHealthIndicator extends AbstractReactiveHealth
return this.client.execute(this::getHealth).flatMap((response) -> doHealthCheck(builder, response));
}
@SuppressWarnings("deprecation") // Requires an update in ReactiveElasticsearchClient
private Mono<ClientResponse> getHealth(WebClient webClient) {
return webClient.get().uri("/_cluster/health/").exchange();
}

@ -20,6 +20,7 @@ import java.net.URI;
import java.time.Duration;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.boot.autoconfigure.AutoConfigurations;
@ -88,6 +89,7 @@ class WebClientAutoConfigurationTests {
void shouldGetPrototypeScopedBean() {
this.contextRunner.withUserConfiguration(WebClientCustomizerConfig.class).run((context) -> {
ClientHttpResponse response = mock(ClientHttpResponse.class);
given(response.getBody()).willReturn(Flux.empty());
given(response.getHeaders()).willReturn(new HttpHeaders());
ClientHttpConnector firstConnector = mock(ClientHttpConnector.class);
given(firstConnector.connect(any(), any(), any())).willReturn(Mono.just(response));
@ -98,8 +100,8 @@ class WebClientAutoConfigurationTests {
WebClient.Builder secondBuilder = context.getBean(WebClient.Builder.class);
secondBuilder.clientConnector(secondConnector).baseUrl("https://second.example.org");
assertThat(firstBuilder).isNotEqualTo(secondBuilder);
firstBuilder.build().get().uri("/foo").exchange().block(Duration.ofSeconds(30));
secondBuilder.build().get().uri("/foo").exchange().block(Duration.ofSeconds(30));
firstBuilder.build().get().uri("/foo").retrieve().toBodilessEntity().block(Duration.ofSeconds(30));
secondBuilder.build().get().uri("/foo").retrieve().toBodilessEntity().block(Duration.ofSeconds(30));
verify(firstConnector).connect(eq(HttpMethod.GET), eq(URI.create("https://first.example.org/foo")), any());
verify(secondConnector).connect(eq(HttpMethod.GET), eq(URI.create("https://second.example.org/foo")),
any());

@ -74,7 +74,7 @@ class WebTestClientAutoConfigurationTests {
this.contextRunner.withUserConfiguration(BaseConfiguration.class)
.withPropertyValues("spring.test.webtestclient.timeout=15m").run((context) -> {
WebTestClient webTestClient = context.getBean(WebTestClient.class);
assertThat(webTestClient).hasFieldOrPropertyWithValue("timeout", Duration.ofMinutes(15));
assertThat(webTestClient).hasFieldOrPropertyWithValue("responseTimeout", Duration.ofMinutes(15));
});
}

@ -140,7 +140,7 @@ class NettyReactiveWebServerFactoryTests extends AbstractReactiveWebServerFactor
WebClient client = WebClient.builder().baseUrl("https://localhost:" + this.webServer.getPort())
.clientConnector(connector).build();
return client.post().uri("/test").contentType(MediaType.TEXT_PLAIN).body(BodyInserters.fromValue("Hello World"))
.exchange().flatMap((response) -> response.bodyToMono(String.class));
.retrieve().bodyToMono(String.class);
}
}

@ -143,8 +143,7 @@ class UndertowReactiveWebServerFactoryTests extends AbstractReactiveWebServerFac
this.webServer.start();
WebClient client = getWebClient(this.webServer.getPort()).build();
Mono<String> result = client.post().uri("/test").contentType(MediaType.TEXT_PLAIN)
.body(BodyInserters.fromValue("Hello World")).exchange()
.flatMap((response) -> response.bodyToMono(String.class));
.body(BodyInserters.fromValue("Hello World")).retrieve().bodyToMono(String.class);
assertThat(result.block(Duration.ofSeconds(30))).isEqualTo("Hello World");
File accessLog = new File(accessLogDirectory, expectedFile);
awaitFile(accessLog);

@ -70,6 +70,7 @@ import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.util.SocketUtils;
import org.springframework.util.unit.DataSize;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientRequestException;
@ -110,8 +111,8 @@ public abstract class AbstractReactiveWebServerFactoryTests {
return port;
});
Mono<String> result = getWebClient(this.webServer.getPort()).build().post().uri("/test")
.contentType(MediaType.TEXT_PLAIN).body(BodyInserters.fromValue("Hello World")).exchange()
.flatMap((response) -> response.bodyToMono(String.class));
.contentType(MediaType.TEXT_PLAIN).body(BodyInserters.fromValue("Hello World")).retrieve()
.bodyToMono(String.class);
assertThat(result.block(Duration.ofSeconds(30))).isEqualTo("Hello World");
assertThat(this.webServer.getPort()).isEqualTo(specificPort);
}
@ -139,8 +140,7 @@ public abstract class AbstractReactiveWebServerFactoryTests {
WebClient client = WebClient.builder().baseUrl("https://localhost:" + this.webServer.getPort())
.clientConnector(connector).build();
Mono<String> result = client.post().uri("/test").contentType(MediaType.TEXT_PLAIN)
.body(BodyInserters.fromValue("Hello World")).exchange()
.flatMap((response) -> response.bodyToMono(String.class));
.body(BodyInserters.fromValue("Hello World")).retrieve().bodyToMono(String.class);
assertThat(result.block(Duration.ofSeconds(30))).isEqualTo("Hello World");
}
@ -161,8 +161,7 @@ public abstract class AbstractReactiveWebServerFactoryTests {
.clientConnector(connector).build();
Mono<String> result = client.post().uri("/test").contentType(MediaType.TEXT_PLAIN)
.body(BodyInserters.fromValue("Hello World")).exchange()
.flatMap((response) -> response.bodyToMono(String.class));
.body(BodyInserters.fromValue("Hello World")).retrieve().bodyToMono(String.class);
StepVerifier.setDefaultTimeout(Duration.ofSeconds(30));
StepVerifier.create(result).expectNext("Hello World").verifyComplete();
@ -234,8 +233,7 @@ public abstract class AbstractReactiveWebServerFactoryTests {
WebClient client = WebClient.builder().baseUrl("https://localhost:" + this.webServer.getPort())
.clientConnector(clientConnector).build();
Mono<String> result = client.post().uri("/test").contentType(MediaType.TEXT_PLAIN)
.body(BodyInserters.fromValue("Hello World")).exchange()
.flatMap((response) -> response.bodyToMono(String.class));
.body(BodyInserters.fromValue("Hello World")).retrieve().bodyToMono(String.class);
assertThat(result.block(Duration.ofSeconds(30))).isEqualTo("Hello World");
}
@ -267,8 +265,7 @@ public abstract class AbstractReactiveWebServerFactoryTests {
WebClient client = WebClient.builder().baseUrl("https://localhost:" + this.webServer.getPort())
.clientConnector(clientConnector).build();
Mono<String> result = client.post().uri("/test").contentType(MediaType.TEXT_PLAIN)
.body(BodyInserters.fromValue("Hello World")).exchange()
.flatMap((response) -> response.bodyToMono(String.class));
.body(BodyInserters.fromValue("Hello World")).retrieve().bodyToMono(String.class);
StepVerifier.create(result).expectError(WebClientRequestException.class).verify(Duration.ofSeconds(10));
}
@ -285,7 +282,7 @@ public abstract class AbstractReactiveWebServerFactoryTests {
@Test
protected void compressionOfResponseToGetRequest() {
WebClient client = prepareCompressionTest();
ResponseEntity<Void> response = client.get().exchange().flatMap((res) -> res.toEntity(Void.class))
ResponseEntity<Void> response = client.get().exchangeToMono(ClientResponse::toBodilessEntity)
.block(Duration.ofSeconds(30));
assertResponseIsCompressed(response);
}
@ -293,7 +290,7 @@ public abstract class AbstractReactiveWebServerFactoryTests {
@Test
protected void compressionOfResponseToPostRequest() {
WebClient client = prepareCompressionTest();
ResponseEntity<Void> response = client.post().exchange().flatMap((res) -> res.toEntity(Void.class))
ResponseEntity<Void> response = client.post().exchangeToMono(ClientResponse::toBodilessEntity)
.block(Duration.ofSeconds(30));
assertResponseIsCompressed(response);
}
@ -304,7 +301,7 @@ public abstract class AbstractReactiveWebServerFactoryTests {
compression.setEnabled(true);
compression.setMinResponseSize(DataSize.ofBytes(3001));
WebClient client = prepareCompressionTest(compression);
ResponseEntity<Void> response = client.get().exchange().flatMap((res) -> res.toEntity(Void.class))
ResponseEntity<Void> response = client.get().exchangeToMono(ClientResponse::toBodilessEntity)
.block(Duration.ofSeconds(30));
assertResponseIsNotCompressed(response);
}
@ -315,7 +312,7 @@ public abstract class AbstractReactiveWebServerFactoryTests {
compression.setEnabled(true);
compression.setMimeTypes(new String[] { "application/json" });
WebClient client = prepareCompressionTest(compression);
ResponseEntity<Void> response = client.get().exchange().flatMap((res) -> res.toEntity(Void.class))
ResponseEntity<Void> response = client.get().exchangeToMono(ClientResponse::toBodilessEntity)
.block(Duration.ofSeconds(30));
assertResponseIsNotCompressed(response);
}
@ -326,8 +323,8 @@ public abstract class AbstractReactiveWebServerFactoryTests {
compression.setEnabled(true);
compression.setExcludedUserAgents(new String[] { "testUserAgent" });
WebClient client = prepareCompressionTest(compression);
ResponseEntity<Void> response = client.get().header("User-Agent", "testUserAgent").exchange()
.flatMap((res) -> res.toEntity(Void.class)).block(Duration.ofSeconds(30));
ResponseEntity<Void> response = client.get().header("User-Agent", "testUserAgent")
.exchangeToMono(ClientResponse::toBodilessEntity).block(Duration.ofSeconds(30));
assertResponseIsNotCompressed(response);
}
@ -337,7 +334,7 @@ public abstract class AbstractReactiveWebServerFactoryTests {
compression.setEnabled(true);
compression.setMimeTypes(new String[] { "application/json" });
WebClient client = prepareCompressionTest(compression, "test~plain");
ResponseEntity<Void> response = client.get().exchange().flatMap((res) -> res.toEntity(Void.class))
ResponseEntity<Void> response = client.get().exchangeToMono(ClientResponse::toBodilessEntity)
.block(Duration.ofSeconds(30));
assertResponseIsNotCompressed(response);
}

Loading…
Cancel
Save