Skip to content

Commit ff8c1f3

Browse files
authored
Merge pull request #183 from Asky-GH/dev
- fixes retry/redirect options customization
2 parents 143c27c + ba0d5fd commit ff8c1f3

File tree

10 files changed

+193
-11
lines changed

10 files changed

+193
-11
lines changed

src/main/java/com/microsoft/graph/http/BaseCollectionRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ public void setShouldRedirect(@Nonnull IShouldRedirect shouldRedirect) {
357357
*
358358
* @return Callback which is called before redirect
359359
*/
360-
@Nullable
360+
@Nonnull
361361
public IShouldRedirect getShouldRedirect() {
362362
return baseRequest.getShouldRedirect();
363363
}
@@ -377,7 +377,7 @@ public void setShouldRetry(@Nonnull IShouldRetry shouldretry) {
377377
*
378378
* @return Callback called before retry
379379
*/
380-
@Nullable
380+
@Nonnull
381381
public IShouldRetry getShouldRetry() {
382382
return baseRequest.getShouldRetry();
383383
}

src/main/java/com/microsoft/graph/http/BaseRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ public void setShouldRedirect(@Nonnull IShouldRedirect shouldRedirect) {
537537
*
538538
* @return Callback which is called before redirect
539539
*/
540-
@Nullable
540+
@Nonnull
541541
public IShouldRedirect getShouldRedirect() {
542542
return shouldRedirect;
543543
}
@@ -557,7 +557,7 @@ public void setShouldRetry(@Nonnull IShouldRetry shouldretry) {
557557
*
558558
* @return Callback called before retry
559559
*/
560-
@Nullable
560+
@Nonnull
561561
public IShouldRetry getShouldRetry() {
562562
return shouldRetry;
563563
}

src/main/java/com/microsoft/graph/http/BaseStreamRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public void setShouldRedirect(@Nonnull final IShouldRedirect shouldRedirect) {
224224
*
225225
* @return Callback which is called before redirect
226226
*/
227-
@Nullable
227+
@Nonnull
228228
public IShouldRedirect getShouldRedirect() {
229229
return baseRequest.getShouldRedirect();
230230
}
@@ -244,7 +244,7 @@ public void setShouldRetry(@Nonnull final IShouldRetry shouldretry) {
244244
*
245245
* @return Callback called before retry
246246
*/
247-
@Nullable
247+
@Nonnull
248248
public IShouldRetry getShouldRetry() {
249249
return baseRequest.getShouldRetry();
250250
}

src/main/java/com/microsoft/graph/http/CoreHttpProvider.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@
5757
import okhttp3.ResponseBody;
5858
import okio.BufferedSink;
5959

60+
import static com.microsoft.graph.httpcore.middlewareoption.RedirectOptions.DEFAULT_MAX_REDIRECTS;
61+
import static com.microsoft.graph.httpcore.middlewareoption.RedirectOptions.DEFAULT_SHOULD_REDIRECT;
62+
import static com.microsoft.graph.httpcore.middlewareoption.RetryOptions.DEFAULT_DELAY;
63+
import static com.microsoft.graph.httpcore.middlewareoption.RetryOptions.DEFAULT_MAX_RETRIES;
64+
import static com.microsoft.graph.httpcore.middlewareoption.RetryOptions.DEFAULT_SHOULD_RETRY;
65+
6066
/**
6167
* HTTP provider based off of OkHttp and msgraph-sdk-java-core library
6268
*/
@@ -236,8 +242,14 @@ public <Result, Body> Request getHttpRequest(@Nonnull final IHttpRequest request
236242
logger.logDebug("Starting to send request, URL " + requestUrl.toString());
237243

238244
// Request level middleware options
239-
final RedirectOptions redirectOptions = request.getMaxRedirects() <= 0 ? null : new RedirectOptions(request.getMaxRedirects(), request.getShouldRedirect());
240-
final RetryOptions retryOptions = request.getShouldRetry() == null ? null : new RetryOptions(request.getShouldRetry(), request.getMaxRetries(), request.getDelay());
245+
final RedirectOptions redirectOptions =
246+
request.getMaxRedirects() == DEFAULT_MAX_REDIRECTS && request.getShouldRedirect().equals(DEFAULT_SHOULD_REDIRECT)
247+
? null
248+
: new RedirectOptions(request.getMaxRedirects(), request.getShouldRedirect());
249+
final RetryOptions retryOptions =
250+
request.getMaxRetries() == DEFAULT_MAX_RETRIES && request.getDelay() == DEFAULT_DELAY && request.getShouldRetry().equals(DEFAULT_SHOULD_RETRY)
251+
? null
252+
: new RetryOptions(request.getShouldRetry(), request.getMaxRetries(), request.getDelay());
241253

242254
final Request coreHttpRequest = convertIHttpRequestToOkHttpRequest(request);
243255
Request.Builder corehttpRequestBuilder = coreHttpRequest

src/main/java/com/microsoft/graph/http/IHttpRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public interface IHttpRequest {
119119
*
120120
* @return Callback which is called before redirect
121121
*/
122-
@Nullable
122+
@Nonnull
123123
IShouldRedirect getShouldRedirect();
124124

125125
/**
@@ -134,7 +134,7 @@ public interface IHttpRequest {
134134
*
135135
* @return Callback called before retry
136136
*/
137-
@Nullable
137+
@Nonnull
138138
IShouldRetry getShouldRetry();
139139

140140
/**

src/main/java/com/microsoft/graph/httpcore/middlewareoption/RetryOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public RetryOptions(@Nullable final IShouldRetry shouldRetry, int maxRetries, lo
6868
if(maxRetries < 0)
6969
throw new IllegalArgumentException("Max retries cannot be negative");
7070

71-
this.mShouldretry = shouldRetry != null ? shouldRetry : DEFAULT_SHOULD_RETRY;
71+
this.mShouldretry = shouldRetry == null ? DEFAULT_SHOULD_RETRY : shouldRetry;
7272
this.mMaxRetries = maxRetries;
7373
this.mDelay = delay;
7474
}

src/test/java/com/microsoft/graph/http/CoreHttpProviderTests.java

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99

1010
import com.microsoft.graph.core.GraphErrorCodes;
1111
import com.microsoft.graph.core.IBaseClient;
12+
import com.microsoft.graph.httpcore.middlewareoption.IShouldRedirect;
13+
import com.microsoft.graph.httpcore.middlewareoption.IShouldRetry;
14+
import com.microsoft.graph.httpcore.middlewareoption.RedirectOptions;
15+
import com.microsoft.graph.httpcore.middlewareoption.RetryOptions;
1216
import com.microsoft.graph.logger.ILogger;
1317
import com.microsoft.graph.logger.LoggerLevel;
1418
import com.microsoft.graph.options.HeaderOption;
@@ -21,6 +25,7 @@
2125
import java.io.ByteArrayInputStream;
2226
import java.io.IOException;
2327
import java.io.InputStream;
28+
import java.net.MalformedURLException;
2429
import java.net.URL;
2530
import java.nio.charset.StandardCharsets;
2631
import java.util.ArrayList;
@@ -37,6 +42,7 @@
3742

3843
import static org.junit.jupiter.api.Assertions.assertEquals;
3944
import static org.junit.jupiter.api.Assertions.assertFalse;
45+
import static org.junit.jupiter.api.Assertions.assertNotNull;
4046
import static org.junit.jupiter.api.Assertions.assertNull;
4147
import static org.junit.jupiter.api.Assertions.assertTrue;
4248
import static org.junit.jupiter.api.Assertions.fail;
@@ -198,5 +204,88 @@ private void setCoreHttpProvider(final Object toSerialize) throws IOException {
198204
mock(ILogger.class),
199205
mClient);
200206
}
207+
@Test
208+
public void getHttpRequestDoesntSetRetryOrRedirectOptionsOnDefaultValues() throws MalformedURLException {
209+
final IHttpRequest absRequest = mock(IHttpRequest.class);
210+
when(absRequest.getRequestUrl()).thenReturn(new URL("https://graph.microsoft.com/v1.0/me"));
211+
when(absRequest.getHttpMethod()).thenReturn(HttpMethod.GET);
212+
final ISerializer serializer = mock(ISerializer.class);
213+
final ILogger logger = mock(ILogger.class);
214+
215+
mProvider = new CoreHttpProvider(serializer,
216+
logger,
217+
new OkHttpClient.Builder().build());
218+
219+
when(absRequest.getMaxRedirects()).thenReturn(RedirectOptions.DEFAULT_MAX_REDIRECTS);
220+
when(absRequest.getShouldRedirect()).thenReturn(RedirectOptions.DEFAULT_SHOULD_REDIRECT);
221+
Request request = mProvider.getHttpRequest(absRequest, String.class, null);
222+
RedirectOptions redirectOptions = request.tag(RedirectOptions.class);
223+
224+
assertNull(redirectOptions);
225+
226+
when(absRequest.getShouldRetry()).thenReturn(RetryOptions.DEFAULT_SHOULD_RETRY);
227+
when(absRequest.getMaxRetries()).thenReturn(RetryOptions.DEFAULT_MAX_RETRIES);
228+
when(absRequest.getDelay()).thenReturn(RetryOptions.DEFAULT_DELAY);
229+
230+
request = mProvider.getHttpRequest(absRequest, String.class, null);
231+
RetryOptions retryOptions = request.tag(RetryOptions.class);
232+
233+
assertNull(retryOptions);
234+
}
235+
236+
@Test
237+
public void getHttpRequestSetsRetryOrRedirectOptionsOnNonDefaultValues() throws MalformedURLException {
238+
final IHttpRequest absRequest = mock(IHttpRequest.class);
239+
when(absRequest.getRequestUrl()).thenReturn(new URL("https://graph.microsoft.com/v1.0/me"));
240+
when(absRequest.getHttpMethod()).thenReturn(HttpMethod.GET);
241+
final ISerializer serializer = mock(ISerializer.class);
242+
final ILogger logger = mock(ILogger.class);
201243

244+
mProvider = new CoreHttpProvider(serializer,
245+
logger,
246+
new OkHttpClient.Builder().build());
247+
248+
// testing all pairs to cover all branches
249+
when(absRequest.getMaxRedirects()).thenReturn(RedirectOptions.DEFAULT_MAX_REDIRECTS -1);
250+
when(absRequest.getShouldRedirect()).thenReturn(mock(IShouldRedirect.class));
251+
Request request = mProvider.getHttpRequest(absRequest, String.class, null);
252+
RedirectOptions redirectOptions = request.tag(RedirectOptions.class);
253+
254+
assertNotNull(redirectOptions);
255+
256+
when(absRequest.getMaxRedirects()).thenReturn(RedirectOptions.DEFAULT_MAX_REDIRECTS);
257+
when(absRequest.getShouldRedirect()).thenReturn(mock(IShouldRedirect.class));
258+
request = mProvider.getHttpRequest(absRequest, String.class, null);
259+
redirectOptions = request.tag(RedirectOptions.class);
260+
261+
assertNotNull(redirectOptions);
262+
263+
// testing all pairs to cover all branches
264+
when(absRequest.getShouldRetry()).thenReturn(mock(IShouldRetry.class));
265+
when(absRequest.getMaxRetries()).thenReturn(RetryOptions.DEFAULT_MAX_RETRIES-1);
266+
when(absRequest.getDelay()).thenReturn(RetryOptions.DEFAULT_DELAY-1);
267+
268+
request = mProvider.getHttpRequest(absRequest, String.class, null);
269+
RetryOptions retryOptions = request.tag(RetryOptions.class);
270+
271+
assertNotNull(retryOptions);
272+
273+
when(absRequest.getShouldRetry()).thenReturn(mock(IShouldRetry.class));
274+
when(absRequest.getMaxRetries()).thenReturn(RetryOptions.DEFAULT_MAX_RETRIES);
275+
when(absRequest.getDelay()).thenReturn(RetryOptions.DEFAULT_DELAY-1);
276+
277+
request = mProvider.getHttpRequest(absRequest, String.class, null);
278+
retryOptions = request.tag(RetryOptions.class);
279+
280+
assertNotNull(retryOptions);
281+
282+
when(absRequest.getShouldRetry()).thenReturn(mock(IShouldRetry.class));
283+
when(absRequest.getMaxRetries()).thenReturn(RetryOptions.DEFAULT_MAX_RETRIES);
284+
when(absRequest.getDelay()).thenReturn(RetryOptions.DEFAULT_DELAY);
285+
286+
request = mProvider.getHttpRequest(absRequest, String.class, null);
287+
retryOptions = request.tag(RetryOptions.class);
288+
289+
assertNotNull(retryOptions);
290+
}
202291
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.microsoft.graph.httpcore;
22

3+
import static org.junit.jupiter.api.Assertions.assertFalse;
34
import static org.junit.jupiter.api.Assertions.assertTrue;
45
import static org.junit.jupiter.api.Assertions.fail;
56

@@ -262,5 +263,20 @@ public void testGetRedirectRelativeLocationRequestURLwithSlash() throws Protocol
262263
String expected = "https://graph.microsoft.com/v1.0/me/testrelativeurl";
263264
assertTrue(request.url().toString().compareTo(expected) == 0);
264265
}
266+
@Test
267+
public void testIsRedirectedIsFalseIfExceedsMaxRedirects() throws ProtocolException, IOException {
268+
RedirectOptions options = new RedirectOptions(0, null);
269+
RedirectHandler redirectHandler = new RedirectHandler(options);
270+
Request httppost = new Request.Builder().url(testmeurl).build();
265271

272+
Response response = new Response.Builder()
273+
.protocol(Protocol.HTTP_1_1)
274+
.code(HttpURLConnection.HTTP_SEE_OTHER)
275+
.message("See Other")
276+
.addHeader("location", "/testrelativeurl")
277+
.request(httppost)
278+
.build();
279+
boolean isRedirected = redirectHandler.isRedirected(httppost, response, 1, options);
280+
assertFalse(isRedirected);
281+
}
266282
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.microsoft.graph.httpcore.middlewareoption;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
import static org.mockito.Mockito.mock;
7+
8+
import org.junit.jupiter.api.Test;
9+
10+
import okhttp3.Response;
11+
12+
public class RedirectOptionsTest {
13+
@Test
14+
public void constructorDefensiveProgramming() {
15+
assertThrows(IllegalArgumentException.class, () -> {
16+
new RedirectOptions(RedirectOptions.MAX_REDIRECTS +1, null);
17+
});
18+
19+
assertThrows(IllegalArgumentException.class, () -> {
20+
new RedirectOptions(-1, null);
21+
});
22+
}
23+
@Test
24+
public void defaultShouldRedirectValue() {
25+
RedirectOptions options = new RedirectOptions();
26+
assertEquals(options.shouldRedirect(), RedirectOptions.DEFAULT_SHOULD_REDIRECT);
27+
}
28+
@Test
29+
public void defaultShouldRedirectIsTrue() {
30+
Response response = mock(Response.class);
31+
assertTrue(RedirectOptions.DEFAULT_SHOULD_REDIRECT.shouldRedirect(response));
32+
}
33+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.microsoft.graph.httpcore.middlewareoption;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
public class RetryOptionsTest {
9+
@Test
10+
public void constructorDefensiveProgramming() {
11+
assertThrows(IllegalArgumentException.class, () -> {
12+
new RetryOptions(null, RetryOptions.MAX_RETRIES +1, 0);
13+
});
14+
15+
assertThrows(IllegalArgumentException.class, () -> {
16+
new RetryOptions(null, RetryOptions.MAX_RETRIES, RetryOptions.MAX_DELAY +1);
17+
});
18+
19+
assertThrows(IllegalArgumentException.class, () -> {
20+
new RetryOptions(null, RetryOptions.MAX_RETRIES, -1);
21+
});
22+
23+
assertThrows(IllegalArgumentException.class, () -> {
24+
new RetryOptions(null, -1, RetryOptions.MAX_DELAY);
25+
});
26+
}
27+
@Test
28+
public void defaultShouldRetryValue() {
29+
RetryOptions options = new RetryOptions();
30+
assertEquals(options.shouldRetry(), RetryOptions.DEFAULT_SHOULD_RETRY);
31+
}
32+
}

0 commit comments

Comments
 (0)