Tuesday, January 3, 2012

Handling Client Side Certificates in LoadRunner for Web Services Testing

Web services requiring client side certificates can be handled in LoadRunner as follows:
  • Create a .pem client certificate file.  
    • Client certificates in other formats can be converted to .pem format using a utility such as openssl.
  • Copy the .pem file to the loadrunner script directory
  • Set the .pem file in the loadrunner script using the web_set_certificate_ex method:
    • web_set_certificate_ex( 
    •         "CertFilePath=clientcertificate.pem", 
    •         "CertFormat=PEM", 
    •         "KeyFilePath=clientcertificate.pem", 
    •         "KeyFormat=PEM", 
    •         "Password=testpassword", 
    •         LAST);
  • Post xml to the secure url
The following script provides an example of the client certificate file usage:

#include "as_web.h"
#include "lrw_custom_body.h"

char soapURL[]        = "URL=https://SERVER:8080/Path/v1";

// Verification text
char expectedResponse[] = "<ResponseMessage>Success!</ResponseMessage>";

char requestXMLBody[] = 
"Body="
    "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:urn=\"urn:test:messages:v1\">\n"
"<soap:Header>\n"
"..."
"</soap:Header>\n"
"<soap:Body>\n"
"..."
"</soap:Body>\n"
    "</soap:Envelope>\n";
                  

Action()
{
PostXML( requestXMLBody, soapUR );
return 0;
}


void PostXML( char* xmlBody, char* soapURL, char* transactionName )
{
   /******** CreateAuthAccount Transaction ********/
   
   web_add_header( "Content-Type", "application/soap+xml" );

   web_set_certificate_ex( 
        "CertFilePath=clientcertificate.pem", 
        "CertFormat=PEM", 
        "KeyFilePath=clientcertificate.pem", 
        "KeyFormat=PEM", 
        "Password=testpassword", 
        LAST); 

   // save the response
   web_reg_save_param( "transactionResponse",
                       "LB=",
                       "RB=",
                       "Search=Body",
                       "NOTFOUND=Warning",
                        LAST );
   
   lr_start_transaction("post");

   web_custom_request( "postXML",
                       soapURL,
                       "Method=POST",
                       xmlBody,
                       LAST );

   // check for errors in response
   if( 0 != strstr( lr_eval_string( "{transactionResponse}" ), "ServiceException" ) )
   {
          lr_end_transaction( "post", LR_FAIL );
 lr_error_message( "ERROR (exception found in response): %s", lr_eval_string( "{transactionResponse}" ) );
   }
   else
   {
  lr_end_transaction("post", LR_PASS);
  lr_log_message( lr_eval_string( "{transactionResponse}" ));
   }
}

No comments:

Post a Comment