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

The following examples show how to use com.gargoylesoftware.htmlunit.html.HtmlPage#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: CsrfIT.java    From krazo with Apache License 2.0 6 votes vote down vote up
/**
 * Checks that CSRF validation works if token sent as header instead of
 * form field.
 *
 * @throws Exception an error occurs or validation fails.
 */
@Test
public void testFormHeaderOk() throws Exception {
    HtmlPage page1 = webClient.getPage(webUrl + "resources/csrf");

    // Check response and CSRF header
    WebResponse res = page1.getWebResponse();
    assertEquals(Response.Status.OK.getStatusCode(), res.getStatusCode());
    assertNotNull(res.getResponseHeaderValue(CSRF_HEADER));

    WebRequest req = new WebRequest(new URL(webUrl + "resources/csrf"));
    req.setHttpMethod(HttpMethod.POST);
    req.setAdditionalHeader(CSRF_HEADER, res.getResponseHeaderValue(CSRF_HEADER));
    res = webClient.loadWebResponse(req);
    assertEquals(Response.Status.OK.getStatusCode(), res.getStatusCode());
}
 
Example 2
Source File: CsrfIT.java    From ozark with Apache License 2.0 6 votes vote down vote up
/**
 * Checks that CSRF validation works if token sent as header instead of
 * form field.
 *
 * @throws Exception an error occurs or validation fails.
 */
@Test
public void testFormHeaderOk() throws Exception {
    HtmlPage page1 = webClient.getPage(webUrl + "resources/csrf");

    // Check response and CSRF header
    WebResponse res = page1.getWebResponse();
    assertEquals(Response.Status.OK.getStatusCode(), res.getStatusCode());
    assertNotNull(res.getResponseHeaderValue(CSRF_HEADER));

    WebRequest req = new WebRequest(new URL(webUrl + "resources/csrf"));
    req.setHttpMethod(HttpMethod.POST);
    req.setAdditionalHeader(CSRF_HEADER, res.getResponseHeaderValue(CSRF_HEADER));
    res = webClient.loadWebResponse(req);
    assertEquals(Response.Status.OK.getStatusCode(), res.getStatusCode());
}
 
Example 3
Source File: CsrfIT.java    From ozark with Apache License 2.0 6 votes vote down vote up
/**
 * Checks that CSRF validation works if token sent as header instead of
 * form field.
 *
 * @throws Exception an error occurs or validation fails.
 */
@Test
public void testFormHeaderOk() throws Exception {
    HtmlPage page1 = webClient.getPage(webUrl + "resources/csrf");

    // Check response and CSRF header
    WebResponse res = page1.getWebResponse();
    assertEquals(Response.Status.OK.getStatusCode(), res.getStatusCode());
    assertNotNull(res.getResponseHeaderValue(CSRF_HEADER));

    WebRequest req = new WebRequest(new URL(webUrl + "resources/csrf"));
    req.setHttpMethod(HttpMethod.POST);
    req.setAdditionalHeader(CSRF_HEADER, res.getResponseHeaderValue(CSRF_HEADER));
    res = webClient.loadWebResponse(req);
    assertEquals(Response.Status.OK.getStatusCode(), res.getStatusCode());
}
 
Example 4
Source File: WebClientTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Common utility for GET after POST redirection on same URLs
 * @param statusCode the code to return from the initial request
 * @throws Exception if the test fails
 */
private void doTestRedirectionSameUrlAfterPost(final int statusCode) throws Exception {
    final String firstContent = "<html><head><title>First</title></head><body></body></html>";
    final String secondContent = "<html><head><title>Second</title></head><body></body></html>";

    final WebClient webClient = getWebClient();

    final List<NameValuePair> headers =
            Collections.singletonList(new NameValuePair("Location", URL_FIRST.toExternalForm()));

    // builds a webconnection that first sends a redirect and then a "normal" response for
    // the same requested URL
    final MockWebConnection webConnection = new MockWebConnection() {
        private int count_ = 0;
        @Override
        public WebResponse getResponse(final WebRequest webRequest) throws IOException {
            ++count_;
            if (count_ == 1) {
                final WebResponse response = super.getResponse(webRequest);
                setResponse(webRequest.getUrl(), secondContent);
                return response;
            }
            return super.getResponse(webRequest);
        }
    };
    webConnection.setResponse(URL_FIRST, firstContent, statusCode, "Some error", MimeType.TEXT_HTML, headers);
    webClient.setWebConnection(webConnection);

    final HtmlPage page = webClient.getPage(new WebRequest(URL_FIRST, HttpMethod.POST));
    final WebResponse webResponse = page.getWebResponse();
    // A redirect should have happened
    assertEquals(200, webResponse.getStatusCode());
    assertEquals(URL_FIRST, webResponse.getWebRequest().getUrl());
    assertEquals("Second", page.getTitleText());
    assertSame(HttpMethod.GET, webResponse.getWebRequest().getHttpMethod());
}
 
Example 5
Source File: WebClientTest.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Basic logic for all the redirection tests.
 *
 * @param statusCode the code to return from the initial request
 * @param initialRequestMethod the initial request
 * @param expectedRedirectedRequestMethod the submit method of the second (redirected) request
 * If a redirect is not expected to happen then this must be null
 * @param newLocation the Location set in the redirection header
 * @param useProxy indicates if the test should be performed with a proxy
 * @throws Exception if the test fails
 */
