Java Code Examples for com.gargoylesoftware.htmlunit.HttpMethod#GET

The following examples show how to use com.gargoylesoftware.htmlunit.HttpMethod#GET . 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: XMLHTTPRequest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Prepares the WebRequest that will be sent.
 * @param content the content to send
 */
private void prepareRequest(final Object content) {
    if (content != null && !Undefined.isUndefined(content)) {
        if (!"".equals(content) && HttpMethod.GET == webRequest_.getHttpMethod()) {
            webRequest_.setHttpMethod(HttpMethod.POST);
        }
        if (HttpMethod.POST == webRequest_.getHttpMethod()
                || HttpMethod.PUT == webRequest_.getHttpMethod()
                || HttpMethod.PATCH == webRequest_.getHttpMethod()) {
            if (content instanceof FormData) {
                ((FormData) content).fillRequest(webRequest_);
            }
            else {
                final String body = Context.toString(content);
                if (!body.isEmpty()) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Setting request body to: " + body);
                    }
                    webRequest_.setRequestBody(body);
                }
            }
        }
    }
}
 
Example 2
Source File: WebConnectionWrapperTest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void wrapper() throws Exception {
    final List<NameValuePair> emptyList = Collections.emptyList();
    final WebResponseData data = new WebResponseData(new byte[]{}, HttpStatus.SC_OK, "", emptyList);
    final WebResponse response = new WebResponse(data, URL_FIRST, HttpMethod.GET, 0);
    final WebRequest wrs = new WebRequest(URL_FIRST);

    final WebConnection realConnection = new WebConnection() {
        @Override
        public WebResponse getResponse(final WebRequest request) {
            assertSame(wrs, request);
            return response;
        }
        @Override
        public void close() {
            // nothing
        }
    };

    try (WebConnectionWrapper wrapper = new WebConnectionWrapper(realConnection)) {
        assertSame(response, wrapper.getResponse(wrs));
    }
}
 
Example 3
Source File: HtmlForm2Test.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void buttonSubmitWithFormMethod() throws Exception {
    final String html = "<!DOCTYPE html>\n"
        + "<html><head></head>\n"
        + "<body>\n"
        + "  <p>hello world</p>\n"
        + "  <form id='myForm' action='" + URL_SECOND
                            + "' method='" + HttpMethod.POST + "'>\n"
        + "    <button id='myButton' type='submit' formmethod='" + HttpMethod.GET
                    + "'>Submit with different form method</button>\n"
        + "  </form>\n"
        + "</body></html>";
    final String secondContent = "second content";

    getMockWebConnection().setResponse(URL_SECOND, secondContent);

    final WebDriver driver = loadPage2(html);
    driver.findElement(By.id("myButton")).click();

    assertEquals(2, getMockWebConnection().getRequestCount());
    assertEquals(URL_SECOND.toString(), getMockWebConnection().getLastWebRequest().getUrl());
    assertEquals(HttpMethod.GET, getMockWebConnection().getLastWebRequest().getHttpMethod());
}
 
Example 4
Source File: HtmlForm2Test.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void inputTypeSubmitWithFormMethod() throws Exception {
    final String html = "<!DOCTYPE html>\n"
        + "<html><head></head>\n"
        + "<body>\n"
        + "  <p>hello world</p>\n"
        + "  <form id='myForm' action='" + URL_SECOND
                            + "' method='" + HttpMethod.POST + "'>\n"
        + "    <input id='myButton' type='submit' formmethod='" + HttpMethod.GET + "' />\n"
        + "  </form>\n"
        + "</body></html>";
    final String secondContent = "second content";

    getMockWebConnection().setResponse(URL_SECOND, secondContent);

    final WebDriver driver = loadPage2(html);
    driver.findElement(By.id("myButton")).click();

    assertEquals(2, getMockWebConnection().getRequestCount());
    assertEquals(URL_SECOND.toString(), getMockWebConnection().getLastWebRequest().getUrl());
    assertEquals(HttpMethod.GET, getMockWebConnection().getLastWebRequest().getHttpMethod());
}
 
Example 5
Source File: HtmlForm2Test.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
@Alerts(DEFAULT = "GET",
        IE = "POST")
