Search in sources :

Example 1 with HttpMethod

use of com.gargoylesoftware.htmlunit.HttpMethod in project htmlunit by HtmlUnit.

the class HtmlForm method getWebRequest.

/**
 * <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 queryFormFields = "";
    Charset enc = getSubmitCharset();
    if (UTF_16 == enc) {
        enc = UTF_8;
    }
    if (HttpMethod.GET == method) {
        if (actionUrl.contains("#")) {
            anchor = StringUtils.substringAfter(actionUrl, "#");
        }
        queryFormFields = URLEncodedUtils.format(HttpClientConverter.nameValuePairsToHttpClient(parameters), enc);
        // action may already contain some query parameters: they have to be removed
        actionUrl = StringUtils.substringBefore(actionUrl, "#");
        actionUrl = StringUtils.substringBefore(actionUrl, "?");
        // parameters have been added to query
        parameters.clear();
    }
    URL url;
    try {
        if (actionUrl.isEmpty()) {
            url = WebClient.expandUrl(htmlPage.getUrl(), actionUrl);
        } else {
            url = htmlPage.getFullyQualifiedUrl(actionUrl);
        }
        if (!queryFormFields.isEmpty()) {
            url = UrlUtils.getUrlWithNewQuery(url, queryFormFields);
        }
        if (HttpMethod.GET == method && browser.hasFeature(FORM_SUBMISSION_URL_WITHOUT_HASH) && UrlUtils.URL_ABOUT_BLANK != url) {
            url = UrlUtils.getUrlWithNewRef(url, null);
        } else if (HttpMethod.POST == method && browser.hasFeature(FORM_SUBMISSION_URL_WITHOUT_HASH) && UrlUtils.URL_ABOUT_BLANK != url && StringUtils.isEmpty(actionUrl)) {
            url = UrlUtils.getUrlWithNewRef(url, null);
        } else if (anchor != null && UrlUtils.URL_ABOUT_BLANK != url) {
            url = UrlUtils.getUrlWithNewRef(url, anchor);
        }
    } catch (final MalformedURLException e) {
        throw new IllegalArgumentException("Not a valid url: " + actionUrl, e);
    }
    final WebRequest request = new WebRequest(url, browser.getHtmlAcceptHeader(), browser.getAcceptEncodingHeader());
    request.setHttpMethod(method);
    request.setRequestParameters(parameters);
    if (HttpMethod.POST == method) {
        request.setEncodingType(FormEncodingType.getInstance(getEnctypeAttribute()));
    }
    request.setCharset(enc);
    request.setRefererlHeader(htmlPage.getUrl());
    if (HttpMethod.POST == method && browser.hasFeature(FORM_SUBMISSION_HEADER_ORIGIN)) {
        try {
            request.setAdditionalHeader(HttpHeader.ORIGIN, UrlUtils.getUrlWithProtocolAndAuthority(htmlPage.getUrl()).toExternalForm());
        } catch (final MalformedURLException e) {
            if (LOG.isWarnEnabled()) {
                LOG.info("Invalid origin url '" + htmlPage.getUrl() + "'");
            }
        }
    }
    if (HttpMethod.POST == method) {
        if (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;
}
Also used : NameValuePair(com.gargoylesoftware.htmlunit.util.NameValuePair) MalformedURLException(java.net.MalformedURLException) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) Charset(java.nio.charset.Charset) BrowserVersion(com.gargoylesoftware.htmlunit.BrowserVersion) HttpMethod(com.gargoylesoftware.htmlunit.HttpMethod) URL(java.net.URL)

Example 2 with HttpMethod

use of com.gargoylesoftware.htmlunit.HttpMethod in project spring-framework by bluecrow1986.

the class HtmlUnitRequestBuilderTests method buildRequestMethods.

@Test
public void buildRequestMethods() {
    for (HttpMethod expectedMethod : HttpMethod.values()) {
        webRequest.setHttpMethod(expectedMethod);
        String actualMethod = requestBuilder.buildRequest(servletContext).getMethod();
        assertThat(actualMethod).isEqualTo(expectedMethod.name());
    }
}
Also used : HttpMethod(com.gargoylesoftware.htmlunit.HttpMethod) Test(org.junit.jupiter.api.Test)

Example 3 with HttpMethod

use of com.gargoylesoftware.htmlunit.HttpMethod in project spring-framework by spring-projects.

the class HtmlUnitRequestBuilderTests method buildRequestMethods.

@Test
public void buildRequestMethods() {
    for (HttpMethod expectedMethod : HttpMethod.values()) {
        webRequest.setHttpMethod(expectedMethod);
        String actualMethod = requestBuilder.buildRequest(servletContext).getMethod();
        assertThat(actualMethod).isEqualTo(expectedMethod.name());
    }
}
Also used : HttpMethod(com.gargoylesoftware.htmlunit.HttpMethod) Test(org.junit.jupiter.api.Test)

Example 4 with HttpMethod

use of com.gargoylesoftware.htmlunit.HttpMethod in project spring-framework-5.2.9.RELEASE by somepeopleHavingDream.

the class HtmlUnitRequestBuilderTests method buildRequestMethods.

@Test
public void buildRequestMethods() {
    for (HttpMethod expectedMethod : HttpMethod.values()) {
        webRequest.setHttpMethod(expectedMethod);
        String actualMethod = requestBuilder.buildRequest(servletContext).getMethod();
        assertThat(actualMethod).isEqualTo(expectedMethod.name());
    }
}
Also used : HttpMethod(com.gargoylesoftware.htmlunit.HttpMethod) Test(org.junit.jupiter.api.Test)

Example 5 with HttpMethod

use of com.gargoylesoftware.htmlunit.HttpMethod in project spring-framework-debug by Joker-5.

the class HtmlUnitRequestBuilderTests method buildRequestMethods.

@Test
public void buildRequestMethods() {
    for (HttpMethod expectedMethod : HttpMethod.values()) {
        webRequest.setHttpMethod(expectedMethod);
        String actualMethod = requestBuilder.buildRequest(servletContext).getMethod();
        assertThat(actualMethod).isEqualTo(expectedMethod.name());
    }
}
Also used : HttpMethod(com.gargoylesoftware.htmlunit.HttpMethod) Test(org.junit.jupiter.api.Test)

Aggregations

HttpMethod (com.gargoylesoftware.htmlunit.HttpMethod)6 Test (org.junit.jupiter.api.Test)4 WebRequest (com.gargoylesoftware.htmlunit.WebRequest)2 NameValuePair (com.gargoylesoftware.htmlunit.util.NameValuePair)2 URL (java.net.URL)2 Charset (java.nio.charset.Charset)2 BrowserVersion (com.gargoylesoftware.htmlunit.BrowserVersion)1 MalformedURLException (java.net.MalformedURLException)1 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)1