From ff340b3aec5193b066b3cf6ee6cbc9c542de48df Mon Sep 17 00:00:00 2001 From: Thomas <> Date: Wed, 3 Apr 2024 18:40:59 +0200 Subject: fix(http): include retry on HTTP POST requests Reason: that feature looks be removed during refactoring from Apache HTTP Client v4.x to 5.x --- .../eaaf/core/test/http/HttpClientFactoryTest.java | 94 ++++++++++++++-------- 1 file changed, 62 insertions(+), 32 deletions(-) (limited to 'eaaf_core_utils/src/test/java/at') 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 243205c9..c566380e 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 @@ -393,38 +393,6 @@ public class HttpClientFactoryTest { } } - @Test - public void httpPostRetryNotAllowed() throws EaafException, InterruptedException, - ClientProtocolException, IOException { - final HttpClientConfiguration config = - new HttpClientConfiguration("jUnit_retry_" + RandomStringUtils.randomAlphabetic(3)); - config.setHttpErrorRetryCount(2); - - 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)); - mockWebServer.enqueue(new MockResponse().setResponseCode(200) - .setBody("GetData")); - - //request webservice - final HttpUriRequest httpGet1 = new HttpPost(mockServerUrl.url().toString()); - try { - client.execute(httpGet1); - Assert.fail("HTTP POST retry not allowed"); - - } catch (final SocketTimeoutException e) { - Assert.assertNotNull("No errorMsg", e.getMessage()); - - } - - } - @Test public void testHttpClientRetryOneTime() throws EaafException, InterruptedException, ClientProtocolException, IOException { @@ -551,6 +519,68 @@ public class HttpClientFactoryTest { } } + @Test + public void testHttpClientRetryOneTimePost() throws EaafException, InterruptedException, + ClientProtocolException, IOException { + final HttpClientConfiguration config = + new HttpClientConfiguration("jUnit_retry_" + RandomStringUtils.randomAlphabetic(3)); + config.setHttpErrorRetryCount(2); + config.setHttpPostRetryAllowed(true); + + 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)); + + String bodyData = RandomStringUtils.randomAlphanumeric(10); + mockWebServer.enqueue(new MockResponse().setResponseCode(200) + .setBody(bodyData)); + + // request webservice + final HttpUriRequest httpGet1 = new HttpPost(mockServerUrl.url().toString()); + final Triple httpResp1 = client.execute(httpGet1, + HttpUtils.bodyStatusCodeResponseHandler()); + Assert.assertEquals("http statusCode", 200, httpResp1.getFirst().getStatusCode()); + Assert.assertEquals("http statusCode", bodyData, new String(StreamUtils.readStream(httpResp1 + .getSecond()))); + + } + + @Test + public void httpPostRetryNotAllowed() throws EaafException, InterruptedException, + ClientProtocolException, IOException { + final HttpClientConfiguration config = + new HttpClientConfiguration("jUnit_retry_" + RandomStringUtils.randomAlphabetic(3)); + config.setHttpErrorRetryCount(2); + config.setHttpPostRetryAllowed(false); + + 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)); + mockWebServer.enqueue(new MockResponse().setResponseCode(200) + .setBody("GetData")); + + // request webservices + final HttpUriRequest httpGet1 = new HttpPost(mockServerUrl.url().toString()); + try { + client.execute(httpGet1); + Assert.fail("HTTP POST retry not allowed"); + + } catch (final SocketTimeoutException e) { + Assert.assertNotNull("No errorMsg", e.getMessage()); + + } + } + @Test public void getCustomClientBasicAuthNoPassword() throws EaafException { final HttpClientConfiguration config = new HttpClientConfiguration("jUnit"); -- cgit v1.2.3