diff --git a/src/main/java/com/microsoft/graph/content/MSBatchRequestStep.java b/src/main/java/com/microsoft/graph/content/MSBatchRequestStep.java index 7386decf8..ac13bb0ab 100644 --- a/src/main/java/com/microsoft/graph/content/MSBatchRequestStep.java +++ b/src/main/java/com/microsoft/graph/content/MSBatchRequestStep.java @@ -12,6 +12,8 @@ public class MSBatchRequestStep { public MSBatchRequestStep(String requestId, Request request, List 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."); diff --git a/src/main/java/com/microsoft/graph/httpcore/RedirectHandler.java b/src/main/java/com/microsoft/graph/httpcore/RedirectHandler.java index 78c3aa787..b04d774c7 100644 --- a/src/main/java/com/microsoft/graph/httpcore/RedirectHandler.java +++ b/src/main/java/com/microsoft/graph/httpcore/RedirectHandler.java @@ -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(); diff --git a/src/main/java/com/microsoft/graph/httpcore/RetryHandler.java b/src/main/java/com/microsoft/graph/httpcore/RetryHandler.java index a8f542398..987f7bf7b 100644 --- a/src/main/java/com/microsoft/graph/httpcore/RetryHandler.java +++ b/src/main/java/com/microsoft/graph/httpcore/RetryHandler.java @@ -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) { diff --git a/src/test/java/com/microsoft/graph/httpcore/RedirectHandlerTest.java b/src/test/java/com/microsoft/graph/httpcore/RedirectHandlerTest.java index 3182b850f..6eed2b7ad 100644 --- a/src/test/java/com/microsoft/graph/httpcore/RedirectHandlerTest.java +++ b/src/test/java/com/microsoft/graph/httpcore/RedirectHandlerTest.java @@ -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 @@ -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); + } } diff --git a/src/test/java/com/microsoft/graph/httpcore/RetryHandlerTest.java b/src/test/java/com/microsoft/graph/httpcore/RetryHandlerTest.java index 1411eb94a..d5ddc3915 100644 --- a/src/test/java/com/microsoft/graph/httpcore/RetryHandlerTest.java +++ b/src/test/java/com/microsoft/graph/httpcore/RetryHandlerTest.java @@ -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(); + } }