问题描述
我正在尝试测试 ClientFactory,它在 demond 上创建 OkHttpClient。要创建客户端,工厂需要文件,其中包含密钥库和信任库。 如果我没有任何信任库和密钥库来提供它,我该如何测试它?以及它创建的测试客户端(客户端应该通过 http/2 和 TLSv1.3 发送请求,并接收响应)?
该工厂还创建了一些默认客户端,我使用 MockWebServer
对其进行了测试。
这是工厂的样子:
public class HttpClientFactory {
private static final Class<?> thisClass = HttpClientFactory.class;
private HttpClientFactory() {}
public static OkHttpClient createClient(String clientType,String configurationFilePath) {
OkHttpClient.Builder clientBuilder;
File file = new File(configurationFilePath);
if (!file.exists()) {
LogUtils.warn(thisClass,"Configuration file is not found.");
return null;
}
try {
PropertiesConfiguration config = new PropertiesConfiguration();
config.setLogger(null);
config.load(file);
config.setFile(file);
String keyStorePassword = (String)config.getProperty(clientType + ".ssl.key-store-password");
String keyStoreType = (String)config.getProperty(clientType + ".ssl.key-store-type");
String keyStorePath = (String)config.getProperty(clientType + ".ssl.key-store");
String trustStorePassword = (String)config.getProperty(clientType + ".ssl.key-store-password");
String trustStorePath = (String)config.getProperty(clientType + ".ssl.trust-store");
String tlsversion = (String)config.getProperty(clientType + ".ssl.protocols");
KeyManager[] keyManagers = getKeyManagers(keyStoreType,keyStorePath,keyStorePassword); //my function that creates KeyManager
TrustManager[] trustManagers = getTrustManagers(keyStoreType,trustStorePath,trustStorePassword); //my function that creates TrustManager
SSLContext sslContext = getSSLContext(keyManagers,trustManagers,tlsversion); //my function that creates SSLContext
clientBuilder = new OkHttpClient.Builder()
.sslSocketFactory(sslContext.getSocketFactory(),(x509trustmanager)trustManagers[0]);
} catch (ConfigurationException e) {
LogUtils.error(thisClass,"Error: Failed to load properties from snef.properties",e);
return null;
} catch (Exception e) {
LogUtils.error(thisClass,"Error: " + e.getMessage());
return null;
}
return clientBuilder.build();
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)