|
6 | 6 | */
|
7 | 7 | package co.elastic.logstash.filters.elasticintegration;
|
8 | 8 |
|
| 9 | +import org.apache.http.HttpException; |
9 | 10 | import org.apache.http.HttpHost;
|
| 11 | +import org.apache.http.HttpRequest; |
| 12 | +import org.apache.http.HttpRequestInterceptor; |
| 13 | +import org.apache.http.message.BasicHeader; |
| 14 | +import org.apache.http.message.BasicHttpRequest; |
| 15 | +import org.apache.http.impl.nio.client.HttpAsyncClientBuilder; |
10 | 16 | import org.elasticsearch.client.RestClient;
|
11 | 17 | import org.elasticsearch.client.RestClientBuilder;
|
12 | 18 | import org.hamcrest.Matchers;
|
|
18 | 24 | import java.util.Collection;
|
19 | 25 | import java.util.Collections;
|
20 | 26 | import java.util.List;
|
| 27 | +import java.util.Objects; |
| 28 | + |
| 29 | +import co.elastic.logstash.filters.elasticintegration.ElasticsearchRestClientBuilder.ElasticApiConfig; |
| 30 | + |
| 31 | +import org.mockito.MockedStatic; |
| 32 | +import org.mockito.ArgumentCaptor; |
21 | 33 |
|
22 | 34 | import static org.hamcrest.MatcherAssert.assertThat;
|
23 | 35 | import static org.hamcrest.Matchers.containsString;
|
| 36 | +import static org.hamcrest.Matchers.equalTo; |
| 37 | +import static org.hamcrest.Matchers.is; |
24 | 38 | import static org.hamcrest.Matchers.stringContainsInOrder;
|
25 | 39 | import static org.junit.jupiter.api.Assertions.assertThrows;
|
| 40 | +import static org.mockito.ArgumentCaptor.forClass; |
26 | 41 | import static org.mockito.Mockito.*;
|
27 | 42 |
|
28 | 43 | class ElasticsearchRestClientBuilderTest {
|
@@ -111,6 +126,41 @@ public void testForHostsFactoryURLsWithoutPort() {
|
111 | 126 | assertThat(illegalStateException.getMessage(), containsString("URLS must include port specification"));
|
112 | 127 | }
|
113 | 128 |
|
| 129 | + @Test |
| 130 | + public void testElasticApiConfigAddsHeaders() throws HttpException, IOException { |
| 131 | + ElasticApiConfig config = new ElasticApiConfig(); |
| 132 | + config.setApiVersion("2023-10-31"); |
| 133 | + |
| 134 | + ElasticsearchRestClientBuilder.HttpClientConfigurator mockConfigurator = |
| 135 | + mock(ElasticsearchRestClientBuilder.HttpClientConfigurator.class); |
| 136 | + |
| 137 | + try (MockedStatic<ElasticsearchRestClientBuilder.HttpClientConfigurator> mockedStatic = |
| 138 | + mockStatic(ElasticsearchRestClientBuilder.HttpClientConfigurator.class)) { |
| 139 | + mockedStatic.when(() -> |
| 140 | + ElasticsearchRestClientBuilder.HttpClientConfigurator.forAddInterceptorFirst( |
| 141 | + any(HttpRequestInterceptor.class))) |
| 142 | + .thenReturn(mockConfigurator); |
| 143 | + |
| 144 | + HttpAsyncClientBuilder builder = HttpAsyncClientBuilder.create(); |
| 145 | + config.configureHttpClient(builder); |
| 146 | + |
| 147 | + ArgumentCaptor<HttpRequestInterceptor> interceptorCaptor = forClass(HttpRequestInterceptor.class); |
| 148 | + mockedStatic.verify(() -> |
| 149 | + ElasticsearchRestClientBuilder.HttpClientConfigurator.forAddInterceptorFirst( |
| 150 | + interceptorCaptor.capture()), times(2)); |
| 151 | + verify(mockConfigurator, times(2)).configure(builder); |
| 152 | + |
| 153 | + // Process interceptors and look for product origin header |
| 154 | + List<HttpRequestInterceptor> interceptors = interceptorCaptor.getAllValues(); |
| 155 | + HttpRequest request = new BasicHttpRequest("GET", "/"); |
| 156 | + for (HttpRequestInterceptor interceptor : interceptors) { |
| 157 | + interceptor.process(request, null); |
| 158 | + } |
| 159 | + assertThat(request.getFirstHeader("x-elastic-product-origin").getValue(), |
| 160 | + is("logstash-filter-elastic_integration")); |
| 161 | + } |
| 162 | + } |
| 163 | + |
114 | 164 | private <T> void validateTranslationToClientBuilderFactory(final Collection<URL> inputUrls, final HttpHost[] expectedInputReceivedByBuilderFactory) {
|
115 | 165 | final ElasticsearchRestClientBuilder.HostsArrayRestClientBuilderFactory hostsArrayRestClientBuilderFactory = spy(HOSTS_ARRAY_REST_CLIENT_BUILDER_FACTORY);
|
116 | 166 | try (RestClient restClient = ElasticsearchRestClientBuilder.forURLs(inputUrls, hostsArrayRestClientBuilderFactory).build()) {
|
|
0 commit comments