/** Test what happens if a custom 404 page is configured, but its file is actually missing. */
  @Test
  public void testCustomErrorPageMissing() throws Exception {
    File appDir = new File(getTemporaryDirectory(), "MyApp");
    File webInf = new File(appDir, "WEB-INF");
    addDeleteOnTearDown(appDir);
    if (!webInf.mkdirs() && !webInf.isDirectory()) {
      fail("Unable to create directory [" + webInf + "]");
    }

    File webxml = new File(appDir, "WEB-INF/web.xml");
    try (FileOutputStream fos = new FileOutputStream(webxml);
        Writer w = new OutputStreamWriter(fos, "UTF-8"); ) {
      w.write(
          "<?xml version='1.0' encoding='UTF-8'?>\n"
              + "<web-app xmlns='http://java.sun.com/xml/ns/j2ee' "
              + " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"
              + " xsi:schemaLocation='http://java.sun.com/xml/ns/j2ee "
              + " http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd'"
              + " version='2.4'>\n"
              + "<error-page>\n<error-code>404</error-code>\n"
              + "<location>/404-absent.html</location>\n</error-page>\n"
              + "</web-app>\n");
    }

    Tomcat tomcat = getTomcatInstance();
    String contextPath = "/MyApp";
    tomcat.addWebapp(null, contextPath, appDir.getAbsolutePath());
    tomcat.start();

    TestCustomErrorClient client = new TestCustomErrorClient(tomcat.getConnector().getLocalPort());

    client.reset();
    client.setRequest(new String[] {"GET /MyApp/missing HTTP/1.0" + CRLF + CRLF});
    client.connect();
    client.processRequest();
    assertTrue(client.isResponse404());
  }
  /** Test https://issues.apache.org/bugzilla/show_bug.cgi?id=50413 Serving a custom error page */
  @Test
  public void testCustomErrorPage() throws Exception {
    File appDir = new File(getTemporaryDirectory(), "MyApp");
    File webInf = new File(appDir, "WEB-INF");
    addDeleteOnTearDown(appDir);
    if (!webInf.mkdirs() && !webInf.isDirectory()) {
      fail("Unable to create directory [" + webInf + "]");
    }

    File webxml = new File(appDir, "WEB-INF/web.xml");
    try (FileOutputStream fos = new FileOutputStream(webxml);
        Writer w = new OutputStreamWriter(fos, "UTF-8"); ) {
      w.write(
          "<?xml version='1.0' encoding='UTF-8'?>\n"
              + "<web-app xmlns='http://java.sun.com/xml/ns/j2ee' "
              + " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"
              + " xsi:schemaLocation='http://java.sun.com/xml/ns/j2ee "
              + " http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd'"
              + " version='2.4'>\n"
              + "<error-page>\n<error-code>404</error-code>\n"
              + "<location>/404.html</location>\n</error-page>\n"
              + "</web-app>\n");
    }

    File error404 = new File(appDir, "404.html");
    try (FileOutputStream fos = new FileOutputStream(error404);
        Writer w = new OutputStreamWriter(fos, "ISO-8859-1")) {
      w.write("It is 404.html");
    }

    Tomcat tomcat = getTomcatInstance();
    String contextPath = "/MyApp";
    tomcat.addWebapp(null, contextPath, appDir.getAbsolutePath());
    tomcat.start();

    TestCustomErrorClient client = new TestCustomErrorClient(tomcat.getConnector().getLocalPort());

    client.reset();
    client.setRequest(new String[] {"GET /MyApp/missing HTTP/1.0" + CRLF + CRLF});
    client.connect();
    client.processRequest();
    assertTrue(client.isResponse404());
    assertEquals("It is 404.html", client.getResponseBody());

    SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
    format.setTimeZone(TimeZone.getTimeZone("GMT"));
    String tomorrow = format.format(new Date(System.currentTimeMillis() + 24 * 60 * 60 * 1000));

    // https://issues.apache.org/bugzilla/show_bug.cgi?id=50413
    //
    client.reset();
    client.setRequest(
        new String[] {
          "GET /MyApp/missing HTTP/1.1"
              + CRLF
              + "Host: localhost"
              + CRLF
              + "Connection: close"
              + CRLF
              + "If-Modified-Since: "
              + tomorrow
              + CRLF
              + CRLF
        });
    client.connect();
    client.processRequest();
    assertTrue(client.isResponse404());
    assertEquals("It is 404.html", client.getResponseBody());

    // https://issues.apache.org/bugzilla/show_bug.cgi?id=50413#c6
    //
    client.reset();
    client.setRequest(
        new String[] {
          "GET /MyApp/missing HTTP/1.1"
              + CRLF
              + "Host: localhost"
              + CRLF
              + "Connection: close"
              + CRLF
              + "Range: bytes=0-100"
              + CRLF
              + CRLF
        });
    client.connect();
    client.processRequest();
    assertTrue(client.isResponse404());
    assertEquals("It is 404.html", client.getResponseBody());
  }