Skip to content

Commit 6d496db

Browse files
committed
Merge branch 'dev' into feature/v2
2 parents 1c84478 + 81079a5 commit 6d496db

File tree

2 files changed

+49
-33
lines changed

2 files changed

+49
-33
lines changed

src/main/java/com/microsoft/graph/httpcore/RetryHandler.java

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import okhttp3.Interceptor;
1717
import okhttp3.Request;
18+
import okhttp3.RequestBody;
1819
import okhttp3.Response;
1920
import okhttp3.ResponseBody;
2021

@@ -38,22 +39,6 @@ public class RetryHandler implements Interceptor{
3839
* Header name for the retry after information
3940
*/
4041
private static final String RETRY_AFTER = "Retry-After";
41-
/**
42-
* Header name for the transfer information
43-
*/
44-
private static final String TRANSFER_ENCODING = "Transfer-Encoding";
45-
/**
46-
* Chunked encoding header value
47-
*/
48-
private static final String TRANSFER_ENCODING_CHUNKED = "chunked";
49-
/**
50-
* Binary content type header value
51-
*/
52-
private static final String APPLICATION_OCTET_STREAM = "application/octet-stream";
53-
/**
54-
* Header name for the content type
55-
*/
56-
private static final String CONTENT_TYPE = "Content-Type";
5742

5843
/**
5944
* Too many requests status code
@@ -117,7 +102,7 @@ boolean retryRequest(Response response, int executionCount, Request request, Ret
117102
shouldRetry =
118103
retryOptions != null
119104
&& executionCount <= retryOptions.maxRetries()
120-
&& checkStatus(statusCode) && isBuffered(response, request)
105+
&& checkStatus(statusCode) && isBuffered(request)
121106
&& shouldRetryCallback != null
122107
&& shouldRetryCallback.shouldRetry(retryOptions.delay(), executionCount, request, response);
123108

@@ -157,28 +142,23 @@ boolean checkStatus(int statusCode) {
157142
|| statusCode == MSClientErrorCodeGatewayTimeout);
158143
}
159144

160-
boolean isBuffered(final Response response, final Request request) {
161-
String methodName = request.method();
162-
if(methodName.equalsIgnoreCase("GET") || methodName.equalsIgnoreCase("DELETE") || methodName.equalsIgnoreCase("HEAD") || methodName.equalsIgnoreCase("OPTIONS"))
163-
return true;
145+
boolean isBuffered(final Request request) {
146+
final String methodName = request.method();
164147

165-
boolean isHTTPMethodPutPatchOrPost = methodName.equalsIgnoreCase("POST") ||
148+
final boolean isHTTPMethodPutPatchOrPost = methodName.equalsIgnoreCase("POST") ||
166149
methodName.equalsIgnoreCase("PUT") ||
167150
methodName.equalsIgnoreCase("PATCH");
168151

169-
if(isHTTPMethodPutPatchOrPost && response != null) {
170-
final String contentTypeHeaderValue = response.header(CONTENT_TYPE);
171-
final boolean isStream = contentTypeHeaderValue!=null && contentTypeHeaderValue.equalsIgnoreCase(APPLICATION_OCTET_STREAM);
172-
if(!isStream) {
173-
final String transferEncoding = response.header(TRANSFER_ENCODING);
174-
final boolean isTransferEncodingChunked = (transferEncoding != null) &&
175-
transferEncoding.equalsIgnoreCase(TRANSFER_ENCODING_CHUNKED);
176-
177-
if(request.body() != null && isTransferEncodingChunked)
178-
return true;
152+
final RequestBody requestBody = request.body();
153+
if(isHTTPMethodPutPatchOrPost && requestBody != null) {
154+
try {
155+
return requestBody.contentLength() != -1L;
156+
} catch (IOException ex) {
157+
// expected
158+
return false;
179159
}
180160
}
181-
return false;
161+
return true;
182162
}
183163

184164
@Override

src/test/java/com/microsoft/graph/httpcore/RetryHandlerTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import static org.junit.jupiter.api.Assertions.assertTrue;
77
import static org.mockito.Mockito.mock;
88

9+
import java.io.IOException;
910
import java.net.HttpURLConnection;
1011

1112
import org.junit.jupiter.api.Test;
@@ -18,6 +19,7 @@
1819
import okhttp3.Request;
1920
import okhttp3.RequestBody;
2021
import okhttp3.Response;
22+
import okio.BufferedSink;
2123

2224
public class RetryHandlerTest {
2325

@@ -154,6 +156,40 @@ public void defensiveProgramming() {
154156
}, "logger cannot be null");
155157
}
156158

159+
@Test
160+
public void testIsBuffered() {
161+
final RetryHandler retryHandler = new RetryHandler();
162+
Request request = new Request.Builder().url("https://localhost").method("GET", null).build();
163+
assertTrue(retryHandler.isBuffered(request), "Get Request is buffered");
164+
165+
request = new Request.Builder().url("https://localhost").method("DELETE", null).build();
166+
assertTrue(retryHandler.isBuffered(request), "Delete Request is buffered");
167+
168+
request = new Request.Builder().url("https://localhost")
169+
.method("POST",
170+
RequestBody.create("{\"key\": 42 }", MediaType.parse("application/json")))
171+
.build();
172+
assertTrue(retryHandler.isBuffered(request), "Post Request is buffered");
173+
174+
request = new Request.Builder().url("https://localhost")
175+
.method("POST",
176+
new RequestBody() {
177+
178+
@Override
179+
public MediaType contentType() {
180+
return MediaType.parse("application/octet-stream");
181+
}
182+
183+
@Override
184+
public void writeTo(BufferedSink sink) throws IOException {
185+
// TODO Auto-generated method stub
186+
187+
}
188+
})
189+
.build();
190+
assertFalse(retryHandler.isBuffered(request), "Post Stream Request is not buffered");
191+
}
192+
157193
Response TestResponse() {
158194
return new Response.Builder()
159195
.code(429)

0 commit comments

Comments
 (0)