-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOAuthInterceptorTest.java
More file actions
118 lines (100 loc) · 4.58 KB
/
OAuthInterceptorTest.java
File metadata and controls
118 lines (100 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package com.contentstack.cms.oauth;
import com.contentstack.cms.core.RetryConfig;
import com.contentstack.cms.models.OAuthTokens;
import okhttp3.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import java.io.IOException;
import java.net.SocketTimeoutException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
@RunWith(MockitoJUnitRunner.class)
public class OAuthInterceptorTest {
private OAuthInterceptor interceptor;
@Mock
private OAuthHandler mockHandler;
@Mock
private OAuthTokens mockTokens;
@Before
public void setup() {
Mockito.lenient().when(mockTokens.isExpired()).thenReturn(false);
Mockito.lenient().when(mockTokens.hasAccessToken()).thenReturn(true);
Mockito.lenient().when(mockHandler.getTokens()).thenReturn(mockTokens);
Mockito.lenient().when(mockHandler.getAccessToken()).thenReturn("test-access-token");
interceptor = new OAuthInterceptor(mockHandler);
interceptor.setRetryConfig(RetryConfig.builder().retryLimit(3).retryDelay(10).build());
}
@Test
public void testRetry_onSocketTimeout_thenSuccess_retriesAndReturnsSuccess() throws IOException {
Request request = new Request.Builder()
.url("https://api.contentstack.io/v3/content_types")
.get()
.build();
TimeoutTestChain chain = new TimeoutTestChain(request, 1, 200);
try (Response response = interceptor.intercept(chain)) {
assertEquals(200, response.code());
assertEquals(2, chain.getProceedCount());
}
}
@Test
public void testRetry_onSocketTimeout_exhaustsRetries_throws() {
Request request = new Request.Builder()
.url("https://api.contentstack.io/v3/content_types")
.get()
.build();
TimeoutTestChain chain = new TimeoutTestChain(request, 5, 200);
assertThrows(SocketTimeoutException.class, () -> interceptor.intercept(chain));
assertEquals(4, chain.getProceedCount()); // 1 initial + 3 retries
}
@Test
public void testRetry_onSocketTimeout_zeroRetryLimit_throwsImmediately() {
interceptor.setRetryConfig(RetryConfig.builder().retryLimit(0).retryDelay(10).build());
Request request = new Request.Builder()
.url("https://api.contentstack.io/v3/content_types")
.get()
.build();
TimeoutTestChain chain = new TimeoutTestChain(request, 5, 200);
assertThrows(SocketTimeoutException.class, () -> interceptor.intercept(chain));
assertEquals(1, chain.getProceedCount());
}
private static class TimeoutTestChain implements Interceptor.Chain {
private final Request originalRequest;
private final int timeoutCount;
private final int successCode;
private int proceedCount = 0;
TimeoutTestChain(Request request, int timeoutCount, int successCode) {
this.originalRequest = request;
this.timeoutCount = timeoutCount;
this.successCode = successCode;
}
int getProceedCount() { return proceedCount; }
@Override
public Request request() { return originalRequest; }
@Override
public Response proceed(Request request) throws IOException {
proceedCount++;
if (proceedCount <= timeoutCount) {
throw new SocketTimeoutException("timeout");
}
return new Response.Builder()
.request(request)
.protocol(Protocol.HTTP_1_1)
.code(successCode)
.message("OK")
.body(ResponseBody.create("{}", MediaType.parse("application/json")))
.build();
}
@Override public Connection connection() { return null; }
@Override public int connectTimeoutMillis() { return 0; }
@Override public Interceptor.Chain withConnectTimeout(int timeout, java.util.concurrent.TimeUnit unit) { return this; }
@Override public int readTimeoutMillis() { return 0; }
@Override public Interceptor.Chain withReadTimeout(int timeout, java.util.concurrent.TimeUnit unit) { return this; }
@Override public int writeTimeoutMillis() { return 0; }
@Override public Interceptor.Chain withWriteTimeout(int timeout, java.util.concurrent.TimeUnit unit) { return this; }
@Override public Call call() { return null; }
}
}