Helper Wizards for Request Matching and Dynamic Response Creation

You can specify requests matcher conditions for transactions. If a user sends a request that matches your request condition, a custom response is rendered. In the request body matcher, you can define conditions on the request of various types such as XML, JSON, RegEx, matches XPath, matches JSON Path and so on.

The helper wizards for XPath and JSON Path help you generate XPaths and JSON Paths for request matchers in a transaction so you don't have to enter the path manually. You just specify what type of value to match in tree-based selection fields and the path is generated for you.

The helper wizard for responses helps you create dynamic values (Magic Strings) in the response content. With magic strings, the information is pulled from the request and rendered on the response so you do not have to worry about entering the syntax manually.

Add a Request Matcher Helper to an Existing Transaction

Follow these steps:

  1. Go to Service Virtualization tab and click Asset Catalog.
  2. In the Transactions tab, expand an existing transaction.
  3. In the Request Matcher section, click the Body tab.
  4. Click + to define a body matcher.
  5. Select matches XPath or matches JSON Path.
    request body matcher

 

Use the Selection Wizard to define paths. The following screenshot shows an example of XML XPath matching, but the same procedure is valid for JSON Paths as well.

selection wizard

  1. Click Selection Wizard.
  2. Paste your XML in the Sample XML / Sample JSON text field.
  3. Click Build Tree List.
    Under Nodes Tree List, a tree-based selector is generated.

    Note: If you are editing transactions created from a RR-pair import, sample content is automatically taken from the Equals XML or Equals JSON body matchers. The wizard throws an error when the XML is invalid and if it does not follow standards.

  4. Select the checkboxes of one or more Nodes for which you want to generate an XPath or JSON Path. For each selected node, define match type and value.
  5. From the Match Type dropdown, select one of the following options for each selected element:
    1. Specific - matches the exact element that you provide in the Match Value text field.
    2. Anything - verifies only the element's existence in your request. When you select Anything, the wizard disables the Match Value field.
    3. RegEx - identifies matching elements by a valid custom regular expression. The input field does not perform any validation, so validate the regex in another tool.
  6. The Match Value column is pre-populated with values that are present in your sample XML. Edit the values as needed.
  7. (Optional) For each Node, define Attribute Matching if applicable.
    • Match Type: No Matching, Specific, Anything, Regex.
      By default, attributes are ignored (No Matching).
    • Match Value: The value or regex that the attribute should match.
  8. (Optional) Enable Ignore Namespace to allow matching across namespaces.
    Example: If enabled, it generates ElementA/ElementB. If disabled, it generates MyNamespace:ElementA/MyNamespace:ElementB.
  9. Click Select to confirm.
    The wizard generates an XPath or JSON Path for the matching logic.

    The sample payload is saved and retained along with the transaction.

Tip: For XPath, if your request XML has a namespace defined, the namespace URL is needed, and it is automatically extracted and populated in the wizard. You don't have to specify the URLs manually. If there are any namespace prefixes, the wizard automatically imports and lists it on the right side of the matcher.
prefix matcher

Adding Multiple Request Matchers

  • You can add more than one request matcher and the request has to pass all the matching conditions to be considered as a match.
  • You need to paste the sample XML/JSON that is used to display the tree only in the first matcher. The sample carries over to the subsequent matchers so you don't have to paste it again each time.
  • After you update your sample XML/JSON, any change in the sample XML/JSON is reflected in the other matchers, but it does not update the XPaths and JSON Paths already generated.
  • After you update your sample XML/JSON, open the wizard again to update your matchers.

Adding Dynamic Values to Responses

The ‘Edit Wizard’ helps you create dynamic values (Magic Strings) in the response content. Magic strings are basically references to information/data from an incoming request that will be replaced with actual values in the response from the service. The wizard switches automatically between XPath and JSON path based on the type of request body. For all other http related entities, the wizard will generate its appropriate magic strings syntax of the request data.

Follow these steps:

  1. Go to Service Virtualization tab and click Asset Catalog.
  2. In the Transactions tab, expand an existing transaction.
  3. In the Response section, click the Body tab.
  4. Type or paste your response content in the text area.
    If the content is a valid JSON or XML, an Edit Wizard link shows on the right.
    response edit wizard
  5. Click the Edit Wizard link.
    A pop-up window with the response content that you entered in the text area shows. The text field contains the original values you entered and an Expression button next to each field to set a dynamic value on that field.
    edit wizard response values
  6. Click the Expression button.
    A detailed subsection displays all the request information that was found in the transaction. This includes Request Body, Request Query Parameters, Request Headers, Request Cookies and Common Parameters.
    request query parameters details
  7. Map the request information to the appropriate fields.
    nodes values
  8. Click Save. The original response in the text area is replaced and the magic strings that were generated based on your selection are used.
    responses magic strings

If you have used XPath or JSON expressions to create matchers, the node display is kept in sync with these existing matchers.

Best Practices for XML Content in Request and Response Wizard

For request matching to work correctly, add element values with special characters in a CDATA section.

Here are some examples of characters that require a CDATA section:

" (double quotes)
< (less than)
> (greater than)
& (ampersand)
' (apostrophe)

An example of how to wrap element values with special characters within CDATA:

<![CDATA[A & B are equal]]>