Java Code Examples for com.gargoylesoftware.htmlunit.Page#getWebResponse()

The following examples show how to use com.gargoylesoftware.htmlunit.Page#getWebResponse() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FrameWindow.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void setEnclosedPage(final Page page) {
    setPageDenied(PageDenied.NONE);
    super.setEnclosedPage(page);

    // we have updated a frame window by javascript write();
    // so we have to disable future updates during initialization
    // see com.gargoylesoftware.htmlunit.html.HtmlPage.loadFrames()
    final WebResponse webResponse = page.getWebResponse();
    if (webResponse instanceof StringWebResponse) {
        final StringWebResponse response = (StringWebResponse) webResponse;
        if (response.isFromJavascript()) {
            final BaseFrameElement frame = getFrameElement();
            frame.setContentLoaded();
        }
    }
}
 
Example 2
Source File: FrameWindow.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void setEnclosedPage(final Page page) {
    super.setEnclosedPage(page);

    // we have updated a frame window by javascript write();
    // so we have to disable future updates during initialization
    // see com.gargoylesoftware.htmlunit.html.HtmlPage.loadFrames()
    final WebResponse webResponse = page.getWebResponse();
    if (webResponse instanceof StringWebResponse) {
        final StringWebResponse response = (StringWebResponse) webResponse;
        if (response.isFromJavascript()) {
            final BaseFrameElement frame = getFrameElement();
            frame.setContentLoaded();
        }
    }
}
 
Example 3
Source File: ExceptionMapperIT.java    From krazo with Apache License 2.0 5 votes vote down vote up
@Test
public void testControllerHappyPath() throws IOException {

    Page page = webClient.getPage(baseURL.toString() + "mvc/mappers/success");
    WebResponse webResponse = page.getWebResponse();

    assertThat(webResponse.getStatusCode(), equalTo(200));
    assertThat(webResponse.getContentType(), startsWith("text/html"));
    assertThat(webResponse.getContentAsString(),
        containsString("<h1>Controller was executed without errors!</h1>"));

}
 
Example 4
Source File: ExceptionMapperIT.java    From krazo with Apache License 2.0 5 votes vote down vote up
@Test
public void testHandledByPlainTextMapper() throws IOException {

    Page page = webClient.getPage(baseURL.toString() + "mvc/mappers/fail-plain-text");
    WebResponse webResponse = page.getWebResponse();

    assertThat(webResponse.getStatusCode(), equalTo(491));
    assertThat(webResponse.getContentType(), startsWith("text/plain"));
    assertThat(webResponse.getContentAsString(),
        equalTo("Exception caught: Error thrown by controller"));

}
 
Example 5
Source File: ExceptionMapperIT.java    From krazo with Apache License 2.0 5 votes vote down vote up
@Test
public void testHandledByMvcViewMapper() throws IOException {

    Page page = webClient.getPage(baseURL.toString() + "mvc/mappers/fail-mvc-view");
    WebResponse webResponse = page.getWebResponse();

    assertThat(webResponse.getStatusCode(), equalTo(492));
    assertThat(webResponse.getContentType(), startsWith("text/html"));
    assertThat(webResponse.getContentAsString(),
        containsString("<h1>Exception caught: Error thrown by controller</h1>"));

}
 
Example 6
Source File: AttachmentTest.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Tests attachment callbacks and the contents of attachments.
 * @throws Exception if an error occurs
 */
