Skip to content

Commit 804f36b

Browse files
committed
- adds unit tests for default retry and redirect options values
1 parent d86d453 commit 804f36b

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

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)