DigestAuthHeaderFunction implements Function { public String execute(List<String> parameterList, TestContext context) { if (parameterList == null || parameterList.size() < 8) { throw new InvalidFunctionUsageException("Function parameters not set correctly - need parameters: username,password,realm,noncekey,method,uri,opaque,algorithm"); } StringBuilder authorizationHeader = new StringBuilder(); String username = parameterList.get(0); String password = parameterList.get(1); String realm = parameterList.get(2); String noncekey = parameterList.get(3); String method = parameterList.get(4); String uri = parameterList.get(5); String opaque = parameterList.get(6); String algorithm = parameterList.get(7); String digest1 = username + ":" + realm + ":" + password; String digest2 = method + ":" + uri; Long expirationTime = System.currentTimeMillis() + nonceValidity; String nonce = Base64.encodeBase64String((expirationTime + ":" + getDigestHex(algorithm, expirationTime + ":" + noncekey)).getBytes()); authorizationHeader.append("Digest username="); authorizationHeader.append(username); authorizationHeader.append(",realm="); authorizationHeader.append(realm); authorizationHeader.append(",nonce="); authorizationHeader.append(nonce); authorizationHeader.append(",uri="); authorizationHeader.append(uri); authorizationHeader.append(",response="); authorizationHeader.append(getDigestHex(algorithm, getDigestHex(algorithm, digest1) + ":" + nonce + ":" + getDigestHex(algorithm, digest2))); authorizationHeader.append(",opaque="); authorizationHeader.append(getDigestHex(algorithm, opaque)); authorizationHeader.append(",algorithm="); authorizationHeader.append(algorithm); return authorizationHeader.toString(); }  String execute(List<String> parameterList, TestContext context); void setNonceValidity(Long nonceValidity);  }
@Test public void testFunction() { String digestHeader = function.execute(FunctionParameterHelper.getParameterList("'username', 'password', 'authRealm', 'acegi', 'POST', 'http: Assert.assertTrue(digestHeader.startsWith("Digest username=username,realm=authRealm,nonce=")); Assert.assertTrue(digestHeader.contains("uri=http: Assert.assertTrue(digestHeader.contains("algorithm=md5")); }
@Test(expectedExceptions = {InvalidFunctionUsageException.class}) public void testNoParameters() { function.execute(Collections.<String>emptyList(), context); }
@Test(expectedExceptions = {InvalidFunctionUsageException.class}) public void testMissingParameters() { function.execute(FunctionParameterHelper.getParameterList("'username', 'password', 'authRealm', 'http: }