@Test
public void basic() throws Exception {
    final String content1 = "<html><body>\n"
        + "<form method='POST' name='form' action='" + URL_SECOND + "'>\n"
        + "<input type='submit' value='ok'>\n"
        + "</form>\n"
        + "<a href='#' onclick='document.form.submit()'>click me</a>\n"
        + "</body></html>";
    final String content2 = "download file contents";

    final WebClient client = getWebClient();
    final List<Attachment> attachments = new ArrayList<>();
    client.setAttachmentHandler(new CollectingAttachmentHandler(attachments));

    final List<NameValuePair> headers = new ArrayList<>();
    headers.add(new NameValuePair("Content-Disposition", "attachment"));

    final MockWebConnection conn = new MockWebConnection();
    conn.setResponse(URL_FIRST, content1);
    conn.setResponse(URL_SECOND, content2, 200, "OK", MimeType.TEXT_HTML, headers);
    client.setWebConnection(conn);
    assertTrue(attachments.isEmpty());

    final HtmlPage result = client.getPage(URL_FIRST);
    final HtmlAnchor anchor = result.getAnchors().get(0);
    final Page clickResult = anchor.click();
    assertEquals(result, clickResult);
    assertEquals(1, attachments.size());
    assertTrue(HtmlPage.class.isInstance(attachments.get(0).getPage()));
    // the attachment is opened inside a new window
    assertEquals(2, client.getWebWindows().size());

    final Attachment attachment = attachments.get(0);
    final Page attachedPage = attachment.getPage();
    final WebResponse attachmentResponse = attachedPage.getWebResponse();
    final InputStream attachmentStream = attachmentResponse.getContentAsStream();
    HttpWebConnectionTest.assertEquals(new ByteArrayInputStream(content2.getBytes()), attachmentStream);
    assertEquals(MimeType.TEXT_HTML, attachmentResponse.getContentType());
    assertEquals(200, attachmentResponse.getStatusCode());
    assertEquals(URL_SECOND, attachmentResponse.getWebRequest().getUrl());
}
 
Example 7
Source File: HtmlUnitFetcher.java    From sparkler with Apache License 2.0 4 votes vote down vote up
@Override
public FetchedData fetch(Resource resource) throws Exception {
    LOG.info("HtmlUnit FETCHER {}", resource.getUrl());
    FetchedData fetchedData;
    try {
        String userAgent = getUserAgent();
        if (StringUtils.isNotBlank(userAgent)) {
            driver.removeRequestHeader(USER_AGENT);
            driver.addRequestHeader(USER_AGENT, userAgent);
        }
        Page page = driver.getPage(resource.getUrl());

        WebResponse response = page.getWebResponse();
        boolean truncated = false;
        try (InputStream stream = response.getContentAsStream()) {
            try (BoundedInputStream boundedStream = new BoundedInputStream(stream, CONTENT_LIMIT)) {
                try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
                    IOUtils.copy(boundedStream, out);
                    fetchedData = new FetchedData(out.toByteArray(), response.getContentType(), response.getStatusCode());
                    long contentLength = page.getWebResponse().getContentLength();
                    if (contentLength > 0 && contentLength < Integer.MAX_VALUE) {
                        fetchedData.setContentLength((int) contentLength);
                        truncated = (contentLength > fetchedData.getContentLength());
                        if (truncated) {
                            LOG.info("Content Truncated: {}, TotalSize={}", resource.getUrl(), contentLength);
                        }
                    }
                }
            }
        }
        resource.setStatus(ResourceStatus.FETCHED.toString());

        List<NameValuePair> respHeaders = page.getWebResponse().getResponseHeaders();
        Map<String, List<String>> headers = new HashMap<>();
        fetchedData.setHeaders(headers);
        if (respHeaders != null && !respHeaders.isEmpty()){
            respHeaders.forEach(item -> {
                if (!headers.containsKey(item.getName())) {
                    headers.put(item.getName(), new ArrayList<>());
                }
                headers.get(item.getName()).add(item.getValue());
            });
        }
        if (truncated){ //add truncated header
            headers.put(TRUNCATED, Collections.singletonList(Boolean.TRUE.toString()));
        }
    } catch (Exception e){
        LOG.warn(e.getMessage(), e);
        fetchedData = new FetchedData(new byte[0], "unknown/unknown", 0); // fixme: use proper status code
        resource.setStatus(ResourceStatus.ERROR.toString());
    }
    fetchedData.setResource(resource);
    return fetchedData;
}