Forum Discussion

Michael_McDonal's avatar
Michael_McDonal
Occasional Contributor
16 years ago

Mock Service response wrapped in CDATA tag when embedding CDATA

I am using the OnRequest script in a Mock Service to return XML constructed with MarkupBuilder. When I do the following:

import groovy.xml.MarkupBuilder
def writer = mockRequest.getHttpResponse().getWriter()
def xml = new MarkupBuilder( writer )
xml.result {
detail("Some text")
}
writer.close()


I get this response:

<result>
  <detail>Some text</detail>
</result>


which is as desired. If however I embed CDATA in the response like so:

 detail( xml.yieldUnescaped("<!CDATA[" + "Some text" + "]]>"))


I get the following:

<data contentType="null" contentLength="63"><![CDATA[<result><!CDATA[Some text]]>
  <detail />
</result>]]></data>


Why the inconsistent response?

Thanks.

2 Replies

  • Michael_McDonal's avatar
    Michael_McDonal
    Occasional Contributor
    I've gotten this to work with the CDATA section. I set the response content-type to "application/xml" and placed the CDATA section in a closure:

    import groovy.xml.MarkupBuilder
    def response = mockRequest.getHttpResponse()
    response.setContentType("application/xml")
    def writer = response.getWriter()
    def xml = new MarkupBuilder( writer )
    xml.result {
    detail() { xml.yieldUnescaped("<!CDATA[" + "Some text" + "]]>") }
    }
    writer.close()
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi Michael,

    great that you solved this! The initial problem was that a document only containing a CDATA section is not a valid XML document, which caused soapUI to wrap it in a "data" element. When adding the content-type header, soapUI "trusts" the response to be XML so it doesn't do any validation which renders the document as you want.. (but I still think it's an invalid XML document...)

    regards!

    /Ole
    eviware.com