Introduce request attributes in RestClient

This commit introduces request attributes in the RestClient and
underlying infrastructure (i.e. HttpRequest).

Closes gh-32027
This commit is contained in:
Arjen Poutsma
2024-06-10 10:06:52 +02:00
parent c36e270481
commit 60b5bbe334
21 changed files with 385 additions and 9 deletions

View File

@@ -115,6 +115,32 @@ class InterceptingClientHttpRequestFactoryTests {
request.execute();
}
@Test
void changeAttribute() throws Exception {
final String attrName = "Foo";
final String attrValue = "Bar";
ClientHttpRequestInterceptor interceptor = (request, body, execution) -> {
System.out.println("interceptor");
request.getAttributes().put(attrName, attrValue);
return execution.execute(request, body);
};
requestMock = new MockClientHttpRequest() {
@Override
protected ClientHttpResponse executeInternal() {
System.out.println("execute");
assertThat(getAttributes()).containsEntry(attrName, attrValue);
return responseMock;
}
};
requestFactory = new InterceptingClientHttpRequestFactory(requestFactoryMock, Collections.singletonList(interceptor));
ClientHttpRequest request = requestFactory.createRequest(URI.create("https://example.com"), HttpMethod.GET);
request.execute();
}
@Test
void changeURI() throws Exception {
final URI changedUri = URI.create("https://example.com/2");

View File

@@ -217,4 +217,10 @@ class ServletServerHttpRequestTests {
assertThat(request.getHeaders().getContentLength()).isEqualTo(result.length);
}
@Test
void attributes() {
request.getAttributes().put("foo", "bar");
assertThat(mockRequest.getAttribute("foo")).isEqualTo("bar");
}
}

View File

@@ -17,6 +17,8 @@
package org.springframework.web.util;
import java.net.URI;
import java.util.Collections;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
@@ -363,6 +365,11 @@ class ForwardedHeaderUtilsTests {
return UriComponentsBuilder.fromUriString("/").build().toUri();
}
@Override
public Map<String, Object> getAttributes() {
return Collections.emptyMap();
}
@Override
public HttpHeaders getHeaders() {
return new HttpHeaders();