how MOCK read request data from attachement
- 6 years ago
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.