Skip to content

Commit ca7e089

Browse files
wip
1 parent 681ae23 commit ca7e089

File tree

2 files changed

+49
-32
lines changed

2 files changed

+49
-32
lines changed

src/test/java/com/influxdb/v3/client/TestUtils.java

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import java.io.FileInputStream;
2525
import java.io.IOException;
26+
import java.net.InetAddress;
2627
import java.net.InetSocketAddress;
2728
import java.net.URI;
2829
import java.nio.charset.StandardCharsets;
@@ -41,9 +42,15 @@
4142
import io.grpc.HttpConnectProxiedSocketAddress;
4243
import io.grpc.ProxyDetector;
4344
import io.netty.handler.ssl.ClientAuth;
45+
import io.netty.handler.ssl.JdkSslContext;
4446
import io.netty.handler.ssl.SslContext;
4547
import io.netty.handler.ssl.SslContextBuilder;
4648
import io.netty.handler.ssl.SslProvider;
49+
import mockwebserver3.Dispatcher;
50+
import mockwebserver3.MockResponse;
51+
import mockwebserver3.MockWebServer;
52+
import mockwebserver3.RecordedRequest;
53+
import okhttp3.Headers;
4754
import org.apache.arrow.flight.FlightServer;
4855
import org.apache.arrow.flight.Location;
4956
import org.apache.arrow.flight.NoOpFlightProducer;
@@ -110,8 +117,11 @@ public static VectorSchemaRoot generateVectorSchemaRoot(final int fieldCount, fi
110117

111118
// fixme
112119
// Create SslContext for mTLS only
113-
public static SslContext createNettySslContext(boolean isServer, String format, String password, String keyFilePath, String trustFilePath, boolean isDisableKeystore, boolean isJdkSslContext)
114-
throws IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException {
120+
public static SslContext createNettySslContext(final boolean isServer, final String format, final String password, final String keyFilePath,
121+
final String trustFilePath, final boolean isDisableKeystore,
122+
final boolean isJdkSslContext)
123+
throws IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException,
124+
UnrecoverableKeyException {
115125
KeyManagerFactory keyManagerFactory = getKeyManagerFactory(format, password, keyFilePath);
116126

117127
TrustManagerFactory trustManagerFactory = getTrustManagerFactory(format, password, trustFilePath);
@@ -136,7 +146,8 @@ public static SslContext createNettySslContext(boolean isServer, String format,
136146
return sslContextBuilder.build();
137147
}
138148

139-
public static ProxyDetector createProxyDetector(@Nonnull final String targetUrl, @Nonnull final String proxyUrl, @Nullable final String username, @Nullable final String password) {
149+
public static ProxyDetector createProxyDetector(@Nonnull final String targetUrl, @Nonnull final String proxyUrl,
150+
@Nullable final String username, @Nullable final String password) {
140151
URI targetUri = URI.create(targetUrl);
141152
URI proxyUri = URI.create(proxyUrl);
142153
return (targetServerAddress) -> {
@@ -154,17 +165,43 @@ public static ProxyDetector createProxyDetector(@Nonnull final String targetUrl,
154165
};
155166
}
156167

168+
public static MockWebServer customDispatchServer(@NotNull final String host, @NotNull final Integer port,
169+
@Nullable final MockResponse mockResponse,
170+
@Nullable final SslContext sslContext,
171+
boolean requireClientAuth) throws IOException {
172+
MockWebServer server = new MockWebServer();
173+
server.setDispatcher(new Dispatcher() {
174+
@NotNull
175+
@Override
176+
public MockResponse dispatch(@NotNull RecordedRequest recordedRequest) {
177+
return mockResponse != null ? mockResponse : new MockResponse(200, Headers.EMPTY, "Success");
178+
}
179+
});
180+
if (sslContext instanceof JdkSslContext) {
181+
server.useHttps(((JdkSslContext) sslContext).context().getSocketFactory());
182+
if (requireClientAuth) {
183+
server.requireClientAuth();
184+
}
185+
}
186+
server.start(InetAddress.getByName(host), port);
187+
return server;
188+
}
189+
157190
@NotNull
158-
private static TrustManagerFactory getTrustManagerFactory(String format, String password, String trustFilePath) throws NoSuchAlgorithmException, KeyStoreException, IOException, CertificateException {
159-
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
191+
private static TrustManagerFactory getTrustManagerFactory(final String format, final String password, final String trustFilePath)
192+
throws NoSuchAlgorithmException, KeyStoreException, IOException, CertificateException {
193+
TrustManagerFactory trustManagerFactory =
194+
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
160195
KeyStore trustStore = KeyStore.getInstance(format);
161196
trustStore.load(new FileInputStream(trustFilePath), password.toCharArray());
162197
trustManagerFactory.init(trustStore);
163198
return trustManagerFactory;
164199
}
165200

166201
@NotNull
167-
private static KeyManagerFactory getKeyManagerFactory(String format, String password, String keyFilePath) throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException {
202+
private static KeyManagerFactory getKeyManagerFactory(final String format, final String password, final String keyFilePath)
203+
throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException,
204+
UnrecoverableKeyException {
168205
KeyStore keyStore = KeyStore.getInstance(format);
169206
keyStore.load(new FileInputStream(keyFilePath), password.toCharArray());
170207
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());

src/test/java/com/influxdb/v3/client/internal/RestClientTest.java

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,9 @@ public void uri() throws InterruptedException, URISyntaxException, SSLException,
297297

298298
@Test
299299
public void proxyUrl() throws InterruptedException, URISyntaxException, IOException, ExecutionException {
300-
try (MockWebServer proxyServer = customDispatchServer("localhost", 10000,
300+
301+
302+
try (MockWebServer proxyServer = TestUtils.customDispatchServer("localhost", 10000,
301303
new MockResponse(200, Headers.EMPTY, ""), null, false);) {
302304

303305
Supplier<HttpProxyHandler> proxy = () -> new HttpProxyHandler(proxyServer.getSocketAddress());
@@ -546,7 +548,7 @@ public void nettyRestMutualSslContext()
546548

547549
var influxDBVersion = "4.0.0";
548550
try (
549-
MockWebServer ignored = customDispatchServer("localhost", 8080,
551+
MockWebServer ignored = TestUtils.customDispatchServer("localhost", 8080,
550552
new MockResponse(200, Headers.of(), "{\"version\":\"" + influxDBVersion + "\"}"),
551553
serverSslContext, true);
552554
RestClient restClient = new RestClient(config);
@@ -582,7 +584,8 @@ public void nettyRestMutualSslContextFail()
582584
.build();
583585

584586
try (
585-
MockWebServer ignored = customDispatchServer("localhost", 8080, new MockResponse(400, Headers.of(), ""),
587+
MockWebServer ignored = TestUtils.customDispatchServer("localhost", 8080,
588+
new MockResponse(400, Headers.of(), ""),
586589
serverSslContext, true);
587590
RestClient restClient = new RestClient(config);
588591
) {
@@ -591,27 +594,4 @@ public void nettyRestMutualSslContextFail()
591594
Assertions.assertThat(e.getMessage()).contains("BAD_CERTIFICATE");
592595
}
593596
}
594-
595-
/*Private functions*/
596-
//fixme refactor this
597-
private MockWebServer customDispatchServer(@NotNull String host, @NotNull Integer port,
598-
@Nullable MockResponse mockResponse, @Nullable SslContext sslContext,
599-
boolean requireClientAuth) throws IOException {
600-
MockWebServer server = new MockWebServer();
601-
server.setDispatcher(new Dispatcher() {
602-
@NotNull
603-
@Override
604-
public MockResponse dispatch(@NotNull RecordedRequest recordedRequest) throws InterruptedException {
605-
return mockResponse != null ? mockResponse : new MockResponse(200, Headers.EMPTY, "Success");
606-
}
607-
});
608-
if (sslContext instanceof JdkSslContext) {
609-
server.useHttps(((JdkSslContext) sslContext).context().getSocketFactory());
610-
if (requireClientAuth) {
611-
server.requireClientAuth();
612-
}
613-
}
614-
server.start(InetAddress.getByName(host), port);
615-
return server;
616-
}
617597
}

0 commit comments

Comments
 (0)