cancel
Showing results for 
Search instead for 
Did you mean: 

Add a pdf as an attachment for a mock response

SOLVED
Highlighted
Occasional Contributor

Add a pdf as an attachment for a mock response

I am reading a file from my system and i need to send it as a pdf attachment in my mock response. I am using ready api. Can any body give me advice please?

 

I have tried the below code and when the request is made, the pdf is downloaded but it fails to load it on the browser.

String filename='C:/soapui.log'
File file = new File(filename);

def headers = mockResponse.responseHeaders
headers["Content-Type"]=["application/pdf"]
headers["Content-Disposition"]=["attachment.pdf"]
mockResponse.responseHeaders=headers

 

How do i return a  file as a pdf,which i am reading in from my system

Any suggestions please?

 

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Occasional Contributor

Re: Add a pdf as an attachment for a mock response

Got the solution.Here you go:

 

import com.itextpdf.text.Document
import com.itextpdf.text.Paragraph
import com.itextpdf.text.pdf.PdfWriter
import org.apache.commons.io.IOUtils


def headers = mockResponse.responseHeaders

Document document = new Document();

File file = new File("response.pdf")
FileOutputStream fos= new FileOutputStream(file)
PdfWriter pdfWriter = PdfWriter.getInstance(document, fos);
document.open();
Paragraph paragraph = new Paragraph();
paragraph.add("Hello groovy!");
document.add(paragraph);
document.close()
fos.close()

 

try

{

def httpResponse = mockRequest.httpResponse
FileInputStream fis= new FileInputStream(file);
OutputStream outputStream = httpResponse.getOutputStream()

IOUtils.copy(fis,outputStream)

fis.close()
outputStream.close()

}


catch (Exception e){

log.info "error"
}
mockRequest.httpResponse.addHeader("Content-Type", "application/pdf")
mockRequest.httpResponse.addHeader("Content-Disposition", "attachment;filename="+file)


View solution in original post

8 REPLIES 8
Highlighted
Staff

Re: Add a pdf as an attachment for a mock response

There's an attachment tab where you can load in your PDF.  This article kind of talks to it - https://www.soapui.org/soap-and-wsdl/headers-and-attachments.html#2-6-Response-Attachments

 

 

Highlighted
Occasional Contributor

Re: Add a pdf as an attachment for a mock response

I do not find that in ready api. Can you be a  bit specific please? @GillerM

Highlighted
Occasional Contributor

Re: Add a pdf as an attachment for a mock response

import com.itextpdf.text.Document
import com.itextpdf.text.Paragraph
import com.itextpdf.text.pdf.PdfWriter

Document document = new Document();
File file =new File("HelloWorld1.pdf")
PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(file));
document.open();
Paragraph paragraph = new Paragraph();
paragraph.add("Hello World!");
document.add(paragraph);
document.close();
mockResponse.contentType=file.toString()
def headers = mockResponse.responseHeaders
headers["Content-Disposition"]=["attachment; filename="+file()]
headers["Content-Type"]=["application/pdf"]

 

when i try to dispose this file in the response, its giving me 0 bytes, where as normally, it creates a file with the content

@TanyaGorbunova

Highlighted
Staff

Re: Add a pdf as an attachment for a mock response

Are you doing this for a SOAP or a REST virt?

 

For a SOAP virt there is a field to add attachments:

SoapVirtAttach.jpg

 

If it's for a REST virt, I'll have to check in with the dev team

 

Highlighted
Occasional Contributor

Re: Add a pdf as an attachment for a mock response

I am looking for a rest virt @GillerM @TanyaGorbunova

Highlighted
Occasional Contributor

Re: Add a pdf as an attachment for a mock response

Got the solution.Here you go:

 

import com.itextpdf.text.Document
import com.itextpdf.text.Paragraph
import com.itextpdf.text.pdf.PdfWriter
import org.apache.commons.io.IOUtils


def headers = mockResponse.responseHeaders

Document document = new Document();

File file = new File("response.pdf")
FileOutputStream fos= new FileOutputStream(file)
PdfWriter pdfWriter = PdfWriter.getInstance(document, fos);
document.open();
Paragraph paragraph = new Paragraph();
paragraph.add("Hello groovy!");
document.add(paragraph);
document.close()
fos.close()

 

try

{

def httpResponse = mockRequest.httpResponse
FileInputStream fis= new FileInputStream(file);
OutputStream outputStream = httpResponse.getOutputStream()

IOUtils.copy(fis,outputStream)

fis.close()
outputStream.close()

}


catch (Exception e){

log.info "error"
}
mockRequest.httpResponse.addHeader("Content-Type", "application/pdf")
mockRequest.httpResponse.addHeader("Content-Disposition", "attachment;filename="+file)


View solution in original post

Highlighted
Contributor

Re: Add a pdf as an attachment for a mock response

Hello,

 

I am using this simple scritp in ServiceV response:

// get the path to the folder where your project resides.
def projectPath = new File(context.mockService.project.path).parent
// Specify the file path that is relative to the project’s path.
def fileName = projectPath + "\\response\\CABIS-MOCK_NIST1.tdf"
log.info "CABIS-MOCK send response with file: " + fileName

import java.io.FileInputStream
import java.io.File

File f = new File(fileName)
FileInputStream fis = new FileInputStream(f)
byte[] b = new byte[f.length()]

//log.info javax.xml.bind.DatatypeConverter.printHexBinary(b) //returns a hex string
//requestContext.mockResponse.responseContent =  javax.xml.bind.DatatypeConverter.printHexBinary(b)
//log.info f.text
requestContext.mockResponse.responseContent = f.text
Highlighted
Contributor

Re: Add a pdf as an attachment for a mock response

Hi Vikititor,

Does the below code gets the .PDF file from the given location, Where should I apply this code in ServiceV pro ?

1. Response has Edit and Script where should i apply ?

2. Clicking on the service, should I apply it on start script ?

This is my url: https://uat.galilee.com.au/Service/generate/one?v=1&&UserName=XXXXXX&Password=XXXXX

Thanks

Senthil kumar. M

New Here?
Join us and watch the welcome video:
Announcements
Script Wanted
Check out this week's challenge!

Create a script to generate an email when an assertion fails for the TechCorner tag!


Scripts created in July:
- A script to change the request body before sending it to the server