All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.neotys.neoload.model.readers.loadrunner.WebCustomRequest Maven / Gradle / Ivy
package com.neotys.neoload.model.readers.loadrunner;
import java.net.URL;
import java.util.List;
import java.util.Optional;
import java.util.Properties;
import com.google.common.base.Preconditions;
import com.neotys.neoload.model.parsers.CPP14Parser.MethodcallContext;
import com.neotys.neoload.model.repository.Header;
import com.neotys.neoload.model.repository.ImmutablePage;
import com.neotys.neoload.model.repository.ImmutablePostBinaryRequest;
import com.neotys.neoload.model.repository.ImmutablePostTextRequest;
import com.neotys.neoload.model.repository.Page;
import com.neotys.neoload.model.repository.PostRequest;
import com.neotys.neoload.model.repository.RecordedFiles;
import org.apache.commons.lang3.StringUtils;
import static com.neotys.neoload.model.readers.loadrunner.MethodUtils.ITEM_BOUNDARY.EXTRARES;
public class WebCustomRequest extends WebRequest {
public static final String LR_HEXA_STR_PATTERN = "\\x";
public static Page toElement(final LoadRunnerVUVisitor visitor, final MethodCall method, final MethodcallContext ctx) {
Preconditions.checkNotNull(method);
ImmutablePage.Builder pageBuilder = ImmutablePage.builder();
final PostRequest postRequest = buildRequest(visitor, method);
if(postRequest == null) {
visitor.readSupportedFunctionWithWarn(method.getName(), ctx, "There is not any body parameter for the following LR function");
return null;
}
visitor.readSupportedFunction(method.getName(), ctx);
pageBuilder.addChilds(postRequest);
// we use the request headers of the main request for the resources.
final List recordedHeaders = getHeadersFromRecordedFile(postRequest.getRecordedFiles().flatMap(RecordedFiles::recordedRequestHeaderFile));
MethodUtils.extractItemListAsStringList(visitor, method.getParameters(), EXTRARES, Optional.of(pageBuilder))
.ifPresent(stringList -> getUrlList(stringList, getUrlFromMethodParameters(visitor.getLeftBrace(), visitor.getRightBrace(), method))
.forEach(url -> pageBuilder.addChilds(buildGetRequestFromURL(visitor, url, Optional.empty(), recordedHeaders))));
return pageBuilder.name(MethodUtils.normalizeString(visitor.getLeftBrace(), visitor.getRightBrace(), method.getParameters().get(0)))
.thinkTime(0)
.build();
}
/**
* Build the "model request" for the LR function passed in method
* @param method represent the LR "web_custom_request" function
* @return the corresponding request of the model
*/
public static PostRequest buildRequest(final LoadRunnerVUVisitor visitor, final MethodCall method) {
URL mainUrl = Preconditions.checkNotNull(getUrlFromMethodParameters(visitor.getLeftBrace(), visitor.getRightBrace(), method));
final Optional snapshotProperties = getSnapshotProperties(visitor, method);
final Optional recordedFiles = getRecordedFilesFromSnapshotProperties(visitor, snapshotProperties);
final List recordedHeaders = getHeadersFromRecordedFile(recordedFiles.flatMap(RecordedFiles::recordedRequestHeaderFile));
if (MethodUtils.getParameterWithName(method, "Body").isPresent()) {
final ImmutablePostTextRequest.Builder builder = ImmutablePostTextRequest.builder()
.name(mainUrl.getPath())
.path(mainUrl.getPath())
.server(visitor.getReader().getServer(mainUrl))
.httpMethod(getMethod(visitor.getLeftBrace(), visitor.getRightBrace(), method))
.contentType(MethodUtils.getParameterValueWithName(visitor.getLeftBrace(), visitor.getRightBrace(), method, "EncType"))
.data(MethodUtils.getParameterValueWithName(visitor.getLeftBrace(), visitor.getRightBrace(), method, "Body").get())
.addAllExtractors(visitor.getCurrentExtractors())
.addAllValidators(visitor.getCurrentValidators())
.addAllHeaders(visitor.getCurrentHeaders())
.addAllHeaders(visitor.getGlobalHeaders())
.addAllHeaders(recordedHeaders)
.addAllParameters(MethodUtils.queryToParameterList(mainUrl.getQuery()))
.recordedFiles(recordedFiles);
visitor.getCurrentHeaders().clear();
return builder.build();
}
if(MethodUtils.getParameterWithName(method, "BodyBinary").isPresent()) {
final ImmutablePostBinaryRequest.Builder builder = ImmutablePostBinaryRequest.builder()
.name(mainUrl.getPath())
.path(mainUrl.getPath())
.server(visitor.getReader().getServer(mainUrl))
.httpMethod(getMethod(visitor.getLeftBrace(), visitor.getRightBrace(), method))
.contentType(MethodUtils.getParameterValueWithName(visitor.getLeftBrace(), visitor.getRightBrace(), method, "EncType"))
.binaryData(extractBinaryBody(visitor.getLeftBrace(), visitor.getRightBrace(), method))
.addAllExtractors(visitor.getCurrentExtractors())
.addAllValidators(visitor.getCurrentValidators())
.addAllHeaders(visitor.getCurrentHeaders())
.addAllHeaders(visitor.getGlobalHeaders())
.addAllHeaders(recordedHeaders)
.addAllParameters(MethodUtils.queryToParameterList(mainUrl.getQuery()))
.recordedFiles(recordedFiles);
visitor.getCurrentHeaders().clear();
return builder.build();
}
return null;
}
public static byte[] extractBinaryBody(final String leftBrace, final String rightBrace, final MethodCall method) {
return hexStringToByteArray(MethodUtils.getParameterValueWithName(leftBrace, rightBrace, method, "BodyBinary").orElse(""));
}
public static byte[] hexStringToByteArray(String s) {
int stringPointer = 0;
int dataPointer = 0;
int globalSize = s.length();
int hexaStringSize = LR_HEXA_STR_PATTERN.length() + 2;
int nbHexa = StringUtils.countMatches(s, LR_HEXA_STR_PATTERN);
int byteSize = (globalSize - nbHexa*hexaStringSize) + nbHexa;
byte[] rawData = new byte[byteSize];
while (stringPointer