Target/Hostname Modification Scripts

Document created by DPM Admin Employee on Jul 21, 2017Last modified by Trishala Kalal on Aug 14, 2017
Version 2Show Document
  • View in full screen mode
  • Script 1: Automatically handle HTTP 302 redirects

The Handle dynamic HTTP 302 redirects script is used to detect a redirect (HTTP 302) in the test and set the next message’s target so subsequent requests go to that new hostname. This script looks to see if the response code was 302. If so, it resets the target of the next message to use the host name and service path specified by the Location header


Get the message that precedes this script.

var origText = msg.getResponse(msg.RESPONSE_TEXT);

Determine if the response is an HTTP 302 redirect.

var pos = origText.indexOf("HTTP/1.1 302 Found");
if (pos == -1)
{
   $context.result.postMessage($context.result.LEVEL_INFO, "did not find 302");
}
else
{

Get the value of the Location header and the following protocol.

var firstPos = origText.indexOf("Location: ");
firstPos = origText.indexOf("http", firstPos + 1);

var lastPos = origText.indexOf("\n", firstPos + 1);

var location = origText.substring(firstPos, lastPos);

Separate the host name and service path.

var doubleSlashPos = location.indexOf("//"); 
firstSlashPos = location.indexOf("/", doubleSlashPos + 2);

Extract the host name and the service path from the location.

var hostName = location.substring(doubleSlashPos + 2, firstSlashPos);

var servicePath = location.substring(firstSlashPos);

Set the host name and service path for the target of the next message.

$context.currentItem.nextItem.target.systemPropertyList.setPropertyValue("HostName", hostName); $context.currentItem.nextItem.target.systemPropertyList.setPropertyValue("ServicePath", servicePath); }

    1. Script 2: Override a Target’s URL for the Instance of a Clip

The change this script makes persists across serial repeats of a clip. This script sets the target for the message immediately after this script. Since it is setting the target’s URL, all messages that use that target use this new URL.


var hostname = 'new_hostname.company.com'; 
$context.result.postMessage($context.result.LEVEL_INFO, "hostname: " + hostname);
$context.currentItem.nextItem.target.systemPropertyList.setPropertyValue("HostName", hostname);

  1. Script 3: Override a target’s use of HTTP/HTTPS for the instance of a clip

This script overrides a target to either use or not use SSL (i.e. HTTP to HTTPS or vice versa).


$context.currentItem.nextItem.target.systemPropertyList.setPropertyValue("UseSSL", “true”);

  1. Script 4: Host Override in Header

In some situations when an application is deployed to a non-production environment, an IP address must be used to reach that environment (i.e. the target location is an IP address). However, the application may only respond to requests where the HTTP Request Headers include the correct hostname in the host header. This script alters the host header to include the appropriate hostname, but sends the message to the IP address. The alternative to using this script would be entering a HOSTS file entry manually on each load server.

Put the one-line script in the test clip just before the first message that utilizes the target that needs to have the host override header in it. More information can be found in the FAQ, How do I override host files for a load test?


$context.currentItem.nextItem.target.systemPropertyList.setPropertyValue
("HttpHostOverride", "YOUR_HOST_URL");

Attachments

    Outcomes