|
|
|
@ -23,7 +23,6 @@ import java.lang.annotation.RetentionPolicy; |
|
|
|
import java.lang.annotation.Target; |
|
|
|
import java.lang.annotation.Target; |
|
|
|
import java.net.URI; |
|
|
|
import java.net.URI; |
|
|
|
import java.net.URISyntaxException; |
|
|
|
import java.net.URISyntaxException; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.function.Consumer; |
|
|
|
import java.util.function.Consumer; |
|
|
|
@ -56,6 +55,7 @@ import org.springframework.util.LinkedMultiValueMap; |
|
|
|
import org.springframework.util.MultiValueMap; |
|
|
|
import org.springframework.util.MultiValueMap; |
|
|
|
import org.springframework.web.testfixture.xml.Pojo; |
|
|
|
import org.springframework.web.testfixture.xml.Pojo; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static java.nio.charset.StandardCharsets.UTF_8; |
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
|
|
|
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
|
|
|
import static org.junit.jupiter.api.Assumptions.assumeFalse; |
|
|
|
import static org.junit.jupiter.api.Assumptions.assumeFalse; |
|
|
|
@ -660,8 +660,8 @@ class RestClientIntegrationTests { |
|
|
|
startServer(requestFactory); |
|
|
|
startServer(requestFactory); |
|
|
|
|
|
|
|
|
|
|
|
String content = "Internal Server error"; |
|
|
|
String content = "Internal Server error"; |
|
|
|
prepareResponse(response -> response.setResponseCode(500) |
|
|
|
prepareResponse(response -> |
|
|
|
.setHeader("Content-Type", "text/plain").setBody(content)); |
|
|
|
response.setResponseCode(500).setHeader("Content-Type", "text/plain").setBody(content)); |
|
|
|
|
|
|
|
|
|
|
|
ResponseEntity<String> result = this.restClient.get() |
|
|
|
ResponseEntity<String> result = this.restClient.get() |
|
|
|
.uri("/").accept(MediaType.APPLICATION_JSON) |
|
|
|
.uri("/").accept(MediaType.APPLICATION_JSON) |
|
|
|
@ -689,7 +689,7 @@ class RestClientIntegrationTests { |
|
|
|
String result = this.restClient.get() |
|
|
|
String result = this.restClient.get() |
|
|
|
.uri("/greeting") |
|
|
|
.uri("/greeting") |
|
|
|
.header("X-Test-Header", "testvalue") |
|
|
|
.header("X-Test-Header", "testvalue") |
|
|
|
.exchange((request, response) -> new String(RestClientUtils.getBody(response), StandardCharsets.UTF_8)); |
|
|
|
.exchange((request, response) -> new String(RestClientUtils.getBody(response), UTF_8)); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(result).isEqualTo("Hello Spring!"); |
|
|
|
assertThat(result).isEqualTo("Hello Spring!"); |
|
|
|
|
|
|
|
|
|
|
|
@ -753,12 +753,12 @@ class RestClientIntegrationTests { |
|
|
|
void exchangeFor404(ClientHttpRequestFactory requestFactory) { |
|
|
|
void exchangeFor404(ClientHttpRequestFactory requestFactory) { |
|
|
|
startServer(requestFactory); |
|
|
|
startServer(requestFactory); |
|
|
|
|
|
|
|
|
|
|
|
prepareResponse(response -> response.setResponseCode(404) |
|
|
|
prepareResponse(response -> |
|
|
|
.setHeader("Content-Type", "text/plain").setBody("Not Found")); |
|
|
|
response.setResponseCode(404).setHeader("Content-Type", "text/plain").setBody("Not Found")); |
|
|
|
|
|
|
|
|
|
|
|
String result = this.restClient.get() |
|
|
|
String result = this.restClient.get() |
|
|
|
.uri("/greeting") |
|
|
|
.uri("/greeting") |
|
|
|
.exchange((request, response) -> new String(RestClientUtils.getBody(response), StandardCharsets.UTF_8)); |
|
|
|
.exchange((request, response) -> new String(RestClientUtils.getBody(response), UTF_8)); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(result).isEqualTo("Not Found"); |
|
|
|
assertThat(result).isEqualTo("Not Found"); |
|
|
|
|
|
|
|
|
|
|
|
@ -770,8 +770,8 @@ class RestClientIntegrationTests { |
|
|
|
void requestInitializer(ClientHttpRequestFactory requestFactory) { |
|
|
|
void requestInitializer(ClientHttpRequestFactory requestFactory) { |
|
|
|
startServer(requestFactory); |
|
|
|
startServer(requestFactory); |
|
|
|
|
|
|
|
|
|
|
|
prepareResponse(response -> response.setHeader("Content-Type", "text/plain") |
|
|
|
prepareResponse(response -> |
|
|
|
.setBody("Hello Spring!")); |
|
|
|
response.setHeader("Content-Type", "text/plain").setBody("Hello Spring!")); |
|
|
|
|
|
|
|
|
|
|
|
RestClient initializedClient = this.restClient.mutate() |
|
|
|
RestClient initializedClient = this.restClient.mutate() |
|
|
|
.requestInitializer(request -> request.getHeaders().add("foo", "bar")) |
|
|
|
.requestInitializer(request -> request.getHeaders().add("foo", "bar")) |
|
|
|
@ -792,9 +792,8 @@ class RestClientIntegrationTests { |
|
|
|
void requestInterceptor(ClientHttpRequestFactory requestFactory) { |
|
|
|
void requestInterceptor(ClientHttpRequestFactory requestFactory) { |
|
|
|
startServer(requestFactory); |
|
|
|
startServer(requestFactory); |
|
|
|
|
|
|
|
|
|
|
|
prepareResponse(response -> response.setHeader("Content-Type", "text/plain") |
|
|
|
prepareResponse(response -> |
|
|
|
.setBody("Hello Spring!")); |
|
|
|
response.setHeader("Content-Type", "text/plain").setBody("Hello Spring!")); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RestClient interceptedClient = this.restClient.mutate() |
|
|
|
RestClient interceptedClient = this.restClient.mutate() |
|
|
|
.requestInterceptor((request, body, execution) -> { |
|
|
|
.requestInterceptor((request, body, execution) -> { |
|
|
|
@ -819,6 +818,7 @@ class RestClientIntegrationTests { |
|
|
|
startServer(requestFactory); |
|
|
|
startServer(requestFactory); |
|
|
|
prepareResponse(response -> |
|
|
|
prepareResponse(response -> |
|
|
|
response.setHeader("Content-Type", "text/plain").setBody("Hello Spring!")); |
|
|
|
response.setHeader("Content-Type", "text/plain").setBody("Hello Spring!")); |
|
|
|
|
|
|
|
|
|
|
|
RestClient restClientWithCookies = this.restClient.mutate() |
|
|
|
RestClient restClientWithCookies = this.restClient.mutate() |
|
|
|
.defaultCookie("testCookie", "firstValue", "secondValue") |
|
|
|
.defaultCookie("testCookie", "firstValue", "secondValue") |
|
|
|
.build(); |
|
|
|
.build(); |
|
|
|
@ -852,8 +852,8 @@ class RestClientIntegrationTests { |
|
|
|
RestClient interceptedClient = this.restClient.mutate().requestInterceptor(interceptor).build(); |
|
|
|
RestClient interceptedClient = this.restClient.mutate().requestInterceptor(interceptor).build(); |
|
|
|
|
|
|
|
|
|
|
|
// header not present
|
|
|
|
// header not present
|
|
|
|
prepareResponse(response -> response |
|
|
|
prepareResponse(response -> |
|
|
|
.setHeader("Content-Type", "text/plain").setBody("Hello Spring!")); |
|
|
|
response.setHeader("Content-Type", "text/plain").setBody("Hello Spring!")); |
|
|
|
|
|
|
|
|
|
|
|
assertThatExceptionOfType(MyException.class).isThrownBy(() -> |
|
|
|
assertThatExceptionOfType(MyException.class).isThrownBy(() -> |
|
|
|
interceptedClient.get() |
|
|
|
interceptedClient.get() |
|
|
|
@ -881,8 +881,8 @@ class RestClientIntegrationTests { |
|
|
|
void defaultHeaders(ClientHttpRequestFactory requestFactory) { |
|
|
|
void defaultHeaders(ClientHttpRequestFactory requestFactory) { |
|
|
|
startServer(requestFactory); |
|
|
|
startServer(requestFactory); |
|
|
|
|
|
|
|
|
|
|
|
prepareResponse(response -> response.setHeader("Content-Type", "text/plain") |
|
|
|
prepareResponse(response -> |
|
|
|
.setBody("Hello Spring!")); |
|
|
|
response.setHeader("Content-Type", "text/plain").setBody("Hello Spring!")); |
|
|
|
|
|
|
|
|
|
|
|
RestClient headersClient = this.restClient.mutate() |
|
|
|
RestClient headersClient = this.restClient.mutate() |
|
|
|
.defaultHeaders(headers -> headers.add("foo", "bar")) |
|
|
|
.defaultHeaders(headers -> headers.add("foo", "bar")) |
|
|
|
@ -903,8 +903,8 @@ class RestClientIntegrationTests { |
|
|
|
void defaultRequest(ClientHttpRequestFactory requestFactory) { |
|
|
|
void defaultRequest(ClientHttpRequestFactory requestFactory) { |
|
|
|
startServer(requestFactory); |
|
|
|
startServer(requestFactory); |
|
|
|
|
|
|
|
|
|
|
|
prepareResponse(response -> response.setHeader("Content-Type", "text/plain") |
|
|
|
prepareResponse(response -> |
|
|
|
.setBody("Hello Spring!")); |
|
|
|
response.setHeader("Content-Type", "text/plain").setBody("Hello Spring!")); |
|
|
|
|
|
|
|
|
|
|
|
RestClient headersClient = this.restClient.mutate() |
|
|
|
RestClient headersClient = this.restClient.mutate() |
|
|
|
.defaultRequest(request -> request.header("foo", "bar")) |
|
|
|
.defaultRequest(request -> request.header("foo", "bar")) |
|
|
|
@ -925,8 +925,8 @@ class RestClientIntegrationTests { |
|
|
|
void defaultRequestOverride(ClientHttpRequestFactory requestFactory) { |
|
|
|
void defaultRequestOverride(ClientHttpRequestFactory requestFactory) { |
|
|
|
startServer(requestFactory); |
|
|
|
startServer(requestFactory); |
|
|
|
|
|
|
|
|
|
|
|
prepareResponse(response -> response.setHeader("Content-Type", "text/plain") |
|
|
|
prepareResponse(response -> |
|
|
|
.setBody("Hello Spring!")); |
|
|
|
response.setHeader("Content-Type", "text/plain").setBody("Hello Spring!")); |
|
|
|
|
|
|
|
|
|
|
|
RestClient headersClient = this.restClient.mutate() |
|
|
|
RestClient headersClient = this.restClient.mutate() |
|
|
|
.defaultRequest(request -> request.accept(MediaType.APPLICATION_JSON)) |
|
|
|
.defaultRequest(request -> request.accept(MediaType.APPLICATION_JSON)) |
|
|
|
@ -948,8 +948,8 @@ class RestClientIntegrationTests { |
|
|
|
void relativeUri(ClientHttpRequestFactory requestFactory) throws URISyntaxException { |
|
|
|
void relativeUri(ClientHttpRequestFactory requestFactory) throws URISyntaxException { |
|
|
|
startServer(requestFactory); |
|
|
|
startServer(requestFactory); |
|
|
|
|
|
|
|
|
|
|
|
prepareResponse(response -> response.setHeader("Content-Type", "text/plain") |
|
|
|
prepareResponse(response -> |
|
|
|
.setBody("Hello Spring!")); |
|
|
|
response.setHeader("Content-Type", "text/plain").setBody("Hello Spring!")); |
|
|
|
|
|
|
|
|
|
|
|
URI uri = new URI(null, null, "/foo bar", null); |
|
|
|
URI uri = new URI(null, null, "/foo bar", null); |
|
|
|
|
|
|
|
|
|
|
|
@ -969,23 +969,28 @@ class RestClientIntegrationTests { |
|
|
|
@ParameterizedRestClientTest |
|
|
|
@ParameterizedRestClientTest |
|
|
|
void cookieAddsCookie(ClientHttpRequestFactory requestFactory) { |
|
|
|
void cookieAddsCookie(ClientHttpRequestFactory requestFactory) { |
|
|
|
startServer(requestFactory); |
|
|
|
startServer(requestFactory); |
|
|
|
prepareResponse(response -> response.setHeader("Content-Type", "text/plain") |
|
|
|
|
|
|
|
.setBody("Hello Spring!")); |
|
|
|
prepareResponse(response -> |
|
|
|
|
|
|
|
response.setHeader("Content-Type", "text/plain").setBody("Hello Spring!")); |
|
|
|
|
|
|
|
|
|
|
|
this.restClient.get() |
|
|
|
this.restClient.get() |
|
|
|
.uri("/greeting") |
|
|
|
.uri("/greeting") |
|
|
|
.cookie("foo", "bar") |
|
|
|
.cookie("c1", "v1a") |
|
|
|
|
|
|
|
.cookie("c1", "v1b") |
|
|
|
|
|
|
|
.cookie("c2", "v2a") |
|
|
|
.retrieve() |
|
|
|
.retrieve() |
|
|
|
.body(String.class); |
|
|
|
.body(String.class); |
|
|
|
|
|
|
|
|
|
|
|
expectRequest(request -> assertThat(request.getHeader("Cookie")).isEqualTo("foo=bar")); |
|
|
|
expectRequest(request -> assertThat(request.getHeader("Cookie")).isEqualTo("c1=v1a; c1=v1b; c2=v2a")); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ParameterizedRestClientTest |
|
|
|
@ParameterizedRestClientTest |
|
|
|
void cookieOverridesDefaultCookie(ClientHttpRequestFactory requestFactory) { |
|
|
|
void cookieOverridesDefaultCookie(ClientHttpRequestFactory requestFactory) { |
|
|
|
startServer(requestFactory); |
|
|
|
startServer(requestFactory); |
|
|
|
prepareResponse(response -> response.setHeader("Content-Type", "text/plain") |
|
|
|
|
|
|
|
.setBody("Hello Spring!")); |
|
|
|
prepareResponse(response -> |
|
|
|
|
|
|
|
response.setHeader("Content-Type", "text/plain").setBody("Hello Spring!")); |
|
|
|
|
|
|
|
|
|
|
|
RestClient restClientWithCookies = this.restClient.mutate() |
|
|
|
RestClient restClientWithCookies = this.restClient.mutate() |
|
|
|
.defaultCookie("testCookie", "firstValue", "secondValue") |
|
|
|
.defaultCookie("testCookie", "firstValue", "secondValue") |
|
|
|
.build(); |
|
|
|
.build(); |
|
|
|
@ -1002,8 +1007,9 @@ class RestClientIntegrationTests { |
|
|
|
@ParameterizedRestClientTest |
|
|
|
@ParameterizedRestClientTest |
|
|
|
void cookiesCanRemoveCookie(ClientHttpRequestFactory requestFactory) { |
|
|
|
void cookiesCanRemoveCookie(ClientHttpRequestFactory requestFactory) { |
|
|
|
startServer(requestFactory); |
|
|
|
startServer(requestFactory); |
|
|
|
prepareResponse(response -> response.setHeader("Content-Type", "text/plain") |
|
|
|
|
|
|
|
.setBody("Hello Spring!")); |
|
|
|
prepareResponse(response -> |
|
|
|
|
|
|
|
response.setHeader("Content-Type", "text/plain").setBody("Hello Spring!")); |
|
|
|
|
|
|
|
|
|
|
|
this.restClient.get() |
|
|
|
this.restClient.get() |
|
|
|
.uri("/greeting") |
|
|
|
.uri("/greeting") |
|
|
|
|