Skip to content

Commit c86ca67

Browse files
committed
Add test file for retry handler middleware
1 parent bdc8a32 commit c86ca67

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.microsoft.graph.httpcore;
2+
3+
import static org.junit.Assert.assertFalse;
4+
import static org.junit.Assert.assertTrue;
5+
import static org.junit.Assert.fail;
6+
7+
import java.io.UnsupportedEncodingException;
8+
9+
import org.apache.http.HttpEntity;
10+
import org.apache.http.HttpResponse;
11+
import org.apache.http.HttpStatus;
12+
import org.apache.http.HttpVersion;
13+
import org.apache.http.client.methods.HttpPost;
14+
import org.apache.http.client.protocol.HttpClientContext;
15+
import org.apache.http.entity.StringEntity;
16+
import org.apache.http.message.BasicHttpResponse;
17+
import org.apache.http.protocol.HttpCoreContext;
18+
import org.junit.Test;
19+
20+
public class RetryHandlerTest {
21+
22+
@Test
23+
public void testRetryHandlerCreation() {
24+
RetryHandler retryhandler = new RetryHandler(2, 2000);
25+
assertTrue(retryhandler.getRetryInterval() == 2000);
26+
}
27+
28+
@Test
29+
public void testRetryRequestWithMaxRetryAttempts() {
30+
RetryHandler retryhandler = new RetryHandler(2, 2000);
31+
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_GATEWAY_TIMEOUT, "Gateway Timeout");
32+
HttpClientContext localContext = HttpClientContext.create();
33+
assertFalse(retryhandler.retryRequest(response, 3, localContext));
34+
}
35+
36+
@Test
37+
public void testRetryRequestForStatusCode() {
38+
RetryHandler retryhandler = new RetryHandler(2, 2000);
39+
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_INTERNAL_SERVER_ERROR, "Internal Server Error");
40+
HttpClientContext localContext = HttpClientContext.create();
41+
assertFalse(retryhandler.retryRequest(response, 1, localContext));
42+
}
43+
44+
@Test
45+
public void testRetryRequestWithTransferEncoding() {
46+
RetryHandler retryhandler = new RetryHandler(2, 2000);
47+
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_GATEWAY_TIMEOUT, "Internal Server Error");
48+
response.setHeader("Transfer-Encoding", "chunked");
49+
HttpPost httppost = new HttpPost("https://graph.microsoft.com/v1.0/");
50+
51+
try {
52+
HttpEntity entity = new StringEntity("TEST");
53+
httppost.setEntity(entity);
54+
HttpClientContext localContext = HttpClientContext.create();
55+
localContext.setAttribute(HttpCoreContext.HTTP_REQUEST, httppost);
56+
assertFalse(retryhandler.retryRequest(response, 1, localContext));
57+
58+
} catch (UnsupportedEncodingException e) {
59+
e.printStackTrace();
60+
fail("Retry handler testRetryHandlerRetryRequest3 test failure");
61+
}
62+
}
63+
64+
@Test
65+
public void testRetryRequestWithExponentialBackOff() {
66+
RetryHandler retryhandler = new RetryHandler(2, 2000);
67+
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_GATEWAY_TIMEOUT, "Internal Server Error");
68+
HttpPost httppost = new HttpPost("https://graph.microsoft.com/v1.0/");
69+
70+
try {
71+
HttpEntity entity = new StringEntity("TEST");
72+
httppost.setEntity(entity);
73+
HttpClientContext localContext = HttpClientContext.create();
74+
localContext.setAttribute(HttpCoreContext.HTTP_REQUEST, httppost);
75+
assertTrue(retryhandler.retryRequest(response, 1, localContext));
76+
assertTrue(retryhandler.getRetryInterval() == 20);
77+
78+
} catch (UnsupportedEncodingException e) {
79+
e.printStackTrace();
80+
fail("Retry handler testRetryHandlerRetryRequest3 test failure");
81+
}
82+
}
83+
84+
@Test
85+
public void testRetryHandlerRetryRequestWithRetryAfterHeader() {
86+
RetryHandler retryhandler = new RetryHandler(2, 2000);
87+
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_GATEWAY_TIMEOUT, "Internal Server Error");
88+
response.setHeader("Retry-After", "100");
89+
HttpPost httppost = new HttpPost("https://graph.microsoft.com/v1.0/");
90+
91+
try {
92+
HttpEntity entity = new StringEntity("TEST");
93+
httppost.setEntity(entity);
94+
HttpClientContext localContext = HttpClientContext.create();
95+
localContext.setAttribute(HttpCoreContext.HTTP_REQUEST, httppost);
96+
assertTrue(retryhandler.retryRequest(response, 1, localContext));
97+
assertTrue(retryhandler.getRetryInterval() == 100);
98+
99+
} catch (UnsupportedEncodingException e) {
100+
e.printStackTrace();
101+
fail("Retry handler testRetryHandlerRetryRequestWithRetryAfterHeader test failure");
102+
}
103+
}
104+
105+
}

0 commit comments

Comments
 (0)