DefaultConnection implements Connection { @Override public void handleRetryAfter(String message) throws AcmeException { assertConnectionIsOpen(); Optional<Instant> retryAfter = getRetryAfterHeader(); if (retryAfter.isPresent()) { throw new AcmeRetryAfterException(message, retryAfter.get()); } } DefaultConnection(HttpConnector httpConnector); @Override void resetNonce(Session session); @Override int sendRequest(URL url, Session session, @Nullable ZonedDateTime ifModifiedSince); @Override int sendCertificateRequest(URL url, Login login); @Override int sendSignedPostAsGetRequest(URL url, Login login); @Override int sendSignedRequest(URL url, JSONBuilder claims, Login login); @Override int sendSignedRequest(URL url, JSONBuilder claims, Session session, KeyPair keypair); @Override JSON readJsonResponse(); @Override List<X509Certificate> readCertificates(); @Override void handleRetryAfter(String message); @Override @Nullable String getNonce(); @Override @Nullable URL getLocation(); @Override Optional<ZonedDateTime> getLastModified(); @Override Optional<ZonedDateTime> getExpiration(); @Override Collection<URL> getLinks(String relation); @Override void close();  }
@Test public void testHandleRetryAfterHeaderDate() throws AcmeException, IOException { Instant retryDate = Instant.now().plus(Duration.ofHours(10)).truncatedTo(ChronoUnit.MILLIS); String retryMsg = "absolute date"; when(mockUrlConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK); when(mockUrlConnection.getHeaderField("Retry-After")).thenReturn(retryDate.toString()); when(mockUrlConnection.getHeaderFieldDate("Retry-After", 0L)).thenReturn(retryDate.toEpochMilli()); try (DefaultConnection conn = new DefaultConnection(mockHttpConnection)) { conn.conn = mockUrlConnection; conn.handleRetryAfter(retryMsg); fail("no AcmeRetryAfterException was thrown"); } catch (AcmeRetryAfterException ex) { assertThat(ex.getRetryAfter(), is(retryDate)); assertThat(ex.getMessage(), is(retryMsg)); } verify(mockUrlConnection, atLeastOnce()).getHeaderField("Retry-After"); }
@Test public void testHandleRetryAfterHeaderDelta() throws AcmeException, IOException { int delta = 10 * 60 * 60; long now = System.currentTimeMillis(); String retryMsg = "relative time"; when(mockUrlConnection.getResponseCode()) .thenReturn(HttpURLConnection.HTTP_OK); when(mockUrlConnection.getHeaderField("Retry-After")) .thenReturn(String.valueOf(delta)); when(mockUrlConnection.getHeaderFieldDate( ArgumentMatchers.eq("Date"), ArgumentMatchers.anyLong())) .thenReturn(now); try (DefaultConnection conn = new DefaultConnection(mockHttpConnection)) { conn.conn = mockUrlConnection; conn.handleRetryAfter(retryMsg); fail("no AcmeRetryAfterException was thrown"); } catch (AcmeRetryAfterException ex) { assertThat(ex.getRetryAfter(), is(Instant.ofEpochMilli(now).plusSeconds(delta))); assertThat(ex.getMessage(), is(retryMsg)); } verify(mockUrlConnection, atLeastOnce()).getHeaderField("Retry-After"); }
@Test public void testHandleRetryAfterHeaderNull() throws AcmeException, IOException { when(mockUrlConnection.getResponseCode()) .thenReturn(HttpURLConnection.HTTP_OK); when(mockUrlConnection.getHeaderField("Retry-After")) .thenReturn(null); try (DefaultConnection conn = new DefaultConnection(mockHttpConnection)) { conn.conn = mockUrlConnection; conn.handleRetryAfter("no header"); } catch (AcmeRetryAfterException ex) { fail("an AcmeRetryAfterException was thrown"); } verify(mockUrlConnection, atLeastOnce()).getHeaderField("Retry-After"); }
@Test public void testHandleRetryAfterNotAccepted() throws AcmeException, IOException { when(mockUrlConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK); try (DefaultConnection conn = new DefaultConnection(mockHttpConnection)) { conn.conn = mockUrlConnection; conn.handleRetryAfter("http ok"); } catch (AcmeRetryAfterException ex) { fail("an AcmeRetryAfterException was thrown"); } }