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.hc.client5.http.impl;
28  
29  import java.io.IOException;
30  import java.net.ConnectException;
31  import java.net.SocketTimeoutException;
32  import java.net.UnknownHostException;
33  import java.util.Date;
34  
35  import javax.net.ssl.SSLException;
36  
37  import org.apache.hc.client5.http.classic.methods.HttpGet;
38  import org.apache.hc.client5.http.utils.DateUtils;
39  import org.apache.hc.core5.http.ConnectionClosedException;
40  import org.apache.hc.core5.http.HttpHeaders;
41  import org.apache.hc.core5.http.HttpResponse;
42  import org.apache.hc.core5.http.message.BasicHttpResponse;
43  import org.apache.hc.core5.util.TimeValue;
44  import org.junit.Assert;
45  import org.junit.Before;
46  import org.junit.Test;
47  
48  public class TestDefaultHttpRequestRetryStrategy {
49  
50      private DefaultHttpRequestRetryStrategy retryStrategy;
51  
52      @Before
53      public void setup() {
54          this.retryStrategy = new DefaultHttpRequestRetryStrategy(3, TimeValue.ofMilliseconds(1234L));
55      }
56  
57      @Test
58      public void testBasics() throws Exception {
59          final HttpResponse response1 = new BasicHttpResponse(503, "Oopsie");
60          Assert.assertTrue(this.retryStrategy.retryRequest(response1, 1, null));
61          Assert.assertTrue(this.retryStrategy.retryRequest(response1, 2, null));
62          Assert.assertTrue(this.retryStrategy.retryRequest(response1, 3, null));
63          Assert.assertFalse(this.retryStrategy.retryRequest(response1, 4, null));
64          final HttpResponse response2 = new BasicHttpResponse(500, "Big Time Oopsie");
65          Assert.assertFalse(this.retryStrategy.retryRequest(response2, 1, null));
66          final HttpResponse response3 = new BasicHttpResponse(429, "Oopsie");
67          Assert.assertTrue(this.retryStrategy.retryRequest(response3, 1, null));
68          Assert.assertTrue(this.retryStrategy.retryRequest(response3, 2, null));
69          Assert.assertTrue(this.retryStrategy.retryRequest(response3, 3, null));
70          Assert.assertFalse(this.retryStrategy.retryRequest(response3, 4, null));
71  
72          Assert.assertEquals(TimeValue.ofMilliseconds(1234L), this.retryStrategy.getRetryInterval(response1, 1, null));
73      }
74  
75      @Test
76      public void testRetryAfterHeaderAsLong() throws Exception {
77          final HttpResponse response = new BasicHttpResponse(503, "Oopsie");
78          response.setHeader(HttpHeaders.RETRY_AFTER, "321");
79  
80          Assert.assertEquals(TimeValue.ofSeconds(321L), this.retryStrategy.getRetryInterval(response, 3, null));
81      }
82  
83      @Test
84      public void testRetryAfterHeaderAsDate() throws Exception {
85          this.retryStrategy = new DefaultHttpRequestRetryStrategy(3, TimeValue.ZERO_MILLISECONDS);
86          final HttpResponse response = new BasicHttpResponse(503, "Oopsie");
87          response.setHeader(HttpHeaders.RETRY_AFTER, DateUtils.formatDate(new Date(System.currentTimeMillis() + 100000L)));
88  
89          Assert.assertTrue(this.retryStrategy.getRetryInterval(response, 3, null).compareTo(TimeValue.ZERO_MILLISECONDS) > 0);
90      }
91  
92      @Test
93      public void testRetryAfterHeaderAsPastDate() throws Exception {
94          final HttpResponse response = new BasicHttpResponse(503, "Oopsie");
95          response.setHeader(HttpHeaders.RETRY_AFTER, DateUtils.formatDate(new Date(System.currentTimeMillis() - 100000L)));
96  
97          Assert.assertEquals(TimeValue.ofMilliseconds(1234L), this.retryStrategy.getRetryInterval(response, 3, null));
98      }
99  
100     @Test
101     public void testInvalidRetryAfterHeader() throws Exception {
102         final HttpResponse response = new BasicHttpResponse(503, "Oopsie");
103         response.setHeader(HttpHeaders.RETRY_AFTER, "Stuff");
104 
105         Assert.assertEquals(TimeValue.ofMilliseconds(1234L), retryStrategy.getRetryInterval(response, 3, null));
106     }
107 
108     @Test
109     public void noRetryOnConnectTimeout() throws Exception {
110         final HttpGet request = new HttpGet("/");
111 
112         Assert.assertFalse(retryStrategy.retryRequest(request, new SocketTimeoutException(), 1, null));
113     }
114 
115     @Test
116     public void noRetryOnConnect() throws Exception {
117         final HttpGet request = new HttpGet("/");
118 
119         Assert.assertFalse(retryStrategy.retryRequest(request, new ConnectException(), 1, null));
120     }
121 
122     @Test
123     public void noRetryOnConnectionClosed() throws Exception {
124         final HttpGet request = new HttpGet("/");
125 
126         Assert.assertFalse(retryStrategy.retryRequest(request, new ConnectionClosedException(), 1, null));
127     }
128 
129     @Test
130     public void noRetryOnSSLFailure() throws Exception {
131         final HttpGet request = new HttpGet("/");
132 
133         Assert.assertFalse(retryStrategy.retryRequest(request, new SSLException("encryption failed"), 1, null));
134     }
135 
136     @Test
137     public void noRetryOnUnknownHost() throws Exception {
138         final HttpGet request = new HttpGet("/");
139 
140         Assert.assertFalse(retryStrategy.retryRequest(request, new UnknownHostException(), 1, null));
141     }
142 
143     @Test
144     public void noRetryOnAbortedRequests() throws Exception {
145         final HttpGet request = new HttpGet("/");
146         request.cancel();
147 
148         Assert.assertFalse(retryStrategy.retryRequest(request, new IOException(), 1, null));
149     }
150 
151     @Test
152     public void retryOnNonAbortedRequests() throws Exception {
153         final HttpGet request = new HttpGet("/");
154 
155         Assert.assertTrue(retryStrategy.retryRequest(request, new IOException(), 1, null));
156     }
157 
158 }