use of org.apache.jmeter.testelement.property.CollectionProperty in project jmeter by apache.
the class CookieManager method clearCookies.
/*
* Remove all the cookies.
*/
private void clearCookies() {
log.debug("Clear all cookies from store");
setProperty(new CollectionProperty(COOKIES, new ArrayList<>()));
}
use of org.apache.jmeter.testelement.property.CollectionProperty in project jmeter by apache.
the class HeaderManager method replace.
@Override
public int replace(String regex, String replaceBy, boolean caseSensitive) throws Exception {
final CollectionProperty hdrs = getHeaders();
int totalReplaced = 0;
for (int i = 0; i < hdrs.size(); i++) {
final JMeterProperty hdr = hdrs.get(i);
Header head = (Header) hdr.getObjectValue();
totalReplaced += JOrphanUtils.replaceValue(regex, replaceBy, caseSensitive, head.getValue(), head::setValue);
}
return totalReplaced;
}
use of org.apache.jmeter.testelement.property.CollectionProperty in project jmeter by apache.
the class HTTPJavaImpl method setConnectionHeaders.
/**
* Extracts all the required headers for that particular URL request and
* sets them in the <code>HttpURLConnection</code> passed in
*
* @param conn
* <code>HttpUrlConnection</code> which represents the URL
* request
* @param u
* <code>URL</code> of the URL request
* @param headerManager
* the <code>HeaderManager</code> containing all the cookies
* for this <code>UrlConfig</code>
* @param cacheManager the CacheManager (may be null)
*/
private void setConnectionHeaders(HttpURLConnection conn, URL u, HeaderManager headerManager, CacheManager cacheManager) {
// Add all the headers from the HeaderManager
Header[] arrayOfHeaders = null;
if (headerManager != null) {
CollectionProperty headers = headerManager.getHeaders();
if (headers != null) {
int i = 0;
arrayOfHeaders = new Header[headers.size()];
for (JMeterProperty jMeterProperty : headers) {
Header header = (Header) jMeterProperty.getObjectValue();
String n = header.getName();
String v = header.getValue();
arrayOfHeaders[i++] = header;
conn.addRequestProperty(n, v);
}
}
}
if (cacheManager != null) {
cacheManager.setHeaders(conn, arrayOfHeaders, u);
}
}
use of org.apache.jmeter.testelement.property.CollectionProperty 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.testelement.property.CollectionProperty in project jmeter by apache.
the class TestValueReplacer method testReverseReplacementXml.
@Test
public void testReverseReplacementXml() throws Exception {
ValueReplacer replacer = new ValueReplacer(variables);
assertTrue(variables.getUserDefinedVariables().containsKey("bounded_regex"));
assertTrue(variables.getUserDefinedVariables().containsKey("normal_regex"));
assertTrue(replacer.containsKey("bounded_regex"));
assertTrue(replacer.containsKey("normal_regex"));
TestElement element = new TestPlan();
element.setProperty(new StringProperty("domain", "<this><is>xml</this></is>"));
List<Object> argsin = new ArrayList<>();
argsin.add("<this><is>xml</this></is>");
argsin.add("And I say: Hello World.");
element.setProperty(new CollectionProperty("args", argsin));
replacer.reverseReplace(element, true);
@SuppressWarnings("unchecked") List<JMeterProperty> args = (List<JMeterProperty>) element.getProperty("args").getObjectValue();
assertEquals("${bounded_regex}", element.getPropertyAsString("domain"));
assertEquals("${bounded_regex}", args.get(0).getStringValue());
}
Aggregations