Add ClientHttpRequestInitializer support
Add `ClientHttpRequestInitializer` interface that can be used with any `HttpAccessor` to initialize each `ClientHttpRequest` before it's used. This provides a useful alternative to `ClientHttpRequestInterceptor` when users need to configure things like `HttpHeaders`. Closes gh-22002
This commit is contained in:
@@ -40,6 +40,7 @@ import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.client.ClientHttpRequest;
|
||||
import org.springframework.http.client.ClientHttpRequestFactory;
|
||||
import org.springframework.http.client.ClientHttpRequestInitializer;
|
||||
import org.springframework.http.client.ClientHttpRequestInterceptor;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.http.converter.GenericHttpMessageConverter;
|
||||
@@ -679,6 +680,32 @@ public class RestTemplateTests {
|
||||
verify(response).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void clientHttpRequestInitializerAndRequestInterceptorAreBothApplied() throws Exception {
|
||||
ClientHttpRequestInitializer initializer = request ->
|
||||
request.getHeaders().add("MyHeader", "MyInitializerValue");
|
||||
ClientHttpRequestInterceptor interceptor = (request, body, execution) -> {
|
||||
request.getHeaders().add("MyHeader", "MyInterceptorValue");
|
||||
return execution.execute(request, body);
|
||||
};
|
||||
template.setClientHttpRequestInitializers(Collections.singletonList(initializer));
|
||||
template.setInterceptors(Collections.singletonList(interceptor));
|
||||
|
||||
MediaType contentType = MediaType.TEXT_PLAIN;
|
||||
given(converter.canWrite(String.class, contentType)).willReturn(true);
|
||||
HttpHeaders requestHeaders = new HttpHeaders();
|
||||
mockSentRequest(POST, "https://example.com", requestHeaders);
|
||||
mockResponseStatus(HttpStatus.OK);
|
||||
|
||||
HttpHeaders entityHeaders = new HttpHeaders();
|
||||
entityHeaders.setContentType(contentType);
|
||||
HttpEntity<String> entity = new HttpEntity<>("Hello World", entityHeaders);
|
||||
template.exchange("https://example.com", POST, entity, Void.class);
|
||||
assertThat(requestHeaders.get("MyHeader")).contains("MyInterceptorValue", "MyInitializerValue");
|
||||
|
||||
verify(response).close();
|
||||
}
|
||||
|
||||
private void mockSentRequest(HttpMethod method, String uri) throws Exception {
|
||||
mockSentRequest(method, uri, new HttpHeaders());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user