@SuppressWarnings("resource")
private void doTestRedirection(
        final int statusCode,
        final HttpMethod initialRequestMethod,
        final HttpMethod expectedRedirectedRequestMethod,
        final String newLocation,
        final boolean useProxy)
    throws Exception {

    final String firstContent = "<html><head><title>First</title></head><body></body></html>";
    final String secondContent = "<html><head><title>Second</title></head><body></body></html>";

    final WebClient webClient;
    final String proxyHost;
    final int proxyPort;
    if (useProxy) {
        proxyHost = "someHost";
        proxyPort = 12233345;
        webClient = new WebClient(getBrowserVersion(), proxyHost, proxyPort);
    }
    else {
        proxyHost = null;
        proxyPort = 0;
        webClient = getWebClient();
    }

    webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
    webClient.getOptions().setPrintContentOnFailingStatusCode(false);

    final List<NameValuePair> headers = Collections.singletonList(new NameValuePair("Location", newLocation));
    final MockWebConnection webConnection = new MockWebConnection();
    webConnection.setResponse(URL_FIRST, firstContent, statusCode, "Some error", MimeType.TEXT_HTML, headers);
    webConnection.setResponse(new URL(newLocation), secondContent);

    webClient.setWebConnection(webConnection);

    final URL url = URL_FIRST;

    HtmlPage page;
    WebResponse webResponse;

    //
    // Second time redirection is turned on (default setting)
    //
    page = webClient.getPage(new WebRequest(url, initialRequestMethod));
    webResponse = page.getWebResponse();
    if (expectedRedirectedRequestMethod == null) {
        // No redirect should have happened
        assertEquals(statusCode, webResponse.getStatusCode());
        assertEquals(initialRequestMethod, webConnection.getLastMethod());
    }
    else {
        // A redirect should have happened
        assertEquals(HttpStatus.SC_OK, webResponse.getStatusCode());
        assertEquals(newLocation, webResponse.getWebRequest().getUrl());
        assertEquals("Second", page.getTitleText());
        assertEquals(expectedRedirectedRequestMethod, webConnection.getLastMethod());
    }
    assertEquals(proxyHost, webConnection.getLastWebRequest().getProxyHost());
    assertEquals(proxyPort, webConnection.getLastWebRequest().getProxyPort());

    //
    // Second time redirection is turned off
    //
    webClient.getOptions().setRedirectEnabled(false);
    page = webClient.getPage(new WebRequest(url, initialRequestMethod));
    webResponse = page.getWebResponse();
    assertEquals(statusCode, webResponse.getStatusCode());
    assertEquals(initialRequestMethod, webConnection.getLastMethod());
    assertEquals(proxyHost, webConnection.getLastWebRequest().getProxyHost());
    assertEquals(proxyPort, webConnection.getLastWebRequest().getProxyPort());

    webClient.close();
}
 
Example 6
Source File: HtmlUnitDownloder.java    From gecco-htmlunit with MIT License 4 votes vote down vote up
public HttpResponse download(HttpRequest request, int timeout) throws DownloadException {
	try {
		URL url = new URL(request.getUrl());
		WebRequest webRequest = new WebRequest(url);
		webRequest.setHttpMethod(HttpMethod.GET);
		if(request instanceof HttpPostRequest) {//post
			HttpPostRequest post = (HttpPostRequest)request;
			webRequest.setHttpMethod(HttpMethod.POST);
			List<NameValuePair> requestParameters = new ArrayList<NameValuePair>();
			for(Map.Entry<String, Object> entry : post.getFields().entrySet()) {
				NameValuePair nvp = new NameValuePair(entry.getKey(), entry.getValue().toString());
				requestParameters.add(nvp);
			}
			webRequest.setRequestParameters(requestParameters);	
		}
		//header
		boolean isMobile = SpiderThreadLocal.get().getEngine().isMobile();
		webRequest.setAdditionalHeader("User-Agent", UserAgent.getUserAgent(isMobile));
		webRequest.setAdditionalHeaders(request.getHeaders());
		//proxy
		HttpHost proxy = Proxys.getProxy();
		if(proxy != null) {
			webRequest.setProxyHost(proxy.getHostName());
			webRequest.setProxyPort(proxy.getPort());
		}
		//timeout
		this.webClient.getOptions().setTimeout(timeout);
		//request,response
		webClient.getPage(webRequest);
		HtmlPage page = webClient.getPage(request.getUrl());
		HttpResponse resp = new HttpResponse();
		WebResponse webResponse = page.getWebResponse();
		int status = webResponse.getStatusCode();
		resp.setStatus(status);
		if(status == 302 || status == 301) {
			String redirectUrl = webResponse.getResponseHeaderValue("Location");
			resp.setContent(UrlUtils.relative2Absolute(request.getUrl(), redirectUrl));
		} else if(status == 200) {
			String content = page.asXml();
			resp.setContent(content);
			resp.setRaw(webResponse.getContentAsStream());
			String contentType = webResponse.getContentType();
			resp.setContentType(contentType);
			String charset = getCharset(request.getCharset(), contentType);
			resp.setCharset(charset);
		} else {
			throw new DownloadException("ERROR : " + status);
		}
		return resp;
	} catch(Exception ex) {
		throw new DownloadException(ex);
	}
}