|
| 1 | +package com.microsoft.bot.connector; |
| 2 | + |
| 3 | +import java.time.Instant; |
| 4 | +import java.time.LocalDateTime; |
| 5 | +import java.time.ZoneId; |
| 6 | +import java.time.ZonedDateTime; |
| 7 | +import java.time.format.DateTimeFormatter; |
| 8 | +import java.time.temporal.TemporalUnit; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +import com.microsoft.aad.msal4j.MsalException; |
| 15 | +import com.microsoft.aad.msal4j.MsalServiceException; |
| 16 | +import com.microsoft.bot.connector.authentication.RetryAfterHelper; |
| 17 | +import com.microsoft.bot.connector.authentication.RetryException; |
| 18 | +import com.microsoft.bot.connector.authentication.RetryParams; |
| 19 | + |
| 20 | +import org.junit.Assert; |
| 21 | +import org.junit.Test; |
| 22 | + |
| 23 | +public class RetryAfterHelperTests { |
| 24 | + |
| 25 | + @Test |
| 26 | + public void TestRetryIncrement() { |
| 27 | + RetryParams result = RetryAfterHelper.processRetry(new ArrayList<String>(), 8); |
| 28 | + Assert.assertTrue(result.getShouldRetry()); |
| 29 | + result = RetryAfterHelper.processRetry(new ArrayList<String>(), 9); |
| 30 | + Assert.assertFalse(result.getShouldRetry()); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void TestRetryDelaySeconds() { |
| 35 | + List<String> headers = new ArrayList<String>(); |
| 36 | + headers.add("10"); |
| 37 | + RetryParams result = RetryAfterHelper.processRetry(headers, 1); |
| 38 | + Assert.assertEquals(result.getRetryAfter(), 10000); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void TestRetryDelayRFC1123Date() { |
| 43 | + Instant instant = Instant.now().plusSeconds(5); |
| 44 | + ZonedDateTime dateTime = instant.atZone(ZoneId.of("UTC")); |
| 45 | + String dateTimeString = dateTime.format(DateTimeFormatter.RFC_1123_DATE_TIME); |
| 46 | + List<String> headers = new ArrayList<String>(); |
| 47 | + headers.add(dateTimeString); |
| 48 | + RetryParams result = RetryAfterHelper.processRetry(headers, 1); |
| 49 | + Assert.assertTrue(result.getShouldRetry()); |
| 50 | + Assert.assertTrue(result.getRetryAfter() > 0); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void TestRetryDelayRFC1123DateInPast() { |
| 55 | + Instant instant = Instant.now().plusSeconds(-5); |
| 56 | + ZonedDateTime dateTime = instant.atZone(ZoneId.of("UTC")); |
| 57 | + String dateTimeString = dateTime.format(DateTimeFormatter.RFC_1123_DATE_TIME); |
| 58 | + List<String> headers = new ArrayList<String>(); |
| 59 | + headers.add(dateTimeString); |
| 60 | + RetryParams result = RetryAfterHelper.processRetry(headers, 1); |
| 61 | + Assert.assertTrue(result.getShouldRetry()); |
| 62 | + // default is 50, so since the time was in the past we should be seeing the default 50 here. |
| 63 | + Assert.assertTrue(result.getRetryAfter() == 50); |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + @Test |
| 68 | + public void TestRetryDelayRFC1123DateEmpty() { |
| 69 | + List<String> headers = new ArrayList<String>(); |
| 70 | + headers.add(""); |
| 71 | + RetryParams result = RetryAfterHelper.processRetry(headers, 1); |
| 72 | + Assert.assertTrue(result.getShouldRetry()); |
| 73 | + // default is 50, so since the time was in the past we should be seeing the default 50 here. |
| 74 | + Assert.assertTrue(result.getRetryAfter() == 50); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void TestRetryDelayRFC1123DateNull() { |
| 79 | + List<String> headers = new ArrayList<String>(); |
| 80 | + headers.add(null); |
| 81 | + RetryParams result = RetryAfterHelper.processRetry(headers, 1); |
| 82 | + Assert.assertTrue(result.getShouldRetry()); |
| 83 | + // default is 50, so since the time was in the past we should be seeing the default 50 here. |
| 84 | + Assert.assertTrue(result.getRetryAfter() == 50); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void TestRetryDelayRFC1123NeaderNull() { |
| 89 | + RetryParams result = RetryAfterHelper.processRetry(null, 1); |
| 90 | + Assert.assertTrue(result.getShouldRetry()); |
| 91 | + // default is 50, so since the time was in the past we should be seeing the default 50 here. |
| 92 | + Assert.assertTrue(result.getRetryAfter() == 50); |
| 93 | + } |
| 94 | + |
| 95 | +} |
0 commit comments