IBM Support

How to use custom code to explicitly set the Cookie in RPT

Question & Answer


Question

How can you use custom code to explicitly set the Cookie in IBM® Rational® Performance Tester (RPT)?

Answer

In RPT if a request in the script contains a "Cookie" entry in the Request Headers, the corresponding Cookie name and value will be included in the request on all subsequent playbacks. In this case, the Cookie was "created" by the client (with a Java applet for example) during recording instead of being supplied by the server.

A user should be able to modify the value of a Cookie (as opposed to a ServerSuppliedCookie) in the header just like you can modify other strings. Please find the sample custom code below to explicitly set the Cookie. Note that httpInfo.getCookieCache().setCookie() class uses the undocumented IHTTPVirtualUserInfo interface. Since RPT does not document this, it is subject to change without notice.

When creating the new Cookie, it will use the same Path and Domain (either explicitly supplied or default) as the original, server-supplied Cookie.


package test;

import java.text.ParseException;

import com.ibm.rational.test.lt.execution.http.cookie.ICookie;
import com.ibm.rational.test.lt.execution.http.cookie.IHTTPVirtualUserInfo;

import com.ibm.rational.test.lt.kernel.IDataArea;
import com.ibm.rational.test.lt.kernel.services.ITestExecutionServices;
import com.ibm.rational.test.lt.kernel.services.IVirtualUserInfo;

/**
* @author IBM Level Three Support
* Version 2 - March 16, 2005
*/

public class SetCookieValue implements
com.ibm.rational.test.lt.kernel.custom.ICustomCode2 {

/**
* Instances of this will be created using the no-arg constructor.
*/
public SetCookieValue() {
}

// args[0] Set-Cookie format:<cookieName>=<cookieValue>[;path=<cookiePath>][;domain=<cookieDomain>]

public String exec(ITestExecutionServices tes, String[] args) {
IDataArea dataArea = tes.findDataArea(IDataArea.VIRTUALUSER);
IHTTPVirtualUserInfo httpInfo =
(IHTTPVirtualUserInfo)dataArea.get(IHTTPVirtualUserInfo.KEY);
IVirtualUserInfo userInfo =
(IVirtualUserInfo)dataArea.get(IVirtualUserInfo.KEY);

String cookieName = args[0].split("=", 2)[0];
ICookie serverSuppliedCookie[] = httpInfo.getCookieCache().getCookieByName(cookieName);
if (serverSuppliedCookie.length != 1)
{
if (serverSuppliedCookie.length == 0)
tes.getTestLogManager().reportMessage("Cannot find existing Cookie named " + cookieName);
else
tes.getTestLogManager().reportMessage("Found more than 1 cookie named " + cookieName);
return null;
}

String newCookieValue = "Generated_Cookie_for_" + userInfo.getUserName();
String newCookie = cookieName + "=" + newCookieValue + ";Path=" + serverSuppliedCookie[0].getPath() +
";Domain=" + serverSuppliedCookie[0].getDomain();

tes.getTestLogManager().reportMessage("Cookie from Server: " + serverSuppliedCookie[0].toString());
tes.getTestLogManager().reportMessage("New Cookie: " + newCookie);

try {
httpInfo.getCookieCache().setCookie(newCookie);
} catch (ParseException e) {
tes.getTestLogManager().reportMessage("ERROR: Can't parse Cookie string: " + newCookie);
}
return null;
}
}


Internal Use Only

Custom code, hence set to Internal use

[{"Product":{"code":"SSMMM5","label":"Rational Performance Tester"},"Business Unit":{"code":"BU004","label":"Hybrid Cloud"},"Component":"General Information","Platform":[{"code":"PF033","label":"Windows"}],"Version":"6.1;6.1.0.1;6.1.1;6.1.2;7.0;7.0.0.1;7.0.0.2;7.0.1;7.0.1.1;7.0.1.2;7.0.2;7.0.2.3;8.0","Edition":""}]

Document Information

Modified date:
16 June 2018

UID

swg21236931