Adding Custom Headers to SOAP Requests

Applies to TestComplete 15.70, last modified on December 17, 2024
Testing web services with TestComplete is obsolete. We recommend that you use ReadyAPI, another SmartBear's automated testing tool for SOAP and REST web services.
If needed, you can run ReadyAPI tests from TestComplete.

General Information

SOAP web services use XML for data exchange between the client application and a web service. A SOAP request consists of the root Envelope element that has two child elements - Header and Body. Header is an optional element that can contain some extra information to be passed to the web service. Body is a required element and contains data specific to the called web service method.

When you call a method of your tested web service from tests, TestComplete generates a SOAP request and sends it to the web service. The SOAP body of the generated request contains parameters of the called web service method. However, the generated SOAP request does not contain the SOAP header.

In some cases, you may need to pass some additional information in the Header element of the SOAP request. For example:

  • If your web service uses custom authentication that is not supported by TestComplete, you can use the Header element to pass authentication information to the web service.

  • If the Header element is used to pass information on how the SOAP body contents should be processed by the web service.

  • If the Header element contains expiration information for the data contained in the Body element.

The rest of this topic explains how you can add custom headers to SOAP requests and provides sample scripts.

Adding Custom SOAP Headers

To access SOAP requests to be sent to your tested web service and to add custom SOAP Headers to a request, use the OnWebServiceRequest event. The event is triggered when you call methods of your tested web service in your test.

To create an event handler that will add custom SOAP headers to SOAP requests, follow the steps below:

  1. Add a handler for the OnWebServiceRequest event to your project (for information on how to do this, see Creating Event Handlers for TestComplete Events).

    A basic script routine that handles the OnWebServiceRequest event should look as follows:

    JavaScript, JScript

    function GeneralEvents_OnWebServiceRequest(Sender, Request, Helper, RequestInfo)
    {

    }

    Python

    def GeneralEvents_OnWebServiceRequest(Sender, Request, Helper, RequestInfo):

    VBScript

    Sub GeneralEvents_OnWebServiceRequest(Sender, Request, Helper, RequestInfo)

    End Sub

    DelphiScript

    procedure GeneralEvents_OnWebServiceRequest(Sender, Request, Helper, RequestInfo);
    begin

    end;

    C++Script, C#Script

    function GeneralEvents_OnWebServiceRequest(Sender, Request, Helper, RequestInfo)
    {

    }

    The event handling routine has the following parameters:

    • Sender - Specifies the Events control that generates the event.

    • Request - Provides access to the content of the SOAP request.

    • Helper - A helper object.

    • RequestInfo - Provides information on the called method and on the tested web service.

  2. In the event handler routine, specify instructions that will add custom headers to the web service requests. To do this, use the Helper.AddHeader method:

    JavaScript, JScript

    Helper.AddHeader("HeaderName", "HeaderNamespace", "HeaderContent");

    Python

    Helper.AddHeader("HeaderName", "HeaderNamespace", "HeaderContent");

    VBScript

    Call Helper.AddHeader("HeaderName", "HeaderNamespace", "HeaderContent")

    DelphiScript

    Helper.AddHeader('HeaderName', 'HeaderNamespace', 'HeaderContent');

    C++Script, C#Script

    Helper["AddHeader"]("HeaderName", "HeaderNamespace", "HeaderContent");

  3. Add the desired web service method call to your test. Every time TestComplete will call the method and sends a SOAP request to the tested web service, the GeneralEvents_OnWebServiceRequest event handling routine will add the specified custom headers to the request.

Note: The OnWebServiceRequest event is triggered for all requests sent to your tested web services’ methods. If your test calls several web service methods and you want to add custom headers only to some web service requests, use the Request and RequestInfo parameters to identify the web service and the method for which the request has been generated.

The example in the section below demonstrates how you can add custom SOAP header to your SOAP requests in TestComplete tests.

Example

The script below demonstrates how you can add a custom Header element to a SOAP request that will be sent to your tested web service.

The Main routine in the example works with the tested web service, SampleWebService, added to the project’s WebServices collection. The routine calls the ProcessData method that requires that authentication information be sent in the request's Header element.

The GeneralEvents_OnWebServiceRequest event handling routine adds SOAP headers that contain authentication information to web service requests.

In order for the example to work correctly, the OnWebServiceRequest event must be added to the project’s events control and linked to the GeneralEvents_OnWebServiceRequest routine.

JavaScript, JScript

function Main()
{
  WebServices.SampleWebService.ProcessData();
}

function GeneralEvents_OnWebServiceRequest(Sender, Request, Helper, RequestInfo)
{

  Helper.AddHeader("UserID", "http://tempuri.org/", "jsmith");
  Helper.AddHeader("Password", "http://tempuri.org/", "25jeL3n");
}

Python

def Main():
  WebServices.SampleWebService.ProcessData;

def GeneralEvents_OnWebServiceRequest(Sender, Request, Helper, RequestInfo):
  Helper.AddHeader('UserID', 'http://tempuri.org/', 'jsmith');
  Helper.AddHeader('Password', 'http://tempuri.org/', '25jeL3n');

VBScript

Sub Main
  WebServices.SampleWebService.ProcessData
End Sub

Sub GeneralEvents_OnWebServiceRequest(Sender, Request, Helper, RequestInfo)

  Call Helper.AddHeader("UserID", "http://tempuri.org/", "jsmith")
  Call Helper.AddHeader("Password", "http://tempuri.org/", "25jeL3n")
End Sub

DelphiScript

procedure Main;
begin
  WebServices.SampleWebService.ProcessData;
end;

procedure GeneralEvents_OnWebServiceRequest(Sender, Request, Helper, RequestInfo);
begin

  Helper.AddHeader('UserID', 'http://tempuri.org/', 'jsmith');
  Helper.AddHeader('Password', 'http://tempuri.org/', '25jeL3n');
end;

C++Script, C#Script

function Main()
{
  WebServices["SampleWebService"]["ProcessData"]();
}

function GeneralEvents_OnWebServiceRequest(Sender, Request, Helper, RequestInfo)
{

  Helper["AddHeader"]("UserID", "http://tempuri.org/", "jsmith");
  Helper["AddHeader"]("Password", "http://tempuri.org/", "25jeL3n");
}

The content of the SOAP request sent to the tested web service will be as follows:

XML

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <UserID xmlns="http://tempuri.org/">jsmith</UserID>
    <Password xmlns="http://tempuri.org/">25jeL3n</Password>
  </s:Header>
  <s:Body xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns1="http://tempuri.org/">
    <ns1:ProcessData></ns1:ProcessData>
  </s:Body>
</s:Envelope>

See Also

About Testing Web Services
About Testing Web Services
Creating Web Service Tests
OnWebServiceRequest Event
Adding Events to Event Controls

Highlight search results