DefaultConnection implements Connection { @Override @Nullable public String getNonce() { assertConnectionIsOpen(); String nonceHeader = conn.getHeaderField(REPLAY_NONCE_HEADER); if (nonceHeader == null || nonceHeader.trim().isEmpty()) { return null; } if (!AcmeUtils.isValidBase64Url(nonceHeader)) { throw new AcmeProtocolException("Invalid replay nonce: " + nonceHeader); } LOG.debug("Replay Nonce: {}", nonceHeader); return nonceHeader; } 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 testNoNonceFromHeader() { when(mockUrlConnection.getHeaderField("Replay-Nonce")).thenReturn(null); assertThat(session.getNonce(), is(nullValue())); try (DefaultConnection conn = new DefaultConnection(mockHttpConnection)) { conn.conn = mockUrlConnection; assertThat(conn.getNonce(), is(nullValue())); } verify(mockUrlConnection).getHeaderField("Replay-Nonce"); verifyNoMoreInteractions(mockUrlConnection); }
@Test public void testGetNonceFromHeader() { when(mockUrlConnection.getHeaderField("Replay-Nonce")) .thenReturn(TestUtils.DUMMY_NONCE); try (DefaultConnection conn = new DefaultConnection(mockHttpConnection)) { conn.conn = mockUrlConnection; assertThat(conn.getNonce(), is(TestUtils.DUMMY_NONCE)); } verify(mockUrlConnection).getHeaderField("Replay-Nonce"); verifyNoMoreInteractions(mockUrlConnection); }
@Test public void testInvalidNonceFromHeader() { String badNonce = "#$%&/*+*#'"; when(mockUrlConnection.getHeaderField("Replay-Nonce")).thenReturn(badNonce); try (DefaultConnection conn = new DefaultConnection(mockHttpConnection)) { conn.conn = mockUrlConnection; conn.getNonce(); fail("Expected to fail"); } catch (AcmeProtocolException ex) { assertThat(ex.getMessage(), org.hamcrest.Matchers.startsWith("Invalid replay nonce")); } verify(mockUrlConnection).getHeaderField("Replay-Nonce"); verifyNoMoreInteractions(mockUrlConnection); }