@Test(timeout=5000)
public void testWebAppProxyServlet() throws Exception {

  configuration.set(YarnConfiguration.PROXY_ADDRESS, "localhost:9090");
  // overriding num of web server threads, see HttpServer.HTTP_MAXTHREADS 
  configuration.setInt("hadoop.http.max.threads", 5);
  WebAppProxyServerForTest proxy = new WebAppProxyServerForTest();
  proxy.init(configuration);
  proxy.start();
  
  int proxyPort = proxy.proxy.proxyServer.getConnectorAddress(0).getPort();
  AppReportFetcherForTest appReportFetcher = proxy.proxy.appReportFetcher;
  // wrong url
  try {
    // wrong url. Set wrong app ID
    URL wrongUrl = new URL("http://localhost:" + proxyPort + "/proxy/app");
    HttpURLConnection proxyConn = (HttpURLConnection) wrongUrl
        .openConnection();
    proxyConn.connect();
    assertEquals(HttpURLConnection.HTTP_INTERNAL_ERROR,
        proxyConn.getResponseCode());
    // set true Application ID in url
    URL url = new URL("http://localhost:" + proxyPort + "/proxy/application_00_0");
    proxyConn = (HttpURLConnection) url.openConnection();
    // set cookie
    proxyConn.setRequestProperty("Cookie", "checked_application_0_0000=true");
    proxyConn.connect();
    assertEquals(HttpURLConnection.HTTP_OK, proxyConn.getResponseCode());
    assertTrue(isResponseCookiePresent(
        proxyConn, "checked_application_0_0000", "true"));
    // cannot found application 1: null
    appReportFetcher.answer = 1;
    proxyConn = (HttpURLConnection) url.openConnection();
    proxyConn.setRequestProperty("Cookie", "checked_application_0_0000=true");
    proxyConn.connect();
    assertEquals(HttpURLConnection.HTTP_NOT_FOUND,
        proxyConn.getResponseCode());
    assertFalse(isResponseCookiePresent(
        proxyConn, "checked_application_0_0000", "true"));
    // cannot found application 2: ApplicationNotFoundException
    appReportFetcher.answer = 4;
    proxyConn = (HttpURLConnection) url.openConnection();
    proxyConn.setRequestProperty("Cookie", "checked_application_0_0000=true");
    proxyConn.connect();
    assertEquals(HttpURLConnection.HTTP_NOT_FOUND,
        proxyConn.getResponseCode());
    assertFalse(isResponseCookiePresent(
        proxyConn, "checked_application_0_0000", "true"));
    // wrong user
    appReportFetcher.answer = 2;
    proxyConn = (HttpURLConnection) url.openConnection();
    proxyConn.connect();
    assertEquals(HttpURLConnection.HTTP_OK, proxyConn.getResponseCode());
    String s = readInputStream(proxyConn.getInputStream());
    assertTrue(s
        .contains("to continue to an Application Master web interface owned by"));
    assertTrue(s.contains("WARNING: The following page may not be safe!"));
    //case if task has a not running status
    appReportFetcher.answer = 3;
    proxyConn = (HttpURLConnection) url.openConnection();
    proxyConn.setRequestProperty("Cookie", "checked_application_0_0000=true");
    proxyConn.connect();
    assertEquals(HttpURLConnection.HTTP_OK, proxyConn.getResponseCode());
    // test user-provided path and query parameter can be appended to the
    // original tracking url
    appReportFetcher.answer = 5;
    URL clientUrl = new URL("http://localhost:" + proxyPort
      + "/proxy/application_00_0/test/tez?x=y&h=p");
    proxyConn = (HttpURLConnection) clientUrl.openConnection();
    proxyConn.connect();
    LOG.info("" + proxyConn.getURL());
    LOG.info("ProxyConn.getHeaderField(): " +  proxyConn.getHeaderField(ProxyUtils.LOCATION));
    assertEquals("http://localhost:" + originalPort
        + "/foo/bar/test/tez?a=b&x=y&h=p#main", proxyConn.getURL().toString());
  } finally {
    proxy.close();
  }
}