View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.impl.client;
28  
29  import static org.mockito.Mockito.mock;
30  import static org.mockito.Mockito.when;
31  
32  import java.io.IOException;
33  import java.net.NoRouteToHostException;
34  import java.net.UnknownHostException;
35  
36  import org.apache.http.client.methods.HttpUriRequest;
37  import org.apache.http.conn.ConnectTimeoutException;
38  import org.apache.http.protocol.HttpContext;
39  import org.apache.http.protocol.HttpCoreContext;
40  import org.junit.Assert;
41  import org.junit.Test;
42  
43  
44  @SuppressWarnings("boxing") // test class
45  public class TestDefaultHttpRequestRetryHandler {
46  
47      @Test
48      public void noRetryOnConnectTimeout() throws Exception {
49          final HttpContext context = mock(HttpContext.class);
50          final HttpUriRequest request = mock(HttpUriRequest.class);
51  
52          final DefaultHttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler();
53          Assert.assertEquals(3, retryHandler.getRetryCount());
54  
55          when(request.isAborted()).thenReturn(Boolean.FALSE);
56          when(context.getAttribute(HttpCoreContext.HTTP_REQUEST)).thenReturn(request);
57  
58          Assert.assertFalse(retryHandler.retryRequest(new ConnectTimeoutException(), 1, context));
59      }
60  
61      @Test
62      public void noRetryOnUnknownHost() throws Exception {
63          final HttpContext context = mock(HttpContext.class);
64          final HttpUriRequest request = mock(HttpUriRequest.class);
65  
66          final DefaultHttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler();
67  
68          when(request.isAborted()).thenReturn(Boolean.FALSE);
69          when(context.getAttribute(HttpCoreContext.HTTP_REQUEST)).thenReturn(request);
70  
71          Assert.assertFalse(retryHandler.retryRequest(new UnknownHostException(), 1, context));
72      }
73  
74      @Test
75      public void noRetryOnAbortedRequests() throws Exception{
76          final HttpContext context = mock(HttpContext.class);
77          final HttpUriRequest request = mock(HttpUriRequest.class);
78  
79          final DefaultHttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler();
80  
81          when(request.isAborted()).thenReturn(Boolean.TRUE);
82          when(context.getAttribute(HttpCoreContext.HTTP_REQUEST)).thenReturn(request);
83  
84          Assert.assertFalse(retryHandler.retryRequest(new IOException(),3,context));
85      }
86  
87      @Test
88      public void retryOnNonAbortedRequests() throws Exception{
89  
90          final HttpContext context = mock(HttpContext.class);
91          final HttpUriRequest request = mock(HttpUriRequest.class);
92  
93          final DefaultHttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler();
94  
95          when(request.isAborted()).thenReturn(Boolean.FALSE);
96          when(context.getAttribute(HttpCoreContext.HTTP_REQUEST)).thenReturn(request);
97  
98          Assert.assertTrue(retryHandler.retryRequest(new IOException(),3,context));
99      }
100 
101     @Test
102     public void noRetryOnConnectionTimeout() throws Exception{
103 
104         final HttpContext context = mock(HttpContext.class);
105         final HttpUriRequest request = mock(HttpUriRequest.class);
106 
107         final DefaultHttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler();
108 
109         when(request.isAborted()).thenReturn(false);
110         when(context.getAttribute(HttpCoreContext.HTTP_REQUEST)).thenReturn(request);
111 
112         Assert.assertFalse(retryHandler.retryRequest(new ConnectTimeoutException(),3,context));
113     }
114 
115     /**
116      * Test that {@link DefaultHttpRequestRetryHandler} doesn't retry a request
117      * when {@link java.net.NoRouteToHostException} is thrown when establishing
118      * a connection
119      */
120     @Test
121     public void noRetryForNoRouteToHostException() {
122         final HttpContext context = mock(HttpContext.class);
123         final HttpUriRequest request = mock(HttpUriRequest.class);
124         when(request.isAborted()).thenReturn(false);
125         when(context.getAttribute(HttpCoreContext.HTTP_REQUEST)).thenReturn(request);
126 
127         final DefaultHttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler();
128         Assert.assertFalse("Request was unexpectedly retried for "
129                         + NoRouteToHostException.class.getName() + " exception",
130                 retryHandler.retryRequest(new NoRouteToHostException(), 1, context));
131     }
132 }