public void firstRun() throws Exception {

    Socket sock = new Socket(host, new Integer(port).intValue());
    OutputStream os = sock.getOutputStream();
    String get = "GET " + contextRoot + "/test.jsp" + " HTTP/1.0\n";
    System.out.println(get);
    os.write(get.getBytes());
    os.write("\n".getBytes());

    InputStream is = sock.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(is));

    // Get the JSESSIONID from the response
    String line = null;
    while ((line = br.readLine()) != null) {
      System.out.println(line);
      if (line.startsWith("Set-Cookie:") || line.startsWith("Set-cookie:")) {
        break;
      }
    }

    if (line == null) {
      throw new Exception("Missing Set-Cookie response header");
    }

    String jsessionId = getSessionIdFromCookie(line, JSESSIONID);

    // Store the JSESSIONID in a file
    FileOutputStream fos = new FileOutputStream(JSESSIONID);
    OutputStreamWriter osw = new OutputStreamWriter(fos);
    osw.write(jsessionId);
    osw.close();

    stat.addStatus(TEST_NAME, stat.PASS);
  }
  public void doTest(String path, int expectedStatus) throws Exception {

    InputStream is = null;
    BufferedReader input = null;

    try {
      URL url = new URL("http://" + host + ":" + port + contextRoot + "/" + path);
      System.out.println("Connecting to: " + url.toString());

      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.setReadTimeout(10000);
      conn.connect();

      int responseCode = conn.getResponseCode();
      if (responseCode != expectedStatus) {
        throw new Exception("Unexpected return code: " + responseCode);
      }

      if (responseCode == HttpURLConnection.HTTP_OK) {
        is = conn.getInputStream();
        input = new BufferedReader(new InputStreamReader(is));
        String response = input.readLine();
      }
    } finally {
      try {
        if (is != null) is.close();
      } catch (IOException ex) {
      }
      try {
        if (input != null) input.close();
      } catch (IOException ex) {
      }
    }
  }
  public void secondRun() throws Exception {
    // Read the JSESSIONID from the previous run
    FileInputStream fis = new FileInputStream(JSESSIONID);
    BufferedReader br = new BufferedReader(new InputStreamReader(fis));
    String jsessionId = br.readLine();
    new File(JSESSIONID).delete();

    Socket sock = new Socket(host, new Integer(port).intValue());
    OutputStream os = sock.getOutputStream();
    String get = "GET " + contextRoot + "/ResumeSession" + " HTTP/1.0\n";
    System.out.println(get);
    os.write(get.getBytes());
    String cookie = "Cookie: " + jsessionId + "\n";
    os.write(cookie.getBytes());
    os.write("\n".getBytes());

    InputStream is = sock.getInputStream();
    br = new BufferedReader(new InputStreamReader(is));

    String line = null;
    boolean found = false;
    while ((line = br.readLine()) != null) {
      System.out.println(line);
      if (line.contains(EXPECTED_RESPONSE)) {
        found = true;
        break;
      }
    }

    if (found) {
      stat.addStatus(TEST_NAME, stat.PASS);
    } else {
      throw new Exception("Wrong response. Expected response: " + EXPECTED_RESPONSE + " not found");
    }
  }
  private void invokeJsp() throws Exception {

    Socket sock = new Socket(host, new Integer(port).intValue());
    OutputStream os = sock.getOutputStream();
    String get = "GET " + contextRoot + "/jsp/test1.jsp" + " HTTP/1.0\n";
    System.out.println(get);
    os.write(get.getBytes());
    os.write("\n".getBytes());

    InputStream is = sock.getInputStream();
    BufferedReader bis = new BufferedReader(new InputStreamReader(is));

    String line = null;
    while ((line = bis.readLine()) != null) {
      if (line.startsWith("Location:")) {
        break;
      }
    }

    if (line != null) {
      System.out.println(line);

      // Check the path
      if (line.startsWith("Location: " + PATH)) {
        fail = false;
      } else {
        System.err.println("Wrong path: " + line + ", expected: " + PATH);
        stat.addStatus(TEST_NAME, stat.FAIL);
        fail = true;
      }
    } else {
      System.err.println("Missing Location response header");
      stat.addStatus(TEST_NAME, stat.FAIL);
    }
  }
  private void invokeJsp() throws Exception {

    Socket sock = null;
    OutputStream os = null;
    InputStream is = null;
    BufferedReader bis = null;
    try {
      sock = new Socket(host, new Integer(port).intValue());
      os = sock.getOutputStream();
      String get = "GET " + contextRoot + "/jsp/test.jsp HTTP/1.0\n";
      System.out.println(get);
      os.write(get.getBytes());
      os.write("\n".getBytes());

      is = sock.getInputStream();
      bis = new BufferedReader(new InputStreamReader(is));

      boolean found = false;
      String line = null;
      while ((line = bis.readLine()) != null) {
        if (line.endsWith(EXPECTED_ERROR) || line.endsWith(EXPECTED_ERROR_JDK6)) {
          found = true;
          break;
        }
      }

      if (!found) {
        throw new Exception(
            "Wrong response, expected: \n"
                + "For JDK 5: "
                + EXPECTED_ERROR
                + '\n'
                + "For JDK 6: "
                + EXPECTED_ERROR_JDK6);
      }
    } finally {
      try {
        if (os != null) os.close();
      } catch (IOException ex) {
      }
      try {
        if (is != null) is.close();
      } catch (IOException ex) {
      }
      try {
        if (sock != null) sock.close();
      } catch (IOException ex) {
      }
      try {
        if (bis != null) bis.close();
      } catch (IOException ex) {
      }
    }
  }
  private void goGet(String host, int port, String contextPath) throws Exception {

    sock = new Socket(host, port);
    OutputStream os = sock.getOutputStream();

    System.out.println(("GET " + contextPath + " HTTP/1.0\n"));
    os.write(("GET " + contextPath + " HTTP/1.0\n").getBytes());
    os.write("\n".getBytes());

    InputStream is = null;
    BufferedReader bis = null;
    String line = null;
    boolean pass = false;
    try {
      is = sock.getInputStream();
      bis = new BufferedReader(new InputStreamReader(is));
      while ((line = bis.readLine()) != null) {
        System.out.println(line);
        // Check if the filter was invoked
        if (EXPECTED_RESPONSE.equals("LLiFFiSSi")) {
          pass = true;
          break;
        }
      }
    } finally {
      try {
        if (is != null) {
          is.close();
        }
      } catch (IOException ioe) {
        // ignore
      }
      try {
        if (bis != null) {
          bis.close();
        }
      } catch (IOException ioe) {
        // ignore
      }
    }

    if (pass) {
      System.out.println("security constraint processed");
      stat.addStatus(TEST_NAME + " PASSED", stat.PASS);
    } else {
      System.out.println("security constraint NOT processed");
      stat.addStatus(TEST_NAME + " FAILED", stat.FAIL);
    }
  }
  private void invoke() throws Exception {

    String url = "http://" + host + ":" + port + contextRoot + "/myurl";
    System.out.println("opening connection to " + url);
    HttpURLConnection conn = (HttpURLConnection) (new URL(url)).openConnection();

    int code = conn.getResponseCode();
    if (code != 200) {
      System.out.println("Unexpected return code: " + code);
      stat.addStatus(TEST_NAME, stat.FAIL);
    } else {
      InputStream is = null;
      BufferedReader input = null;
      String line = null;
      try {
        is = conn.getInputStream();
        input = new BufferedReader(new InputStreamReader(is));
        line = input.readLine();
        System.out.println("line = " + line);
      } finally {
        try {
          if (is != null) {
            is.close();
          }
        } catch (IOException ioe) {
          // ignore
        }
        try {
          if (input != null) {
            input.close();
          }
        } catch (IOException ioe) {
          // ignore
        }
      }
      if (EXPECTED_RESPONSE.equals(line)) {
        stat.addStatus(TEST_NAME, stat.PASS);
      } else {
        System.out.println(
            "Wrong response. Expected: " + EXPECTED_RESPONSE + ", received: " + line);
        stat.addStatus(TEST_NAME, stat.FAIL);
      }
    }
  }
  public void doTest() {
    try {
      Socket sock = new Socket(host, new Integer(port).intValue());
      OutputStream os = sock.getOutputStream();
      String get = "GET " + contextRoot + "/index.jsp HTTP/1.0\n";
      System.out.print(get);
      os.write(get.getBytes());
      os.write("\n".getBytes());

      InputStream is = sock.getInputStream();
      BufferedReader br = new BufferedReader(new InputStreamReader(is));

      String line = null;
      int cookieCount = 0;
      String cookie = null;
      String value = null;
      while ((line = br.readLine()) != null) {
        System.out.println(line);
        if (line.startsWith("Set-Cookie: ")) {
          cookieCount += 1;
          int start = line.indexOf("=");
          int end = line.indexOf(";");
          if (end != -1) {
            cookie = line.substring(start + 1, end);
          } else {
            cookie = line.substring(start + 1);
          }
        } else if (line.startsWith("result=")) {
          value = line.substring("result=".length());
        }
      }

      boolean status = (cookieCount == 1) && cookie != null && cookie.equals(value);
      stat.addStatus(TEST_NAME, ((status) ? stat.PASS : stat.FAIL));
    } catch (Exception ex) {
      System.out.println(TEST_NAME + " test failed");
      stat.addStatus(TEST_NAME, stat.FAIL);
      ex.printStackTrace();
    }

    return;
  }
  public void doTest() throws Exception {

    URL url = new URL("http://" + host + ":" + port + contextRoot + "/test");
    System.out.println("Connecting to: " + url.toString());

    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.connect();
    int responseCode = conn.getResponseCode();

    if (responseCode != 200) {
      throw new Exception("Unexpected return code: " + responseCode);
    }

    InputStream is = null;
    BufferedReader input = null;
    try {
      is = conn.getInputStream();
      input = new BufferedReader(new InputStreamReader(is));
      String line = input.readLine();
      if (!EXPECTED_RESPONSE.equals(line)) {
        throw new Exception(
            "Wrong response. Expected: " + EXPECTED_RESPONSE + ", received: " + line);
      }
    } finally {
      try {
        if (is != null) {
          is.close();
        }
      } catch (IOException ioe) {
        // ignore
      }
      try {
        if (input != null) {
          input.close();
        }
      } catch (IOException ioe) {
        // ignore
      }
    }
  }
  public void doTest() {

    BufferedReader bis = null;
    try {
      URL url = new URL("http://" + host + ":" + port + contextRoot + "/jsp/test.jspx");
      System.out.println("Connecting to: " + url.toString());
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.connect();
      int responseCode = conn.getResponseCode();
      if (responseCode != 200) {
        stat.addStatus(
            "Wrong response code. Expected: 200" + ", received: " + responseCode, stat.FAIL);
      } else {

        bis = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line = null;
        String lastLine = null;
        while ((line = bis.readLine()) != null) {
          lastLine = line;
        }
        if (!EXPECTED.equals(lastLine)) {
          stat.addStatus(
              "Wrong response body. Expected: " + EXPECTED + ", received: " + lastLine, stat.FAIL);
        } else {
          stat.addStatus(TEST_NAME, stat.PASS);
        }
      }
    } catch (Exception ex) {
      System.out.println(TEST_NAME + " test failed.");
      stat.addStatus(TEST_NAME, stat.FAIL);
      ex.printStackTrace();
    } finally {
      try {
        if (bis != null) bis.close();
      } catch (IOException ex) {
      }
    }
  }
  public void doTest() {

    BufferedReader bis = null;
    try {
      URL url = new URL("http://" + host + ":" + port + contextRoot + "/jsp/test.jsp");
      System.out.println("Connecting to: " + url.toString());
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.connect();
      int responseCode = conn.getResponseCode();
      if (responseCode != 200) {
        System.err.println("Wrong response code. Expected: 200" + ", received: " + responseCode);
        stat.addStatus(TEST_NAME, stat.FAIL);
      } else {

        bis = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line = null;
        int index = 0;
        while ((line = bis.readLine()) != null) {
          if (line.trim().length() == 0) continue;
          if (!line.equals(expected[index++])) {
            System.err.println("Wrong response: " + line + ", expected: " + expected[index]);
            stat.addStatus(TEST_NAME, stat.FAIL);
            return;
          }
        }
        stat.addStatus(TEST_NAME, stat.PASS);
      }
    } catch (Exception ex) {
      ex.printStackTrace();
      stat.addStatus(TEST_NAME, stat.FAIL);
    } finally {
      try {
        if (bis != null) bis.close();
      } catch (IOException ex) {
      }
    }
  }
  private static void goGet(String host, int port, String result, String contextPath)
      throws Exception {
    long time = System.currentTimeMillis();
    Socket s = new Socket(host, port);
    s.setSoTimeout(5000);
    OutputStream os = s.getOutputStream();

    System.out.println(("GET " + contextPath + " HTTP/1.1\n"));
    os.write(("GET " + contextPath + " HTTP/1.1\n").getBytes());
    os.write(("Host: localhost\n").getBytes());
    os.write("\n".getBytes());

    InputStream is = s.getInputStream();
    System.out.println("Time: " + (System.currentTimeMillis() - time));
    BufferedReader bis = new BufferedReader(new InputStreamReader(is));
    String line = null;

    try {
      int index;
      while ((line = bis.readLine()) != null) {
        index = line.indexOf(result);
        System.out.println(line);
        if (index != -1) {
          index = line.indexOf(":");
          String status = line.substring(index + 1);

          if (status.equalsIgnoreCase("PASS")) {
            stat.addStatus("web-readerThreadsConfig: " + line.substring(0, index), stat.PASS);
          } else {
            stat.addStatus("web-readerThreadsConfig: " + line.substring(0, index), stat.FAIL);
          }
          count++;
        }
      }
    } catch (Exception ex) {
    }
  }
  private static void goGet(String host, int port, String contextPath) throws Exception {
    Socket s = new Socket(host, port);
    OutputStream os = s.getOutputStream();

    System.out.println("GET " + contextPath + " HTTP/1.0");
    os.write(("GET " + contextPath + " HTTP/1.0\n").getBytes());
    os.write("\n".getBytes());

    InputStream is = s.getInputStream();
    BufferedReader bis = new BufferedReader(new InputStreamReader(is));
    String line = null;

    int count = 0;
    try {
      while ((line = bis.readLine()) != null) {
        if (line.trim().length() > 0) System.out.println(line);
        if (line.indexOf("xxBodyTagxx") >= 0) count++;
      }
    } catch (Exception ex) {
      ex.printStackTrace();
      throw new Exception("Test UNPREDICTED-FAILURE");
    }
    if (count == 1) pass = true;
  }