### Question:
Http4Util { public static void enableAuth(final AbstractHttpClient client, final Keychain keychain, final KeyId keyId) { if (client == null) { throw new NullPointerException("client"); } if (keychain == null) { throw new NullPointerException("keychain"); } client.getAuthSchemes().register(Constants.SCHEME, new AuthSchemeFactory() { public AuthScheme newInstance(HttpParams params) { return new Http4SignatureAuthScheme(); } }); Signer signer = new Signer(keychain, keyId); client.getCredentialsProvider().setCredentials(AuthScope.ANY, new SignerCredentials(signer)); client.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF, Arrays.asList(Constants.SCHEME)); HttpClientParams.setAuthenticating(client.getParams(), true); } private  Http4Util(); static void enableAuth(final AbstractHttpClient client, final Keychain keychain, final KeyId keyId);  }### 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)); DefaultHttpClient client = new DefaultHttpClient(); Http4Util.enableAuth(client, provider, getKeyId()); HttpUriRequest request = new HttpGet(getAbsoluteUrl(TEST_URL)); HttpResponse response = client.execute(request); assertEquals("should return 200", 200, response.getStatusLine().getStatusCode()); } }); }
@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 provider = new DefaultKeychain(); provider.add(new SSHKey(KeyFormat.SSH_RSA, keyPair)); DefaultHttpClient client = new DefaultHttpClient(); Http4Util.enableAuth(client, provider, getKeyId()); HttpUriRequest badRequest = new HttpGet(getAbsoluteUrl(TEST_URL)); HttpResponse badResponse = client.execute(badRequest); badResponse.getEntity().writeTo(new NullOutputStream()); assertEquals("should return 401", 401, badResponse.getStatusLine().getStatusCode()); HttpUriRequest goodRequest = new HttpGet(getAbsoluteUrl(TEST_URL)); goodRequest.addHeader("x-test", "foo"); HttpResponse goodResponse = client.execute(goodRequest); goodResponse.getEntity().writeTo(new NullOutputStream()); assertEquals("should return 200", 200, goodResponse.getStatusLine().getStatusCode()); } }); }