Adding custom actions to requests

After an HTTP test is generated, you might want to add a few actions before or after a particular request is processed. For instance, for preprocessors, you can modify the headers before sending them to the server. Similarly, for postprocessors, you can extract data from the response and set it to a variable.

About this task

The actions to be processed must be coded in Java. Note that the pre processor is the last thing to be sent to the server after all the processing (data substitution) for the request is complete. Also, a postprocessor is the last thing to be received after all response processing is complete from the server.

You can add preprocessors and postprocessors at the test level and at the request level. When they are added at both the levels, the ones at the request level takes precedence.

The preprocessors and postprocessors for the HTTP extension uses the following interfaces and methods:

  • IHTTPRequestInterface: Use this interface to process an action before the request is processed.

    • setURI: Use this method to set or change the URL that is sent to the server.

    • getHeader: Use this method to get the name of the header that requires a value. If there are more than one header with the same name, only the first value is returned.

    • removeHeader: Use this method to remove the name of the header.

    • setHeader: Use this method to set the name and value for the header. If the header does not exist, it is created. If the header exists, the value is changed. If there is more than one header with the same name, only the first header is changed.

    • getContent: Use this method to view the content of the HTTP request.

    • setContent: Use this method to set the content or to override the existing content of the HTTP request.

  • IHTTPResponseInterface: Use this interface to process an action after the request is processed.

    • getResponseContent: Use this method to view the content of the response. If the response content exceeds the specified limit, the content is truncated.

    • getResponseHeader: Use this method to view the name of the header. If there are more than one header with the same name in the response, only the first header is returned.

    • getReturnCode: Use this method to view the return code of the current response.

  • IHTTPRequestCommonInterface: Use this interface for both request and response interfaces.

    • getURI: Use this method to view the current URI of the request.

    • getHeaders: Use this method to view all of the headers associated with the request for pre processing or all of the headers associated with the response for post processing.

Preprocessor for the HTTP extension

The following is the sample code for the preprocessor for the HTTP extension:

package customproc;

import com.ibm.rational.test.lt.kernel.services.ITestExecutionServices;
import com.ibm.rational.test.lt.execution.http.external.IHTTPRequestPreProcessor;
import com.ibm.rational.test.lt.execution.http.external.IHTTPRequestInterface;

/**
 * @author Sample Provider
 */
public class mypreproc implements com.ibm.rational.test.lt.execution.http.external.IHTTPRequestPreProcessor {

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

	/**
	 * For javadoc select 'Help Contents' in the
	 * Help menu and select 'Extending Rational Performance Tester functionality' -> 'Extending test execution with custom code'
	 */
	public void exec(ITestExecutionServices tes,
			com.ibm.rational.test.lt.execution.http.external.IHTTPRequestInterface arg) {
		// IHTTPRequestInterface allows users to interact with the HTTP request before the request is sent. 
		// This is the last thing to happen before the request is sent to the server
		// Users also have the full capability of the tes methods that can be used in custom code.  You can store values in 
		// user data area to retrieve at a later time, logging, and many other things
		
		// User can retrieve and manipulate header values
		arg.getHeader("Content-Type");  // users can get a header value
		arg.setHeader("sessionId", "123");  // users can add a header or change a header value by calling setHeader

		// User can retrieve and manipulate POST/PATCH/PUT content
		arg.getContent(1);  // This is used to retrieve the POST/PATCH/PUT data associated with a request.  
		// In the case of multi-part mime there can be more than one "chunk".  Specify the number of the chunk of data you would like
		
		arg.setContent(1, "This is the content I really wanted"); // user can override what is in the POST data chunk with anything they would like

		// User can get and set the URI
		arg.getURI(); // retrieve the current URI
		arg.setURI("/myURIIsGreat/go");
		
		return;
	}

}

Postprocessor for the HTTP extension

The following is the sample code for the postprocessor for the HTTP extension:

package customproc;

import java.util.List;

import com.ibm.rational.test.lt.execution.http.external.HTTPHeaderData;
import com.ibm.rational.test.lt.kernel.services.ITestExecutionServices;

/**
 * @author Sample Provider
 */
public class mypostproc implements com.ibm.rational.test.lt.execution.http.external.IHTTPRequestPostProcessor {

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

	/**
	 * For javadoc select 'Help Contents' in the
	 * Help menu and select 'Extending Rational Performance Tester functionality' -> 'Extending test execution with custom code'
	 */
	public void exec(ITestExecutionServices tes,
			com.ibm.rational.test.lt.execution.http.external.IHTTPResponseInterface arg) {
		// Users can retrieve information from the response directly without having to create references
		
		List<? extends HTTPHeaderData> hdrs = arg.getHeaders(); // this returns the entire list of headers
		for (HTTPHeaderData hdr: hdrs){
			if (hdr.name.equals("MyHeader")) {
				// do some processing. // maybe store value in a variable to use later
				tes.setValue("MyVar", ITestExecutionServices.STORAGE_USER, hdr.value);
				break;
			}
		}
		// the alternative to above is to instead just call directory with the header name you want
		arg.getResponseHeader("MyHeader");
		
		// you can get the response content and perform some kind of data extraction on it
		arg.getResponseContent();
		
		// you can evaluate the return code and do what you want with it. Maybe make a custom counter and report it
		arg.getReturnCode();
		
		return;
	}

}

Procedure

To add a preprocessor or postprocessor:
  1. Select a request in the Test Content area of the Test editor.
  2. Click the Advanced tab in the Test Details area of the Test editor.
  3. Click Create in Preprocessor to add a custom action before a request is processed.
  4. Specify a name for the Java file, and then click Finish.
    A new Java file opens. Add the custom actions to be processed before the request and save the file.
  5. Click Create in Postprocessor to add a custom action after a request is processed, and then follow step 4.

What to do next

You can now run the test to verify if the actions specified in preprocessors or postprocessors returned expected results.