mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-20 20:59:07 +00:00
Use RestClient in integration tests
Now that `RestClient` offers a better alternative, this commit revisits our integration tests to use `RestClient` instead of `RestTemplate`. Closes gh-36573
This commit is contained in:
+1
-5
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.springframework.http.server.reactive;
|
||||
|
||||
import java.net.URI;
|
||||
import java.time.Duration;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
@@ -25,9 +24,7 @@ import reactor.core.scheduler.Scheduler;
|
||||
import reactor.core.scheduler.Schedulers;
|
||||
|
||||
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
||||
import org.springframework.http.RequestEntity;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.testfixture.http.server.reactive.bootstrap.AbstractHttpHandlerIntegrationTests;
|
||||
import org.springframework.web.testfixture.http.server.reactive.bootstrap.HttpServer;
|
||||
|
||||
@@ -51,8 +48,7 @@ class AsyncIntegrationTests extends AbstractHttpHandlerIntegrationTests {
|
||||
void basicTest(HttpServer httpServer) throws Exception {
|
||||
startServer(httpServer);
|
||||
|
||||
URI url = URI.create("http://localhost:" + port);
|
||||
ResponseEntity<String> response = new RestTemplate().exchange(RequestEntity.get(url).build(), String.class);
|
||||
ResponseEntity<String> response = getRestClient().get().retrieve().toEntity(String.class);
|
||||
|
||||
assertThat(response.getBody()).isEqualTo("hello");
|
||||
}
|
||||
|
||||
+9
-15
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.springframework.http.server.reactive;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -24,10 +23,8 @@ import java.util.Map;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.http.HttpCookie;
|
||||
import org.springframework.http.RequestEntity;
|
||||
import org.springframework.http.ResponseCookie;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.testfixture.http.server.reactive.bootstrap.AbstractHttpHandlerIntegrationTests;
|
||||
import org.springframework.web.testfixture.http.server.reactive.bootstrap.HttpServer;
|
||||
|
||||
@@ -52,10 +49,9 @@ class CookieIntegrationTests extends AbstractHttpHandlerIntegrationTests {
|
||||
public void basicTest(HttpServer httpServer) throws Exception {
|
||||
startServer(httpServer);
|
||||
|
||||
URI url = URI.create("http://localhost:" + port);
|
||||
String header = "SID=31d4d96e407aad42; lang=en-US";
|
||||
ResponseEntity<Void> response = new RestTemplate().exchange(
|
||||
RequestEntity.get(url).header("Cookie", header).build(), Void.class);
|
||||
ResponseEntity<Void> response = getRestClient().get()
|
||||
.header("Cookie", "SID=31d4d96e407aad42; lang=en-US")
|
||||
.retrieve().toBodilessEntity();
|
||||
|
||||
Map<String, List<HttpCookie>> requestCookies = this.cookieHandler.requestCookies;
|
||||
assertThat(requestCookies).hasSize(2);
|
||||
@@ -79,10 +75,9 @@ class CookieIntegrationTests extends AbstractHttpHandlerIntegrationTests {
|
||||
public void partitionedAttributeTest(HttpServer httpServer) throws Exception {
|
||||
startServer(httpServer);
|
||||
|
||||
URI url = URI.create("http://localhost:" + port);
|
||||
String header = "SID=31d4d96e407aad42; lang=en-US";
|
||||
ResponseEntity<Void> response = new RestTemplate().exchange(
|
||||
RequestEntity.get(url).header("Cookie", header).build(), Void.class);
|
||||
ResponseEntity<Void> response = getRestClient().get()
|
||||
.header("Cookie", "SID=31d4d96e407aad42; lang=en-US")
|
||||
.retrieve().toBodilessEntity();
|
||||
|
||||
List<String> headerValues = response.getHeaders().get("Set-Cookie");
|
||||
assertThat(headerValues).hasSize(2);
|
||||
@@ -97,10 +92,9 @@ class CookieIntegrationTests extends AbstractHttpHandlerIntegrationTests {
|
||||
public void cookiesWithSameNameTest(HttpServer httpServer) throws Exception {
|
||||
startServer(httpServer);
|
||||
|
||||
URI url = new URI("http://localhost:" + port);
|
||||
String header = "SID=31d4d96e407aad42; lang=en-US; lang=zh-CN";
|
||||
new RestTemplate().exchange(
|
||||
RequestEntity.get(url).header("Cookie", header).build(), Void.class);
|
||||
ResponseEntity<Void> response = getRestClient().get()
|
||||
.header("Cookie", "SID=31d4d96e407aad42; lang=en-US; lang=zh-CN")
|
||||
.retrieve().toBodilessEntity();
|
||||
|
||||
Map<String, List<HttpCookie>> requestCookies = this.cookieHandler.requestCookies;
|
||||
assertThat(requestCookies).hasSize(2);
|
||||
|
||||
+1
-7
@@ -16,14 +16,11 @@
|
||||
|
||||
package org.springframework.http.server.reactive;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Random;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.http.RequestEntity;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.testfixture.http.server.reactive.bootstrap.AbstractHttpHandlerIntegrationTests;
|
||||
import org.springframework.web.testfixture.http.server.reactive.bootstrap.HttpServer;
|
||||
|
||||
@@ -49,11 +46,8 @@ class EchoHandlerIntegrationTests extends AbstractHttpHandlerIntegrationTests {
|
||||
public void echo(HttpServer httpServer) throws Exception {
|
||||
startServer(httpServer);
|
||||
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
byte[] body = randomBytes();
|
||||
RequestEntity<byte[]> request = RequestEntity.post(URI.create("http://localhost:" + port)).body(body);
|
||||
ResponseEntity<byte[]> response = restTemplate.exchange(request, byte[].class);
|
||||
ResponseEntity<byte[]> response = getRestClient().post().body(body).retrieve().toEntity(byte[].class);
|
||||
|
||||
assertThat(response.getBody()).isEqualTo(body);
|
||||
}
|
||||
|
||||
+16
-19
@@ -21,10 +21,9 @@ import java.net.URI;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.client.NoOpResponseErrorHandler;
|
||||
import org.springframework.web.client.ResponseErrorHandler;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.client.RestClient;
|
||||
import org.springframework.web.testfixture.http.server.reactive.bootstrap.AbstractHttpHandlerIntegrationTests;
|
||||
import org.springframework.web.testfixture.http.server.reactive.bootstrap.HttpServer;
|
||||
import org.springframework.web.testfixture.http.server.reactive.bootstrap.JettyCoreHttpServer;
|
||||
@@ -37,8 +36,6 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*/
|
||||
class ErrorHandlerIntegrationTests extends AbstractHttpHandlerIntegrationTests {
|
||||
|
||||
private static final ResponseErrorHandler NO_OP_ERROR_HANDLER = new NoOpResponseErrorHandler();
|
||||
|
||||
private final ErrorHandler handler = new ErrorHandler();
|
||||
|
||||
|
||||
@@ -47,16 +44,20 @@ class ErrorHandlerIntegrationTests extends AbstractHttpHandlerIntegrationTests {
|
||||
return handler;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected RestClient initRestClient(RestClient.Builder builder) {
|
||||
return builder
|
||||
.defaultStatusHandler(HttpStatusCode::is5xxServerError, (req, res) -> {})
|
||||
.defaultStatusHandler(HttpStatusCode::is4xxClientError, (req, res) -> {})
|
||||
.build();
|
||||
}
|
||||
|
||||
@ParameterizedHttpServerTest
|
||||
void responseBodyError(HttpServer httpServer) throws Exception {
|
||||
startServer(httpServer);
|
||||
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
restTemplate.setErrorHandler(NO_OP_ERROR_HANDLER);
|
||||
|
||||
URI url = URI.create("http://localhost:" + port + "/response-body-error");
|
||||
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
|
||||
ResponseEntity<String> response = getRestClient().get().uri("/response-body-error")
|
||||
.retrieve().toEntity(String.class);
|
||||
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
@@ -65,11 +66,8 @@ class ErrorHandlerIntegrationTests extends AbstractHttpHandlerIntegrationTests {
|
||||
void handlingError(HttpServer httpServer) throws Exception {
|
||||
startServer(httpServer);
|
||||
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
restTemplate.setErrorHandler(NO_OP_ERROR_HANDLER);
|
||||
|
||||
URI url = URI.create("http://localhost:" + port + "/handling-error");
|
||||
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
|
||||
ResponseEntity<String> response = getRestClient().get().uri("/handling-error")
|
||||
.retrieve().toEntity(String.class);
|
||||
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
@@ -78,11 +76,10 @@ class ErrorHandlerIntegrationTests extends AbstractHttpHandlerIntegrationTests {
|
||||
void emptyPathSegments(HttpServer httpServer) throws Exception {
|
||||
startServer(httpServer);
|
||||
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
restTemplate.setErrorHandler(NO_OP_ERROR_HANDLER);
|
||||
|
||||
URI url = URI.create("http://localhost:" + port + "//");
|
||||
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
|
||||
ResponseEntity<String> response = getRestClient().get()
|
||||
.uri(URI.create("http://localhost:" + this.server.getPort() + "//"))
|
||||
.retrieve().toEntity(String.class);
|
||||
|
||||
// Jetty 10+ rejects empty path segments, see https://github.com/eclipse/jetty.project/issues/6302,
|
||||
// but an application can apply CompactPathRule via RewriteHandler:
|
||||
|
||||
+3
-7
@@ -16,8 +16,6 @@
|
||||
|
||||
package org.springframework.http.server.reactive;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
@@ -27,14 +25,12 @@ import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.RequestEntity;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.codec.multipart.FilePart;
|
||||
import org.springframework.http.codec.multipart.FormFieldPart;
|
||||
import org.springframework.http.codec.multipart.Part;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.WebHandler;
|
||||
import org.springframework.web.server.adapter.HttpWebHandlerAdapter;
|
||||
@@ -81,9 +77,9 @@ class MultipartHttpHandlerIntegrationTests extends AbstractHttpHandlerIntegratio
|
||||
parts.add("fooPart", fooPart);
|
||||
parts.add("barPart", barPart);
|
||||
|
||||
URI url = URI.create("http://localhost:" + port + "/form-parts");
|
||||
ResponseEntity<Void> response = new RestTemplate().exchange(
|
||||
RequestEntity.post(url).contentType(mediaType).body(parts), Void.class);
|
||||
ResponseEntity<Void> response = getRestClient().post().uri("/form-parts")
|
||||
.contentType(MediaType.MULTIPART_FORM_DATA).body(parts)
|
||||
.retrieve().toBodilessEntity();
|
||||
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
}
|
||||
|
||||
+1
-8
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.springframework.http.server.reactive;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Random;
|
||||
|
||||
import org.reactivestreams.Publisher;
|
||||
@@ -25,9 +24,7 @@ import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
||||
import org.springframework.http.RequestEntity;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.testfixture.http.server.reactive.bootstrap.AbstractHttpHandlerIntegrationTests;
|
||||
import org.springframework.web.testfixture.http.server.reactive.bootstrap.HttpServer;
|
||||
|
||||
@@ -59,11 +56,7 @@ class RandomHandlerIntegrationTests extends AbstractHttpHandlerIntegrationTests
|
||||
|
||||
// TODO: fix Reactor support
|
||||
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
byte[] body = randomBytes();
|
||||
RequestEntity<byte[]> request = RequestEntity.post(URI.create("http://localhost:" + port)).body(body);
|
||||
ResponseEntity<byte[]> response = restTemplate.exchange(request, byte[].class);
|
||||
ResponseEntity<byte[]> response = getRestClient().post().body(randomBytes()).retrieve().toEntity(byte[].class);
|
||||
|
||||
assertThat(response.getBody()).isNotNull();
|
||||
assertThat(response.getHeaders().getContentLength()).isEqualTo(RESPONSE_SIZE);
|
||||
|
||||
+1
-5
@@ -21,9 +21,7 @@ import java.net.URI;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.RequestEntity;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.testfixture.http.server.reactive.bootstrap.AbstractHttpHandlerIntegrationTests;
|
||||
import org.springframework.web.testfixture.http.server.reactive.bootstrap.HttpServer;
|
||||
|
||||
@@ -44,9 +42,7 @@ class ServerHttpRequestIntegrationTests extends AbstractHttpHandlerIntegrationTe
|
||||
void checkUri(HttpServer httpServer) throws Exception {
|
||||
startServer(httpServer);
|
||||
|
||||
URI url = URI.create("http://localhost:" + port + "/foo?param=bar");
|
||||
RequestEntity<Void> request = RequestEntity.post(url).build();
|
||||
ResponseEntity<Void> response = new RestTemplate().exchange(request, Void.class);
|
||||
ResponseEntity<Void> response = getRestClient().post().uri("/foo?param=bar").retrieve().toBodilessEntity();
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
+5
-12
@@ -32,10 +32,9 @@ import org.junit.jupiter.api.Test;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.RequestEntity;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.client.RestClient;
|
||||
import org.springframework.web.testfixture.http.server.reactive.bootstrap.HttpServer;
|
||||
import org.springframework.web.testfixture.http.server.reactive.bootstrap.ReactorHttpsServer;
|
||||
|
||||
@@ -52,10 +51,7 @@ class ServerHttpsRequestIntegrationTests {
|
||||
|
||||
private final HttpServer server = new ReactorHttpsServer();
|
||||
|
||||
private int port;
|
||||
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
private RestClient restClient;
|
||||
|
||||
@BeforeEach
|
||||
void startServer() throws Exception {
|
||||
@@ -63,8 +59,6 @@ class ServerHttpsRequestIntegrationTests {
|
||||
this.server.afterPropertiesSet();
|
||||
this.server.start();
|
||||
|
||||
// Set dynamically chosen port
|
||||
this.port = this.server.getPort();
|
||||
|
||||
SSLContextBuilder builder = new SSLContextBuilder();
|
||||
builder.loadTrustMaterial(new TrustSelfSignedStrategy());
|
||||
@@ -77,7 +71,8 @@ class ServerHttpsRequestIntegrationTests {
|
||||
setConnectionManager(connectionManager).build();
|
||||
HttpComponentsClientHttpRequestFactory requestFactory =
|
||||
new HttpComponentsClientHttpRequestFactory(httpclient);
|
||||
this.restTemplate = new RestTemplate(requestFactory);
|
||||
this.restClient = RestClient.builder().baseUrl("https://localhost:" + this.server.getPort())
|
||||
.requestFactory(requestFactory).build();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
@@ -87,9 +82,7 @@ class ServerHttpsRequestIntegrationTests {
|
||||
|
||||
@Test
|
||||
void checkUri() {
|
||||
URI url = URI.create("https://localhost:" + port + "/foo?param=bar");
|
||||
RequestEntity<Void> request = RequestEntity.post(url).build();
|
||||
ResponseEntity<Void> response = this.restTemplate.exchange(request, Void.class);
|
||||
ResponseEntity<Void> response = this.restClient.post().uri("/foo?param=bar").retrieve().toBodilessEntity();
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
+2
-8
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.springframework.http.server.reactive;
|
||||
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Random;
|
||||
|
||||
@@ -24,9 +23,7 @@ import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.http.RequestEntity;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.testfixture.http.server.reactive.bootstrap.AbstractHttpHandlerIntegrationTests;
|
||||
import org.springframework.web.testfixture.http.server.reactive.bootstrap.HttpServer;
|
||||
|
||||
@@ -54,12 +51,9 @@ class WriteOnlyHandlerIntegrationTests extends AbstractHttpHandlerIntegrationTes
|
||||
void writeOnly(HttpServer httpServer) throws Exception {
|
||||
startServer(httpServer);
|
||||
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
this.body = randomBytes();
|
||||
RequestEntity<byte[]> request = RequestEntity.post(URI.create("http://localhost:" + port))
|
||||
.body("".getBytes(StandardCharsets.UTF_8));
|
||||
ResponseEntity<byte[]> response = restTemplate.exchange(request, byte[].class);
|
||||
ResponseEntity<byte[]> response = getRestClient().post().body("".getBytes(StandardCharsets.UTF_8))
|
||||
.retrieve().toEntity(byte[].class);
|
||||
|
||||
assertThat(response.getBody()).isEqualTo(body);
|
||||
}
|
||||
|
||||
+1
-6
@@ -17,17 +17,14 @@
|
||||
package org.springframework.http.server.reactive;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URI;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.RequestEntity;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.ZeroCopyHttpOutputMessage;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.testfixture.http.server.reactive.bootstrap.AbstractHttpHandlerIntegrationTests;
|
||||
import org.springframework.web.testfixture.http.server.reactive.bootstrap.HttpServer;
|
||||
import org.springframework.web.testfixture.http.server.reactive.bootstrap.JettyCoreHttpServer;
|
||||
@@ -59,9 +56,7 @@ class ZeroCopyIntegrationTests extends AbstractHttpHandlerIntegrationTests {
|
||||
|
||||
startServer(httpServer);
|
||||
|
||||
URI url = URI.create("http://localhost:" + port);
|
||||
RequestEntity<?> request = RequestEntity.get(url).build();
|
||||
ResponseEntity<byte[]> response = new RestTemplate().exchange(request, byte[].class);
|
||||
ResponseEntity<byte[]> response = getRestClient().get().retrieve().toEntity(byte[].class);
|
||||
|
||||
assertThat(response.hasBody()).isTrue();
|
||||
assertThat(response.getHeaders().getContentLength()).isEqualTo(springLogoResource.contentLength());
|
||||
|
||||
+11
-7
@@ -38,11 +38,12 @@ import org.junit.jupiter.api.TestInstance.Lifecycle;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
||||
import org.springframework.util.FileSystemUtils;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.client.RestClient;
|
||||
import org.springframework.web.context.request.ServletWebRequest;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -58,12 +59,10 @@ class WebRequestDataBinderIntegrationTests {
|
||||
|
||||
private final PartListServlet partListServlet = new PartListServlet();
|
||||
|
||||
private final RestTemplate template = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
|
||||
private RestClient restClient;
|
||||
|
||||
private Server jettyServer;
|
||||
|
||||
private String baseUrl;
|
||||
|
||||
private Path tempDirectory;
|
||||
|
||||
|
||||
@@ -91,7 +90,8 @@ class WebRequestDataBinderIntegrationTests {
|
||||
|
||||
Connector[] connectors = jettyServer.getConnectors();
|
||||
NetworkConnector connector = (NetworkConnector) connectors[0];
|
||||
baseUrl = "http://localhost:" + connector.getLocalPort();
|
||||
this.restClient = RestClient.builder().baseUrl("http://localhost:" + connector.getLocalPort())
|
||||
.requestFactory(new HttpComponentsClientHttpRequestFactory()).build();
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
@@ -117,7 +117,9 @@ class WebRequestDataBinderIntegrationTests {
|
||||
parts.add("firstPart", firstPart);
|
||||
parts.add("secondPart", "secondValue");
|
||||
|
||||
template.postForLocation(baseUrl + "/parts", parts);
|
||||
this.restClient.post().uri("/parts")
|
||||
.contentType(MediaType.MULTIPART_FORM_DATA).body(parts)
|
||||
.retrieve().toBodilessEntity();
|
||||
|
||||
assertThat(bean.getFirstPart()).isNotNull();
|
||||
assertThat(bean.getSecondPart()).isNotNull();
|
||||
@@ -134,7 +136,9 @@ class WebRequestDataBinderIntegrationTests {
|
||||
Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg");
|
||||
parts.add("partList", logo);
|
||||
|
||||
template.postForLocation(baseUrl + "/partlist", parts);
|
||||
this.restClient.post().uri("/partlist")
|
||||
.contentType(MediaType.MULTIPART_FORM_DATA).body(parts)
|
||||
.retrieve().toBodilessEntity();
|
||||
|
||||
assertThat(bean.getPartList()).isNotNull();
|
||||
assertThat(bean.getPartList()).hasSize(parts.get("partList").size());
|
||||
|
||||
+11
-12
@@ -35,6 +35,7 @@ import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpRequest;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.client.ClientHttpRequest;
|
||||
import org.springframework.http.client.ClientHttpRequestExecution;
|
||||
@@ -49,6 +50,7 @@ import org.springframework.util.StreamUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.BDDMockito.willThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -135,15 +137,15 @@ class RestClientObservationTests {
|
||||
|
||||
@Test
|
||||
void shouldContributeServerErrorOutcome() throws Exception {
|
||||
ResponseErrorHandler errorHandler = mock();
|
||||
given(errorHandler.hasError(response)).willReturn(true);
|
||||
this.client = this.client.mutate().defaultStatusHandler(errorHandler).build();
|
||||
RestClient.ResponseSpec.ErrorHandler errorHandler = mock();
|
||||
this.client = this.client.mutate()
|
||||
.defaultStatusHandler(HttpStatusCode::is5xxServerError, errorHandler).build();
|
||||
|
||||
String url = "https://example.org";
|
||||
mockSentRequest(GET, url);
|
||||
mockResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
willThrow(new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR))
|
||||
.given(errorHandler).handleError(URI.create(url), GET, response);
|
||||
.given(errorHandler).handle(any(), any());
|
||||
|
||||
assertThatExceptionOfType(HttpServerErrorException.class).isThrownBy(() ->
|
||||
client.get().uri(url).retrieve().toBodilessEntity());
|
||||
@@ -257,7 +259,8 @@ class RestClientObservationTests {
|
||||
@Test
|
||||
void openScopeWithObservation() throws Exception {
|
||||
this.client = createBuilder().requestInterceptor(new ObservationContextInterceptor(this.observationRegistry))
|
||||
.defaultStatusHandler(new ObservationErrorHandler(this.observationRegistry)).build();
|
||||
.defaultStatusHandler(HttpStatusCode::is2xxSuccessful, new ObservationErrorHandler(this.observationRegistry))
|
||||
.build();
|
||||
mockSentRequest(GET, "https://example.org");
|
||||
mockResponseStatus(HttpStatus.OK);
|
||||
mockResponseBody("Hello World", MediaType.TEXT_PLAIN);
|
||||
@@ -334,7 +337,7 @@ class RestClientObservationTests {
|
||||
}
|
||||
}
|
||||
|
||||
static class ObservationErrorHandler implements ResponseErrorHandler {
|
||||
static class ObservationErrorHandler implements RestClient.ResponseSpec.ErrorHandler {
|
||||
|
||||
final TestObservationRegistry observationRegistry;
|
||||
|
||||
@@ -343,14 +346,10 @@ class RestClientObservationTests {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasError(ClientHttpResponse response) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleError(URI uri, HttpMethod httpMethod, ClientHttpResponse response) {
|
||||
public void handle(HttpRequest request, ClientHttpResponse response) throws IOException {
|
||||
assertThat(this.observationRegistry.getCurrentObservationScope()).isNotNull();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+11
-34
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.springframework.web.server.session;
|
||||
|
||||
import java.net.URI;
|
||||
import java.time.Clock;
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
@@ -26,10 +25,8 @@ import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.RequestEntity;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.server.reactive.HttpHandler;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.WebHandler;
|
||||
import org.springframework.web.server.WebSession;
|
||||
@@ -47,8 +44,6 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*/
|
||||
class WebSessionIntegrationTests extends AbstractHttpHandlerIntegrationTests {
|
||||
|
||||
private final RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
private final DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
|
||||
|
||||
private final TestWebHandler handler = new TestWebHandler();
|
||||
@@ -64,16 +59,14 @@ class WebSessionIntegrationTests extends AbstractHttpHandlerIntegrationTests {
|
||||
public void createSession(HttpServer httpServer) throws Exception {
|
||||
startServer(httpServer);
|
||||
|
||||
RequestEntity<Void> request = RequestEntity.get(createUri()).build();
|
||||
ResponseEntity<Void> response = this.restTemplate.exchange(request, Void.class);
|
||||
ResponseEntity<Void> response = getRestClient().get().retrieve().toBodilessEntity();
|
||||
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
String id = extractSessionId(response.getHeaders());
|
||||
assertThat(id).isNotNull();
|
||||
assertThat(this.handler.getSessionRequestCount()).isEqualTo(1);
|
||||
|
||||
request = RequestEntity.get(createUri()).header("Cookie", "SESSION=" + id).build();
|
||||
response = this.restTemplate.exchange(request, Void.class);
|
||||
response = getRestClient().get().cookie("SESSION", id).retrieve().toBodilessEntity();
|
||||
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
assertThat(response.getHeaders().get("Set-Cookie")).isNull();
|
||||
@@ -85,8 +78,7 @@ class WebSessionIntegrationTests extends AbstractHttpHandlerIntegrationTests {
|
||||
startServer(httpServer);
|
||||
|
||||
// First request: no session yet, new session created
|
||||
RequestEntity<Void> request = RequestEntity.get(createUri()).build();
|
||||
ResponseEntity<Void> response = this.restTemplate.exchange(request, Void.class);
|
||||
ResponseEntity<Void> response = getRestClient().get().retrieve().toBodilessEntity();
|
||||
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
String id = extractSessionId(response.getHeaders());
|
||||
@@ -94,8 +86,7 @@ class WebSessionIntegrationTests extends AbstractHttpHandlerIntegrationTests {
|
||||
assertThat(this.handler.getSessionRequestCount()).isEqualTo(1);
|
||||
|
||||
// Second request: same session
|
||||
request = RequestEntity.get(createUri()).header("Cookie", "SESSION=" + id).build();
|
||||
response = this.restTemplate.exchange(request, Void.class);
|
||||
response = getRestClient().get().cookie("SESSION", id).retrieve().toBodilessEntity();
|
||||
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
assertThat(response.getHeaders().get("Set-Cookie")).isNull();
|
||||
@@ -108,8 +99,7 @@ class WebSessionIntegrationTests extends AbstractHttpHandlerIntegrationTests {
|
||||
store.setClock(Clock.offset(store.getClock(), Duration.ofMinutes(31)));
|
||||
|
||||
// Third request: expired session, new session created
|
||||
request = RequestEntity.get(createUri()).header("Cookie", "SESSION=" + id).build();
|
||||
response = this.restTemplate.exchange(request, Void.class);
|
||||
response = getRestClient().get().cookie("SESSION", id).retrieve().toBodilessEntity();
|
||||
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
id = extractSessionId(response.getHeaders());
|
||||
@@ -122,8 +112,7 @@ class WebSessionIntegrationTests extends AbstractHttpHandlerIntegrationTests {
|
||||
startServer(httpServer);
|
||||
|
||||
// First request: no session yet, new session created
|
||||
RequestEntity<Void> request = RequestEntity.get(createUri()).build();
|
||||
ResponseEntity<Void> response = this.restTemplate.exchange(request, Void.class);
|
||||
ResponseEntity<Void> response = getRestClient().get().retrieve().toBodilessEntity();
|
||||
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
String id = extractSessionId(response.getHeaders());
|
||||
@@ -134,9 +123,7 @@ class WebSessionIntegrationTests extends AbstractHttpHandlerIntegrationTests {
|
||||
store.setClock(Clock.offset(store.getClock(), Duration.ofMinutes(31)));
|
||||
|
||||
// Second request: session expires
|
||||
URI uri = URI.create("http://localhost:" + this.port + "/?expire");
|
||||
request = RequestEntity.get(uri).header("Cookie", "SESSION=" + id).build();
|
||||
response = this.restTemplate.exchange(request, Void.class);
|
||||
response = getRestClient().get().uri("/?expire").cookie("SESSION", id).retrieve().toBodilessEntity();
|
||||
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
String value = response.getHeaders().getFirst("Set-Cookie");
|
||||
@@ -149,8 +136,7 @@ class WebSessionIntegrationTests extends AbstractHttpHandlerIntegrationTests {
|
||||
startServer(httpServer);
|
||||
|
||||
// First request: no session yet, new session created
|
||||
RequestEntity<Void> request = RequestEntity.get(createUri()).build();
|
||||
ResponseEntity<Void> response = this.restTemplate.exchange(request, Void.class);
|
||||
ResponseEntity<Void> response = getRestClient().get().retrieve().toBodilessEntity();
|
||||
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
String oldId = extractSessionId(response.getHeaders());
|
||||
@@ -158,9 +144,7 @@ class WebSessionIntegrationTests extends AbstractHttpHandlerIntegrationTests {
|
||||
assertThat(this.handler.getSessionRequestCount()).isEqualTo(1);
|
||||
|
||||
// Second request: session id changes
|
||||
URI uri = URI.create("http://localhost:" + this.port + "/?changeId");
|
||||
request = RequestEntity.get(uri).header("Cookie", "SESSION=" + oldId).build();
|
||||
response = this.restTemplate.exchange(request, Void.class);
|
||||
response = getRestClient().get().uri("/?changeId").cookie("SESSION", oldId).retrieve().toBodilessEntity();
|
||||
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
String newId = extractSessionId(response.getHeaders());
|
||||
@@ -174,17 +158,14 @@ class WebSessionIntegrationTests extends AbstractHttpHandlerIntegrationTests {
|
||||
startServer(httpServer);
|
||||
|
||||
// First request: no session yet, new session created
|
||||
RequestEntity<Void> request = RequestEntity.get(createUri()).build();
|
||||
ResponseEntity<Void> response = this.restTemplate.exchange(request, Void.class);
|
||||
ResponseEntity<Void> response = getRestClient().get().retrieve().toBodilessEntity();
|
||||
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
String id = extractSessionId(response.getHeaders());
|
||||
assertThat(id).isNotNull();
|
||||
|
||||
// Second request: invalidates session
|
||||
URI uri = URI.create("http://localhost:" + this.port + "/?invalidate");
|
||||
request = RequestEntity.get(uri).header("Cookie", "SESSION=" + id).build();
|
||||
response = this.restTemplate.exchange(request, Void.class);
|
||||
response = getRestClient().get().uri("/?invalidate").cookie("SESSION", id).retrieve().toBodilessEntity();
|
||||
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
String value = response.getHeaders().getFirst("Set-Cookie");
|
||||
@@ -205,10 +186,6 @@ class WebSessionIntegrationTests extends AbstractHttpHandlerIntegrationTests {
|
||||
return null;
|
||||
}
|
||||
|
||||
private URI createUri() {
|
||||
return URI.create("http://localhost:" + this.port + "/");
|
||||
}
|
||||
|
||||
|
||||
private static class TestWebHandler implements WebHandler {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user