44import static org .junit .Assert .assertNotNull ;
55import static org .junit .Assert .assertTrue ;
66
7+ import java .io .IOException ;
78import java .net .HttpURLConnection ;
89
910import org .junit .Test ;
1617import okhttp3 .Request ;
1718import okhttp3 .RequestBody ;
1819import okhttp3 .Response ;
20+ import okio .BufferedSink ;
1921
2022public class RetryHandlerTest {
2123
@@ -76,7 +78,7 @@ public void testRetryRequestWithMaxRetryAttempts() {
7678 // Default retry options with Number of maxretries default to 3
7779 RetryOptions retryOptions = new RetryOptions ();
7880 // Try to execute one more than allowed default max retries
79- int executionCount = RetryOptions .DEFAULT_MAX_RETRIES + 1 ;
81+ int executionCount = RetryOptions .DEFAULT_MAX_RETRIES + 1 ;
8082 assertFalse (retryhandler .retryRequest (response , executionCount , httpget , retryOptions ));
8183 }
8284
@@ -87,7 +89,7 @@ public void testRetryRequestForStatusCode() {
8789 Response response = new Response .Builder ()
8890 .protocol (Protocol .HTTP_1_1 )
8991 // For status code 500 which is not in (429 503 504), So NO retry
90- .code (HTTP_SERVER_ERROR )
92+ .code (HTTP_SERVER_ERROR )
9193 .message ( "Internal Server Error" )
9294 .request (httpget )
9395 .build ();
@@ -100,7 +102,7 @@ public void testRetryRequestWithTransferEncoding() {
100102 Request httppost = new Request .Builder ().url (testmeurl ).post (RequestBody .create (MediaType .parse ("application/json" ), "TEST" )).build ();
101103 Response response = new Response .Builder ()
102104 .protocol (Protocol .HTTP_1_1 )
103- .code (HttpURLConnection .HTTP_GATEWAY_TIMEOUT )
105+ .code (HttpURLConnection .HTTP_GATEWAY_TIMEOUT )
104106 .message ( "gateway timeout" )
105107 .request (httppost )
106108 .addHeader ("Transfer-Encoding" , "chunked" )
@@ -114,15 +116,15 @@ public void testRetryRequestWithExponentialBackOff() {
114116 Request httppost = new Request .Builder ().url (testmeurl ).post (RequestBody .create (MediaType .parse ("application/json" ), "TEST" )).build ();
115117 Response response = new Response .Builder ()
116118 .protocol (Protocol .HTTP_1_1 )
117- .code (HttpURLConnection .HTTP_GATEWAY_TIMEOUT )
119+ .code (HttpURLConnection .HTTP_GATEWAY_TIMEOUT )
118120 .message ( "gateway timeout" )
119121 .request (httppost )
120122 .addHeader ("Transfer-Encoding" , "chunked" )
121123 .build ();
122-
124+
123125 assertTrue (retryhandler .retryRequest (response , 1 , httppost , new RetryOptions ()));
124126 }
125-
127+
126128 @ Test
127129 public void testGetRetryAfterWithHeader () {
128130 RetryHandler retryHandler = new RetryHandler ();
@@ -131,7 +133,7 @@ public void testGetRetryAfterWithHeader() {
131133 delay = retryHandler .getRetryAfter (TestResponse ().newBuilder ().addHeader ("Retry-After" , "1" ).build (), 2 , 3 );
132134 assertTrue (delay == 1000 );
133135 }
134-
136+
135137 @ Test
136138 public void testGetRetryAfterOnFirstExecution () {
137139 RetryHandler retryHandler = new RetryHandler ();
@@ -140,14 +142,48 @@ public void testGetRetryAfterOnFirstExecution() {
140142 delay = retryHandler .getRetryAfter (TestResponse (), 3 , 2 );
141143 assertTrue (delay > 3100 );
142144 }
143-
145+
144146 @ Test
145147 public void testGetRetryAfterMaxExceed () {
146148 RetryHandler retryHandler = new RetryHandler ();
147149 long delay = retryHandler .getRetryAfter (TestResponse (), 190 , 1 );
148150 assertTrue (delay == 180000 );
149151 }
150-
152+ @ Test
153+ public void testIsBuffered () {
154+ final RetryHandler retryHandler = new RetryHandler ();
155+ Request request = new Request .Builder ().url ("https://localhost" ).method ("GET" , null ).build ();
156+ assertTrue ("Get Request is buffered" , retryHandler .isBuffered (request ));
157+
158+ request = new Request .Builder ().url ("https://localhost" ).method ("DELETE" , null ).build ();
159+ assertTrue ("Delete Request is buffered" , retryHandler .isBuffered (request ));
160+
161+ request = new Request .Builder ().url ("https://localhost" )
162+ .method ("POST" ,
163+ RequestBody .create (MediaType .parse ("application/json" ),
164+ "{\" key\" : 42 }" ))
165+ .build ();
166+ assertTrue ("Post Request is buffered" , retryHandler .isBuffered (request ));
167+
168+ request = new Request .Builder ().url ("https://localhost" )
169+ .method ("POST" ,
170+ new RequestBody () {
171+
172+ @ Override
173+ public MediaType contentType () {
174+ return MediaType .parse ("application/octet-stream" );
175+ }
176+
177+ @ Override
178+ public void writeTo (BufferedSink sink ) throws IOException {
179+ // TODO Auto-generated method stub
180+
181+ }
182+ })
183+ .build ();
184+ assertFalse ("Post Stream Request is not buffered" , retryHandler .isBuffered (request ));
185+ }
186+
151187 Response TestResponse () {
152188 return new Response .Builder ()
153189 .code (429 )
0 commit comments