Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class MSBatchRequestStep {
public MSBatchRequestStep(String requestId, Request request, List<String> arrayOfDependsOnIds) {
if(requestId == null)
throw new IllegalArgumentException("Request Id cannot be null.");
if(requestId.length() == 0)
throw new IllegalArgumentException("Request Id cannot be empty.");
if(request == null)
new IllegalArgumentException("Request cannot be null.");

Expand Down
10 changes: 8 additions & 2 deletions src/main/java/com/microsoft/graph/httpcore/RedirectHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,15 @@ Request getRedirect(
final Request request,
final Response userResponse) throws ProtocolException {
String location = userResponse.header("Location");
if (location == null) return null;
if (location == null || location.length() == 0) return null;

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

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

Expand Down
17 changes: 12 additions & 5 deletions src/main/java/com/microsoft/graph/httpcore/RetryHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,24 @@ && checkStatus(statusCode) && isBuffered(response, request)
return shouldRetry;
}

/**
* Get retry after in milliseconds
* @param response Response
* @param delay Delay in seconds
* @param executionCount Execution count of retries
* @return Retry interval in milliseconds
*/
long getRetryAfter(Response response, long delay, int executionCount) {
String retryAfterHeader = response.header(RETRY_AFTER);
long retryDelay = RetryOptions.DEFAULT_DELAY;
double retryDelay = RetryOptions.DEFAULT_DELAY * DELAY_MILLISECONDS;
if(retryAfterHeader != null) {
retryDelay = Long.parseLong(retryAfterHeader);
retryDelay = Long.parseLong(retryAfterHeader) * DELAY_MILLISECONDS;
} else {
retryDelay = (long)((Math.pow(2.0, (double)executionCount)-1)*0.5);
retryDelay = executionCount < 2 ? retryDelay : retryDelay + delay + (long)Math.random();
retryDelay = (double)((Math.pow(2.0, (double)executionCount)-1)*0.5);
retryDelay = (executionCount < 2 ? delay : retryDelay + delay) + (double)Math.random();
retryDelay *= DELAY_MILLISECONDS;
}
return Math.min(retryDelay, RetryOptions.MAX_DELAY);
return (long)Math.min(retryDelay, RetryOptions.MAX_DELAY * DELAY_MILLISECONDS);
}

boolean checkStatus(int statusCode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
public class RedirectHandlerTest {

String testmeurl = "https://graph.microsoft.com/v1.0/me/";
String testurl = "https://graph.microsoft.com/v1.0/";
String testurl = "https://graph.microsoft.com/v1.0";
String differenthosturl = "https://graph.abc.com/v1.0/";

@Test
Expand Down Expand Up @@ -228,5 +228,39 @@ public void testGetRedirectForPostMethodWithStatusCodeSeeOther() {
fail("Redirect handler testGetRedirectForPostMethod1 failure");
}
}

@Test
public void testGetRedirectForRelativeURL() throws ProtocolException {
RedirectHandler redirectHandler = new RedirectHandler();
Request httppost = new Request.Builder().url(testurl).build();

Response response = new Response.Builder()
.protocol(Protocol.HTTP_1_1)
.code(HttpURLConnection.HTTP_SEE_OTHER)
.message("See Other")
.addHeader("location", "/testrelativeurl")
.request(httppost)
.build();

Request request = redirectHandler.getRedirect(httppost, response);
assertTrue(request.url().toString().compareTo(testurl+"/testrelativeurl") == 0);
}

@Test
public void testGetRedirectRelativeLocationRequestURLwithSlash() throws ProtocolException {
RedirectHandler redirectHandler = new RedirectHandler();
Request httppost = new Request.Builder().url(testmeurl).build();

Response response = new Response.Builder()
.protocol(Protocol.HTTP_1_1)
.code(HttpURLConnection.HTTP_SEE_OTHER)
.message("See Other")
.addHeader("location", "/testrelativeurl")
.request(httppost)
.build();
Request request = redirectHandler.getRedirect(httppost, response);
String expected = "https://graph.microsoft.com/v1.0/me/testrelativeurl";
assertTrue(request.url().toString().compareTo(expected) == 0);
}

}
33 changes: 33 additions & 0 deletions src/test/java/com/microsoft/graph/httpcore/RetryHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,37 @@ public void testRetryRequestWithExponentialBackOff() {

assertTrue(retryhandler.retryRequest(response, 1, httppost, new RetryOptions()));
}

@Test
public void testGetRetryAfterWithHeader() {
RetryHandler retryHandler = new RetryHandler();
long delay = retryHandler.getRetryAfter(TestResponse().newBuilder().addHeader("Retry-After", "60").build(), 1, 1);
assertTrue(delay == 60000);
delay = retryHandler.getRetryAfter(TestResponse().newBuilder().addHeader("Retry-After", "1").build(), 2, 3);
assertTrue(delay == 1000);
}

@Test
public void testGetRetryAfterOnFirstExecution() {
RetryHandler retryHandler = new RetryHandler();
long delay = retryHandler.getRetryAfter(TestResponse(), 3, 1);
assertTrue(delay > 3000);
delay = retryHandler.getRetryAfter(TestResponse(), 3, 2);
assertTrue(delay > 3100);
}

@Test
public void testGetRetryAfterMaxExceed() {
RetryHandler retryHandler = new RetryHandler();
long delay = retryHandler.getRetryAfter(TestResponse(), 190, 1);
assertTrue(delay == 180000);
}

Response TestResponse() {
return new Response.Builder()
.code(429)
.message("message")
.request(new Request.Builder().url("https://localhost").build())
.protocol(Protocol.HTTP_1_0).build();
}
}