DefaultConnection implements Connection { @Override public void resetNonce(Session session) throws AcmeException { assertConnectionIsClosed(); try { session.setNonce(null); URL newNonceUrl = session.resourceUrl(Resource.NEW_NONCE); LOG.debug("HEAD {}", newNonceUrl); conn = httpConnector.openConnection(newNonceUrl, session.networkSettings()); conn.setRequestMethod("HEAD"); conn.setRequestProperty(ACCEPT_LANGUAGE_HEADER, session.getLocale().toLanguageTag()); conn.connect(); logHeaders(); int rc = conn.getResponseCode(); if (rc != HttpURLConnection.HTTP_OK && rc != HttpURLConnection.HTTP_NO_CONTENT) { throwAcmeException(); } String nonce = getNonce(); if (nonce == null) { throw new AcmeProtocolException("Server did not provide a nonce"); } session.setNonce(nonce); } catch (IOException ex) { throw new AcmeNetworkException(ex); } finally { conn = null; } } 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 testResetNonce() throws AcmeException, IOException { when(mockHttpConnection.openConnection(eq(new URL("https: .thenReturn(mockUrlConnection); when(mockUrlConnection.getResponseCode()) .thenReturn(HttpURLConnection.HTTP_NO_CONTENT); assertThat(session.getNonce(), is(nullValue())); try (DefaultConnection conn = new DefaultConnection(mockHttpConnection)) { conn.resetNonce(session); fail("missing Replay-Nonce header not detected"); } catch (AcmeProtocolException ex) { } assertThat(session.getNonce(), is(nullValue())); when(mockUrlConnection.getHeaderField("Replay-Nonce")) .thenReturn(TestUtils.DUMMY_NONCE); try (DefaultConnection conn = new DefaultConnection(mockHttpConnection)) { conn.resetNonce(session); } assertThat(session.getNonce(), is(TestUtils.DUMMY_NONCE)); verify(mockUrlConnection, atLeastOnce()).setRequestMethod("HEAD"); verify(mockUrlConnection, atLeastOnce()).setRequestProperty("Accept-Language", "ja-JP"); verify(mockUrlConnection, atLeastOnce()).connect(); verify(mockUrlConnection, atLeastOnce()).getResponseCode(); verify(mockUrlConnection, atLeastOnce()).getHeaderField("Replay-Nonce"); verify(mockUrlConnection, atLeastOnce()).getHeaderFields(); verifyNoMoreInteractions(mockUrlConnection); }