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");
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.
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.
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.