Add UriBuilderFactoryArgumentResolver

See gh-31413
This commit is contained in:
Olga MaciaszekSharma
2023-10-10 16:53:11 +02:00
committed by rstoyanchev
parent 364186e699
commit 0cd196e3dd
15 changed files with 589 additions and 24 deletions

View File

@@ -47,6 +47,8 @@ import org.springframework.web.service.annotation.GetExchange;
import org.springframework.web.service.annotation.PostExchange;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
import org.springframework.web.testfixture.servlet.MockMultipartFile;
import org.springframework.web.util.DefaultUriBuilderFactory;
import org.springframework.web.util.UriBuilderFactory;
import static org.assertj.core.api.Assertions.assertThat;
@@ -60,12 +62,17 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class WebClientAdapterTests {
private static final String ANOTHER_SERVER_RESPONSE_BODY = "Hello Spring 2!";
private MockWebServer server;
private MockWebServer anotherServer;
@BeforeEach
void setUp() {
this.server = new MockWebServer();
this.anotherServer = anotherServer();
}
@SuppressWarnings("ConstantConditions")
@@ -74,6 +81,10 @@ public class WebClientAdapterTests {
if (this.server != null) {
this.server.shutdown();
}
if (this.anotherServer != null) {
this.anotherServer.shutdown();
}
}
@@ -157,6 +168,60 @@ public class WebClientAdapterTests {
"Content-Type: text/plain;charset=UTF-8", "Content-Length: 5", "test2");
}
@Test
void uriBuilderFactory() throws Exception {
String ignoredResponseBody = "hello";
prepareResponse(response -> response.setResponseCode(200).setBody(ignoredResponseBody));
UriBuilderFactory factory = new DefaultUriBuilderFactory(this.anotherServer.url("/")
.toString());
String actualBody = initService().getWithUriBuilderFactory(factory);
assertThat(actualBody).isEqualTo(ANOTHER_SERVER_RESPONSE_BODY);
assertThat(this.anotherServer.takeRequest().getPath()).isEqualTo("/greeting");
assertThat(this.server.getRequestCount()).isEqualTo(0);
}
@Test
void uriBuilderFactoryWithPathVariableAndRequestParam() throws Exception {
String ignoredResponseBody = "hello";
prepareResponse(response -> response.setResponseCode(200).setBody(ignoredResponseBody));
UriBuilderFactory factory = new DefaultUriBuilderFactory(this.anotherServer.url("/")
.toString());
String actualBody = initService().getWithUriBuilderFactory(factory, "123", "test");
assertThat(actualBody).isEqualTo(ANOTHER_SERVER_RESPONSE_BODY);
assertThat(this.anotherServer.takeRequest().getPath())
.isEqualTo("/greeting/123?param=test");
assertThat(this.server.getRequestCount()).isEqualTo(0);
}
@Test
void ignoredUriBuilderFactory() throws Exception {
String expectedResponseBody = "hello";
prepareResponse(response -> response.setResponseCode(200).setBody(expectedResponseBody));
URI dynamicUri = this.server.url("/greeting/123").uri();
UriBuilderFactory factory = new DefaultUriBuilderFactory(this.anotherServer.url("/")
.toString());
String actualBody = initService().getWithIgnoredUriBuilderFactory(dynamicUri, factory);
assertThat(actualBody).isEqualTo(expectedResponseBody);
assertThat(this.server.takeRequest().getRequestUrl().uri()).isEqualTo(dynamicUri);
assertThat(this.anotherServer.getRequestCount()).isEqualTo(0);
}
private static MockWebServer anotherServer() {
MockWebServer anotherServer = new MockWebServer();
MockResponse response = new MockResponse();
response.setHeader("Content-Type", "text/plain")
.setBody(ANOTHER_SERVER_RESPONSE_BODY);
anotherServer.enqueue(response);
return anotherServer;
}
private Service initService() {
WebClient webClient = WebClient.builder().baseUrl(this.server.url("/").toString()).build();
return initService(webClient);
@@ -191,6 +256,16 @@ public class WebClientAdapterTests {
@PostExchange
void postMultipart(MultipartFile file, @RequestPart String anotherPart);
@GetExchange("/greeting")
String getWithUriBuilderFactory(UriBuilderFactory uriBuilderFactory);
@GetExchange("/greeting/{id}")
String getWithUriBuilderFactory(UriBuilderFactory uriBuilderFactory,
@PathVariable String id, @RequestParam String param);
@GetExchange("/greeting")
String getWithIgnoredUriBuilderFactory(URI uri, UriBuilderFactory uriBuilderFactory);
}
}

