Add a pdf as an attachment for a mock response
- 9 years ago
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.responseHeadersDocument 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)