繁体   English   中英

如何正确地在断言中编写Groovy脚本以解码响应并将其接收为pdf?

[英]How to correctly write groovy script in assertion to decode response and recive it in pdf?

我正在尝试通过断言脚本从“其余请求”响应中接收具有同意书的pdf文档。 我尝试了几种方法,但是对于每种方法,结果都超出了预期。 您能否回顾一下我的一些选择并提出一些解决方案? 这是我使用“ groovy脚本”的第一步,并且我对结束码/解码功能不太熟悉,所以请理解我是否犯了一些重大错误。

//选项号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内容。

从选项1,我能与它还是老样子编码liket此内容收到PDF文件:“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()

断言已传递,但stil pdf文件具有空白页。 我确定这是Base64编码的文档,我需要对其进行解码。

对我有用的最终解决方案是(但请记住在以Base64编码的JSON中有响应):

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

如果响应内容包含base64编码的pdf,则应执行以下操作将解码的pdf写入文件:

def content = messageExchange.response.responseContent
new File( "F:\\Test\\Testing\\Files\\assertion\\$fileName").bytes = content.decodeBase64()

groovy中的字符串具有内置方法decodeBase64() ,该方法将解码后的内容作为字节返回

然后,您只需要将字节写入文件即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM