Пример #1
0
  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) {
      }
    }
  }
Пример #2
0
  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);
      }
    }
  }
Пример #3
0
  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
      }
    }
  }
Пример #4
0
  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) {
      }
    }
  }
Пример #5
0
  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) {
      }
    }
  }