Exemplo n.º 1
0
  /**
   * Verify that if an initial request comes to a FacesSevlet mapped using a prefix path, that the
   * client is redirected to the context root. Additionally verify that after request to a
   * particular view has been made, if a subsequent request is made to the prefix path, context root
   * redirection occurs.
   */
  public void testVerifyPathBehavior() throws Exception {
    final String welcomePage = "WELCOMEPAGE";
    HtmlPage page = getPage("/faces");
    WebResponse response = page.getWebResponse();
    assertTrue(welcomePage.equals(response.getContentAsString().trim()));

    // Ok, now get a page
    HtmlPage textPage = getHtmlPage("/faces/hello.jsp");
    response = textPage.getWebResponse();
    assertTrue("/hello.jsp PASSED".equals(response.getContentAsString().trim()));

    page = getPage("/faces");
    response = page.getWebResponse();
    assertTrue("WELCOMEPAGE".equals(response.getContentAsString().trim()));
  }
  /** {@inheritDoc} */
  @Override
  protected void doExecute() throws Exception {
    // Collect parameters.
    final Map<String, String> params = new HashMap<String, String>();
    params.put("_csrf", loginJson.getJSONObject("data").getString("_csrf"));
    params.put("_eventId_submit", "");
    params.put("bp_ks1", "");
    params.put("bp_ks2", "");
    params.put("bp_ks3", "");
    params.put("bp_mid", "");
    params.put("execution", loginJson.getJSONObject("data").getString("flowExecutionKey"));
    params.put("token", getPaypalToken());

    // Send the XHR.
    final WebResponse r =
        new XHR()
            .url(loginJson.getJSONObject("data").getString("flowExecutionUrl"))
            .POST()
            .postParams(params)
            .fire();

    // Create flow control link that points back to the DW system.
    final JSONObject json2 = new JSONObject(r.getContentAsString());
    final HtmlElement a = HtmlPageUtils.createHtmlElement("a", Context.getPage().getBody());
    a.setAttribute(
        "href", json2.getJSONObject("data").getJSONObject("flowControl").getString("location"));

    // Click that link.
    loadPageByClick(a);
  }
Exemplo n.º 3
0
  public void testImageInsideCollection() throws Exception {
    execute("CRUD.new");
    execute("Collection.new", "viewObject=xava_view_section0_ingredients");
    execute("ImageEditor.changeImage", "newImageProperty=image");
    assertNoErrors();
    assertAction("LoadImage.loadImage");
    String imageUrl = System.getProperty("user.dir") + "/test-images/cake.gif";
    setFileValue("newImage", imageUrl);
    execute("LoadImage.loadImage");
    assertNoErrors();

    HtmlPage page = (HtmlPage) getWebClient().getCurrentWindow().getEnclosedPage();
    URL url = page.getWebResponse().getRequestSettings().getUrl();

    String urlPrefix = url.getProtocol() + "://" + url.getHost() + ":" + url.getPort();

    HtmlImage image = (HtmlImage) page.getElementsByName(decorateId("image")).get(0);
    String imageURL = null;
    if (image.getSrcAttribute().startsWith("/")) {
      imageURL = urlPrefix + image.getSrcAttribute();
    } else {
      String urlBase = Strings.noLastToken(url.getPath(), "/");
      imageURL = urlPrefix + urlBase + image.getSrcAttribute();
    }
    WebResponse response = getWebClient().getPage(imageURL).getWebResponse();
    assertTrue("Image not obtained", response.getContentAsString().length() > 0);
    assertEquals("Result is not an image", "image", response.getContentType());
  }
  private void ProcessResult(WebTest test, WebResponse response) {
    long pageLoadTime = response.getLoadTime();
    int statusCode = response.getStatusCode();
    String content = response.getContentAsString();

    boolean responseContentValid = true;
    if (!test.getRespShouldContain().isEmpty()) {
      if (!content.contains(test.getRespShouldContain())) {
        responseContentValid = false;
      }
    } else if (!test.getRespShouldNotContain().isEmpty()) {
      if (content.contains(test.getRespShouldNotContain())) {
        responseContentValid = false;
      }
    }

    Orville.LOG()
        .info(
            String.format(
                "Page load status code: %d, page load time: %d ms, response valid: %s\n",
                statusCode, pageLoadTime, (responseContentValid == true) ? "true" : "false"));

    boolean statusCodeValid =
        (test.getRespHttpCode() != 0 ? statusCode == test.getRespHttpCode() : true);
    boolean testPassed = (statusCodeValid && responseContentValid) ? true : false;

    StringBuffer urlBuffer = new StringBuffer();
    urlBuffer.append("http://orville-agent-management.herokuapp.com/test_results/");
    // urlBuffer.append("http://localhost:3000/test_results/");
    urlBuffer.append(test.getTestId() + "/");
    urlBuffer.append(Orville.AgentGuid() + "/");
    urlBuffer.append(testPassed + "/");
    urlBuffer.append(pageLoadTime);

    // Need to make sure the URL doesn't have any spaces
    String url = urlBuffer.toString().replaceAll("\\s+", "%20");

    Orville.LOG().info("Test result URL: " + url);

    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpPost httpPost = null;

    try {
      httpPost = new HttpPost(url);
      HttpResponse postResponse = httpclient.execute(httpPost);

      Orville.LOG()
          .fine(
              String.format(
                  "Test result data posted, response code: %d\n",
                  postResponse.getStatusLine().getStatusCode()));

    } catch (IOException e) {
      Orville.LOG()
          .warning(
              String.format(
                  "Unable to connect with Redstone test results service: %s\n", e.getMessage()));
    }
  }
Exemplo n.º 5
0
 public String getResponse(URL url) {
   WebResponse webResponse = null;
   try {
     webResponse = webClient.loadWebResponse(new WebRequest(url, HttpMethod.POST));
   } catch (IOException e) {
     e.printStackTrace();
   }
   if (webResponse != null) {
     return webResponse.getContentAsString();
   }
   return null;
 }
  @Test
  public void testNotAuthenticated() {

    WebResponse response = responseFromServer("/servlet");

    assertEquals(401, response.getStatusCode());

    assertTrue(
        "Response did not contain the \"WWW-Authenticate\" header, but should have",
        response.getResponseHeaderValue("WWW-Authenticate") != null);

    assertDefaultNotAuthenticated(response.getContentAsString());
  }
Exemplo n.º 7
0
 @Test
 public void testDataList() throws Exception {
   HtmlPage page = webClient.getPage(webUrl + "faces/datalist.xhtml");
   HtmlElement elem = page.getHtmlElementById("colors");
   String xml = elem.asXml();
   assertTrue(xml.contains("<datalist"));
   assertTrue(xml.contains("id=\"" + "colors" + "\""));
   WebResponse resp = page.getWebResponse();
   String text = resp.getContentAsString();
   assertTrue(text.contains("<option id=" + '"' + "r" + '"' + " value=" + '"' + "red" + '"'));
   assertTrue(text.contains("<option id=" + '"' + "b" + '"' + " value=" + '"' + "blue" + '"'));
   assertTrue(text.contains("<option id=" + '"' + "g" + '"' + " value=" + '"' + "green" + '"'));
 }
  @Test
  public void testNotAuthenticatedWrongName() {

    DefaultCredentialsProvider credentialsProvider = new DefaultCredentialsProvider();
    credentialsProvider.addCredentials("romo", "secret1");

    getWebClient().setCredentialsProvider(credentialsProvider);

    WebResponse response = responseFromServer("/servlet");

    assertEquals(401, response.getStatusCode());

    assertTrue(
        "Response did not contain the \"WWW-Authenticate\" header, but should have",
        response.getResponseHeaderValue("WWW-Authenticate") != null);

    assertDefaultNotAuthenticated(response.getContentAsString());
  }
  /**
   * Tries to uncompress the JavaScript code in the provided response.
   *
   * @param response the response to uncompress
   * @return a new response with uncompressed JavaScript code or the original response in case of
   *     failure
   */
  protected WebResponse uncompressJavaScript(final WebResponse response) {
    final WebRequest request = response.getWebRequest();
    final String scriptName = request.getUrl().toString();
    final String scriptSource = response.getContentAsString();

    // skip if it is already formatted? => TODO

    final ContextFactory factory = new ContextFactory();
    final ContextAction action =
        new ContextAction() {
          public Object run(final Context cx) {
            cx.setOptimizationLevel(-1);
            final Script script = cx.compileString(scriptSource, scriptName, 0, null);
            return cx.decompileScript(script, 4);
          }
        };

    try {
      final String decompileScript = (String) factory.call(action);
      final List<NameValuePair> responseHeaders =
          new ArrayList<NameValuePair>(response.getResponseHeaders());
      for (int i = responseHeaders.size() - 1; i >= 0; i--) {
        if ("content-encoding".equalsIgnoreCase(responseHeaders.get(i).getName())) {
          responseHeaders.remove(i);
        }
      }
      final WebResponseData wrd =
          new WebResponseData(
              decompileScript.getBytes(),
              response.getStatusCode(),
              response.getStatusMessage(),
              responseHeaders);
      return new WebResponse(
          wrd,
          response.getWebRequest().getUrl(),
          response.getWebRequest().getHttpMethod(),
          response.getLoadTime());
    } catch (final Exception e) {
      LOG.warn("Failed to decompress JavaScript response. Delivering as it.", e);
    }

    return response;
  }
