use of org.apache.jmeter.protocol.http.util.HTTPArgument in project jmeter by apache.
the class TestHTTPSamplersAgainstHttpMirrorServer method testPostRequest_BodyFromParameterValues.
private void testPostRequest_BodyFromParameterValues(int samplerType, String samplerDefaultEncoding) throws Exception {
// ensure only values are used
final String titleField = "";
String titleValue = "mytitle";
// ensure only values are used
final String descriptionField = "";
String descriptionValue = "mydescription";
// Test sending data with default encoding
HTTPSamplerBase sampler = createHttpSampler(samplerType);
setupUrl(sampler, "");
setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
((HTTPArgument) sampler.getArguments().getArgument(0)).setAlwaysEncoded(false);
((HTTPArgument) sampler.getArguments().getArgument(1)).setAlwaysEncoded(false);
HTTPSampleResult res = executeSampler(sampler);
String expectedPostBody = titleValue + descriptionValue;
checkPostRequestBody(sampler, res, samplerDefaultEncoding, "", expectedPostBody, CONTENT_TYPE_TEXT_PLAIN);
// Test sending data as ISO-8859-1
sampler = createHttpSampler(samplerType);
String contentEncoding = ISO_8859_1;
setupUrl(sampler, contentEncoding);
setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
((HTTPArgument) sampler.getArguments().getArgument(0)).setAlwaysEncoded(false);
((HTTPArgument) sampler.getArguments().getArgument(1)).setAlwaysEncoded(false);
res = executeSampler(sampler);
expectedPostBody = titleValue + descriptionValue;
checkPostRequestBody(sampler, res, samplerDefaultEncoding, contentEncoding, expectedPostBody, CONTENT_TYPE_TEXT_PLAIN);
// Test sending data as UTF-8
sampler = createHttpSampler(samplerType);
contentEncoding = "UTF-8";
titleValue = "mytitle\u0153\u20a1\u0115\u00c5";
descriptionValue = "mydescription\u0153\u20a1\u0115\u00c5";
setupUrl(sampler, contentEncoding);
setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
((HTTPArgument) sampler.getArguments().getArgument(0)).setAlwaysEncoded(false);
((HTTPArgument) sampler.getArguments().getArgument(1)).setAlwaysEncoded(false);
res = executeSampler(sampler);
expectedPostBody = titleValue + descriptionValue;
checkPostRequestBody(sampler, res, samplerDefaultEncoding, contentEncoding, expectedPostBody, CONTENT_TYPE_TEXT_PLAIN);
// Test sending data as UTF-8, with values that will change when urlencoded
sampler = createHttpSampler(samplerType);
contentEncoding = "UTF-8";
titleValue = "mytitle/=";
descriptionValue = "mydescription /\\";
setupUrl(sampler, contentEncoding);
setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
((HTTPArgument) sampler.getArguments().getArgument(0)).setAlwaysEncoded(false);
((HTTPArgument) sampler.getArguments().getArgument(1)).setAlwaysEncoded(false);
res = executeSampler(sampler);
expectedPostBody = titleValue + descriptionValue;
checkPostRequestBody(sampler, res, samplerDefaultEncoding, contentEncoding, expectedPostBody, CONTENT_TYPE_TEXT_PLAIN);
// Test sending data as UTF-8, with values that will change when urlencoded, and where
// we tell the sampler to urlencode the parameter value
sampler = createHttpSampler(samplerType);
contentEncoding = "UTF-8";
titleValue = "mytitle/=";
descriptionValue = "mydescription /\\";
setupUrl(sampler, contentEncoding);
setupFormData(sampler, true, titleField, titleValue, descriptionField, descriptionValue);
res = executeSampler(sampler);
expectedPostBody = URLEncoder.encode(titleValue + descriptionValue, contentEncoding);
checkPostRequestBody(sampler, res, samplerDefaultEncoding, contentEncoding, expectedPostBody, CONTENT_TYPE_TEXT_PLAIN);
// Test sending data as UTF-8, with values that have been urlencoded
sampler = createHttpSampler(samplerType);
contentEncoding = "UTF-8";
titleValue = "mytitle%2F%3D";
descriptionValue = "mydescription+++%2F%5C";
setupUrl(sampler, contentEncoding);
setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
((HTTPArgument) sampler.getArguments().getArgument(0)).setAlwaysEncoded(false);
((HTTPArgument) sampler.getArguments().getArgument(1)).setAlwaysEncoded(false);
res = executeSampler(sampler);
expectedPostBody = titleValue + descriptionValue;
checkPostRequestBody(sampler, res, samplerDefaultEncoding, contentEncoding, expectedPostBody, CONTENT_TYPE_TEXT_PLAIN);
// Test sending data as UTF-8, with values that have been urlencoded, and
// where we tell the sampler to urlencode the parameter values
sampler = createHttpSampler(samplerType);
contentEncoding = "UTF-8";
titleValue = "mytitle%2F%3D";
descriptionValue = "mydescription+++%2F%5C";
setupUrl(sampler, contentEncoding);
setupFormData(sampler, true, titleField, titleValue, descriptionField, descriptionValue);
res = executeSampler(sampler);
expectedPostBody = titleValue + descriptionValue;
checkPostRequestBody(sampler, res, samplerDefaultEncoding, contentEncoding, expectedPostBody, CONTENT_TYPE_TEXT_PLAIN);
// Test sending data as UTF-8, with values similar to __VIEWSTATE parameter that .net uses
sampler = createHttpSampler(samplerType);
contentEncoding = "UTF-8";
titleValue = "/wEPDwULLTE2MzM2OTA0NTYPZBYCAgMPZ/rA+8DZ2dnZ2dnZ2d/GNDar6OshPwdJc=";
descriptionValue = "mydescription";
setupUrl(sampler, contentEncoding);
setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
((HTTPArgument) sampler.getArguments().getArgument(0)).setAlwaysEncoded(false);
((HTTPArgument) sampler.getArguments().getArgument(1)).setAlwaysEncoded(false);
res = executeSampler(sampler);
expectedPostBody = titleValue + descriptionValue;
checkPostRequestBody(sampler, res, samplerDefaultEncoding, contentEncoding, expectedPostBody, CONTENT_TYPE_TEXT_PLAIN);
// Test sending data as UTF-8, with + as part of the value,
// where the value is set in sampler as not urluencoded, but the
// isalwaysencoded flag of the argument is set to false.
// This mimics the HTTPConstants.addNonEncodedArgument, which the
// Proxy server calls in some cases
sampler = createHttpSampler(samplerType);
contentEncoding = "UTF-8";
titleValue = "mytitle++";
descriptionValue = "mydescription+";
setupUrl(sampler, contentEncoding);
setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
((HTTPArgument) sampler.getArguments().getArgument(0)).setAlwaysEncoded(false);
((HTTPArgument) sampler.getArguments().getArgument(1)).setAlwaysEncoded(false);
res = executeSampler(sampler);
expectedPostBody = titleValue + descriptionValue;
checkPostRequestBody(sampler, res, samplerDefaultEncoding, contentEncoding, expectedPostBody, CONTENT_TYPE_TEXT_PLAIN);
// Test sending data as UTF-8, where user defined variables are used
// to set the value for form data
JMeterUtils.setLocale(Locale.ENGLISH);
TestPlan testPlan = new TestPlan();
JMeterVariables vars = new JMeterVariables();
vars.put("title_prefix", "a test\u00c5");
vars.put("description_suffix", "the_end");
JMeterContextService.getContext().setVariables(vars);
JMeterContextService.getContext().setSamplingStarted(true);
ValueReplacer replacer = new ValueReplacer();
replacer.setUserDefinedVariables(testPlan.getUserDefinedVariables());
sampler = createHttpSampler(samplerType);
contentEncoding = "UTF-8";
titleValue = "${title_prefix}mytitle\u0153\u20a1\u0115\u00c5";
descriptionValue = "mydescription\u0153\u20a1\u0115\u00c5${description_suffix}";
setupUrl(sampler, contentEncoding);
setupFormData(sampler, false, titleField, titleValue, descriptionField, descriptionValue);
((HTTPArgument) sampler.getArguments().getArgument(0)).setAlwaysEncoded(false);
((HTTPArgument) sampler.getArguments().getArgument(1)).setAlwaysEncoded(false);
// Replace the variables in the sampler
replacer.replaceValues(sampler);
res = executeSampler(sampler);
String expectedTitleValue = "a test\u00c5mytitle\u0153\u20a1\u0115\u00c5";
String expectedDescriptionValue = "mydescription\u0153\u20a1\u0115\u00c5the_end";
expectedPostBody = expectedTitleValue + expectedDescriptionValue;
checkPostRequestBody(sampler, res, samplerDefaultEncoding, contentEncoding, expectedPostBody, CONTENT_TYPE_TEXT_PLAIN);
}
use of org.apache.jmeter.protocol.http.util.HTTPArgument in project jmeter by apache.
the class HTTPSamplerBase method addNonEncodedArgument.
public void addNonEncodedArgument(String name, String value, String metadata, String contentType) {
HTTPArgument arg = new HTTPArgument(name, value, metadata, false);
arg.setContentType(contentType);
arg.setAlwaysEncoded(false);
this.getArguments().addArgument(arg);
}
use of org.apache.jmeter.protocol.http.util.HTTPArgument in project jmeter by apache.
the class HTTPSamplerBase method getQueryString.
/**
* Gets the QueryString attribute of the UrlConfig object, using the
* specified encoding to encode the parameter values put into the URL
*
* @param contentEncoding the encoding to use for encoding parameter values
* @return the QueryString value
*/
public String getQueryString(final String contentEncoding) {
CollectionProperty arguments = getArguments().getArguments();
// Optimisation : avoid building useless objects if empty arguments
if (arguments.isEmpty()) {
return "";
}
String lContentEncoding = contentEncoding;
// Check if the sampler has a specified content encoding
if (JOrphanUtils.isBlank(lContentEncoding)) {
// We use the encoding which should be used according to the HTTP spec, which is UTF-8
lContentEncoding = EncoderCache.URL_ARGUMENT_ENCODING;
}
StringBuilder buf = new StringBuilder(arguments.size() * 15);
PropertyIterator iter = arguments.iterator();
boolean first = true;
while (iter.hasNext()) {
HTTPArgument item = null;
/*
* N.B. Revision 323346 introduced the ClassCast check, but then used iter.next()
* to fetch the item to be cast, thus skipping the element that did not cast.
* Reverted to work more like the original code, but with the check in place.
* Added a warning message so can track whether it is necessary
*/
Object objectValue = iter.next().getObjectValue();
try {
item = (HTTPArgument) objectValue;
} catch (ClassCastException e) {
// NOSONAR
log.warn("Unexpected argument type: {} cannot be cast to HTTPArgument", objectValue.getClass().getName());
item = new HTTPArgument((Argument) objectValue);
}
final String encodedName = item.getEncodedName();
if (encodedName.isEmpty()) {
// Skip parameters with a blank name (allows use of optional variables in parameter lists)
continue;
}
if (!first) {
buf.append(QRY_SEP);
} else {
first = false;
}
buf.append(encodedName);
if (item.getMetaData() == null) {
buf.append(ARG_VAL_SEP);
} else {
buf.append(item.getMetaData());
}
// Encode the parameter value in the specified content encoding
try {
buf.append(item.getEncodedValue(lContentEncoding));
} catch (UnsupportedEncodingException e) {
// NOSONAR
log.warn("Unable to encode parameter in encoding {}, parameter value not included in query string", lContentEncoding);
}
}
return buf.toString();
}
use of org.apache.jmeter.protocol.http.util.HTTPArgument in project metersphere by metersphere.
the class MsHTTPSamplerProxy method httpArguments.
private Arguments httpArguments(List<KeyValue> list) {
Arguments arguments = new Arguments();
list.stream().filter(KeyValue::isValid).filter(KeyValue::isEnable).forEach(keyValue -> {
try {
String value = StringUtils.isNotEmpty(keyValue.getValue()) && keyValue.getValue().startsWith("@") ? ScriptEngineUtils.buildFunctionCallString(keyValue.getValue()) : keyValue.getValue();
HTTPArgument httpArgument = new HTTPArgument(keyValue.getName(), value);
if (keyValue.getValue() == null) {
httpArgument.setValue("");
}
httpArgument.setAlwaysEncoded(keyValue.isUrlEncode());
if (StringUtils.isNotBlank(keyValue.getContentType())) {
httpArgument.setContentType(keyValue.getContentType());
}
arguments.addArgument(httpArgument);
} catch (Exception e) {
}
});
return arguments;
}
Aggregations