ExpiryCheckHandler { public static Optional<Integer> getExpireAfterConcerningCaseOfCorruptHeaderAndInfinite(MultiMap headers) { try { final Integer expireValue = getExpireValue(headers.get(EXPIRE_AFTER_HEADER)); return Optional.ofNullable(expireValue); } catch (NumberFormatException e) { log.warn("Conceal exception (because we're not allowed to throw by design): " + e.getClass().getSimpleName() + ": " + e.getMessage()); return Optional.empty(); } } private  ExpiryCheckHandler(); static void updateServerTimestampHeader(HttpRequest request); static void updateServerTimestampHeader(MultiMap headers); static Integer getExpireAfter(MultiMap headers); static Optional<Integer> getExpireAfterConcerningCaseOfCorruptHeaderAndInfinite(MultiMap headers); static Integer getQueueExpireAfter(MultiMap headers); static boolean isExpired(MultiMap headers, Long timestamp); static DateTime getExpirationTime(int expireAfter); static Optional<String> getExpirationTimeAsString(int expireAfter); static DateTime parseDateTime(String datetime); static String printDateTime(DateTime datetime); static void setExpireAfter(HttpServerRequest request, int expireAfter); static void setQueueExpireAfter(MultiMap headers, int queueExpireAfter); static final String SERVER_TIMESTAMP_HEADER; static final String EXPIRE_AFTER_HEADER; static final String QUEUE_EXPIRE_AFTER_HEADER; }
@Test public void getExpireAfterConcerningCaseOfCorruptHeaderAndInfinite_returnsValueSpecifiedInHeader(TestContext testContext) throws Exception { final String headerValue = "2147483640"; final MultiMap headers = createHeaderReturningSimpleValue(HTTP_HEADER_X_EXPIRE_AFTER, headerValue); final int returnedExpireValue = ExpiryCheckHandler.getExpireAfterConcerningCaseOfCorruptHeaderAndInfinite(headers).get(); testContext.assertEquals(headerValue, "" + returnedExpireValue); }
@Test public void getExpireAfterConcerningCaseOfCorruptHeaderAndInfinite_returnsZeroIfHeaderSpecifiesZero(TestContext testContext) { final String headerValue = "0"; final MultiMap headers = createHeaderReturningSimpleValue(HTTP_HEADER_X_EXPIRE_AFTER, headerValue); final int returnedExpireValue = ExpiryCheckHandler.getExpireAfterConcerningCaseOfCorruptHeaderAndInfinite(headers).get(); testContext.assertEquals(headerValue, "" + returnedExpireValue); }
@Test public void getExpireAfterConcerningCaseOfCorruptHeaderAndInfinite_returnsNullIfHeaderNotPresent(TestContext testContext) throws Exception { final String headerValue = null; final MultiMap headers = createHeaderReturningSimpleValue(HTTP_HEADER_X_EXPIRE_AFTER, headerValue); final boolean didReturnSomething = ExpiryCheckHandler.getExpireAfterConcerningCaseOfCorruptHeaderAndInfinite(headers).isPresent(); testContext.assertFalse(didReturnSomething); }
@Test public void getExpireAfterConcerningCaseOfCorruptHeaderAndInfinite_returnsMinusOneWhenHeaderMinusOne(TestContext testContext) throws Exception { final String headerValue = "-1"; final MultiMap headers = createHeaderReturningSimpleValue(HTTP_HEADER_X_EXPIRE_AFTER, headerValue); final int returnedExpireValue = ExpiryCheckHandler.getExpireAfterConcerningCaseOfCorruptHeaderAndInfinite(headers).get(); testContext.assertEquals(headerValue, "" + returnedExpireValue); }
@Test public void getExpireAfterConcerningCaseOfCorruptHeaderAndInfinite_concealsNumberFormatExceptionIfHeaderValueBelowMinusOne(TestContext testContext) { final String headerValue = "-2143483640"; final MultiMap headers = createHeaderReturningSimpleValue(HTTP_HEADER_X_EXPIRE_AFTER, headerValue); ExpiryCheckHandler.getExpireAfterConcerningCaseOfCorruptHeaderAndInfinite(headers); }