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;
}
Aggregations