wcfstorm

 

Randomizing service requests

Requests to WCF or web services can be randomized by implementing the IMethodInvokePipeline plugin interface exposed by WCFStorm.Plugin.dll.  This interface defines 2 methods.

 public interface IMethodInvokePipeline
{
   
void EditServiceEndpoint(ServiceEndpoint svcEndpoint);
   
string EditMethodInput(string inputXml, string previousResponseXml);        
}

 

* void EditServiceEndpoint(ServiceEndpoint svcEndpoint):

If you have to access the ServiceEndpoint instance auto-generated by WCFStorm, you can modify it in this method.  From here, you'll have access to the Binding, Behavior and Address  information.

  * string EditMethodInput(string inputXml, string previousResponseXml)

 From this method method you can modify the outgoing request by returning a new input xml.  This method takes 2 parameters, inputXml and previousResponseXml.  "InputXml" is the request that is about to get sent out to the service.  "PreviousResponseXml" as its name implies is the previous response of the WCF service when its method was invoked.  If the service method will be invoked for the first time previousResponseXml is empty (String.empty).

The inputXml and previousResponseXml are the XML that is shown in the "XML View" of WCFStorm.

Steps to create a plugin for randomizing input.

Scenario :
We have a WCF service that has a method called Login and takes the credentials (username, pwsd) as parameter.  We
want to test this service by providing random login information.

The service has the following contract:
[ServiceContract]
public interface ITestService{ 
      [OperationContract]
       string Login(SvcCredentials credentials 
}
[DataContract]
public class SvcCredentials
{
    [DataMember] public string Username{get;set;}
    [DataMember] public string Password {get;set;}
}

To test this service with random login info, we have to

  1. Create a C# class library project
  2. Add reference to WcfStorm.Plugin.dll (located in the same folder as wcfstorm.exe)
  3. If needed add further references. Ensure that Copy Local is set to true!
  4. Implement IMethodInvokePipeline
  5. Leave EditServiceEndpoint method empty.  (We'll not be modifying the internal ServiceEndpoint instance)
  6. Implement EditMethodInput.  Please see sample code below.

    Important:
      * Decorate the class that implements IMethodInvokePipeline with the [TargetService] attribute
      * Decorate the EditMethodInput method with the [TargetMethod] attribute.

    Sample implementation: (Please see download link at the bottom for the full code)
    [TargetMethod(MethodName = "Login", TargetType = TargetType.Specific)]
    public string EditMethodInput(string inputXml, string previousResponseXml)
    {
        #region Sample InputXml
        
    /* "XML View" from WCFStorm.
         * 
         * 
             
                
                      <-- isNull="false" means string is Empty.
                    
                
             
           
         * 
         */
        #endregion

        var doc = new XmlDocument();
        doc.LoadXml(inputXml);

        
    var usernameXpath = "/Login/MethodParameters/credentials/Username";
        
    var passwdXpath = "/Login/MethodParameters/credentials/Password";

        
    Random r = new Random();
        
    var index = r.Next(1, 10);
        
    var keyPair = this._logins.ElementAt(index);
        doc.SelectSingleNode(usernameXpath).InnerXml = keyPair.Key;
        doc.SelectSingleNode(passwdXpath).InnerXml = keyPair.Value;
      
        
    //return the modified XML input.
        return doc.OuterXml;
    }

    public void EditServiceEndpoint(System.ServiceModel.Description.ServiceEndpoint svcEndpoint)
    {
        
    //Do nothing.       
    }
     
  7. Compile (Important : Target the .NET 3.0 framework)
  8. To load the newly compiled plugin, follow the steps below in WcfStorm.  If the plugin is loaded successfully, the dll icon (#4) will show.

 

Sample Code Download