public void inputTypeImageWithFormMethod() throws Exception {
    final String html = "<!DOCTYPE html>\n"
        + "<html><head></head>\n"
        + "<body>\n"
        + "  <p>hello world</p>\n"
        + "  <form id='myForm' action='" + URL_SECOND
                            + "' method='" + HttpMethod.POST + "'>\n"
        + "    <input id='myButton' type='image' alt='Submit' formmethod='" + HttpMethod.GET + "' />\n"
        + "  </form>\n"
        + "</body></html>";
    final String secondContent = "second content";

    getMockWebConnection().setResponse(URL_SECOND, secondContent);

    final WebDriver driver = loadPage2(html);
    driver.findElement(By.id("myButton")).click();

    assertEquals(2, getMockWebConnection().getRequestCount());
    assertEquals(URL_SECOND.toString(),
            UrlUtils.getUrlWithNewQuery(getMockWebConnection().getLastWebRequest().getUrl(), null));
    assertEquals(getExpectedAlerts()[0], getMockWebConnection().getLastWebRequest().getHttpMethod().name());
}
 
Example 6
Source File: XMLHTTPRequest.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Prepares the WebRequest that will be sent.
 * @param content the content to send
 */
private void prepareRequest(final Object content) {
    if (content != null && content != Undefined.instance) {
        if (!"".equals(content) && HttpMethod.GET == webRequest_.getHttpMethod()) {
            webRequest_.setHttpMethod(HttpMethod.POST);
        }
        if (HttpMethod.POST == webRequest_.getHttpMethod()
                || HttpMethod.PUT == webRequest_.getHttpMethod()
                || HttpMethod.PATCH == webRequest_.getHttpMethod()) {
            if (content instanceof FormData) {
                ((FormData) content).fillRequest(webRequest_);
            }
            else {
                final String body = Context.toString(content);
                if (!body.isEmpty()) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Setting request body to: " + body);
                    }
                    webRequest_.setRequestBody(body);
                }
            }
        }
    }
}
 
Example 7
Source File: XMLHttpRequest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
private boolean isPreflight() {
    final HttpMethod method = webRequest_.getHttpMethod();
    if (method != HttpMethod.GET && method != HttpMethod.HEAD && method != HttpMethod.POST) {
        return true;
    }
    for (final Entry<String, String> header : webRequest_.getAdditionalHeaders().entrySet()) {
        if (isPreflightHeader(header.getKey().toLowerCase(Locale.ROOT), header.getValue())) {
            return true;
        }
    }
    return false;
}
 
Example 8
Source File: XMLHTTPRequest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
private boolean isPreflight() {
    final HttpMethod method = webRequest_.getHttpMethod();
    if (method != HttpMethod.GET && method != HttpMethod.HEAD && method != HttpMethod.POST) {
        return true;
    }
    for (final Entry<String, String> header : webRequest_.getAdditionalHeaders().entrySet()) {
        if (isPreflightHeader(header.getKey().toLowerCase(Locale.ROOT),
                header.getValue())) {
            return true;
        }
    }
    return false;
}
 
Example 9
Source File: XMLHttpRequest.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
private boolean isPreflight() {
    final HttpMethod method = webRequest_.getHttpMethod();
    if (method != HttpMethod.GET && method != HttpMethod.HEAD && method != HttpMethod.POST) {
        return true;
    }
    for (final Entry<String, String> header : webRequest_.getAdditionalHeaders().entrySet()) {
        if (isPreflightHeader(header.getKey().toLowerCase(Locale.ROOT), header.getValue())) {
            return true;
        }
    }
    return false;
}
 
Example 10
Source File: XMLHTTPRequest.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
private boolean isPreflight() {
    final HttpMethod method = webRequest_.getHttpMethod();
    if (method != HttpMethod.GET && method != HttpMethod.HEAD && method != HttpMethod.POST) {
        return true;
    }
    for (final Entry<String, String> header : webRequest_.getAdditionalHeaders().entrySet()) {
        if (isPreflightHeader(header.getKey().toLowerCase(Locale.ROOT),
                header.getValue())) {
            return true;
        }
    }
    return false;
}
 
Example 11
Source File: HtmlForm.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
 *
 * Gets the request for a submission of this form with the specified SubmittableElement.
 * @param submitElement the element that caused the submit to occur
 * @return the request
 */
