Browse Source

Polishing in RestClientAdapterTests and WebClientAdapterTests

pull/36359/head
rstoyanchev 1 month ago
parent
commit
e106fc0434
  1. 10
      spring-web/src/test/java/org/springframework/web/client/support/RestClientAdapterTests.java
  2. 28
      spring-webflux/src/test/java/org/springframework/web/reactive/function/client/support/WebClientAdapterTests.java

10
spring-web/src/test/java/org/springframework/web/client/support/RestClientAdapterTests.java

@ -330,7 +330,7 @@ class RestClientAdapterTests { @@ -330,7 +330,7 @@ class RestClientAdapterTests {
prepareResponse(builder ->
builder.setHeader("Content-Type", "text/plain").body("Hello Spring 2!"));
InputStream inputStream = initService().getInputStream();
InputStream inputStream = initService(Service.class).getInputStream();
RecordedRequest request = this.anotherServer.takeRequest();
assertThat(request.getTarget()).isEqualTo("/input-stream");
@ -341,7 +341,7 @@ class RestClientAdapterTests { @@ -341,7 +341,7 @@ class RestClientAdapterTests {
void getInputStreamWithError() {
prepareResponse(builder -> builder.code(400).body("rejected"));
assertThatThrownBy(() -> initService().getInputStream())
assertThatThrownBy(() -> initService(Service.class).getInputStream())
.isExactlyInstanceOf(HttpClientErrorException.BadRequest.class)
.hasMessage("400 Client Error: \"rejected\"");
}
@ -352,7 +352,7 @@ class RestClientAdapterTests { @@ -352,7 +352,7 @@ class RestClientAdapterTests {
builder.setHeader("Content-Type", "text/plain").body("Hello Spring 2!"));
String body = "test stream";
initService().postOutputStream(outputStream -> outputStream.write(body.getBytes()));
initService(Service.class).postOutputStream(outputStream -> outputStream.write(body.getBytes()));
RecordedRequest request = this.anotherServer.takeRequest();
assertThat(request.getTarget()).isEqualTo("/output-stream");
@ -377,11 +377,11 @@ class RestClientAdapterTests { @@ -377,11 +377,11 @@ class RestClientAdapterTests {
assertThat(responseEntity.getBody()).isNull();
}
private Service initService() {
private <S> S initService(Class<S> serviceType) {
String url = this.anotherServer.url("/").toString();
RestClient restClient = RestClient.builder().baseUrl(url).build();
RestClientAdapter adapter = RestClientAdapter.create(restClient);
return HttpServiceProxyFactory.builderFor(adapter).build().createClient(Service.class);
return HttpServiceProxyFactory.builderFor(adapter).build().createClient(serviceType);
}
private void prepareResponse(Function<MockResponse.Builder, MockResponse.Builder> f) {

28
spring-webflux/src/test/java/org/springframework/web/reactive/function/client/support/WebClientAdapterTests.java

@ -101,7 +101,7 @@ class WebClientAdapterTests { @@ -101,7 +101,7 @@ class WebClientAdapterTests {
void greeting() {
prepareResponse(builder -> builder.setHeader("Content-Type", "text/plain").body("Hello Spring!"));
StepVerifier.create(initService().getGreeting())
StepVerifier.create(initService(Service.class).getGreeting())
.expectNext("Hello Spring!")
.expectComplete()
.verify(Duration.ofSeconds(5));
@ -121,7 +121,7 @@ class WebClientAdapterTests { @@ -121,7 +121,7 @@ class WebClientAdapterTests {
prepareResponse(response -> response.setHeader("Content-Type", "text/plain").body("Hello Spring!"));
StepVerifier.create(initService(webClient).getGreetingWithAttribute("myAttributeValue"))
StepVerifier.create(initService(webClient, Service.class).getGreetingWithAttribute("myAttributeValue"))
.expectNext("Hello Spring!")
.expectComplete()
.verify(Duration.ofSeconds(5));
@ -135,7 +135,7 @@ class WebClientAdapterTests { @@ -135,7 +135,7 @@ class WebClientAdapterTests {
prepareResponse(response -> response.code(200).body(expectedBody));
URI dynamicUri = this.server.url("/greeting/123").uri();
String actualBody = initService().getGreetingById(dynamicUri, "456");
String actualBody = initService(Service.class).getGreetingById(dynamicUri, "456");
assertThat(actualBody).isEqualTo(expectedBody);
assertThat(this.server.takeRequest().getUrl().uri()).isEqualTo(dynamicUri);
@ -149,7 +149,7 @@ class WebClientAdapterTests { @@ -149,7 +149,7 @@ class WebClientAdapterTests {
map.add("param1", "value 1");
map.add("param2", "value 2");
initService().postForm(map);
initService(Service.class).postForm(map);
RecordedRequest request = this.server.takeRequest();
assertThat(request.getHeaders().get("Content-Type")).isEqualTo("application/x-www-form-urlencoded");
@ -164,7 +164,7 @@ class WebClientAdapterTests { @@ -164,7 +164,7 @@ class WebClientAdapterTests {
MultipartFile file = new MockMultipartFile(
fileName, originalFileName, MediaType.APPLICATION_JSON_VALUE, "test".getBytes());
initService().postMultipart(file, "test2");
initService(Service.class).postMultipart(file, "test2");
RecordedRequest request = this.server.takeRequest();
assertThat(request.getHeaders().get("Content-Type")).startsWith("multipart/form-data;boundary=");
@ -183,7 +183,7 @@ class WebClientAdapterTests { @@ -183,7 +183,7 @@ class WebClientAdapterTests {
persons.add(new Person("John"));
persons.add(new Person("Richard"));
initService().postPersonSet(persons);
initService(Service.class).postPersonSet(persons);
RecordedRequest request = server.takeRequest();
assertThat(request.getMethod()).isEqualTo("POST");
@ -200,7 +200,7 @@ class WebClientAdapterTests { @@ -200,7 +200,7 @@ class WebClientAdapterTests {
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML_VALUE)
.build();
initService(webClient).postObject(new Person("John"));
initService(webClient, Service.class).postObject(new Person("John"));
RecordedRequest request = server.takeRequest();
assertThat(request.getMethod()).isEqualTo("POST");
@ -215,7 +215,7 @@ class WebClientAdapterTests { @@ -215,7 +215,7 @@ class WebClientAdapterTests {
prepareResponse(response -> response.code(200).body(ignoredResponseBody));
UriBuilderFactory factory = new DefaultUriBuilderFactory(this.anotherServer.url("/").toString());
String actualBody = initService().getWithUriBuilderFactory(factory);
String actualBody = initService(Service.class).getWithUriBuilderFactory(factory);
assertThat(actualBody).isEqualTo(ANOTHER_SERVER_RESPONSE_BODY);
assertThat(this.anotherServer.takeRequest().getTarget()).isEqualTo("/greeting");
@ -228,7 +228,7 @@ class WebClientAdapterTests { @@ -228,7 +228,7 @@ class WebClientAdapterTests {
prepareResponse(response -> response.code(200).body(ignoredResponseBody));
UriBuilderFactory factory = new DefaultUriBuilderFactory(this.anotherServer.url("/").toString());
String actualBody = initService().getWithUriBuilderFactory(factory, "123", "test");
String actualBody = initService(Service.class).getWithUriBuilderFactory(factory, "123", "test");
assertThat(actualBody).isEqualTo(ANOTHER_SERVER_RESPONSE_BODY);
assertThat(this.anotherServer.takeRequest().getTarget()).isEqualTo("/greeting/123?param=test");
@ -242,7 +242,7 @@ class WebClientAdapterTests { @@ -242,7 +242,7 @@ class WebClientAdapterTests {
URI dynamicUri = this.server.url("/greeting/123").uri();
UriBuilderFactory factory = new DefaultUriBuilderFactory(this.anotherServer.url("/").toString());
String actualBody = initService().getWithIgnoredUriBuilderFactory(dynamicUri, factory);
String actualBody = initService(Service.class).getWithIgnoredUriBuilderFactory(dynamicUri, factory);
assertThat(actualBody).isEqualTo(expectedResponseBody);
assertThat(this.server.takeRequest().getUrl().uri()).isEqualTo(dynamicUri);
@ -285,14 +285,14 @@ class WebClientAdapterTests { @@ -285,14 +285,14 @@ class WebClientAdapterTests {
return anotherServer;
}
private Service initService() {
private <S> S initService(Class<S> serviceType) {
WebClient webClient = WebClient.builder().baseUrl(this.server.url("/").toString()).build();
return initService(webClient);
return initService(webClient, serviceType);
}
private Service initService(WebClient webClient) {
private <S> S initService(WebClient webClient, Class<S> serviceType) {
WebClientAdapter adapter = WebClientAdapter.create(webClient);
return HttpServiceProxyFactory.builderFor(adapter).build().createClient(Service.class);
return HttpServiceProxyFactory.builderFor(adapter).build().createClient(serviceType);
}
private void prepareResponse(Function<MockResponse.Builder, MockResponse.Builder> f) {

Loading…
Cancel
Save