Skip to content

Commit a2d1d9d

Browse files
committed
Add unit test for the proxy option.
1 parent 1c4cfd0 commit a2d1d9d

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

src/test/java/co/elastic/logstash/filters/elasticintegration/ElasticsearchRestClientBuilderTest.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
import org.apache.http.HttpHost;
1111
import org.apache.http.HttpRequest;
1212
import org.apache.http.HttpRequestInterceptor;
13-
import org.apache.http.message.BasicHeader;
1413
import org.apache.http.message.BasicHttpRequest;
1514
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
1615
import org.elasticsearch.client.RestClient;
1716
import org.elasticsearch.client.RestClientBuilder;
1817
import org.hamcrest.Matchers;
18+
import org.mockito.Mockito;
1919
import org.junit.jupiter.api.Test;
2020

2121
import java.io.IOException;
@@ -24,7 +24,6 @@
2424
import java.util.Collection;
2525
import java.util.Collections;
2626
import java.util.List;
27-
import java.util.Objects;
2827

2928
import co.elastic.logstash.filters.elasticintegration.ElasticsearchRestClientBuilder.ElasticApiConfig;
3029

@@ -33,7 +32,6 @@
3332

3433
import static org.hamcrest.MatcherAssert.assertThat;
3534
import static org.hamcrest.Matchers.containsString;
36-
import static org.hamcrest.Matchers.equalTo;
3735
import static org.hamcrest.Matchers.is;
3836
import static org.hamcrest.Matchers.stringContainsInOrder;
3937
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -67,6 +65,35 @@ public void testForCloudIdFactory() {
6765
}
6866
}
6967

68+
@Test
69+
public void testProxyConfigSetProxy() {
70+
ElasticsearchRestClientBuilder.ProxyConfig proxyConfig = new ElasticsearchRestClientBuilder.ProxyConfig();
71+
proxyConfig.setProxy("https://proxy.mycorp.com:9043");
72+
73+
// Verify the proxy is configured in the HttpAsyncClientBuilder
74+
HttpAsyncClientBuilder httpClientBuilder = Mockito.mock(HttpAsyncClientBuilder.class);
75+
proxyConfig.configureHttpClient(httpClientBuilder);
76+
77+
// Verify setProxy was called with the correct HttpHost
78+
ArgumentCaptor<HttpHost> proxyCaptor = ArgumentCaptor.forClass(HttpHost.class);
79+
verify(httpClientBuilder).setProxy(proxyCaptor.capture());
80+
81+
HttpHost capturedProxy = proxyCaptor.getValue();
82+
assertThat(capturedProxy.getHostName(), is("proxy.mycorp.com"));
83+
assertThat(capturedProxy.getPort(), is(9043));
84+
assertThat(capturedProxy.getSchemeName(), is("https"));
85+
}
86+
87+
@Test
88+
public void testNoProxyConfigurationDoesNotSetProxy() {
89+
ElasticsearchRestClientBuilder.ProxyConfig proxyConfig = new ElasticsearchRestClientBuilder.ProxyConfig();
90+
HttpAsyncClientBuilder httpClientBuilder = Mockito.mock(HttpAsyncClientBuilder.class);
91+
proxyConfig.configureHttpClient(httpClientBuilder);
92+
93+
// Verify setProxy was never called
94+
verify(httpClientBuilder, never()).setProxy(any(HttpHost.class));
95+
}
96+
7097
@Test
7198
public void testForHostsFactorySingleURL() {
7299
final Collection<URL> inputUrls = Collections.singleton(parseURL("https://169.0.0.254:9201/mypath"));

0 commit comments

Comments
 (0)