1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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")
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 }