我正在尝试通过断言脚本从“休息请求”响应中接收包含内容的 pdf 文档。 我尝试了几种方法,但是对于每种方法,结果都与预期的不同。您能否回顾一下我的几个选项并提出一些解决方案?这是我使用“groovy 脚本”的第一步,我对 endcode/decode 功能不太熟悉,所以请理解我是否犯了一些巨大的错误提前谢谢。
//选项编号1
import org.apache.commons.codec.binary.Base64;
import groovy.json.JsonSlurper
import com.itextpdf.text.*
import com.itextpdf.text.pdf.PdfWriter;
def content = messageExchange.response.responseContent
def cont = messageExchange.response.responseContent
def fileName = messageExchange.modelItem.testStep.testCase.getPropertyValue("DOC") + '_test.pdf'
Base64 coder = new Base64();
//assert null != response, "response is null"
def encodedString = cont.getBytes("UTF-8").encodeBase64().toString()
def decoded = encodedString.decodeBase64();
def res = new File( "F:\\Test\\Testing\\Files\\assertion\\$fileName")
res.write(content, "UTF-8")
res.delete();
res << encodedString
log.info res
结果:
我期待具有正确 pdf 内容的文档。
From Option 1 I'm able to received pdf file with content which is stil encoded liket this: "JVBERi0xLjQNCiXvv73vv73vv73vv70NCjEgMCBvYmoKPDwKL0F1dGhvciAoQW5ua2F0aHJpbi BTdGVwaGFuKQovQ3JlYXRpb25EYXRlIChEOjIwMTkwNDE4MTcwNTI2KzAzJzAwJykKL0NyZWF0 b3IgKFBERi1YQ2hhbmdlIE9mZmljZSBBZGRpbikKL0NyZWF0b3JUb29sIChQREYtWENoYW5nZS..."
- 很多页而不是我期望的 2 页
//选项2
import org.apache.commons.codec.binary.Base64;
import groovy.json.JsonSlurper
import com.itextpdf.text.*
import com.itextpdf.text.pdf.PdfWriter;
def content = messageExchange.response.responseContent
def cont = messageExchange.response.responseContent
def fileName = messageExchange.modelItem.testStep.testCase.getPropertyValue("DOC") + '_test.pdf'
Base64 coder = new Base64();
//assert null != response, "response is null"
def encodedString = cont.getBytes("UTF-8").encodeBase64().toString()
def decoded = encodedString.decodeBase64();
def res = new File( "F:\\Test\\Testing\\Files\\assertion\\$fileName")
res.write(content, "UTF-8")
//res.delete(); -> without this line
res << encodedString
log.info res
我期待具有正确 pdf 内容的文档。 结果: 从选项 2 - 创建的文件有 2 个空白页
//选项3
import org.apache.commons.codec.binary.Base64;
import groovy.json.JsonSlurper
import com.itextpdf.text.*
import com.itextpdf.text.pdf.PdfWriter;
def fileName = messageExchange.modelItem.testStep.testCase.getPropertyValue("DOC_PID") + '_test.pdf'
def cont = messageExchange.response.responseContent
String content = cont
def encoded = content.getBytes("UTF-8").encodeBase64().toString()
byte[] decoded = encoded.decodeBase64()
def document = new Document()
PdfWriter.getInstance(document,new FileOutputStream(fileName));
我期待具有正确 pdf 内容的文档。 结果: 从选项 3 - 我收到带有消息“文件名(访问被拒绝)的错误窗口”
哪个选项是最好的?以及如何改进它?
*感谢您的回复,首先我需要承认我犯了错误,并且我采取了错误的响应类型,它是“原始”,我应该使用具有正确响应的“XML”。此外,我对影响响应的“最大尺寸”属性也有限制。现在我设置了正确的大小,并更改了响应的内容。代码如下所示:
import com.eviware.soapui.support.XmlHolder
def cont = new XmlHolder(messageExchange.responseContentAsXml)
content = cont["//*:data"]
def fileName = messageExchange.modelItem.testStep.testCase.getPropertyValue("DOC") + '_test.pdf'
new File( "F:\\Test\\Testing\\Files\\assertion\\$fileName").bytes = content.decodeBase64()
断言已通过,但 pdf 文件仍有空白页。我确定这是 Base64 编码的文档,我需要对其进行解码。
对我有用的最终解决方案是(但请记住在 JSON 中有响应,它在 Base64 中编码):
import org.apache.commons.codec.binary.Base64;
import groovy.json.JsonSlurper
//grab the response
def content = messageExchange.response.responseContent
def jsonSlurper = new JsonSlurper().parseText(content)
assert !(jsonSlurper.isEmpty())
document_content = jsonSlurper.fileContent
def fileName = messageExchange.modelItem.testStep.testCase.getPropertyValue("DOC") + '_.pdf'
new File( ""F:\\Test\\Testing\\Files\\assertion\\$fileName"").bytes = document_content.decodeBase64()
log.info fileName