1414import org .apache .http .client .protocol .HttpClientContext ;
1515import org .apache .http .entity .StringEntity ;
1616import org .apache .http .message .BasicHttpResponse ;
17+ import org .apache .http .protocol .HttpContext ;
1718import org .apache .http .protocol .HttpCoreContext ;
1819import org .junit .Test ;
1920
21+ import com .microsoft .graph .httpcore .middlewareoption .IShouldRetry ;
22+ import com .microsoft .graph .httpcore .middlewareoption .RetryOptions ;
23+
2024public class RetryHandlerTest {
2125
2226 int maxRetries = 2 ;
23- int retryInterval = 2000 ;
27+ int retryInterval = 1000 ;
2428 String testurl = "https://graph.microsoft.com/v1.0/" ;
2529
2630 @ Test
2731 public void testRetryHandlerCreation () {
28- RetryHandler retryhandler = new RetryHandler (maxRetries , retryInterval );
32+ RetryHandler retryhandler = new RetryHandler ();
2933 assertTrue (retryhandler .getRetryInterval () == retryInterval );
3034 }
3135
36+ @ Test
37+ public void testRetryHandlerWithRetryOptions () {
38+ RetryOptions option = new RetryOptions ();
39+ RetryHandler retryhandler = new RetryHandler (option );
40+ HttpResponse response = new BasicHttpResponse (HttpVersion .HTTP_1_1 , HttpStatus .SC_GATEWAY_TIMEOUT , "Gateway Timeout" );
41+ HttpClientContext localContext = HttpClientContext .create ();
42+ assertTrue (retryhandler .retryRequest (response , 1 , localContext ));
43+ }
44+
45+ @ Test
46+ public void testRetryHandlerWithCustomRetryOptions () {
47+ RetryOptions option = new RetryOptions (new IShouldRetry () {
48+ public boolean shouldRetry (HttpResponse response , int executionCount , HttpContext context ) {
49+ return false ;
50+ }
51+ });
52+ RetryHandler retryhandler = new RetryHandler (option );
53+ HttpResponse response = new BasicHttpResponse (HttpVersion .HTTP_1_1 , HttpStatus .SC_GATEWAY_TIMEOUT , "Gateway Timeout" );
54+ HttpClientContext localContext = HttpClientContext .create ();
55+ assertTrue (!retryhandler .retryRequest (response , 1 , localContext ));
56+ }
57+
3258 @ Test
3359 public void testRetryRequestWithMaxRetryAttempts () {
34- RetryHandler retryhandler = new RetryHandler (maxRetries , retryInterval );
60+ RetryHandler retryhandler = new RetryHandler ();
3561 HttpResponse response = new BasicHttpResponse (HttpVersion .HTTP_1_1 , HttpStatus .SC_GATEWAY_TIMEOUT , "Gateway Timeout" );
3662 HttpClientContext localContext = HttpClientContext .create ();
3763 assertFalse (retryhandler .retryRequest (response , 3 , localContext ));
3864 }
3965
4066 @ Test
4167 public void testRetryRequestForStatusCode () {
42- RetryHandler retryhandler = new RetryHandler (maxRetries , retryInterval );
68+ RetryHandler retryhandler = new RetryHandler ();
4369 HttpResponse response = new BasicHttpResponse (HttpVersion .HTTP_1_1 , HttpStatus .SC_INTERNAL_SERVER_ERROR , "Internal Server Error" );
4470 HttpClientContext localContext = HttpClientContext .create ();
4571 assertFalse (retryhandler .retryRequest (response , 1 , localContext ));
4672 }
4773
4874 @ Test
4975 public void testRetryRequestWithTransferEncoding () {
50- RetryHandler retryhandler = new RetryHandler (maxRetries , retryInterval );
76+ RetryHandler retryhandler = new RetryHandler ();
5177 HttpResponse response = new BasicHttpResponse (HttpVersion .HTTP_1_1 , HttpStatus .SC_GATEWAY_TIMEOUT , "Internal Server Error" );
5278 response .setHeader ("Transfer-Encoding" , "chunked" );
5379 HttpPost httppost = new HttpPost (testurl );
@@ -67,7 +93,7 @@ public void testRetryRequestWithTransferEncoding() {
6793
6894 @ Test
6995 public void testRetryRequestWithExponentialBackOff () {
70- RetryHandler retryhandler = new RetryHandler (maxRetries , retryInterval );
96+ RetryHandler retryhandler = new RetryHandler ();
7197 HttpResponse response = new BasicHttpResponse (HttpVersion .HTTP_1_1 , HttpStatus .SC_GATEWAY_TIMEOUT , "Internal Server Error" );
7298 HttpPost httppost = new HttpPost (testurl );
7399
@@ -87,7 +113,7 @@ public void testRetryRequestWithExponentialBackOff() {
87113
88114 @ Test
89115 public void testRetryHandlerRetryRequestWithRetryAfterHeader () {
90- RetryHandler retryhandler = new RetryHandler (maxRetries , retryInterval );
116+ RetryHandler retryhandler = new RetryHandler ();
91117 HttpResponse response = new BasicHttpResponse (HttpVersion .HTTP_1_1 , HttpStatus .SC_GATEWAY_TIMEOUT , "Internal Server Error" );
92118 response .setHeader ("Retry-After" , "100" );
93119 HttpPost httppost = new HttpPost (testurl );
0 commit comments