DefaultConnection implements Connection { @Override public Optional<ZonedDateTime> getLastModified() { assertConnectionIsOpen(); String header = conn.getHeaderField(LAST_MODIFIED_HEADER); if (header != null) { try { return Optional.of(ZonedDateTime.parse(header, RFC_1123_DATE_TIME)); } catch (DateTimeParseException ex) { LOG.debug("Ignored invalid Last-Modified date: {}", header, ex); } } return Optional.empty(); } 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 testLastModifiedUnset() { when(mockUrlConnection.getHeaderField("Last-Modified")).thenReturn(null); try (DefaultConnection conn = new DefaultConnection(mockHttpConnection)) { conn.conn = mockUrlConnection; assertThat(conn.getLastModified().isPresent(), is(false)); } verify(mockUrlConnection).getHeaderField("Last-Modified"); verifyNoMoreInteractions(mockUrlConnection); }
@Test public void testLastModifiedSet() { when(mockUrlConnection.getHeaderField("Last-Modified")).thenReturn("Thu, 07 May 2020 19:42:46 GMT"); try (DefaultConnection conn = new DefaultConnection(mockHttpConnection)) { conn.conn = mockUrlConnection; Optional<ZonedDateTime> lm = conn.getLastModified(); assertThat(lm.isPresent(), is(true)); assertThat(lm.get().format(DateTimeFormatter.ISO_DATE_TIME), is("2020-05-07T19:42:46Z")); } verify(mockUrlConnection).getHeaderField("Last-Modified"); verifyNoMoreInteractions(mockUrlConnection); }
@Test public void testLastModifiedInvalid() { when(mockUrlConnection.getHeaderField("Last-Modified")).thenReturn("iNvAlId"); try (DefaultConnection conn = new DefaultConnection(mockHttpConnection)) { conn.conn = mockUrlConnection; assertThat(conn.getLastModified().isPresent(), is(false)); } verify(mockUrlConnection).getHeaderField("Last-Modified"); verifyNoMoreInteractions(mockUrlConnection); }