AbstractAcmeProvider implements AcmeProvider { @Override public JSON directory(Session session, URI serverUri) throws AcmeException { ZonedDateTime expires = session.getDirectoryExpires(); if (expires != null && expires.isAfter(ZonedDateTime.now())) { return null; } try (Connection conn = connect(serverUri)) { ZonedDateTime lastModified = session.getDirectoryLastModified(); int rc = conn.sendRequest(resolve(serverUri), session, lastModified); if (lastModified != null && rc == HttpURLConnection.HTTP_NOT_MODIFIED) { return null; } session.setDirectoryLastModified(conn.getLastModified().orElse(null)); session.setDirectoryExpires(conn.getExpiration().orElse(null)); String nonce = conn.getNonce(); if (nonce != null) { session.setNonce(nonce); } return conn.readJsonResponse(); } }  @Override Connection connect(URI serverUri); @Override JSON directory(Session session, URI serverUri); @Override Challenge createChallenge(Login login, JSON data);  }
@Test public void testResources() throws AcmeException { final Connection connection = mock(Connection.class); final Session session = mock(Session.class); when(connection.readJsonResponse()).thenReturn(getJSON("directory")); AbstractAcmeProvider provider = new TestAbstractAcmeProvider(connection); JSON map = provider.directory(session, SERVER_URI); assertThat(map.toString(), sameJSONAs(TestUtils.getJSON("directory").toString())); verify(connection).sendRequest(RESOLVED_URL, session, null); verify(connection).getNonce(); verify(connection).getLastModified(); verify(connection).getExpiration(); verify(connection).readJsonResponse(); verify(connection).close(); verifyNoMoreInteractions(connection); }
@Test public void testResourcesCacheControl() throws AcmeException { ZonedDateTime lastModified = ZonedDateTime.now().minus(13, ChronoUnit.DAYS); ZonedDateTime expiryDate = ZonedDateTime.now().plus(60, ChronoUnit.DAYS); final Connection connection = mock(Connection.class); final Session session = mock(Session.class); when(connection.readJsonResponse()).thenReturn(getJSON("directory")); when(connection.getLastModified()).thenReturn(Optional.of(lastModified)); when(connection.getExpiration()).thenReturn(Optional.of(expiryDate)); when(session.getDirectoryExpires()).thenReturn(null); when(session.getDirectoryLastModified()).thenReturn(null); AbstractAcmeProvider provider = new TestAbstractAcmeProvider(connection); JSON map = provider.directory(session, SERVER_URI); assertThat(map.toString(), sameJSONAs(TestUtils.getJSON("directory").toString())); verify(session).setDirectoryLastModified(eq(lastModified)); verify(session).setDirectoryExpires(eq(expiryDate)); verify(session).getDirectoryExpires(); verify(session).getDirectoryLastModified(); verifyNoMoreInteractions(session); verify(connection).sendRequest(RESOLVED_URL, session, null); verify(connection).getNonce(); verify(connection).getLastModified(); verify(connection).getExpiration(); verify(connection).readJsonResponse(); verify(connection).close(); verifyNoMoreInteractions(connection); }
@Test public void testResourcesNotExprired() throws AcmeException { ZonedDateTime expiryDate = ZonedDateTime.now().plus(60, ChronoUnit.DAYS); final Connection connection = mock(Connection.class); final Session session = mock(Session.class); when(session.getDirectoryExpires()).thenReturn(expiryDate); AbstractAcmeProvider provider = new TestAbstractAcmeProvider(); JSON map = provider.directory(session, SERVER_URI); assertThat(map, is(nullValue())); verify(session).getDirectoryExpires(); verifyNoMoreInteractions(session); verifyNoMoreInteractions(connection); }
@Test public void testResourcesExprired() throws AcmeException { ZonedDateTime expiryDate = ZonedDateTime.now().plus(60, ChronoUnit.DAYS); ZonedDateTime pastExpiryDate = ZonedDateTime.now().minus(10, ChronoUnit.MINUTES); final Connection connection = mock(Connection.class); final Session session = mock(Session.class); when(connection.readJsonResponse()).thenReturn(getJSON("directory")); when(connection.getExpiration()).thenReturn(Optional.of(expiryDate)); when(connection.getLastModified()).thenReturn(Optional.empty()); when(session.getDirectoryExpires()).thenReturn(pastExpiryDate); AbstractAcmeProvider provider = new TestAbstractAcmeProvider(connection); JSON map = provider.directory(session, SERVER_URI); assertThat(map.toString(), sameJSONAs(TestUtils.getJSON("directory").toString())); verify(session).setDirectoryExpires(eq(expiryDate)); verify(session).setDirectoryLastModified(eq(null)); verify(session).getDirectoryExpires(); verify(session).getDirectoryLastModified(); verifyNoMoreInteractions(session); verify(connection).sendRequest(RESOLVED_URL, session, null); verify(connection).getNonce(); verify(connection).getLastModified(); verify(connection).getExpiration(); verify(connection).readJsonResponse(); verify(connection).close(); verifyNoMoreInteractions(connection); }
@Test public void testResourcesIfModifiedSince() throws AcmeException { ZonedDateTime modifiedSinceDate = ZonedDateTime.now().minus(60, ChronoUnit.DAYS); final Connection connection = mock(Connection.class); final Session session = mock(Session.class); when(connection.sendRequest(eq(RESOLVED_URL), eq(session), eq(modifiedSinceDate))) .thenReturn(HttpURLConnection.HTTP_NOT_MODIFIED); when(connection.getLastModified()).thenReturn(Optional.of(modifiedSinceDate)); when(session.getDirectoryLastModified()).thenReturn(modifiedSinceDate); AbstractAcmeProvider provider = new TestAbstractAcmeProvider(connection); JSON map = provider.directory(session, SERVER_URI); assertThat(map, is(nullValue())); verify(session).getDirectoryExpires(); verify(session).getDirectoryLastModified(); verifyNoMoreInteractions(session); verify(connection).sendRequest(RESOLVED_URL, session, modifiedSinceDate); verify(connection).close(); verifyNoMoreInteractions(connection); }
@Test public void testResourcesHttpError() throws AcmeException, IOException { final HttpURLConnection conn = mock(HttpURLConnection.class); final HttpConnector connector = mock(HttpConnector.class); final Connection connection = new DefaultConnection(connector); when(connector.openConnection(any(), any())).thenReturn(conn); when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_INTERNAL_ERROR); when(conn.getResponseMessage()).thenReturn("Internal error"); AbstractAcmeProvider provider = new TestAbstractAcmeProvider(connection); Session session = TestUtils.session(provider); try { provider.directory(session, SERVER_URI); fail("HTTP error was ignored"); } catch (AcmeException ex) { } }