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.UnknownHostException;
34  
35  import org.apache.http.client.methods.HttpUriRequest;
36  import org.apache.http.conn.ConnectTimeoutException;
37  import org.apache.http.protocol.HttpContext;
38  import org.apache.http.protocol.HttpCoreContext;
39  import org.junit.Assert;
40  import org.junit.Test;
41  
42  
43  @SuppressWarnings("boxing") // test class
44  public class TestDefaultHttpRequestRetryHandler {
45  
46      @Test
47      public void noRetryOnConnectTimeout() throws Exception {
48          final HttpContext context = mock(HttpContext.class);
49          final HttpUriRequest request = mock(HttpUriRequest.class);
50  
51          final DefaultHttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler();
52          Assert.assertEquals(3, retryHandler.getRetryCount());
53  
54          when(request.isAborted()).thenReturn(Boolean.FALSE);
55          when(context.getAttribute(HttpCoreContext.HTTP_REQUEST)).thenReturn(request);
56  
57          Assert.assertFalse(retryHandler.retryRequest(new ConnectTimeoutException(), 1, context));
58      }
59  
60      @Test
61      public void noRetryOnUnknownHost() throws Exception {
62          final HttpContext context = mock(HttpContext.class);
63          final HttpUriRequest request = mock(HttpUriRequest.class);
64  
65          final DefaultHttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler();
66  
67          when(request.isAborted()).thenReturn(Boolean.FALSE);
68          when(context.getAttribute(HttpCoreContext.HTTP_REQUEST)).thenReturn(request);
69  
70          Assert.assertFalse(retryHandler.retryRequest(new UnknownHostException(), 1, context));
71      }
72  
73      @Test
74      public void noRetryOnAbortedRequests() throws Exception{
75          final HttpContext context = mock(HttpContext.class);
76          final HttpUriRequest request = mock(HttpUriRequest.class);
77  
78          final DefaultHttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler();
79  
80          when(request.isAborted()).thenReturn(Boolean.TRUE);
81          when(context.getAttribute(HttpCoreContext.HTTP_REQUEST)).thenReturn(request);
82  
83          Assert.assertFalse(retryHandler.retryRequest(new IOException(),3,context));
84      }
85  
86      @Test
87      public void retryOnNonAbortedRequests() throws Exception{
88  
89          final HttpContext context = mock(HttpContext.class);
90          final HttpUriRequest request = mock(HttpUriRequest.class);
91  
92          final DefaultHttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler();
93  
94          when(request.isAborted()).thenReturn(Boolean.FALSE);
95          when(context.getAttribute(HttpCoreContext.HTTP_REQUEST)).thenReturn(request);
96  
97          Assert.assertTrue(retryHandler.retryRequest(new IOException(),3,context));
98      }
99  
100     @Test
101     public void noRetryOnConnectionTimeout() throws Exception{
102 
103         final HttpContext context = mock(HttpContext.class);
104         final HttpUriRequest request = mock(HttpUriRequest.class);
105 
106         final DefaultHttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler();
107 
108         when(request.isAborted()).thenReturn(false);
109         when(context.getAttribute(HttpCoreContext.HTTP_REQUEST)).thenReturn(request);
110 
111         Assert.assertFalse(retryHandler.retryRequest(new ConnectTimeoutException(),3,context));
112     }
113 
114 }