Search in sources :

Example 36 with CollectionProperty

use of org.apache.jmeter.testelement.property.CollectionProperty in project jmeter by apache.

the class HTTPHC4Impl method setConnectionHeaders.

/**
 * Extracts all the required non-cookie headers for that particular URL request and
 * sets them in the <code>HttpMethod</code> passed in
 *
 * @param request       <code>HttpRequest</code> which represents the request
 * @param url           <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)
 */
protected void setConnectionHeaders(HttpRequestBase request, URL url, HeaderManager headerManager, CacheManager cacheManager) {
    if (headerManager != null) {
        CollectionProperty headers = headerManager.getHeaders();
        if (headers != null) {
            for (JMeterProperty jMeterProperty : headers) {
                org.apache.jmeter.protocol.http.control.Header header = (org.apache.jmeter.protocol.http.control.Header) jMeterProperty.getObjectValue();
                String headerName = header.getName();
                // Don't allow override of Content-Length
                if (!HTTPConstants.HEADER_CONTENT_LENGTH.equalsIgnoreCase(headerName)) {
                    String headerValue = header.getValue();
                    if (HTTPConstants.HEADER_HOST.equalsIgnoreCase(headerName)) {
                        int port = getPortFromHostHeader(headerValue, url.getPort());
                        // remove any port specification
                        // $NON-NLS-1$ $NON-NLS-2$
                        headerValue = headerValue.replaceFirst(":\\d+$", "");
                        if (port != -1 && port == url.getDefaultPort()) {
                            // no need to specify the port if it is the default
                            port = -1;
                        }
                        if (port == -1) {
                            request.addHeader(HEADER_HOST, headerValue);
                        } else {
                            request.addHeader(HEADER_HOST, headerValue + ":" + port);
                        }
                    } else {
                        request.addHeader(headerName, headerValue);
                    }
                }
            }
        }
    }
    if (cacheManager != null) {
        cacheManager.setHeaders(url, request);
    }
}
Also used : CollectionProperty(org.apache.jmeter.testelement.property.CollectionProperty) JMeterProperty(org.apache.jmeter.testelement.property.JMeterProperty) Header(org.apache.http.Header) BufferedHeader(org.apache.http.message.BufferedHeader)

Example 37 with CollectionProperty

use of org.apache.jmeter.testelement.property.CollectionProperty in project jmeter by apache.

the class TestCookieManagerThreadIteration method testCookieManagerWhenThreadIterationIsNewUser.

@Test
public void testCookieManagerWhenThreadIterationIsNewUser() throws NoSuchFieldException, IllegalAccessException {
    // Controlled by Thread Group
    jmvars.putObject(SAME_USER, false);
    jmctx.setVariables(jmvars);
    CookieManager cookieManagerDynamic = new CookieManager();
    cookieManagerDynamic.setThreadContext(jmctx);
    cookieManagerDynamic.getCookies().clear();
    cookieManagerDynamic.testStarted();
    Cookie cookieDynamic = new Cookie();
    cookieDynamic.setName(DYNAMIC_COOKIE);
    cookieManagerDynamic.getCookies().addItem(cookieDynamic);
    cookieManagerDynamic.setControlledByThread(true);
    Field privateStringField = CookieManager.class.getDeclaredField("initialCookies");
    privateStringField.setAccessible(true);
    CookieManager cookieManagerStatic = new CookieManager();
    Cookie cookieStatic = new Cookie();
    cookieStatic.setName(STATIC_COOKIE);
    cookieManagerStatic.getCookies().addItem(cookieStatic);
    CollectionProperty initialCookies = cookieManagerStatic.getCookies();
    privateStringField.set(cookieManagerDynamic, initialCookies);
    assertTrue("Before the iteration,the quantity of cookies should be 1", cookieManagerDynamic.getCookies().size() == 1);
    assertEquals("Before the iteration, the value of cookie should be what user have set", DYNAMIC_COOKIE, cookieManagerDynamic.getCookies().get(0).getName());
    cookieManagerDynamic.testIterationStart(null);
    assertEquals("After the iteration, the value of cookie should be the initial cookies", STATIC_COOKIE, cookieManagerDynamic.getCookies().get(0).getName());
    assertTrue("After the iteration, the quantity of cookies should be 1", cookieManagerDynamic.getCookies().size() == 1);
    // Controlled by CookieManager
    jmvars.putObject(SAME_USER, true);
    jmctx.setVariables(jmvars);
    cookieManagerDynamic.setThreadContext(jmctx);
    cookieManagerDynamic.getCookies().clear();
    cookieManagerDynamic.testStarted();
    cookieDynamic.setName(DYNAMIC_COOKIE);
    cookieManagerDynamic.getCookies().addItem(cookieDynamic);
    cookieManagerDynamic.setClearEachIteration(true);
    cookieManagerDynamic.setControlledByThread(false);
    cookieManagerStatic.getCookies().clear();
    cookieManagerStatic.getCookies().addItem(cookieStatic);
    initialCookies = cookieManagerStatic.getCookies();
    privateStringField.set(cookieManagerDynamic, initialCookies);
    assertEquals("Before the iteration, the value of cookie should be what user have set", DYNAMIC_COOKIE, cookieManagerDynamic.getCookies().get(0).getName());
    assertTrue("Before the iteration,the quantity of cookies should be 1", cookieManagerDynamic.getCookies().size() == 1);
    cookieManagerDynamic.testIterationStart(null);
    assertEquals("After the iteration, the value of cookie should be the initial cookies", STATIC_COOKIE, cookieManagerDynamic.getCookies().get(0).getName());
    assertTrue("After the iteration, the quantity of cookies should be 1", cookieManagerDynamic.getCookies().size() == 1);
}
Also used : Field(java.lang.reflect.Field) CollectionProperty(org.apache.jmeter.testelement.property.CollectionProperty) Test(org.junit.jupiter.api.Test)

