Forum Discussion

saska_filip's avatar
saska_filip
New Contributor
10 years ago

how to process mime http response

Hello everybody,

 

I have a problem with processing MIME response. I am testing a HTTP service, which return a multipart response containing 2 parts. One is a response xml containing cid reference to another XML document sent as the other MIME part.

Please, how can I access the response in PropertyTransfer? I need to extract some information from the MIME response and paste it to subsequent test step.

 

Please help.

 

Thank you,

Filip

  • Hi Rao,

    thank you for the answer, this helped me a lot! I ran your script and ran into problems with getting the response from messageExhange. So ditched the option to use it as an assert script and got the attachment from testrunner:

     

    def testStep = testRunner.testCase.getTestStepByName( "GetDataRequest" )
    def response = testStep.testRequest.response
    def attachments = response.attachments

     

    then I converted the attachment into String using your script:

    attachments[i].inputStream.text

     

    After this it was easy to extract the information I needed from the string. This is the script that is working for me:

    def testStep = testRunner.testCase.getTestStepByName( "GetDataRequest" )
    def response = testStep.testRequest.response
    def attachments = response.attachments
    def attachmentCount = attachments.length
    
    def myInvoiceID = testRunner.testCase.getTestStepByName("GeneralProperties").getPropertyValue("InvoiceNumber")
    
    assert attachmentCount > 1, "No invoice was returned by AN"
    
    def attachmentContent
    def invoiceID
    def result = false
    
    for(i=1; i< attachmentCount; i++) {
    	attachmentContent = attachments[i].inputStream.text
    
    	assert attachmentContent.contains("invoiceID"), "Invoice ID is missing"
    
    	invoiceID = attachmentContent.split("invoiceID=")[1]
    	invoiceID = invoiceID.split("\"")[1]	
    
    	if (invoiceID.equals(myInvoiceID) ) {
    		result = true
    	}	
    }
    
    if(!result) {
    	log.info("Invoice was not found. Going back to GetPending")
    	testRunner.gotoStepByName( "Wait10s_2")
    } else {
    	log.info("Invoice was found. Proceeding with invoice acknowledgement")
    }

     

    Thank you all for your contribution. Hope it helps somebody else sometime :)

     

    Kind regards,

    Filip

  • kondasamy's avatar
    kondasamy
    Regular Contributor

    The second part could be seen in the Attachment tab of the XML response viewer. Could you please let us know the below informations; so we can have more chances to help you,

    1) What kind of MIME attachment is returned (PDF, TXT, DOCX etc.,?)

    2) Would you like to extract information that lies within the MIME part?

     

    Additionally, I don't think we have a provision to access the attachment related information using Property Transfer/ Property Expansion. I hope, it could be achieved using Groovy script! Here is a gist of the concept - http://stackoverflow.com/questions/9066049/attaching-file-in-soapui-with-groovy

     

    Thanks,

    Samy

     

    Did my reply answer your question? Give Kudos or Accept it as a Solution to help others, Thanks. ↓↓↓

    • saska_filip's avatar
      saska_filip
      New Contributor

      Hi Samy,
      thank you for the reply. However, there are a few catches. For one, the response doesn't display on the XML response viewer. It only displays on the "Raw" response tab, despite the first part having Content-Type: text/xml; charset=UTF-8 in its header.

      To answer your question:
      1) The main message and its attachment are both XML documents (Content-Type: text/xml; charset=UTF-8 )
      2) Yes, that is exactly what I want to do.

      Ok, I don't insist on property transfer, I am ok with doing it with the Groovy script. Please, can you provide a sample how to access the MIME attachment via Groovy? Even as a plain text, I can parse it from there. I tried with the XMLHolder na ran into problems because of the MIME header.

       

      My response, as displayed in the Raw response tab:

       

      HTTP/1.1 200 Apple
      Date: Wed, 18 Nov 2015 14:07:22 GMT
      Server: Apache
      Content-Length: 16633
      Content-Encoding: gzip
      Vary: User-Agent
      Keep-Alive: timeout=15, max=99
      Connection: Keep-Alive
      Content-Type: multipart/mixed; boundary="----=_Part_61_1965847066.1447855642896"

      ------=_Part_61_1965847066.1447855642896
      Content-Type: text/xml; charset=UTF-8
      Content-ID: 1447855642896.1811071090@app253.snv.ariba.com

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE cXML SYSTEM "http://xml.cxml.org/schemas/cXML/1.2.028/Private.dtd">

      <cXML timestamp="2015-11-18T06:07:22-08:00" payloadID="1447855642896-4009407626544432757@216.109.111.75">

      <Response>
      <Status code="200" text="OK"/>
      <DataResponse>

      <Attachment>
      <URL>1447855642895.1673476516@app253.snv.ariba.com</URL>
      </Attachment>

      </DataResponse>
      </Response>
      </cXML>

       

      ------=_Part_61_1965847066.1447855642896
      Content-Type: text/xml; charset=UTF-8
      Content-ID: 1447855642895.1673476516@app253.snv.ariba.com

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE cXML SYSTEM "http://xml.cxml.org/schemas/cXML/1.2.028/InvoiceDetail.dtd">
      <cXML payloadID="Payload-4683743002075904415" signatureVersion="1.0" timestamp="2015-11-16T14:53:16.456+0100" version="1.2.028"><Header><From><Credential
      domain="NetworkID"><Identity>AN0T</Identity></Credential><Credential
      domain="VendorID"><Identity>1</Identity></Credential><Credential
      domain="PrivateID"><Identity>12</Identity></Credential></From><To><Credential
      domain="NetworkID"><Identity>AN010T</Identity></Credential></To><Sender><Credential domain="NetworkID"><Identity>AN0101</Identity><SharedSecret>ar4</SharedSecret></Credential><UserAgent>manual post - SOAUI</UserAgent></Sender></Header>
      <Request Id="cXMLData" deploymentMode="test"></Request></cXML>
      ------=_Part_61_1965847066.1447855642896--

       

       

      Thank you very much,
      Filip

      • nmrao's avatar
        nmrao
        Champion Level 3

        saska_filip,

        Here is the script assertion that you are looking for. Core part of it taken from here

         

        Note that the script could not be tested as I don't have such environment. Appreciate if you can try and post the outcome.

         

        /**
        * Get the attachment from the response and
        * Set the attachment string as request for next/given step dynamically
        * This assumes single attachment in current response
        * Script can be used as script assertion **/
        /*you can use it as property expansion as well for below
        two properties if you do not want to modify here*/
        //set the required content type of attachment def expectedContentType = 'text/xml; charset=UTF-8'
        //set the next request step name where dynamic request has to be created def nextStepName='step2' def actualContentType = messageExchange.responseAttachments[0].contentType
        //assert if it is matching with response received assert actualContentType == expectedContentType, "Content type is not matching to ${expectedContentType}" //if the assertion is passed then only read the attachment text def attachmentText = messageExchange.responseAttachments[0].inputStream.text

        //assert the text of attachment is not null
        assert attachmentText, "Attachment text is empty or null" //Get the existing request for the next step def nextStepRequest = context.testCase.getTestStepByName(nextStepName).getTestRequest() //Set the dynamic value from the attachment text to next step request. nextStepRequest.setRequestContent(attachmentText)