DefaultConnection implements Connection { @Override public int sendSignedPostAsGetRequest(URL url, Login login) throws AcmeException { return sendSignedRequest(url, null, login.getSession(), login.getKeyPair(), login.getAccountLocation(), MIME_JSON); } 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 testSendSignedPostAsGetRequest() throws Exception { final String nonce1 = URL_ENCODER.encodeToString("foo-nonce-1-foo".getBytes()); final String nonce2 = URL_ENCODER.encodeToString("foo-nonce-2-foo".getBytes()); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); when(mockUrlConnection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK); when(mockUrlConnection.getOutputStream()).thenReturn(outputStream); try (DefaultConnection conn = new DefaultConnection(mockHttpConnection) { @Override public void resetNonce(Session session) { assertThat(session, is(sameInstance(DefaultConnectionTest.this.session))); if (session.getNonce() == null) { session.setNonce(nonce1); } else { fail("unknown nonce"); } } @Override public String getNonce() { assertThat(session, is(sameInstance(DefaultConnectionTest.this.session))); if (session.getNonce() == nonce1) { return nonce2; } else { fail("unknown nonce"); return null; } } }) { conn.sendSignedPostAsGetRequest(requestUrl, login); } verify(mockUrlConnection).setRequestMethod("POST"); verify(mockUrlConnection).setRequestProperty("Accept", "application/json"); verify(mockUrlConnection).setRequestProperty("Accept-Charset", "utf-8"); verify(mockUrlConnection).setRequestProperty("Accept-Language", "ja-JP"); verify(mockUrlConnection).setRequestProperty("Content-Type", "application/jose+json"); verify(mockUrlConnection).connect(); verify(mockUrlConnection).setDoOutput(true); verify(mockUrlConnection).setFixedLengthStreamingMode(outputStream.toByteArray().length); verify(mockUrlConnection).getResponseCode(); verify(mockUrlConnection).getOutputStream(); verify(mockUrlConnection, atLeast(0)).getHeaderFields(); verifyNoMoreInteractions(mockUrlConnection); JSON data = JSON.parse(new String(outputStream.toByteArray(), "utf-8")); String encodedHeader = data.get("protected").asString(); String encodedSignature = data.get("signature").asString(); String encodedPayload = data.get("payload").asString(); StringBuilder expectedHeader = new StringBuilder(); expectedHeader.append('{'); expectedHeader.append("\"nonce\":\"").append(nonce1).append("\","); expectedHeader.append("\"url\":\"").append(requestUrl).append("\","); expectedHeader.append("\"alg\":\"RS256\","); expectedHeader.append("\"kid\":\"").append(accountUrl).append('"'); expectedHeader.append('}'); assertThat(new String(URL_DECODER.decode(encodedHeader), UTF_8), sameJSONAs(expectedHeader.toString())); assertThat(new String(URL_DECODER.decode(encodedPayload), UTF_8), is("")); assertThat(encodedSignature, not(emptyOrNullString())); JsonWebSignature jws = new JsonWebSignature(); jws.setCompactSerialization(CompactSerializer.serialize(encodedHeader, encodedPayload, encodedSignature)); jws.setKey(login.getKeyPair().getPublic()); assertThat(jws.verifySignature(), is(true)); }