Use provided HttpClient consistently

This commit harmonizes the behavior of HttpComponents5MessageSender to
reuse a provided HttpClient, be it provided via constructor or property.

Closes gh-1512
This commit is contained in:
Stéphane Nicoll
2025-03-27 18:48:49 +01:00
parent 93eb5f4aa4
commit 611eb9b98d
2 changed files with 40 additions and 35 deletions

View File

@@ -25,30 +25,39 @@ import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
/**
* Tests for {@link HttpComponents5MessageSender}.
*
* @author Lars Uffmann
* @author Stephane Nicoll
*/
class HttpComponents5MessageSenderTest {
@Test
void afterPropertiesSetShouldProperlyInitializeHttpClient() throws Exception {
HttpComponents5MessageSender messageSender = new HttpComponents5MessageSender();
assertThat(messageSender.getHttpClient()).isNull();
Duration timeout = Duration.ofSeconds(1);
assertThatCode(() -> messageSender.setConnectionTimeout(timeout)).doesNotThrowAnyException();
messageSender.setConnectionTimeout(Duration.ofSeconds(1));
messageSender.afterPropertiesSet();
assertThat(messageSender.getHttpClient()).isNotNull();
}
@Test
void afterPropertiesSetShouldUseAlreadyProvidedHttpClientIfAvailable() throws Exception {
void afterPropertiesSetShouldUseAlreadyProvidedHttpClientIfAvailableWithConstructor() throws Exception {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpComponents5MessageSender messageSender = new HttpComponents5MessageSender(httpClient);
assertThatCode(() -> messageSender.setConnectionTimeout(Duration.ofSeconds(1)))
.isInstanceOf(IllegalStateException.class);
messageSender.afterPropertiesSet();
assertThat(messageSender.getHttpClient()).isSameAs(httpClient);
}
Duration timeout = Duration.ofSeconds(1);
assertThatCode(() -> messageSender.setConnectionTimeout(timeout)).isInstanceOf(IllegalStateException.class);
@Test
void afterPropertiesSetShouldUseAlreadyProvidedHttpClientIfAvailableWithProperty() throws Exception {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpComponents5MessageSender messageSender = new HttpComponents5MessageSender();
messageSender.setHttpClient(httpClient);
messageSender.afterPropertiesSet();
assertThat(messageSender.getHttpClient()).isSameAs(httpClient);
}