Forum Discussion
paashaas
9 years agoOccasional Contributor
I've got same kind of problem.
I'm using Ready! API and 'the ServiceV for a rest service to mock a download of a file with an url.
The filename is the parameter of the REST api and the directory is set as a custom project property.
def fileDir = context.mockService.getProperty("fileDir").value
def filename = requestContext.getProperty("filename")
def file = new File(fileDir + '\\' + filename)
mockResponse.contentType = 'application/octect-stream'
def headers = mockResponse.getResponseHeaders()
def headerKey = 'Content-Disposition'
def headerValue = 'attachment; filename="' + filename + '"'
headers[headerKey] = [headerValue]
mockResponse.responseHeaders = headers
if (file.exists() && file.isFile()){
byte[] fileBytes = file.getBytes()
if (fileBytes.length > 0){
try{
//mockResponse.responseContent="bytes:" + fileBytes.length
com.eviware.soapui.impl.rest.mock.RestMockResult mockResult = mockResponse.getMockResult()
mockResult.setRawResponseData(fileBytes)
//mockResponse.responseContent=javax.xml.bind.DatatypeConverter.printHexBinary(fileBytes)
}
catch(Exception e){
mockResponse.responseContent = "Exception:" + e.getMessage() + e
}
}
else{
mockResponse.responseContent = ""
}
}
else{
mockResponse.responseContent = "File does not exist!"
}paashaas
9 years agoOccasional Contributor
Binary is needed to support all kind of file types (.txt, .pdf, .docx, etc)
Also the code below doesn't work.
com.eviware.soapui.impl.rest.mock.RestMockResult mockResult = mockResponse.getMockResult()
InputStream inputS = new ByteArrayInputStream(fileBytes);
long length=fileBytes.length
com.eviware.soapui.support.Tools.readAndWrite(inputS, length, mockResult.getOutputStream());
inputS.close();When running, this exception is written to file:
java.io.IOException: Closed