简体   繁体   English

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

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

I'm trying to receive pdf document with conent from 'rest request' respons via assertion script. 我正在尝试通过断言脚本从“其余请求”响应中接收具有同意书的pdf文档。 I was trying in few ways, but for each of them the result was diffrent then expected. 我尝试了几种方法,但是对于每种方法,结果都超出了预期。 Could you please review my few options and propose some solutions ? 您能否回顾一下我的一些选择并提出一些解决方案? That's my first steps with 'groovy scripts' and I'm not so familiar with endcode/decode functionalities, so please be understanding if i made some huge mistakes Thanks in advance. 这是我使用“ groovy脚本”的第一步,并且我对结束码/解码功能不太熟悉,所以请理解我是否犯了一些重大错误。

//Option number 1 //选项号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

Result: 结果:

I'm expecting document with correct pdf content. 我希望文档中包含正确的pdf内容。

From Option 1 I'm able to received pdf file with content which is stil encoded liket this: "JVBERi0xLjQNCiXvv73vv73vv73vv70NCjEgMCBvYmoKPDwKL0F1dGhvciAoQW5ua2F0aHJpbi BTdGVwaGFuKQovQ3JlYXRpb25EYXRlIChEOjIwMTkwNDE4MTcwNTI2KzAzJzAwJykKL0NyZWF0 b3IgKFBERi1YQ2hhbmdlIE9mZmljZSBBZGRpbikKL0NyZWF0b3JUb29sIChQREYtWENoYW5nZS..." 从选项1,我能与它还是老样子编码liket此内容收到PDF文件:“JVBERi0xLjQNCiXvv73vv73vv73vv70NCjEgMCBvYmoKPDwKL0F1dGhvciAoQW5ua2F0aHJpbi BTdGVwaGFuKQovQ3JlYXRpb25EYXRlIChEOjIwMTkwNDE4MTcwNTI2KzAzJzAwJykKL0NyZWF0 b3IgKFBERi1YQ2hhbmdlIE9mZmljZSBBZGRpbikKL0NyZWF0b3JUb29sIChQREYtWENoYW5nZS ......”

  • many pages instead of 2 which I expect 很多页面,而不是我期望的2页

//Option 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

I'm expecting document with correct pdf content. 我希望文档中包含正确的pdf内容。 Result: From Option 2 - File is created with 2 blank pages 结果: 从选项2-使用2个空白页创建文件

//Option 3 //选项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));

I'm expecting document with correct pdf content. 我希望文档中包含正确的pdf内容。 Result: From Option 3 - i'm reciving error window with message "name of file (access is denied) 结果: 从选项3-我正在接收错误窗口,并显示消息“文件名(访问被拒绝)

Which option is the best ? 哪个选项最好? and how to improve it ? 以及如何改善呢?


*Thanks for response, at first I need to admitt that i made mistake, and i took wrong type of response it was 'Raw', and i should use 'XML' which has correct response. *感谢响应,起初我需要承认自己犯了错误,并且我对响应的类型进行了错误处理,这是“原始”,我应该使用具有正确响应的“ XML”。 Also I had limitation in 'Max size' property which affected response. 我在“最大尺寸”属性中也有限制,这会影响响应。 Now i set correct size, and I changed content of response. 现在,我设置了正确的大小,并且更改了响应的内容。 Code looks like that: 代码如下所示:

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()

Assertion is passed but stil pdf file has blank pages. 断言已传递,但stil pdf文件具有空白页。 I'm sure that this is Base64 encoded document and I need to decode it. 我确定这是Base64编码的文档,我需要对其进行解码。

Final solution which works for me is(but remeber to have response in JSON which is encode in 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

if response content contains base64 encoded pdf then following should work to write decoded pdf into a file: 如果响应内容包含base64编码的pdf,则应执行以下操作将解码的pdf写入文件:

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

String in groovy has built-in method decodeBase64() that returns decoded content as bytes groovy中的字符串具有内置方法decodeBase64() ,该方法将解码后的内容作为字节返回

Then you just need to write bytes into a file. 然后,您只需要将字节写入文件即可。

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

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