Submit and vote on feature ideas.

Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

Add an XML header to a request with out a schema

Options
LegacyForum
LegacyForum Posts: 1,664 ✭✭
edited December 2016 in SOAtest
Hello,

I have the following scenario

I need to execute an HTTP post of an XML message, which has an schema, but also it needs an XML header (that is not the same as an HTTP Header)
This XML header is not included in the schema (because it defines the body of the message).

The XML should look like this
<?xml version="1.0" ?>
<?Label 21247|RTAV|20590|SUCCESS?> <!-- this is the XML header -->
<RtavMessage xmlns="rtav.fidelio.2.0">
<!-- Body of the message -->
</RtavMessage>


So I need an appropriate way to add the XML header in the message before being sent.

I thought in a couple of solutions (with no luck)

Option #1
I created an extension tool, that reads a "Template" data source, and adds the XML header using scripting. It is saved then in a Text Databank
Why not using an XML databank? Because it formats the xml filtering out the XML Header

Once I have the result in the Text Databank, I want to use it as input from a messaging client, but I cannot
The Data Source Column Name of my Test Databank is called "Message"

This is my script
var SOAPUtil = Packages.soaptest.api.SOAPUtil


function postMessage(context)
{
var source = context.getValue("TextDB", "Message")
return SOAPUtil.getXMLFromString([source])
}
The result that I get after that is the word "text" and nothing else...

Option #2
Create a Messaging Client using the same script I used in Option #1 that modifies the template, returning an xml message.
The problem here is that the function in the Messaging Client does not allow any parameter.
So, I tried to access the data source directly, and I faced the same problem than in Option #1. I don't know how to read the Data Source.
While in the Extension Tool I used
var strInput = input.toString()
I cannot do the same here because I don't have input variable.

Can anyone help me?
Thanks
Tagged:

Comments

  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    Options
    Eugenia,

    I believe you are referring to an XML processing instruction:
    http://www.w3.org/TR/2008/REC-xml-20081126/#sec-pi

    The Messaging Client's Form Input and Form XML views do not support XML processing instructions. However, you can use scripting to add the desired processing instruction. I would recommend the following approach:

    1.) Configure your Messaging client to use your XML schema document. This will enable you to configure the elements using the Form Input view. For more information, refer to the SOAtest User's Guide under "SOAtest Tutorial -> Testing Plain XML Services".

    2.) Chain an Extension tool to the Messaging Client's Request Traffic output (right-click -> Add Output...). In general, tools chained to the Request Traffic output can modify the outgoing request message before it is sent. Here is an example JavaScript for your Extension tool:

    CODE
    function addPi(input, context) {
    inputStr = String(input)
    xmlProlog = inputStr.substr(0, inputStr.indexOf("?>") + 2)
    xmlBody = inputStr.substr(xmlProlog.length)
    myPi = "<?Label 21247|RTAV|20590|SUCCESS?>"
    return xmlProlog + myPi + xmlBody
    }

  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    Options
    Hi Benken,
    I took your code and changed it a bit and it worked. Here it is with my comments

    CODE

    var SOAPUtil = Packages.soaptest.api.SOAPUtil

    function addPi(input, context) {
    var inputStr = String(input)
    var myPi = "<?Label 21247|RTAV|20590|SUCCESS?>"
    return myPi + inputStr

    }


    I had to remove line

    xmlProlog = inputStr.substr(0, inputStr.indexOf("?>") + 2)

    because in this stage, the xml is not formed yet and the first Process Instruction is not present, so the indexOf was = -1

    So the <?Label .... ?> process instruction is the first one at that moment.
    After that, before the message is posted to the endpoint, the tool adds the "<?xml version="1.0" encoding="UTF-8"?>" P.I. at the top of the message.

    Thanks for your quick answer

    Eugenia

Tagged