View File

@@ -22,15 +22,22 @@ import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestAttribute
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.reactive.function.client.ClientRequest
import org.springframework.web.reactive.function.client.ExchangeFunction
import org.springframework.web.reactive.function.client.WebClient
import org.springframework.web.service.annotation.GetExchange
import org.springframework.web.service.invoker.HttpServiceProxyFactory
import org.springframework.web.service.invoker.createClient
import org.springframework.web.util.DefaultUriBuilderFactory
import org.springframework.web.util.UriBuilderFactory
import reactor.core.publisher.Mono
import reactor.test.StepVerifier
import java.net.URI
import java.time.Duration
import java.util.function.Consumer
@@ -40,20 +47,24 @@ import java.util.function.Consumer
*
* @author DongHyeon Kim
* @author Sebastien Deleuze
* @author Olga Maciaszek-Sharma
*/
@Suppress("DEPRECATION")
class KotlinWebClientHttpServiceProxyTests {
private lateinit var server: MockWebServer
private lateinit var anotherServer: MockWebServer
@BeforeEach
fun setUp() {
server = MockWebServer()
anotherServer = anotherServer()
}
@AfterEach
fun shutdown() {
server.shutdown()
anotherServer.shutdown()
}
@Test
@@ -120,6 +131,55 @@ class KotlinWebClientHttpServiceProxyTests {
}
}
@Test
@Throws(InterruptedException::class)
fun getWithFactoryPathVariableAndRequestParam() {
prepareResponse { response: MockResponse ->
response.setHeader(
"Content-Type",
"text/plain"
).setBody("Hello Spring!")
}
val factory: UriBuilderFactory = DefaultUriBuilderFactory(anotherServer.url("/")
.toString())
val actualResponse: ResponseEntity<String> = initHttpService()
.getWithUriBuilderFactory(factory, "123",
"test")
val request = anotherServer.takeRequest()
assertThat(actualResponse.statusCode).isEqualTo(HttpStatus.OK)
assertThat(actualResponse.body).isEqualTo("Hello Spring 2!")
assertThat(request.method).isEqualTo("GET")
assertThat(request.path).isEqualTo("/greeting/123?param=test")
assertThat(server.requestCount).isEqualTo(0)
}
@Test
@Throws(InterruptedException::class)
fun getWithIgnoredUriBuilderFactory() {
prepareResponse { response: MockResponse ->
response.setHeader(
"Content-Type",
"text/plain"
).setBody("Hello Spring!")
}
val dynamicUri = server.url("/greeting/123").uri()
val factory: UriBuilderFactory = DefaultUriBuilderFactory(anotherServer.url("/")
.toString())
val actualResponse: ResponseEntity<String> = initHttpService()
.getWithIgnoredUriBuilderFactory(dynamicUri, factory)
val request = server.takeRequest()
assertThat(actualResponse.statusCode).isEqualTo(HttpStatus.OK)
assertThat(actualResponse.body).isEqualTo("Hello Spring!")
assertThat(request.method).isEqualTo("GET")
assertThat(request.path).isEqualTo("/greeting/123")
assertThat(anotherServer.requestCount).isEqualTo(0)
}
private fun initHttpService(): TestHttpService {
val webClient = WebClient.builder().baseUrl(
server.url("/").toString()
@@ -138,6 +198,14 @@ class KotlinWebClientHttpServiceProxyTests {
server.enqueue(response)
}
private fun anotherServer(): MockWebServer {
val anotherServer = MockWebServer()
val response = MockResponse()
response.setHeader("Content-Type", "text/plain").setBody("Hello Spring 2!")
anotherServer.enqueue(response)
return anotherServer
}
private interface TestHttpService {
@GetExchange("/greeting")
suspend fun getGreetingSuspending(): String
@@ -150,5 +218,12 @@ class KotlinWebClientHttpServiceProxyTests {
@GetExchange("/greeting")
suspend fun getGreetingSuspendingWithAttribute(@RequestAttribute myAttribute: String): String
@GetExchange("/greeting/{id}")
fun getWithUriBuilderFactory(uriBuilderFactory: UriBuilderFactory?,
@PathVariable id: String?, @RequestParam param: String?): ResponseEntity<String>
@GetExchange("/greeting")
fun getWithIgnoredUriBuilderFactory(uri: URI?, uriBuilderFactory: UriBuilderFactory?): ResponseEntity<String>
}
}