From 32adc6de87bf2ca7de0ba45093ffb84aa9f98e38 Mon Sep 17 00:00:00 2001 From: Bilal Al Date: Thu, 5 Sep 2024 08:45:53 -0700 Subject: [PATCH] added coverage for kerberos in factory class --- .../io/split/client/SplitFactoryImplTest.java | 51 ++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/client/src/test/java/io/split/client/SplitFactoryImplTest.java b/client/src/test/java/io/split/client/SplitFactoryImplTest.java index 85defb82..6ff04aac 100644 --- a/client/src/test/java/io/split/client/SplitFactoryImplTest.java +++ b/client/src/test/java/io/split/client/SplitFactoryImplTest.java @@ -30,6 +30,7 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; +import java.net.InetSocketAddress; import java.net.Proxy; import java.net.URISyntaxException; import java.util.HashMap; @@ -38,6 +39,7 @@ import okhttp3.Authenticator; import okhttp3.OkHttpClient; import okhttp3.logging.HttpLoggingInterceptor; +import okhttp3.Interceptor; import static org.mockito.Mockito.when; @@ -367,7 +369,7 @@ public void testLocalhosJsonInputStreamNullAndFileTypeNull() throws URISyntaxExc } @Test - public void testFactoryKerberosInstance() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, URISyntaxException, IOException { + public void testBuildKerberosClientParams() throws URISyntaxException, IOException { PowerMockito.mockStatic(SplitFactoryImpl.class); ArgumentCaptor proxyCaptor = ArgumentCaptor.forClass(Proxy.class); @@ -410,4 +412,51 @@ public void testFactoryKerberosInstance() throws NoSuchMethodException, Invocati Assert.assertEquals("HTTP @ https://sdk.split-stage.io:6060", proxyCaptor.getValue().toString()); Assert.assertTrue(logCaptor.getValue() instanceof okhttp3.logging.HttpLoggingInterceptor); } + + @Test + public void testFactoryKerberosInstance() throws URISyntaxException, IOException { + OkHttpClient okHttpClient = new OkHttpClient.Builder().build(); + PowerMockito.stub(PowerMockito.method(SplitFactoryImpl.class, "buildOkHttpClient")).toReturn(okHttpClient); + PowerMockito.stub(PowerMockito.method(SplitFactoryImpl.class, "getProxyAuthenticator")).toReturn(null); + + SplitClientConfig splitClientConfig = SplitClientConfig.builder() + .setBlockUntilReadyTimeout(10000) + .authScheme(HttpAuthScheme.KERBEROS) + .kerberosPrincipalName("bilal@localhost") + .proxyPort(6060) + .proxyHost(ENDPOINT) + .build(); + + Map kerberosOptions = new HashMap(); + kerberosOptions.put("com.sun.security.auth.module.Krb5LoginModule", "required"); + kerberosOptions.put("refreshKrb5Config", "false"); + kerberosOptions.put("doNotPrompt", "false"); + kerberosOptions.put("useTicketCache", "true"); + + RequestDecorator requestDecorator = new RequestDecorator(null); + SDKMetadata sdkmeta = new SDKMetadata("java-1.2.3", "1.2.3.4", "someIP"); + SplitHttpClient splitHttpClient = SplitFactoryImpl.buildSplitHttpClient("qwer", + splitClientConfig, + sdkmeta, + requestDecorator); + Assert.assertTrue(splitHttpClient instanceof SplitHttpClientKerberosImpl); + } + + @Test + public void testBuildOkHttpClient() { + SplitClientConfig splitClientConfig = SplitClientConfig.builder() + .setBlockUntilReadyTimeout(10000) + .authScheme(HttpAuthScheme.KERBEROS) + .kerberosPrincipalName("bilal@localhost") + .proxyPort(6060) + .proxyHost(ENDPOINT) + .build(); + HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(); + Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("host", 8080)); + OkHttpClient okHttpClient = SplitFactoryImpl.buildOkHttpClient(proxy, + splitClientConfig, loggingInterceptor, Authenticator.NONE); + assertEquals(Authenticator.NONE, okHttpClient.authenticator()); + assertEquals(proxy, okHttpClient.proxy()); + assertEquals(loggingInterceptor, okHttpClient.interceptors().get(0)); + } } \ No newline at end of file