Exemplo n.º 10
0
  public void testChangeImage() throws Exception {
    addImage();

    HtmlPage page = (HtmlPage) getWebClient().getCurrentWindow().getEnclosedPage();
    URL url = page.getWebResponse().getRequestSettings().getUrl();
    String urlPrefix = url.getProtocol() + "://" + url.getHost() + ":" + url.getPort();

    HtmlImage image = (HtmlImage) page.getElementsByName(decorateId("photo")).get(0);
    String imageURL = null;
    if (image.getSrcAttribute().startsWith("/")) {
      imageURL = urlPrefix + image.getSrcAttribute();
    } else {
      String urlBase = Strings.noLastToken(url.getPath(), "/");
      imageURL = urlPrefix + urlBase + image.getSrcAttribute();
    }
    WebResponse response = getWebClient().getPage(imageURL).getWebResponse();
    assertTrue("Image not obtained", response.getContentAsString().length() > 0);
    assertEquals("Result is not an image", "image", response.getContentType());
  }
Exemplo n.º 11
0
  public void testDeleteImage() throws Exception {
    addImage();

    execute("ImageEditor.deleteImage", "newImageProperty=photo");
    assertNoErrors();
    HtmlPage page = (HtmlPage) getWebClient().getCurrentWindow().getEnclosedPage();
    URL url = page.getWebResponse().getRequestSettings().getUrl();
    String urlPrefix = url.getProtocol() + "://" + url.getHost() + ":" + url.getPort();

    HtmlImage image = (HtmlImage) page.getElementsByName(decorateId("photo")).get(0);
    String imageURL = null;
    if (image.getSrcAttribute().startsWith("/")) {
      imageURL = urlPrefix + image.getSrcAttribute();
    } else {
      String urlBase = Strings.noLastToken(url.getPath(), "/");
      imageURL = urlPrefix + urlBase + image.getSrcAttribute();
    }
    WebResponse response = getWebClient().getPage(imageURL).getWebResponse();
    assertTrue("Image obtained", response.getContentAsString().length() == 0);
  }
Exemplo n.º 12
0
  public static void savePage(String path) {

    // path = transformPath(path);

    if (!Main.list.contains(path) && path.contains("https://www.darts-ip.com")) {

      System.out.println("");
      System.out.println("=================================================");
      System.out.println("");
      System.out.println(path);
      System.out.println("");

      Main.list.add(path);

      String content = "";
      String fileName = "";
      HtmlPage page = null;

      // ----------HTML-UNIT----------

      try {

        WebClient webClient = new WebClient();
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        page = webClient.getPage(path);
        WebResponse response = page.getWebResponse();
        content = response.getContentAsString();
        fileName = path + ".html";

      } catch (Exception e) {
        System.out.println("");
        System.out.println("ERROR in Page.savePage #HTML-UNIT");
        System.out.println(e);
        System.out.println("");
      }

      // ----------SAVE-HTML-FILE----------

      try {

        PrintWriter writer = new PrintWriter(fileName, "UTF-8");
        writer.print(content);
        writer.close();

      } catch (Exception e) {
        System.out.println("");
        System.out.println("ERROR in ERROR in Page.savePage #SAVE-HTML-FILE");
        System.out.println(e);
        System.out.println("");
      }

      // ----------ZIP-HTML-FILE----------

      try {

        FileInputStream fi = new FileInputStream(fileName);
        BufferedInputStream buffi = new BufferedInputStream(fi, Main.BUFFER);
        ZipEntry entry = new ZipEntry(fileName);
        Main.out.putNextEntry(entry);

        int count;
        while ((count = buffi.read(Main.data, 0, Main.BUFFER)) != -1) {
          Main.out.write(Main.data, 0, count);
        }

        Main.out.closeEntry();
        buffi.close();

      } catch (Exception e) {
        System.out.println("");
        System.out.println("ERROR in Page.savePage #ZIP-HTML-FILE");
        System.out.println(e);
        System.out.println("");
      }

      // ----------DELETE-TMP-FILE----------

      try {
        new File(fileName).delete();

      } catch (Exception e) {
        System.out.println("");
        System.out.println("ERROR in Page.savePage #DELETE-TMP-FILE");
        System.out.println(e);
        System.out.println("");
      }

      System.out.println("");
      System.out.println("=================================================");

      // ----------EXPLORE-LINKS----------

      exploreLinks(page);
    }
  }