@Test
@TestDir
@TestJetty
@TestHdfs
public void testDelegationTokenOperations() throws Exception {

  createHttpFSServer(true);
  URL url = new URL(TestJettyHelper.getJettyURL(),
                    "/webhdfs/v1/?op=GETHOMEDIRECTORY");
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  Assert.assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED,
                      conn.getResponseCode());
  AuthenticationToken token =
    new AuthenticationToken("u", "p",
      HttpFSKerberosAuthenticationHandlerForTesting.TYPE);
  token.setExpires(System.currentTimeMillis() + 100000000);
  Signer signer = new Signer("secret".getBytes());
  String tokenSigned = signer.sign(token.toString());
  url = new URL(TestJettyHelper.getJettyURL(),
                "/webhdfs/v1/?op=GETHOMEDIRECTORY");
  conn = (HttpURLConnection) url.openConnection();
  conn.setRequestProperty("Cookie",
                          AuthenticatedURL.AUTH_COOKIE  + "=" + tokenSigned);
  Assert.assertEquals(HttpURLConnection.HTTP_OK,
                      conn.getResponseCode());
  url = new URL(TestJettyHelper.getJettyURL(),
                "/webhdfs/v1/?op=GETDELEGATIONTOKEN");
  conn = (HttpURLConnection) url.openConnection();
  conn.setRequestProperty("Cookie",
                          AuthenticatedURL.AUTH_COOKIE  + "=" + tokenSigned);
  Assert.assertEquals(HttpURLConnection.HTTP_OK,
                      conn.getResponseCode());
  JSONObject json = (JSONObject)
    new JSONParser().parse(new InputStreamReader(conn.getInputStream()));
  json = (JSONObject)
    json.get(HttpFSKerberosAuthenticator.DELEGATION_TOKEN_JSON);
  String tokenStr = (String)
      json.get(HttpFSKerberosAuthenticator.DELEGATION_TOKEN_URL_STRING_JSON);
  url = new URL(TestJettyHelper.getJettyURL(),
                "/webhdfs/v1/?op=GETHOMEDIRECTORY&delegation=" + tokenStr);
  conn = (HttpURLConnection) url.openConnection();
  Assert.assertEquals(HttpURLConnection.HTTP_OK,
                      conn.getResponseCode());
  url = new URL(TestJettyHelper.getJettyURL(),
                "/webhdfs/v1/?op=RENEWDELEGATIONTOKEN&token=" + tokenStr);
  conn = (HttpURLConnection) url.openConnection();
  conn.setRequestMethod("PUT");
  Assert.assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED,
                      conn.getResponseCode());
  url = new URL(TestJettyHelper.getJettyURL(),
                "/webhdfs/v1/?op=RENEWDELEGATIONTOKEN&token=" + tokenStr);
  conn = (HttpURLConnection) url.openConnection();
  conn.setRequestMethod("PUT");
  conn.setRequestProperty("Cookie",
                          AuthenticatedURL.AUTH_COOKIE  + "=" + tokenSigned);
  Assert.assertEquals(HttpURLConnection.HTTP_OK,
                      conn.getResponseCode());
  url = new URL(TestJettyHelper.getJettyURL(),
                "/webhdfs/v1/?op=CANCELDELEGATIONTOKEN&token=" + tokenStr);
  conn = (HttpURLConnection) url.openConnection();
  conn.setRequestMethod("PUT");
  Assert.assertEquals(HttpURLConnection.HTTP_OK,
                      conn.getResponseCode());
  url = new URL(TestJettyHelper.getJettyURL(),
                "/webhdfs/v1/?op=GETHOMEDIRECTORY&delegation=" + tokenStr);
  conn = (HttpURLConnection) url.openConnection();
  Assert.assertEquals(HttpURLConnection.HTTP_FORBIDDEN,
                      conn.getResponseCode());
}