OkHttpClientFactory { static OkHttpClient create(Logger logger) { OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder(); int readTimeoutSec = DEFAULT_READ_TIMEOUT_SEC; if (!System.getProperty(READ_TIMEOUT_SEC_PROPERTY, "").isEmpty()) { readTimeoutSec = Integer.parseInt(System.getProperty(READ_TIMEOUT_SEC_PROPERTY)); } okHttpClientBuilder.connectTimeout(CONNECT_TIMEOUT_MILLISECONDS, TimeUnit.MILLISECONDS); okHttpClientBuilder.readTimeout(readTimeoutSec, TimeUnit.SECONDS); okHttpClientBuilder.cookieJar(COOKIE_JAR); ConnectionSpec tls = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS) .allEnabledTlsVersions() .allEnabledCipherSuites() .supportsTlsExtensions(true) .build(); okHttpClientBuilder.connectionSpecs(asList(tls, ConnectionSpec.CLEARTEXT)); X509TrustManager systemDefaultTrustManager = systemDefaultTrustManager(); okHttpClientBuilder.sslSocketFactory(systemDefaultSslSocketFactory(systemDefaultTrustManager, logger), systemDefaultTrustManager); final String proxyUser = System.getProperty("http.proxyUser", ""); if (!System.getProperty("http.proxyHost", "").isEmpty() && !proxyUser.isEmpty()) { okHttpClientBuilder.proxyAuthenticator((route, response) -> { if (response.request().header(PROXY_AUTHORIZATION) != null) { return null; } if (HttpURLConnection.HTTP_PROXY_AUTH == response.code()) { String credential = Credentials.basic(proxyUser, System.getProperty("http.proxyPassword")); return response.request().newBuilder().header(PROXY_AUTHORIZATION, credential).build(); } return null; }); } return okHttpClientBuilder.build(); } private  OkHttpClientFactory();   }
@Test public void support_custom_timeouts() { int readTimeoutSec = 2000; System.setProperty(SONAR_WS_TIMEOUT, String.valueOf(readTimeoutSec)); Logger logger = mock(Logger.class); OkHttpClient underTest = OkHttpClientFactory.create(logger); assertThat(underTest.readTimeoutMillis()).isEqualTo(readTimeoutSec * 1000); }
@Test public void support_custom_timeouts_throws_exception_on_non_number() { System.setProperty(SONAR_WS_TIMEOUT, "fail"); Logger logger = mock(Logger.class); assertThatThrownBy(() -> OkHttpClientFactory.create(logger)).isInstanceOf(NumberFormatException.class); }