public WebRequest getWebRequest(final SubmittableElement submitElement) {
    final HtmlPage htmlPage = (HtmlPage) getPage();
    final List<NameValuePair> parameters = getParameterListForSubmit(submitElement);
    final HttpMethod method;
    final String methodAttribute = getMethodAttribute();
    if ("post".equalsIgnoreCase(methodAttribute)) {
        method = HttpMethod.POST;
    }
    else {
        if (!"get".equalsIgnoreCase(methodAttribute) && StringUtils.isNotBlank(methodAttribute)) {
            notifyIncorrectness("Incorrect submit method >" + getMethodAttribute() + "<. Using >GET<.");
        }
        method = HttpMethod.GET;
    }

    final BrowserVersion browser = getPage().getWebClient().getBrowserVersion();
    String actionUrl = getActionAttribute();
    String anchor = null;
    String queryFromFields = "";
    if (HttpMethod.GET == method) {
        if (actionUrl.contains("#")) {
            anchor = StringUtils.substringAfter(actionUrl, "#");
        }
        final Charset enc = getPage().getCharset();
        queryFromFields =
            URLEncodedUtils.format(Arrays.asList(NameValuePair.toHttpClient(parameters)), enc);

        // action may already contain some query parameters: they have to be removed
        actionUrl = StringUtils.substringBefore(actionUrl, "#");
        actionUrl = StringUtils.substringBefore(actionUrl, "?");
        parameters.clear(); // parameters have been added to query
    }
    URL url;
    try {
        if (actionUrl.isEmpty()) {
            url = WebClient.expandUrl(htmlPage.getUrl(), actionUrl);
        }
        else {
            url = htmlPage.getFullyQualifiedUrl(actionUrl);
        }

        if (!queryFromFields.isEmpty()) {
            url = UrlUtils.getUrlWithNewQuery(url, queryFromFields);
        }

        if (HttpMethod.GET == method && browser.hasFeature(FORM_SUBMISSION_URL_WITHOUT_HASH)
                && WebClient.URL_ABOUT_BLANK != url) {
            url = UrlUtils.getUrlWithNewRef(url, null);
        }
        else if (HttpMethod.POST == method
                && browser.hasFeature(FORM_SUBMISSION_URL_WITHOUT_HASH)
                && WebClient.URL_ABOUT_BLANK != url
                && StringUtils.isEmpty(actionUrl)) {
            url = UrlUtils.getUrlWithNewRef(url, null);
        }
        else if (anchor != null
                && WebClient.URL_ABOUT_BLANK != url) {
            url = UrlUtils.getUrlWithNewRef(url, anchor);
        }
    }
    catch (final MalformedURLException e) {
        throw new IllegalArgumentException("Not a valid url: " + actionUrl);
    }

    final WebRequest request = new WebRequest(url, method);
    request.setAdditionalHeader(HttpHeader.ACCEPT, browser.getHtmlAcceptHeader());
    request.setAdditionalHeader(HttpHeader.ACCEPT_ENCODING, "gzip, deflate");
    request.setRequestParameters(parameters);
    if (HttpMethod.POST == method) {
        request.setEncodingType(FormEncodingType.getInstance(getEnctypeAttribute()));
    }
    request.setCharset(getSubmitCharset());

    String referer = htmlPage.getUrl().toExternalForm();
    request.setAdditionalHeader(HttpHeader.REFERER, referer);

    if (HttpMethod.POST == method
            && browser.hasFeature(FORM_SUBMISSION_HEADER_ORIGIN)) {
        referer = StringUtils.stripEnd(referer, "/");
        request.setAdditionalHeader(HttpHeader.ORIGIN, referer);
    }
    if (HttpMethod.POST == method
            && browser.hasFeature(FORM_SUBMISSION_HEADER_CACHE_CONTROL_MAX_AGE)) {
        request.setAdditionalHeader(HttpHeader.CACHE_CONTROL, "max-age=0");
    }
    if (browser.hasFeature(FORM_SUBMISSION_HEADER_CACHE_CONTROL_NO_CACHE)) {
        request.setAdditionalHeader(HttpHeader.CACHE_CONTROL, "no-cache");
    }

    return request;
}
 
Example 12
Source File: Security1290Test.java    From configuration-as-code-plugin with MIT License 4 votes vote down vote up
private void assertRightPermissionConfigurations(String relativeUrl, JenkinsRule.WebClient adminWc, JenkinsRule.WebClient userWc) throws IOException {
    WebRequest request = new WebRequest(new URL(j.getURL() + relativeUrl), HttpMethod.GET);

    assertEquals(HttpURLConnection.HTTP_OK, adminWc.getPage(request).getWebResponse().getStatusCode());
    assertEquals(HttpURLConnection.HTTP_FORBIDDEN, userWc.getPage(request).getWebResponse().getStatusCode());
}