This issue was first discovered with circleci but the SSLHandShakeException is not reproduce in dev environment with my Mac. The detail error page is here. This post provides a verified way to bypass all hostname verification (or you can add specified conditions inside) for non-security test case to apply on Web API testing.
Above picture shows the exception details. Generally a RESTful WebAPI test could be constructed with OkHttpClient lib:
@Before publicvoidsetUp()throws Exception { client = new OkHttpClient(); }
JSONObject jsonResp = new JSONObject(response.body().string()); String ip = jsonResp.getString("ip"); Assert.assertTrue(checkIpv4(ip)); }
Solution
It is usually caused by a self-signed certificate or an unknown certificate signer. The official solution plan is to download the SSL certificate and install it to Java SSL keystore. However, for a quick fix to test environment only concerning about the function instead of security perspective here (suggest to keep security part in a seperate test set). If we would like to bypass all hostname verification, here is the solution with a customized hostverifier. With Java8 we can use lambda to replace anonymous class when the interface is a functional one.
@Before publicvoidsetUp()throws Exception { //Changed to customized SSL context and hostVerifier. OkHttpClient.Builder clientBuilder = new OkHttpClient().newBuilder(); X509TrustManager trustManager = new X509TrustManager() { @Override public X509Certificate[] getAcceptedIssuers() { returnnew X509Certificate[0]; }
@Override publicvoidcheckServerTrusted(final X509Certificate[] chain, final String authType)throws CertificateException { }
@Override publicvoidcheckClientTrusted(final X509Certificate[] chain, final String authType)throws CertificateException { } }; final TrustManager[] trustAllCerts = new TrustManager[]{ trustManager };