From f0618b0484569752de96ea071551d00869146b92 Mon Sep 17 00:00:00 2001 From: Jaikiran Pai Date: Sun, 22 Aug 2021 19:17:57 +0530 Subject: [PATCH] Don't retry a request for NoRouteToHostException --- .../client/DefaultHttpRequestRetryHandler.java | 2 ++ .../TestDefaultHttpRequestRetryHandler.java | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/httpclient/src/main/java/org/apache/http/impl/client/DefaultHttpRequestRetryHandler.java b/httpclient/src/main/java/org/apache/http/impl/client/DefaultHttpRequestRetryHandler.java index 7ac28908c..a9495d900 100644 --- a/httpclient/src/main/java/org/apache/http/impl/client/DefaultHttpRequestRetryHandler.java +++ b/httpclient/src/main/java/org/apache/http/impl/client/DefaultHttpRequestRetryHandler.java @@ -30,6 +30,7 @@ package org.apache.http.impl.client; import java.io.IOException; import java.io.InterruptedIOException; import java.net.ConnectException; +import java.net.NoRouteToHostException; import java.net.UnknownHostException; import java.util.Arrays; import java.util.Collection; @@ -103,6 +104,7 @@ public class DefaultHttpRequestRetryHandler implements HttpRequestRetryHandler { InterruptedIOException.class, UnknownHostException.class, ConnectException.class, + NoRouteToHostException.class, SSLException.class)); } diff --git a/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultHttpRequestRetryHandler.java b/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultHttpRequestRetryHandler.java index baec3be13..59092e0b0 100644 --- a/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultHttpRequestRetryHandler.java +++ b/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultHttpRequestRetryHandler.java @@ -30,6 +30,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.io.IOException; +import java.net.NoRouteToHostException; import java.net.UnknownHostException; import org.apache.http.client.methods.HttpUriRequest; @@ -111,4 +112,21 @@ public class TestDefaultHttpRequestRetryHandler { Assert.assertFalse(retryHandler.retryRequest(new ConnectTimeoutException(),3,context)); } + /** + * Test that {@link DefaultHttpRequestRetryHandler} doesn't retry a request + * when {@link java.net.NoRouteToHostException} is thrown when establishing + * a connection + */ + @Test + public void noRetryForNoRouteToHostException() { + final HttpContext context = mock(HttpContext.class); + final HttpUriRequest request = mock(HttpUriRequest.class); + when(request.isAborted()).thenReturn(false); + when(context.getAttribute(HttpCoreContext.HTTP_REQUEST)).thenReturn(request); + + final DefaultHttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler(); + Assert.assertFalse("Request was unexpectedly retried for " + + NoRouteToHostException.class.getName() + " exception", + retryHandler.retryRequest(new NoRouteToHostException(), 1, context)); + } }