Forum Discussion

UPC_Broadband_L's avatar
UPC_Broadband_L
Occasional Contributor
12 years ago

Problems removing CDATA tags using Events

I currently have in place a REST service whose response returns a CDATA tag with xml contents. Reading some of your available guides, I tried to remove the tag so that soapUI Pro can handle the contents as regular XML (for proper XML formatting, etc).

I found two approaches using Events at the project level:
- SubmitListener.afterSubmit
def content = submit.response.responseContent
content = content.replaceAll( "<!\\[CDATA\\[", "" )
content = content.replaceAll( "]]>", "" )
log.info( content )
submit.response.responseContent = content


- RequestFilter.afterRequest
// get response content
def content = context.httpResponse.responseContent
// manipulate content
log.info( content )
content = content.replaceAll( "<!\\[CDATA\\[", "" )
content = content.replaceAll( "]]>", "" )
// write it back
context.httpResponse.responseContent = content


However, the response always arrives with the tag. If I make errors in the event scripts, there are errors being shown when executing any REST request on a Test Case, but I can never spot the execution itself or even the "log.info( content )".

Screenshots attached.
  • Hi,

    I tried the SubmitListener.afterSubmit event with the code from http://www.soapui.org/Functional-Testin ... the-rescue, and it is removing the CDATA tags for me:


    if( submit.response == null )
    return

    def content = submit.response.responseContent
    content = content.replaceAll( "<!\\[CDATA\\[", "" )
    content = content.replaceAll( "]]>", "" )

    log.info( content )

    submit.response.responseContent = content


    I have not used the RequestFilter.afterRequest with the code you pasted. You will need one of the events disabled while running. Is there other errors occuring before getting to the event handlers?


    Regards,
    Marcus
    SmartBear Support
  • UPC_Broadband_L's avatar
    UPC_Broadband_L
    Occasional Contributor
    I just copied the code you sent into my SubmitListener.afterSubmit event, disabled (and deleted) the other one, and still nothing. I assume the settings I set on the screenshots in the original post are correct (ex: Target field). I tried changing those but got no results.

    Executing the test request shows no errors (in the error log tab or soapUI log tab). I added a screenshot with the Raw view for the request & logging as well, in case it helps.
  • Hi,

    I'm attaching my sample project. Look at the Sample expanded TestSuite -> cdata transfer example.

    1. Start the SampleServiceSoapBinding mock service.
    2. Run the test case and notice the CDATA tags in the Test Request: search response.
    3. Enable the event SubmitListener.afterSubmit.
    4. Re run the testcase and notice the CDATA tags are removed from the Test Request: search response.



    Regards,
    Marcus
    SmartBear Support
  • Hi,

    Thanks for sending the sample project. Yes, it works, but it's a different situation

    Unfortunately, changing the email for our main account (the posting account) caused us to not receive any activation email and left our account utterly locked. Haven't heard from the forum admins for about a week, you should really consider looking into that.
    Fortunately, we eventually found that we had another pro Account. Most people won't, again, keep track of your admin email.
    ----------------------------------------------------------------------------------
    Since I couldn't get any support for several days, I kept looking into it, and reached a few additional conclusions:

    As seen above, this is a REST service. The response is the "Main" XML. However, the Raw response is something like:

    <data contentType="application/xml; charset=utf-8" contentLength="857">
    <![CDATA[<Main publisher="mss.cto.pp" publicationTime="2013-11-21T14:13:37" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn::metadata:2008">
    <Description>
    <LocationTable>
    <Schedule IDRef="_Test_1"/>
    <Schedule IDRef="_Test_2"/>
    </LocationTable>
    <InformationTable>
    <Information Id="_Test_1">
    <Name length="medium"> Test 1</Name>
    <URL>00a2</URL>
    <Genre href="urn::metadata:cs:CS:2008:04"/>
    </Information>
    <Information Id="_Test_2">
    <Name length="medium"> Test 2</Name>
    <URL>00a3</URL>
    <Genre href="urn::metadata:cs:CS:2008:04"/>
    </Information>
    </InformationTable>
    </Description>
    </Main>]]>
    </data>


    As you can see, the whole contents have a CDATA flag applied BEFORE the Main element. I've used the SubmitListener.afterSubmit Event at a project level with the usual code and like we discussed, it didn't work.

    The replacement will only work for anything inside Main (inclusive). For example:
    - a content = content.replaceAll( "data", "" ) or content = content.replaceAll( "contentType", "" ) does nothing.
    - a content = content.replaceAll( "Main", "water" ) will replace correctly.

    Ergo, I can never remove the CDATA tag with the Event script (at least not like this).
    ----------------------------------------------
    I've also tried to use a script and even Property Transfers to store the result into a property.
    - Transfering the initial response to a property works, and I don't see the CDATA there afterwards (only from "Main" onwards, which might hint why the replace CDATA above fails to work). However, I can't filter inside it by XPath (see below).

    Script:

    // allServices is the step name
    def inputHolder = "allServices#Response"
    def holderRawReq = groovyUtils.getXmlHolder(inputHolder)


    This returns a "org.apache.xmlbeans.XmlException: error: Unexpected element: CDATA error at line:x", the getXmlHolder line.

    The same happens if I try to use an XPath expression in a 2nd Property Transfer, with Source as the Property I started with.

    - Hope this gives you further input. It might be a fringe situation that soapUI can't handle.
  • Hi,

    The code still works for me even when I have the CDATA tag in a different location:
    soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sam="http://www.soapui.org/sample/">
    <soapenv:Body>
    <sam:searchResponse>
    <![CDATA[<item>
    <id>Item 1</id>
    <description>One handy protocol droid. This droid is fluent
    in over six million forms of communication and has a lovely golden color.
    Built by an enthusiast. Mindwiped only once. Can be carried on your back.</description>
    <price>1</price>
    </item>
    <item>
    <id>Item 2</id>
    <description>Box of chocolate with several popular items. Covered in Glucose.
    Recommended safety distance when eating Spring Surprise, 10 feet.</description>
    <price>1.99</price>
    </item>
    <item>
    <id>Item 3</id>
    <description>One towel. Massively useful thing.
    Impress the strags with this amazing cloth!</description>
    <price>2.99</price>
    </item>
    <item>
    <id>Item 4</id>
    <description>Ono-Sendai 7. The newest cowboy tech from Hosaka.
    Break the ICE with this conversation piece.</description>
    <price>4.99</price>
    </item>
    <item>
    <id>Item 5</id>
    <description>soapui Pro 1.7.5. Necessary when testing Web Services.</description>
    <price>349</price>
    </item>
    <item>
    <id>item 6</id>
    <description><![CDATA[<item><width>123</width><height>345</height>
    07.<length>098</length><isle>A34</isle></item>></description>
    <price>123</price>
    </item>]]>
    </sam:searchResponse>
    </soapenv:Body>
    </soapenv:Envelope>

    results in:
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sam="http://www.soapui.org/sample/">
    <soapenv:Body>
    <sam:searchResponse><item>
    <id>Item 1</id>
    <description>One handy protocol droid. This droid is fluent
    in over six million forms of communication and has a lovely golden color.
    Built by an enthusiast. Mindwiped only once. Can be carried on your back.</description>
    <price>1</price>
    </item>
    <item>
    <id>Item 2</id>
    <description>Box of chocolate with several popular items. Covered in Glucose.
    Recommended safety distance when eating Spring Surprise, 10 feet.</description>
    <price>1.99</price>
    </item>
    <item>
    <id>Item 3</id>
    <description>One towel. Massively useful thing.
    Impress the strags with this amazing cloth!</description>
    <price>2.99</price>
    </item>
    <item>
    <id>Item 4</id>
    <description>Ono-Sendai 7. The newest cowboy tech from Hosaka.
    Break the ICE with this conversation piece.</description>
    <price>4.99</price>
    </item>
    <item>
    <id>Item 5</id>
    <description>soapui Pro 1.7.5. Necessary when testing Web Services.</description>
    <price>349</price>
    </item>
    <item>
    <id>item 6</id>
    <description><item><width>123</width><height>345</height>
    07.<length>098</length><isle>A34</isle></item>></description>
    <price>123</price>
    </item></sam:searchResponse>
    </soapenv:Body>
    </soapenv:Envelope>

    The CDATA tag is removed.

    content = content.replaceAll( "data", "" ) or content = content.replaceAll( "contentType", "" ) does nothing


    That's not related to the replacing of the CDATA tag, however, I tried similar scenarios and it worked for me as well.

    Using the SubmitListener.afterSubmit event I am not running into any of these problems.


    Regards,
    Marcus
    SmartBear Support
  • Also to clarify

    The code still works for me ....



    I mean this code:


    if( submit.response == null )
    return

    def content = submit.response.responseContent
    content = content.replaceAll( "<!\\[CDATA\\[", "" )
    content = content.replaceAll( "]]>", "" )
    log.info( content )
    submit.response.responseContent = content



    Regards,
    Marcus
    SmartBear Support
  • UPC_Broadband_L's avatar
    UPC_Broadband_L
    Occasional Contributor
    Hi,

    Well, the situation is a bit different. The REST service seems to show the following differences:
    - The "data" item you see in the "XML" tab (see screenshot) isn't necessarily part of the main response xml. You can see that on the "Raw" tab screenshot for example (only Main onwards shows up after the header, not data).
    - Opening the REST query on a regular browser window never shows "data" (just regular, perfectly formatted XML starting on Main).

    Ergo, the CDATA tag shown on the "XML" tab is before Main. The soapUI event does not "find it" in the response from Main onwards, but it is still there.
    Which is why I stated the "replaceAll" example, I can replace anything after the Main item, but not before.

    The Event replacement definitely does not work in this situation. As you could see on the screenshots / previous posts, I *did* use the code exactly as is in the main Project file.

    If necessary, I can send you the project file.
  • Hi,

    It seems that the <data contentType="application/xml; charset=utf-8" contentLength="857"> <![CDATA[ is not in the responseContent. So there is probably another method that needs to be called to get the full response from what I can see posted so far.

    Replace def content = submit.response.responseContent with String content = new String (submit.response.getRawResponseBody()) in the code snippet I previously posted and see if that makes a difference.

    Attach project if that still doesn't work.


    Regards,
    Marcus
    SmartBear Support