Example 38 with CollectionProperty

use of org.apache.jmeter.testelement.property.CollectionProperty in project jmeter by apache.

the class TestCookieManagerThreadIteration method testCookieManagerWhenThreadIterationIsSameUser.

@Test
public void testCookieManagerWhenThreadIterationIsSameUser() throws NoSuchFieldException, IllegalAccessException {
    // Controlled by Thread Group
    jmvars.putObject(SAME_USER, true);
    jmctx.setVariables(jmvars);
    CookieManager cookieManagerDynamic = new CookieManager();
    cookieManagerDynamic.setThreadContext(jmctx);
    cookieManagerDynamic.getCookies().clear();
    cookieManagerDynamic.testStarted();
    Cookie cookieDynamic = new Cookie();
    cookieDynamic.setName(DYNAMIC_COOKIE);
    cookieManagerDynamic.getCookies().addItem(cookieDynamic);
    cookieManagerDynamic.setControlledByThread(true);
    Field privateStringField = CookieManager.class.getDeclaredField("initialCookies");
    privateStringField.setAccessible(true);
    CookieManager cookieManagerStatic = new CookieManager();
    Cookie cookieStatic = new Cookie();
    cookieStatic.setName(STATIC_COOKIE);
    cookieManagerStatic.getCookies().addItem(cookieStatic);
    CollectionProperty initialCookies = cookieManagerStatic.getCookies();
    privateStringField.set(cookieManagerDynamic, initialCookies);
    assertTrue("Before the iteration,the quantity of cookies should be 1", cookieManagerDynamic.getCookies().size() == 1);
    assertEquals("Before the iteration, the value of cookie should be what user have set", DYNAMIC_COOKIE, cookieManagerDynamic.getCookies().get(0).getName());
    cookieManagerDynamic.testIterationStart(null);
    assertEquals("After the iteration, the value of cookie should be what user have set", DYNAMIC_COOKIE, cookieManagerDynamic.getCookies().get(0).getName());
    assertTrue("After the iteration, the quantity of cookies should be 1", cookieManagerDynamic.getCookies().size() == 1);
    // Controlled by CookieManager
    jmvars.putObject(SAME_USER, false);
    jmctx.setVariables(jmvars);
    cookieManagerDynamic.setControlledByThread(false);
    cookieManagerDynamic.getCookies().clear();
    cookieManagerDynamic.testStarted();
    cookieDynamic.setName(DYNAMIC_COOKIE);
    cookieManagerDynamic.getCookies().addItem(cookieDynamic);
    cookieManagerDynamic.setClearEachIteration(false);
    cookieManagerStatic.getCookies().clear();
    cookieStatic.setName(STATIC_COOKIE);
    privateStringField.set(cookieManagerDynamic, initialCookies);
    assertEquals("Before the iteration, the value of cookie should be what user have set", DYNAMIC_COOKIE, cookieManagerDynamic.getCookies().get(0).getName());
    assertTrue("Before the iteration,the quantity of cookies should be 1", cookieManagerDynamic.getCookies().size() == 1);
    cookieManagerDynamic.testIterationStart(null);
    assertEquals("After the iteration, the value of cookie should be what user have set", DYNAMIC_COOKIE, cookieManagerDynamic.getCookies().get(0).getName());
    assertTrue("After the iteration, the quantity of cookies should be 1", cookieManagerDynamic.getCookies().size() == 1);
}
Also used : Field(java.lang.reflect.Field) CollectionProperty(org.apache.jmeter.testelement.property.CollectionProperty) Test(org.junit.jupiter.api.Test)

Example 39 with CollectionProperty

use of org.apache.jmeter.testelement.property.CollectionProperty in project jmeter by apache.

the class TestValueReplacer method testReverseReplacement.

@Test
public void testReverseReplacement() throws Exception {
    ValueReplacer replacer = new ValueReplacer(variables);
    assertTrue(variables.getUserDefinedVariables().containsKey("server"));
    assertTrue(replacer.containsKey("server"));
    TestElement element = new TestPlan();
    element.setProperty(new StringProperty("domain", "jakarta.apache.org"));
    List<Object> argsin = new ArrayList<>();
    argsin.add("username is jack");
    argsin.add("his_password");
    element.setProperty(new CollectionProperty("args", argsin));
    replacer.reverseReplace(element);
    assertEquals("${server}", element.getPropertyAsString("domain"));
    @SuppressWarnings("unchecked") List<JMeterProperty> args = (List<JMeterProperty>) element.getProperty("args").getObjectValue();
    assertEquals("username is ${username}", args.get(0).getStringValue());
    assertEquals("${password}", args.get(1).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)

Example 40 with CollectionProperty

use of org.apache.jmeter.testelement.property.CollectionProperty in project jmeter by apache.

the class SmtpPanel method getHeaderFields.

public CollectionProperty getHeaderFields() {
    CollectionProperty result = new CollectionProperty();
    result.setName(SmtpSampler.HEADER_FIELDS);
    for (Map.Entry<JTextField, JTextField> header : headerFields.entrySet()) {
        String name = header.getKey().getText();
        String value = header.getValue().getText();
        Argument argument = new Argument(name, value);
        result.addItem(argument);
    }
    return result;
}
Also used : CollectionProperty(org.apache.jmeter.testelement.property.CollectionProperty) Argument(org.apache.jmeter.config.Argument) JTextField(javax.swing.JTextField) HashMap(java.util.HashMap) Map(java.util.Map)

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