Search in sources :

Example 6 with HttpMethod

use of com.gargoylesoftware.htmlunit.HttpMethod in project posters-advanced-loadtest-suite by Xceptance.

the class HttpRequest method buildRequest.

private WebRequest buildRequest(final HtmlPage page) throws MalformedURLException, URISyntaxException {
    // Often an URL string is (relative or absolute) not full qualified (for example '/foo/bar.html').
    // So a full qualified URL is build first.
    final URL pageURL = page.getFullyQualifiedUrl(getUrl());
    // Create the basic request
    final HttpMethod method = getMethod();
    final WebRequest request = new WebRequest(pageURL, method);
    // Set charset
    final Charset charset = getContentCharset(page);
    request.setCharset(charset);
    // Add either POST parameters or request body
    if (HttpMethod.POST.equals(method) || HttpMethod.PUT.equals(method) || HttpMethod.PATCH.equals(method)) {
        final String requestBody = getRequestBody();
        if (requestBody != null) {
            request.setRequestBody(requestBody);
        } else {
            final List<NameValuePair> parameters = getPostParams();
            if (parameters != null) {
                request.setRequestParameters(parameters);
            }
        }
    } else {
        Assert.assertTrue("Post parameter(s) or request body is allowed for POST|PUT|PATCH requests only. Current method is " + method, getRequestBody() == null && (getPostParams() == null || getPostParams().isEmpty()));
    }
    // Cache
    if (!isCached()) {
        // If HttpRequest caching is disabled (DEFAULT) set the internal HttpRequest flag.
        request.setXHR();
    }
    // Compression
    if (Context.get().configuration.applyHeaderGzip) {
        request.getAdditionalHeaders().put("Accept-Encoding", "gzip, deflate");
    }
    // Add standard HttpRequest headers
    request.getAdditionalHeaders().put("Referer", page.getUrl().toExternalForm());
    // Add additional configured headers
    final Map<String, String> aditionalHeaders = getAdditionalHeaders();
    if (aditionalHeaders != null) {
        request.getAdditionalHeaders().putAll(aditionalHeaders);
    }
    // Remove unwanted headers
    final Set<String> headerExclusions = getHeaderExclusions();
    if (headerExclusions != null) {
        headerExclusions.forEach(headerName -> request.getAdditionalHeaders().remove(headerName));
    }
    return request;
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(com.gargoylesoftware.htmlunit.util.NameValuePair) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) Charset(java.nio.charset.Charset) URL(java.net.URL) HttpMethod(com.gargoylesoftware.htmlunit.HttpMethod)

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