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);
}
}
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);
}
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);
}
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());
}
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;
}
Aggregations