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