Add RequestAttributeArgumentResolver

Closes gh-28458
This commit is contained in:
rstoyanchev
2022-05-17 14:02:23 +01:00
parent 495507e5d4
commit 496c1dcae1
7 changed files with 199 additions and 20 deletions

View File

@@ -103,6 +103,7 @@ public class WebClientAdapter implements HttpClientAdapter {
bodySpec.headers(headers -> headers.putAll(requestValues.getHeaders()));
bodySpec.cookies(cookies -> cookies.putAll(requestValues.getCookies()));
bodySpec.attributes(attributes -> attributes.putAll(requestValues.getAttributes()));
if (requestValues.getBodyValue() != null) {
bodySpec.bodyValue(requestValues.getBodyValue());

View File

@@ -19,6 +19,8 @@ package org.springframework.web.reactive.function.client.support;
import java.io.IOException;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import okhttp3.mockwebserver.MockResponse;
@@ -29,11 +31,13 @@ import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.service.annotation.GetExchange;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for {@link HttpServiceProxyFactory HTTP Service proxy}
@@ -45,22 +49,10 @@ public class WebClientHttpServiceProxyTests {
private MockWebServer server;
private TestHttpService httpService;
@BeforeEach
void setUp() {
this.server = new MockWebServer();
WebClient webClient = WebClient
.builder()
.clientConnector(new ReactorClientHttpConnector())
.baseUrl(this.server.url("/").toString())
.build();
WebClientAdapter clientAdapter = new WebClientAdapter(webClient);
HttpServiceProxyFactory proxyFactory = HttpServiceProxyFactory.builder(clientAdapter).build();
this.httpService = proxyFactory.createClient(TestHttpService.class);
}
@SuppressWarnings("ConstantConditions")
@@ -78,12 +70,47 @@ public class WebClientHttpServiceProxyTests {
prepareResponse(response ->
response.setHeader("Content-Type", "text/plain").setBody("Hello Spring!"));
StepVerifier.create(this.httpService.getGreeting())
StepVerifier.create(initHttpService().getGreeting())
.expectNext("Hello Spring!")
.expectComplete()
.verify(Duration.ofSeconds(5));
}
@Test
void greetingWithRequestAttribute() {
Map<String, Object> attributes = new HashMap<>();
WebClient webClient = WebClient.builder()
.baseUrl(this.server.url("/").toString())
.filter((request, next) -> {
attributes.putAll(request.attributes());
return next.exchange(request);
})
.build();
prepareResponse(response ->
response.setHeader("Content-Type", "text/plain").setBody("Hello Spring!"));
StepVerifier.create(initHttpService(webClient).getGreetingWithAttribute("myAttributeValue"))
.expectNext("Hello Spring!")
.expectComplete()
.verify(Duration.ofSeconds(5));
assertThat(attributes).containsEntry("myAttribute", "myAttributeValue");
}
private TestHttpService initHttpService() {
WebClient webClient = WebClient.builder().baseUrl(this.server.url("/").toString()).build();
return initHttpService(webClient);
}
private TestHttpService initHttpService(WebClient webClient) {
WebClientAdapter clientAdapter = new WebClientAdapter(webClient);
HttpServiceProxyFactory proxyFactory = HttpServiceProxyFactory.builder(clientAdapter).build();
return proxyFactory.createClient(TestHttpService.class);
}
private void prepareResponse(Consumer<MockResponse> consumer) {
MockResponse response = new MockResponse();
consumer.accept(response);
@@ -96,6 +123,9 @@ public class WebClientHttpServiceProxyTests {
@GetExchange("/greeting")
Mono<String> getGreeting();
@GetExchange("/greeting")
Mono<String> getGreetingWithAttribute(@RequestAttribute String myAttribute);
}