diff options
Diffstat (limited to 'eaaf_core_utils/src/test/java')
-rw-r--r-- | eaaf_core_utils/src/test/java/at/gv/egiz/eaaf/core/test/http/HttpClientFactoryTest.java | 185 |
1 files changed, 93 insertions, 92 deletions
diff --git a/eaaf_core_utils/src/test/java/at/gv/egiz/eaaf/core/test/http/HttpClientFactoryTest.java b/eaaf_core_utils/src/test/java/at/gv/egiz/eaaf/core/test/http/HttpClientFactoryTest.java index c35ef604..510c6e47 100644 --- a/eaaf_core_utils/src/test/java/at/gv/egiz/eaaf/core/test/http/HttpClientFactoryTest.java +++ b/eaaf_core_utils/src/test/java/at/gv/egiz/eaaf/core/test/http/HttpClientFactoryTest.java @@ -9,6 +9,12 @@ import java.security.KeyStoreException; import java.security.Provider; import java.security.cert.X509Certificate; +import at.gv.egiz.eaaf.core.exceptions.EaafException; +import at.gv.egiz.eaaf.core.impl.credential.EaafKeyStoreFactory; +import at.gv.egiz.eaaf.core.impl.data.Pair; +import at.gv.egiz.eaaf.core.impl.http.HttpClientConfiguration; +import at.gv.egiz.eaaf.core.impl.http.IHttpClientFactory; + import org.apache.commons.lang3.RandomStringUtils; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; @@ -25,11 +31,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import at.gv.egiz.eaaf.core.exceptions.EaafException; -import at.gv.egiz.eaaf.core.impl.credential.EaafKeyStoreFactory; -import at.gv.egiz.eaaf.core.impl.data.Pair; -import at.gv.egiz.eaaf.core.impl.http.HttpClientConfiguration; -import at.gv.egiz.eaaf.core.impl.http.IHttpClientFactory; import okhttp3.HttpUrl; import okhttp3.mockwebserver.MockResponse; import okhttp3.mockwebserver.MockWebServer; @@ -44,7 +45,7 @@ public class HttpClientFactoryTest { @Autowired private EaafKeyStoreFactory keyStoreFactory; @Autowired private IHttpClientFactory httpClientFactory; - + private MockWebServer mockWebServer = null; private HttpUrl mockServerUrl; @@ -54,7 +55,7 @@ public class HttpClientFactoryTest { */ @Before public void setup() { - + } /** @@ -87,26 +88,26 @@ public class HttpClientFactoryTest { } @Test - public void defaultHttpClientRetryOneTime() throws EaafException, InterruptedException, - ClientProtocolException, IOException { - CloseableHttpClient client = httpClientFactory.getHttpClient(); + public void defaultHttpClientRetryOneTime() throws EaafException, InterruptedException, + ClientProtocolException, IOException { + final CloseableHttpClient client = httpClientFactory.getHttpClient(); Assert.assertNotNull("No httpClient", client); - + mockWebServer = new MockWebServer(); mockServerUrl = mockWebServer.url("/sp/junit"); mockWebServer.enqueue(new MockResponse() .setSocketPolicy(SocketPolicy.NO_RESPONSE) - .setResponseCode(HttpURLConnection.HTTP_NO_CONTENT)); + .setResponseCode(HttpURLConnection.HTTP_NO_CONTENT)); mockWebServer.enqueue(new MockResponse().setResponseCode(200) .setBody("GetData")); //request webservice final HttpUriRequest httpGet1 = new HttpGet(mockServerUrl.url().toString()); - final CloseableHttpResponse httpResp1 = client.execute(httpGet1); + final CloseableHttpResponse httpResp1 = client.execute(httpGet1); Assert.assertEquals("http statusCode", 200, httpResp1.getStatusLine().getStatusCode()); - + } - + @Test public void getCustomClientsDefault() throws EaafException { final HttpClientConfiguration config = new HttpClientConfiguration("jUnit"); @@ -133,7 +134,7 @@ public class HttpClientFactoryTest { } @Test - public void getCustomClientBasicAuth() throws EaafException, ClientProtocolException, + public void getCustomClientBasicAuth() throws EaafException, ClientProtocolException, IOException, InterruptedException { final HttpClientConfiguration config = new HttpClientConfiguration("jUnit"); config.setAuthMode("password"); @@ -181,107 +182,107 @@ public class HttpClientFactoryTest { } @Test - public void httpPostRetryNotAllowed() throws EaafException, InterruptedException, + public void httpPostRetryNotAllowed() throws EaafException, InterruptedException, ClientProtocolException, IOException { - final HttpClientConfiguration config = + final HttpClientConfiguration config = new HttpClientConfiguration("jUnit_retry_" + RandomStringUtils.randomAlphabetic(3)); config.setHttpErrorRetryCount(2); config.setHttpErrorRetryPost(false); - - CloseableHttpClient client = httpClientFactory.getHttpClient(config); + + final CloseableHttpClient client = httpClientFactory.getHttpClient(config); Assert.assertNotNull("No httpClient", client); - - + + mockWebServer = new MockWebServer(); mockServerUrl = mockWebServer.url("/sp/junit"); mockWebServer.enqueue(new MockResponse() .setSocketPolicy(SocketPolicy.NO_RESPONSE) - .setResponseCode(HttpURLConnection.HTTP_NO_CONTENT)); + .setResponseCode(HttpURLConnection.HTTP_NO_CONTENT)); mockWebServer.enqueue(new MockResponse().setResponseCode(200) .setBody("GetData")); //request webservice final HttpUriRequest httpGet1 = new HttpPost(mockServerUrl.url().toString()); try { - client.execute(httpGet1); + client.execute(httpGet1); Assert.fail("HTTP POST retry not allowed"); - - } catch (SocketTimeoutException e) { + + } catch (final SocketTimeoutException e) { Assert.assertNotNull("No errorMsg", e.getMessage()); - + } - + } - + @Test - public void httpPostRetryOneTime() throws EaafException, InterruptedException, + public void httpPostRetryOneTime() throws EaafException, InterruptedException, ClientProtocolException, IOException { - final HttpClientConfiguration config = + final HttpClientConfiguration config = new HttpClientConfiguration("jUnit_retry_" + RandomStringUtils.randomAlphabetic(3)); config.setHttpErrorRetryCount(2); config.setHttpErrorRetryPost(true); - - CloseableHttpClient client = httpClientFactory.getHttpClient(config); + + final CloseableHttpClient client = httpClientFactory.getHttpClient(config); Assert.assertNotNull("No httpClient", client); - - + + mockWebServer = new MockWebServer(); mockServerUrl = mockWebServer.url("/sp/junit"); mockWebServer.enqueue(new MockResponse() .setSocketPolicy(SocketPolicy.NO_RESPONSE) - .setResponseCode(HttpURLConnection.HTTP_NO_CONTENT)); + .setResponseCode(HttpURLConnection.HTTP_NO_CONTENT)); mockWebServer.enqueue(new MockResponse().setResponseCode(200) .setBody("GetData")); //request webservice final HttpUriRequest httpGet1 = new HttpPost(mockServerUrl.url().toString()); - final CloseableHttpResponse httpResp1 = client.execute(httpGet1); + final CloseableHttpResponse httpResp1 = client.execute(httpGet1); Assert.assertEquals("http statusCode", 200, httpResp1.getStatusLine().getStatusCode()); - + } - + @Test - public void testHttpClientRetryOneTime() throws EaafException, InterruptedException, + public void testHttpClientRetryOneTime() throws EaafException, InterruptedException, ClientProtocolException, IOException { - final HttpClientConfiguration config = + final HttpClientConfiguration config = new HttpClientConfiguration("jUnit_retry_" + RandomStringUtils.randomAlphabetic(3)); config.setHttpErrorRetryCount(2); - - CloseableHttpClient client = httpClientFactory.getHttpClient(config); + + final CloseableHttpClient client = httpClientFactory.getHttpClient(config); Assert.assertNotNull("No httpClient", client); - - + + mockWebServer = new MockWebServer(); mockServerUrl = mockWebServer.url("/sp/junit"); mockWebServer.enqueue(new MockResponse() .setSocketPolicy(SocketPolicy.NO_RESPONSE) - .setResponseCode(HttpURLConnection.HTTP_NO_CONTENT)); + .setResponseCode(HttpURLConnection.HTTP_NO_CONTENT)); mockWebServer.enqueue(new MockResponse().setResponseCode(200) .setBody("GetData")); //request webservice final HttpUriRequest httpGet1 = new HttpGet(mockServerUrl.url().toString()); - final CloseableHttpResponse httpResp1 = client.execute(httpGet1); + final CloseableHttpResponse httpResp1 = client.execute(httpGet1); Assert.assertEquals("http statusCode", 200, httpResp1.getStatusLine().getStatusCode()); - + } - + @Test - public void testHttpClientRetryTwoTime() throws EaafException, InterruptedException, + public void testHttpClientRetryTwoTime() throws EaafException, InterruptedException, ClientProtocolException, IOException { - final HttpClientConfiguration config = + final HttpClientConfiguration config = new HttpClientConfiguration("jUnit_retry_" + RandomStringUtils.randomAlphabetic(3)); config.setHttpErrorRetryCount(2); - - CloseableHttpClient client = httpClientFactory.getHttpClient(config); + + final CloseableHttpClient client = httpClientFactory.getHttpClient(config); Assert.assertNotNull("No httpClient", client); - - + + mockWebServer = new MockWebServer(); mockServerUrl = mockWebServer.url("/sp/junit"); mockWebServer.enqueue(new MockResponse() .setSocketPolicy(SocketPolicy.NO_RESPONSE) - .setResponseCode(HttpURLConnection.HTTP_NO_CONTENT)); + .setResponseCode(HttpURLConnection.HTTP_NO_CONTENT)); mockWebServer.enqueue(new MockResponse() .setSocketPolicy(SocketPolicy.NO_RESPONSE) .setResponseCode(HttpURLConnection.HTTP_NO_CONTENT)); @@ -290,26 +291,26 @@ public class HttpClientFactoryTest { //request webservice final HttpUriRequest httpGet1 = new HttpGet(mockServerUrl.url().toString()); - final CloseableHttpResponse httpResp1 = client.execute(httpGet1); + final CloseableHttpResponse httpResp1 = client.execute(httpGet1); Assert.assertEquals("http statusCode", 200, httpResp1.getStatusLine().getStatusCode()); - + } - + @Test - public void testHttpClientRetryMaxReached() throws EaafException, InterruptedException, + public void testHttpClientRetryMaxReached() throws EaafException, InterruptedException, ClientProtocolException, IOException { - final HttpClientConfiguration config = + final HttpClientConfiguration config = new HttpClientConfiguration("jUnit_retry_" + RandomStringUtils.randomAlphabetic(3)); config.setHttpErrorRetryCount(2); - - CloseableHttpClient client = httpClientFactory.getHttpClient(config); + + final CloseableHttpClient client = httpClientFactory.getHttpClient(config); Assert.assertNotNull("No httpClient", client); - + mockWebServer = new MockWebServer(); mockServerUrl = mockWebServer.url("/sp/junit"); mockWebServer.enqueue(new MockResponse() .setSocketPolicy(SocketPolicy.NO_RESPONSE) - .setResponseCode(HttpURLConnection.HTTP_NO_CONTENT)); + .setResponseCode(HttpURLConnection.HTTP_NO_CONTENT)); mockWebServer.enqueue(new MockResponse() .setSocketPolicy(SocketPolicy.NO_RESPONSE) .setResponseCode(HttpURLConnection.HTTP_NO_CONTENT)); @@ -322,45 +323,45 @@ public class HttpClientFactoryTest { //request webservice final HttpUriRequest httpGet1 = new HttpGet(mockServerUrl.url().toString()); try { - client.execute(httpGet1); + client.execute(httpGet1); Assert.fail("Max retry failed"); - - } catch (SocketTimeoutException e) { + + } catch (final SocketTimeoutException e) { Assert.assertNotNull("No errorMsg", e.getMessage()); - - } + + } } - + @Test - public void testHttpClientNoRetry() throws EaafException, InterruptedException, + public void testHttpClientNoRetry() throws EaafException, InterruptedException, ClientProtocolException, IOException { - final HttpClientConfiguration config = + final HttpClientConfiguration config = new HttpClientConfiguration("jUnit_retry_" + RandomStringUtils.randomAlphabetic(3)); config.setHttpErrorRetryCount(0); - - CloseableHttpClient client = httpClientFactory.getHttpClient(config); + + final CloseableHttpClient client = httpClientFactory.getHttpClient(config); Assert.assertNotNull("No httpClient", client); - + mockWebServer = new MockWebServer(); mockServerUrl = mockWebServer.url("/sp/junit"); mockWebServer.enqueue(new MockResponse() .setSocketPolicy(SocketPolicy.NO_RESPONSE) - .setResponseCode(HttpURLConnection.HTTP_NO_CONTENT)); + .setResponseCode(HttpURLConnection.HTTP_NO_CONTENT)); mockWebServer.enqueue(new MockResponse().setResponseCode(200) .setBody("GetData")); //request webservice final HttpUriRequest httpGet1 = new HttpGet(mockServerUrl.url().toString()); try { - client.execute(httpGet1); + client.execute(httpGet1); Assert.fail("Max retry failed"); - - } catch (SocketTimeoutException e) { + + } catch (final SocketTimeoutException e) { Assert.assertNotNull("No errorMsg", e.getMessage()); - - } + + } } - + @Test public void getCustomClientBasicAuthNoPassword() throws EaafException { final HttpClientConfiguration config = new HttpClientConfiguration("jUnit"); @@ -488,7 +489,7 @@ public class HttpClientFactoryTest { } @Test - public void getCustomClientX509AuthWithWrongAlias() throws EaafException, KeyStoreException, + public void getCustomClientX509AuthWithWrongAlias() throws EaafException, KeyStoreException, ClientProtocolException, IOException { final HttpClientConfiguration config = new HttpClientConfiguration("jUnit"); config.setAuthMode("ssl"); @@ -533,7 +534,7 @@ public class HttpClientFactoryTest { Assert.assertEquals("http statusCode", 200, httpResp2.getStatusLine().getStatusCode()); } - + @Test public void getCustomClientX509AuthWithHsmFacade() throws EaafException, ClientProtocolException, IOException, KeyStoreException { @@ -543,26 +544,26 @@ public class HttpClientFactoryTest { clientConfig.setSslKeyAlias("authhandler-sign"); clientConfig.setDisableTlsHostCertificateValidation(true); - - + + final CloseableHttpClient client = httpClientFactory.getHttpClient(clientConfig); Assert.assertNotNull("httpClient", client); //set-up mock-up web-server with SSL client authentication final Pair<KeyStore, Provider> sslClientKeyStore = keyStoreFactory.buildNewKeyStore(clientConfig.getKeyStoreConfig()); - X509Certificate clientRootCert = (X509Certificate) sslClientKeyStore.getFirst() + final X509Certificate clientRootCert = (X509Certificate) sslClientKeyStore.getFirst() .getCertificateChain(clientConfig.getSslKeyAlias())[1]; - X509Certificate clientEeCert = (X509Certificate) sslClientKeyStore.getFirst() + final X509Certificate clientEeCert = (X509Certificate) sslClientKeyStore.getFirst() .getCertificateChain(clientConfig.getSslKeyAlias())[0]; - + final String localhost = InetAddress.getByName("localhost").getCanonicalHostName(); final HeldCertificate localhostCertificate = new HeldCertificate.Builder() .addSubjectAlternativeName(localhost) .build(); final HandshakeCertificates serverCertificates = new HandshakeCertificates.Builder() - .addTrustedCertificate(clientRootCert) .addTrustedCertificate(clientEeCert) + .addTrustedCertificate(clientRootCert) .heldCertificate(localhostCertificate) .build(); mockWebServer = new MockWebServer(); @@ -578,5 +579,5 @@ public class HttpClientFactoryTest { Assert.assertEquals("http statusCode", 200, httpResp2.getStatusLine().getStatusCode()); } - + } |