Search in sources :

Example 86 with CollectionProperty

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<>()));
}
Also used : CollectionProperty(org.apache.jmeter.testelement.property.CollectionProperty) ArrayList(java.util.ArrayList)

Example 87 with CollectionProperty

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;
}
Also used : CollectionProperty(org.apache.jmeter.testelement.property.CollectionProperty) JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty)

Example 88 with CollectionProperty

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);
    }
}
Also used : CollectionProperty(org.apache.jmeter.testelement.property.CollectionProperty) JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) Header(org.apache.jmeter.protocol.http.control.Header)

Example 89 with CollectionProperty

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();
}
Also used : CollectionProperty(org.apache.jmeter.testelement.property.CollectionProperty) HTTPArgument(org.apache.jmeter.protocol.http.util.HTTPArgument) PropertyIterator(org.apache.jmeter.testelement.property.PropertyIterator) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 90 with CollectionProperty

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());
}
Also used : CollectionProperty(org.apache.jmeter.testelement.property.CollectionProperty) JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) TestPlan(org.apache.jmeter.testelement.TestPlan) ArrayList(java.util.ArrayList) StringProperty(org.apache.jmeter.testelement.property.StringProperty) ArrayList(java.util.ArrayList) List(java.util.List) ConfigTestElement(org.apache.jmeter.config.ConfigTestElement) TestElement(org.apache.jmeter.testelement.TestElement) Test(org.junit.jupiter.api.Test)

Aggregations

CollectionProperty (org.apache.jmeter.testelement.property.CollectionProperty)91 JMeterProperty (org.apache.jmeter.testelement.property.JMeterProperty)29 ArrayList (java.util.ArrayList)19 NullProperty (org.apache.jmeter.testelement.property.NullProperty)13 Test (org.junit.Test)11 PropertyIterator (org.apache.jmeter.testelement.property.PropertyIterator)8 Test (org.junit.jupiter.api.Test)7 IOException (java.io.IOException)5 List (java.util.List)4 StringProperty (org.apache.jmeter.testelement.property.StringProperty)4 File (java.io.File)3 Argument (org.apache.jmeter.config.Argument)3 PowerTableModel (org.apache.jmeter.gui.util.PowerTableModel)3 TestPlan (org.apache.jmeter.testelement.TestPlan)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Field (java.lang.reflect.Field)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 UnknownHostException (java.net.UnknownHostException)2 LinkedList (java.util.LinkedList)2