Forum Discussion

vikititor's avatar
vikititor
Contributor
5 years ago

how MOCK read request data from attachement

Hello, 

exists some way, how to read this request in ReadyAPI MOCK service? 

My AUT (Application under test) sends text file in POST. I need to return JSON with somem value form the text file.

But how to do that?

 

My RAW request looks like:

POST http://localhost:9000/rest/submit HTTP/1.1
Content-Type: multipart/form-data; boundary="----=_Part_2_1159636768.1573464138308"
MIME-Version: 1.0
Content-Length: 132451
Host: localhost:9000
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.5.2 (Java/1.8.0_181)


------=_Part_2_1159636768.1573464138308
Content-Type: application/octet-stream; name=example1.tdf
Content-Transfer-Encoding: binary
Content-Disposition: form-data; name="example1.tdf"; filename="example1.tdf"

1.01:1431.02:03001.09:MBI20191107151008-IDE-000651.11:00.001.12:00.00

I must extract this string "MBI20191107151008-IDE-00065" from it = each time will have differed value. And then send his value to the AUT in JSON.

 

How to read it? It is even possible in Soapui?

  • Hi,

     

    It is even possible in Soapui?

    Yes, I believe that this should be possible in SoapUI.

     

    But, considering that the question was posted to the ServiceVirtualization board but not to the SoapUI one, this is what I did using ServiceV:

    -- For the POST /rest/submit VirtAction I set dispatch style to Script;

    -- Pasted this code to the script dispatch window:

    def attachments = mockRequest.getRequestAttachments();
    //log.info attachments.size();
    
    //def attachmentName = attachments[0].name;
    //log.info "Name: $attachmentName";
    //log.info "URL: ${attachments[0].url}";
    //log.info "Part: ${attachments[0].part}";
    //log.info "Encoding: ${attachments[0].contentEncoding}";
    
    def attachmentInputStream = attachments[0].inputStream;
    
    def textValue = new String(attachmentInputStream.bytes, "UTF-8");
    //log.info textValue;
    context.setProperty("textFileValue", textValue);
    /*
    // This part of code will save attachment's content to the file
    def attachmentContent = attachmentInputStream
    def file = new File("D:\\$attachmentName");
    file.append(attachmentInputStream);
    */
    attachmentInputStream.close();
    //log.info context.getProperty("textFileValue").toString();
    

    -- For the response that will be sent as reply I opened Script section (will be on the right after you click on the response name) and entered this code:

    import groovy.json.JsonSlurper
    import groovy.json.JsonOutput
    
    def textParam = context.getProperty("textFileValue").toString();
    def payload = '{ "id" : "' + textParam + '"}';
    //log.info(payload);
    
    def json = new JsonSlurper().parseText(payload.toString());
    log.info(json);
    
    mockResponse.responseContent = new JsonOutput().toJson(json);
    

    And I got the value from request attachment as a value of the id json entry in response.

    You will need to:

    a) Write the code that will extract required value from the textFileValue property; and

    b) Define the proper json structure for the payload variable.

     

    Hope this will help.

     

    P.S. In case you need to implement the same approach but in SoapUI, I believe that it will be not a problem to adopt the above code for SoapUI.

     

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    It is even possible in Soapui?

    Yes, I believe that this should be possible in SoapUI.

     

    But, considering that the question was posted to the ServiceVirtualization board but not to the SoapUI one, this is what I did using ServiceV:

    -- For the POST /rest/submit VirtAction I set dispatch style to Script;

    -- Pasted this code to the script dispatch window:

    def attachments = mockRequest.getRequestAttachments();
    //log.info attachments.size();
    
    //def attachmentName = attachments[0].name;
    //log.info "Name: $attachmentName";
    //log.info "URL: ${attachments[0].url}";
    //log.info "Part: ${attachments[0].part}";
    //log.info "Encoding: ${attachments[0].contentEncoding}";
    
    def attachmentInputStream = attachments[0].inputStream;
    
    def textValue = new String(attachmentInputStream.bytes, "UTF-8");
    //log.info textValue;
    context.setProperty("textFileValue", textValue);
    /*
    // This part of code will save attachment's content to the file
    def attachmentContent = attachmentInputStream
    def file = new File("D:\\$attachmentName");
    file.append(attachmentInputStream);
    */
    attachmentInputStream.close();
    //log.info context.getProperty("textFileValue").toString();
    

    -- For the response that will be sent as reply I opened Script section (will be on the right after you click on the response name) and entered this code:

    import groovy.json.JsonSlurper
    import groovy.json.JsonOutput
    
    def textParam = context.getProperty("textFileValue").toString();
    def payload = '{ "id" : "' + textParam + '"}';
    //log.info(payload);
    
    def json = new JsonSlurper().parseText(payload.toString());
    log.info(json);
    
    mockResponse.responseContent = new JsonOutput().toJson(json);
    

    And I got the value from request attachment as a value of the id json entry in response.

    You will need to:

    a) Write the code that will extract required value from the textFileValue property; and

    b) Define the proper json structure for the payload variable.

     

    Hope this will help.

     

    P.S. In case you need to implement the same approach but in SoapUI, I believe that it will be not a problem to adopt the above code for SoapUI.