### Question:
AsyncUtil { public static Future<Response> login(final AsyncHttpClient client, final Signer signer, final Request loginRequest) throws IOException { return login(client, signer, loginRequest, new AsyncCompletionHandler<Response>() { @Override public Response onCompleted(Response response) throws Exception { return response; } }); } private  AsyncUtil(); static void enableAuth(final AsyncHttpClient client, final Signer signer); static void enableAuth(final AsyncHttpClient client, final Signer signer, final SignatureCalculator calcBefore); static Future<Response> login(final AsyncHttpClient client,
                                         final Signer signer,
                                         final Request loginRequest); static Future<T> login(final AsyncHttpClient client,
                                      final Signer signer,
                                      final Request loginRequest,
                                      AsyncCompletionHandler<T> responseHandler); static Future<T> login(final AsyncHttpClient client,
                             final Signer signer,
                             final Request loginRequest,
                             final AsyncCompletionHandler<T> responseHandler,
                             final SignatureCalculator calcBefore); static void calculateSignature(Signer signer, Request request, RequestBuilderBase<?> requestBuilder);  }### Answer:
@Test public void testLogin() { TestBody.test(new HttpServerTestBody() { @Override protected void execute() throws Exception { List<String> headers = Arrays.asList( Constants.HEADER_REQUEST_TARGET, Constants.HEADER_DATE); setServlet(new AdminServlet(headers)); KeyPair keyPair = KeyTestUtil.getKeyPairFromProperties("b2048", "id_rsa"); DefaultKeychain provider = new DefaultKeychain(); provider.add(new SSHKey(KeyFormat.SSH_RSA, keyPair)); AsyncHttpClient client = new AsyncHttpClient(); Signer signer = new Signer(provider, getKeyId()); Boolean response = AsyncUtil.login( client, signer, client.prepareGet(getAbsoluteUrl(TEST_URL)).build(), DEFAULT_HANDLER ).get(); assertTrue("login should be successful", response); } } ); }
@Test public void testAllHeaders() { TestBody.test(new HttpServerTestBody() { @Override protected void execute() throws Exception { List<String> headers = Arrays.asList( Constants.HEADER_REQUEST_TARGET, Constants.HEADER_DATE, "x-test" ); setServlet(new AdminServlet(headers)); KeyPair keyPair = KeyTestUtil.getKeyPairFromProperties("b2048", "id_rsa"); DefaultKeychain keychain = new DefaultKeychain(); keychain.add(new SSHKey(KeyFormat.SSH_RSA, keyPair)); Signer signer = new Signer(keychain, getKeyId()); AsyncHttpClient client = new AsyncHttpClient(); Boolean badResponse = AsyncUtil.login( client, signer, client.prepareGet(getAbsoluteUrl(TEST_URL)).build(), DEFAULT_HANDLER ).get(); assertFalse("login should not be successful", badResponse); Request goodRequestNoLogin = client.prepareGet(getAbsoluteUrl(TEST_URL)).addHeader("x-test", "foo").build(); Boolean notLoggedIn = client.executeRequest(goodRequestNoLogin, DEFAULT_HANDLER).get(); assertFalse("bad subsequent request is not successful", notLoggedIn); signer.rotateKeys(); Boolean goodResponse = AsyncUtil.login( client, signer, client.prepareGet(getAbsoluteUrl(TEST_URL)) .addHeader("x-test", "foo").build(), DEFAULT_HANDLER ).get(); assertTrue("login should be successful", goodResponse); Request goodRequestAfterLogin = client.prepareGet(getAbsoluteUrl(TEST_URL)).addHeader("x-test", "foo").build(); Boolean loggedIn = client.executeRequest(goodRequestAfterLogin, DEFAULT_HANDLER).get(); assertTrue("good subsequent request is successful", loggedIn); } }); }