Skip to content

Commit 4db7fce

Browse files
Merge pull request #25 from microsoftgraph/getRetryDelay-fix
Retry delay fix
2 parents 51fa9b7 + 1d53c73 commit 4db7fce

File tree

5 files changed

+90
-8
lines changed

5 files changed

+90
-8
lines changed

src/main/java/com/microsoft/graph/content/MSBatchRequestStep.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public class MSBatchRequestStep {
1212
public MSBatchRequestStep(String requestId, Request request, List<String> arrayOfDependsOnIds) {
1313
if(requestId == null)
1414
throw new IllegalArgumentException("Request Id cannot be null.");
15+
if(requestId.length() == 0)
16+
throw new IllegalArgumentException("Request Id cannot be empty.");
1517
if(request == null)
1618
new IllegalArgumentException("Request cannot be null.");
1719

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,15 @@ Request getRedirect(
6666
final Request request,
6767
final Response userResponse) throws ProtocolException {
6868
String location = userResponse.header("Location");
69-
if (location == null) return null;
69+
if (location == null || location.length() == 0) return null;
7070

71-
// TODO: If Location header is relative reference then final URL should be relative to original target URL.
71+
// For relative URL in location header, the new url to redirect is relative to original request
72+
if(location.startsWith("/")) {
73+
if(request.url().toString().endsWith("/")) {
74+
location = location.substring(1);
75+
}
76+
location = request.url() + location;
77+
}
7278

7379
HttpUrl requestUrl = userResponse.request().url();
7480

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,24 @@ && checkStatus(statusCode) && isBuffered(response, request)
8181
return shouldRetry;
8282
}
8383

84+
/**
85+
* Get retry after in milliseconds
86+
* @param response Response
87+
* @param delay Delay in seconds
88+
* @param executionCount Execution count of retries
89+
* @return Retry interval in milliseconds
90+
*/
8491
long getRetryAfter(Response response, long delay, int executionCount) {
8592
String retryAfterHeader = response.header(RETRY_AFTER);
86-
long retryDelay = RetryOptions.DEFAULT_DELAY;
93+
double retryDelay = RetryOptions.DEFAULT_DELAY * DELAY_MILLISECONDS;
8794
if(retryAfterHeader != null) {
88-
retryDelay = Long.parseLong(retryAfterHeader);
95+
retryDelay = Long.parseLong(retryAfterHeader) * DELAY_MILLISECONDS;
8996
} else {
90-
retryDelay = (long)((Math.pow(2.0, (double)executionCount)-1)*0.5);
91-
retryDelay = executionCount < 2 ? retryDelay : retryDelay + delay + (long)Math.random();
97+
retryDelay = (double)((Math.pow(2.0, (double)executionCount)-1)*0.5);
98+
retryDelay = (executionCount < 2 ? delay : retryDelay + delay) + (double)Math.random();
9299
retryDelay *= DELAY_MILLISECONDS;
93100
}
94-
return Math.min(retryDelay, RetryOptions.MAX_DELAY);
101+
return (long)Math.min(retryDelay, RetryOptions.MAX_DELAY * DELAY_MILLISECONDS);
95102
}
96103

97104
boolean checkStatus(int statusCode) {

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
public class RedirectHandlerTest {
2222

2323
String testmeurl = "https://graph.microsoft.com/v1.0/me/";
24-
String testurl = "https://graph.microsoft.com/v1.0/";
24+
String testurl = "https://graph.microsoft.com/v1.0";
2525
String differenthosturl = "https://graph.abc.com/v1.0/";
2626

2727
@Test
@@ -228,5 +228,39 @@ public void testGetRedirectForPostMethodWithStatusCodeSeeOther() {
228228
fail("Redirect handler testGetRedirectForPostMethod1 failure");
229229
}
230230
}
231+
232+
@Test
233+
public void testGetRedirectForRelativeURL() throws ProtocolException {
234+
RedirectHandler redirectHandler = new RedirectHandler();
235+
Request httppost = new Request.Builder().url(testurl).build();
236+
237+
Response response = new Response.Builder()
238+
.protocol(Protocol.HTTP_1_1)
239+
.code(HttpURLConnection.HTTP_SEE_OTHER)
240+
.message("See Other")
241+
.addHeader("location", "/testrelativeurl")
242+
.request(httppost)
243+
.build();
244+
245+
Request request = redirectHandler.getRedirect(httppost, response);
246+
assertTrue(request.url().toString().compareTo(testurl+"/testrelativeurl") == 0);
247+
}
248+
249+
@Test
250+
public void testGetRedirectRelativeLocationRequestURLwithSlash() throws ProtocolException {
251+
RedirectHandler redirectHandler = new RedirectHandler();
252+
Request httppost = new Request.Builder().url(testmeurl).build();
253+
254+
Response response = new Response.Builder()
255+
.protocol(Protocol.HTTP_1_1)
256+
.code(HttpURLConnection.HTTP_SEE_OTHER)
257+
.message("See Other")
258+
.addHeader("location", "/testrelativeurl")
259+
.request(httppost)
260+
.build();
261+
Request request = redirectHandler.getRedirect(httppost, response);
262+
String expected = "https://graph.microsoft.com/v1.0/me/testrelativeurl";
263+
assertTrue(request.url().toString().compareTo(expected) == 0);
264+
}
231265

232266
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,37 @@ public void testRetryRequestWithExponentialBackOff() {
122122

123123
assertTrue(retryhandler.retryRequest(response, 1, httppost, new RetryOptions()));
124124
}
125+
126+
@Test
127+
public void testGetRetryAfterWithHeader() {
128+
RetryHandler retryHandler = new RetryHandler();
129+
long delay = retryHandler.getRetryAfter(TestResponse().newBuilder().addHeader("Retry-After", "60").build(), 1, 1);
130+
assertTrue(delay == 60000);
131+
delay = retryHandler.getRetryAfter(TestResponse().newBuilder().addHeader("Retry-After", "1").build(), 2, 3);
132+
assertTrue(delay == 1000);
133+
}
134+
135+
@Test
136+
public void testGetRetryAfterOnFirstExecution() {
137+
RetryHandler retryHandler = new RetryHandler();
138+
long delay = retryHandler.getRetryAfter(TestResponse(), 3, 1);
139+
assertTrue(delay > 3000);
140+
delay = retryHandler.getRetryAfter(TestResponse(), 3, 2);
141+
assertTrue(delay > 3100);
142+
}
143+
144+
@Test
145+
public void testGetRetryAfterMaxExceed() {
146+
RetryHandler retryHandler = new RetryHandler();
147+
long delay = retryHandler.getRetryAfter(TestResponse(), 190, 1);
148+
assertTrue(delay == 180000);
149+
}
150+
151+
Response TestResponse() {
152+
return new Response.Builder()
153+
.code(429)
154+
.message("message")
155+
.request(new Request.Builder().url("https://localhost").build())
156+
.protocol(Protocol.HTTP_1_0).build();
157+
}
125158
}

0 commit comments

Comments
 (0)