Forum Discussion

Meisinotti's avatar
Meisinotti
Occasional Contributor
6 years ago

XML declaration in request disappears after property transfer

I am using application/xml requests in http test steps. At the beginnig of each request a xml declariation is needed <?xml version="1.0" encoding="ISO-8859-15"?>. It works fine as long as I run the request stand alone. But when run a property transfer before, the xml declaration will be removed an the test step fails. What can I do?

 

By the way - I found a similar question here. But the link to the answer does not work!

  • So, alt least I got a way that worked for me - than you for you help!

     

    //next line parses the Properties step and gets the value of the declaration
    def declaration = testRunner.testCase.testSuite.getPropertyValue("prexml")
    log.info(declaration)

     

    //next line parses the Properties step and gets the value of the XML without the declaration
    def XML = context.expand('${UC1.116_init#Request}')
    log.info(XML)

     

    //concatenate the values together adding a <br> between them
    def wellFormedXML = declaration +" \n" + XML
    log.info(wellFormedXML)

     

    testRunner.getTestCase().getTestStepByName("UC1.116_init").getProperty('Request').setValue(wellFormedXML)

  • richie's avatar
    richie
    Community Hero

    Hi,

     

     

    I haven't seen issues for a missing prolog/declaration for a looooooooooong time. 

     

    I believe way back (early 2000s) the presence of the declaration in the file content was one of the wellformed rule requirements and if it wasn't present, the xml was considered malformed.  It's been awhile since XMLparsers actually enforced this so Im a bit surprised you have this issue.

     

    Anyway - Im going off on one.

     

    When you say "the test step fails" - what do you actually mean?  Are you getting a 400 type response from your app server or do you include an assertion verifying the presence of the declaration?  

     

    If its just an assertion I'd say remove - cos asserting on the declaration really doesn't add any value to your tests.  If your app server's xml parser is throwing a 400 or similar then I was wondering if you could use an event handler to insert the declaration back into the step's payload content before the step is executed?

    I was wondering if you could use an event handler to insert the declaration back into the payload content after property transfer but before the step has executed?

     

    Cheers,

     

    richie

    • Meisinotti's avatar
      Meisinotti
      Occasional Contributor

      Thank you for your answer! Unfortunately things are more complicated:

       

      1. It's possible that the XML request is not well formed. Unfortunatele I cannot correct this, because I have to deal with a server which cannot handle standard XML.

       

      2. The test step fails because the request is not even accepted by the server (because of the missing declaration) - Error 500.

       

      3. You suggest to use an event handler to add the declaration again. But how exatly do I have to do that? Do I need a groovy skript or how can the be accomplished?

       

      Regards

      Marcus

      • richie's avatar
        richie
        Community Hero

        Hey Meisinotti 

         

        Firstly unless you've got tailored responses that have altered how the app server handles error responses, I wouldn't expect a 500 response from a malformed xml request - 5xx http status codes are server errors - not client errors.   I'd expect something like a 400 (bad request) if you were injecting malformed xml - so I'd suggest you double check the server is working fine, cos it doesnt matter how wellformed/malformed your xml is if the app server's busted :)

         

        Also - can you please describe clearly (maybe a screenshot as well) as what you have in your test right now - you say you pass the XML content via a property transfer and the xfer is actually removing the declaration?  I'm just wondering how the content/payload of your requests are setup.

         

        I've thought about it a bit more and it's probably feasible to do this without bothering with the event handler - all you need to do is to concatenate the xml declaration and a line break before the parent tag in your xml. 

        One of the coders on the forum will probably whip out some 3 lines of cool code that does the job - but in the meantime....

         

        I've only used a couple of event handlers before - so I'm not sure - but I'm guessing it would be something like 'TestRunListener.beforeStep' or 'TestRunListener.beforeRun'  - essentially depending on your testcase object (teststep) hierarchy, I'm thinking for this to work, you'd want the event to fire before the specific test step is submitted to the endpoint.

         

        UPDATE to ticket:  I had another look at the event handlers help page - it says you'll know which event handlers to pick if you know the soapUI API (which I dont) - it mentions the SubmitListener (relates to submitting a request) and RequestFilter (relates to sending a request) - unsure what the difference between submitting and sending is here - but anyway - I'll play around with it later

         

        The 'Target' value you'd specify would be the relevant test step name.

         

        In the field below after the correct event handler is selected you'd need a bit of groovy to grab the content request, concatenate the declaration to the request content and publish the concatenated whole to the endpointn - so something LIKE the following (Im not a groovy coder - -this is a guess on my part)

         

        def extractedRequest = context.expand( '${REST Request#Request}' ).toString();
        
        def wellFormedXML = "<?xml version="1.0" encoding="ISO-8859-15"?> \n" + extractedRequest

        //bit unsure how to finish it off cos Im not very au fait with event handlers nor groovy - but....
        //....Im guessing you'd want to publish the the wellFormedXML variable as the content of the request, but I'm...
        //....wondering how the event handler deals with this - so I'll look at this later tonight

        I need to do a little investigation as to how to publish the concatenated xml content to the endpoint after the event handler has fired - like I say above - I've only used 2 different types of event handler and both times it was on a response not a request - I'll play with it tonight and try and give you an answer by tomorrow - but quite possibly one of the coders will answer this too.

         

        Cheers,

         

        rich