如何从soapUI响应中提取字节数组?

发布于 2024-07-12 09:28:05 字数 749 浏览 6 评论 0原文

我正在使用soapUI 测试网络服务。 其中一个响应如下所示:

    <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:getInputConfigFilesResponse xmlns:ns2="http://ws.pdb.ericsson.se/">
         <return>UEsDBBQACAAIAO1GNToAAAAAAAAAAAAAAAANAAAAc2NyaXB0cy9lbGxza [...] AATAAAAAAAAAAAAAAAAAAGXAAAbGRhcF9ub2RlX2NvbmZpZ3VyYXRpb24vZ2VuZXJhdGVkX2xkaWZfZmlsZXMvX210YXM0X2N4cDQ0NF9yNF9JbXNtb2JpbGUubGRpZlBLAQIUABQACAAIAO1GNTp8eBuZRAEAABMDAAAmAAAAAAAAAAAAAAAAAKJiAABsZGFwX25vZGVfY29uZmlndXJhdGlvbi9lbGxzaC1jb21tYW5kc1BLBQYAAAAABQAFAIgBAAA6ZAAAAAA=</return>
      </ns2:getInputConfigFilesResponse>
   </S:Body>
</S:Envelope>

响应是一个字节数组,应表示一个 zip 文件。 我该如何去验证这一点?

I am testing a web service with soapUI. One of the responses looks like this:

    <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:getInputConfigFilesResponse xmlns:ns2="http://ws.pdb.ericsson.se/">
         <return>UEsDBBQACAAIAO1GNToAAAAAAAAAAAAAAAANAAAAc2NyaXB0cy9lbGxza [...] AATAAAAAAAAAAAAAAAAAAGXAAAbGRhcF9ub2RlX2NvbmZpZ3VyYXRpb24vZ2VuZXJhdGVkX2xkaWZfZmlsZXMvX210YXM0X2N4cDQ0NF9yNF9JbXNtb2JpbGUubGRpZlBLAQIUABQACAAIAO1GNTp8eBuZRAEAABMDAAAmAAAAAAAAAAAAAAAAAKJiAABsZGFwX25vZGVfY29uZmlndXJhdGlvbi9lbGxzaC1jb21tYW5kc1BLBQYAAAAABQAFAIgBAAA6ZAAAAAA=</return>
      </ns2:getInputConfigFilesResponse>
   </S:Body>
</S:Envelope>

The response is a byte array which should represent a zip file. How can I go about verifying this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

椵侞 2024-07-19 09:28:06

尽管在语义上是正确的,但我认为内森提出的解决方案有些不完整。

我猜他忘记在步骤 1 之前对提取的文本进行 Base64 解码。我还将进行一项改进,以便通过使用第 3 方 ZIP 实用程序验证临时文件而无需人工干预。

这将转化为访问临时 ZIP 文件并检查它是否是有效的 ZIP 文件。 因此,实现您的要求的算法是:

  1. 访问元素 Base64 编码的文本
    内容并进行 Base64 解码 将
  2. ZIP 原始文本输出到临时文件中,从而创建一个 ZIP 文件
  3. 检查临时创建的 ZIP 文件是否有效

考虑到所有这些,完整的 Groovy 脚本如下所示:

import org.apache.commons.codec.binary.Base64

// Step 1: Access element Base64-encoded text content and Base64 decode it
String tempZipFilename = "temp.zip"
def textBase64 = context.expand(
  '${Step#Request#//ns2:getInputConfigFilesResponse[1]/return[1]}' )
def b64 = new Base64()
def zipTextBytes = b64.decode(textBase64.getBytes())

// Step 2: Output ZIP raw text into a temporary file
def zipFile = new java.io.File(tempZipFilename)
FileOutputStream fos = new java.io.FileOutputStream(zipFile)
fos.write( zipTextBytes )
fos.flush()
fos.close()
log.info "Temporary ZIP file stored as: ${zipFile.getCanonicalPath()}"

// Step 3: Check if the temporarily created ZIP file is valid
boolean responseValid
try {
  zipFile = new java.util.zip.ZipFile(tempZipFilename)
  responseValid = true
  log.info "Number of files in the ZIP file: ${zipFile.size()}"
} catch (java.util.zip.ZipException e) {
  responseValid = false
  log.error "The received response contains a bad ZIP"
}
log.info "Web service response is " + (responseValid ? "valid" : "invalid")

请告诉我是否这样对你有用,对我也有用。 :-)

干杯!
尚齐

拉 我建议在这个问题中添加“ZIP”标签,以便人们可以更轻松地找到处理此处嵌入的 Groovy 中的 ZIP 的解决方案。

Although semantically correct, I believe Nathan's proposed solution is somewhat incomplete.

I guess he forgot do Base64 decode the extracted text prior to step 1. I would also include an improvement so that no human intervention is needed by verifying the temporary file using 3rd party ZIP utilities.

That would translate into accessing the temporary ZIP file and checking whether it's a valid ZIP file. Therefore, an algorithm for implementing your requirement would be:

  1. Access element Base64-encoded text
    content and Base64 decode it
  2. Output ZIP raw text into a temporary file, thus creating a ZIP file
  3. Check if the temporarily created ZIP file is valid

With all that in mind, here's how the complete Groovy script would look like:

import org.apache.commons.codec.binary.Base64

// Step 1: Access element Base64-encoded text content and Base64 decode it
String tempZipFilename = "temp.zip"
def textBase64 = context.expand(
  '${Step#Request#//ns2:getInputConfigFilesResponse[1]/return[1]}' )
def b64 = new Base64()
def zipTextBytes = b64.decode(textBase64.getBytes())

// Step 2: Output ZIP raw text into a temporary file
def zipFile = new java.io.File(tempZipFilename)
FileOutputStream fos = new java.io.FileOutputStream(zipFile)
fos.write( zipTextBytes )
fos.flush()
fos.close()
log.info "Temporary ZIP file stored as: ${zipFile.getCanonicalPath()}"

// Step 3: Check if the temporarily created ZIP file is valid
boolean responseValid
try {
  zipFile = new java.util.zip.ZipFile(tempZipFilename)
  responseValid = true
  log.info "Number of files in the ZIP file: ${zipFile.size()}"
} catch (java.util.zip.ZipException e) {
  responseValid = false
  log.error "The received response contains a bad ZIP"
}
log.info "Web service response is " + (responseValid ? "valid" : "invalid")

Please let me know if this works for you as it does for me. :-)

Cheers!
Shonzilla

p.s. I would suggest adding "ZIP" tag to this question so that people can more easily find a solution for handling ZIPs from Groovy that's embedded here.

琉璃繁缕 2024-07-19 09:28:06

你将不得不使用一些groovy。

  1. 将字节数组输出到文件。
  2. 执行一些命令行(7zip、zip 等),并输出可解析的输出。
  3. 确保它是正确的输出。

编辑:添加了一些示例代码。

def saveFile = new java.io.File(saveTestDir + "\\testreturn.zip")

FileOutputStream fos = new java.io.FileOutputStream(saveFile);

def zipBytes = context.expand( '${Step#Request#//ns2:getInputConfigFilesResponse[1]/return[1]}' );

fos.write( zipBytes.getBytes() );< /code>

fos.flush();

fos.close();

You're going to have to use some groovy.

  1. output the byte array to a file.
  2. execute some command line (7zip, zip whatever) with an output you can parse.
  3. make sure it's the correct output.

Edit: added some sample code.

def saveFile = new java.io.File(saveTestDir + "\\testreturn.zip")

FileOutputStream fos = new java.io.FileOutputStream(saveFile);

def zipBytes = context.expand( '${Step#Request#//ns2:getInputConfigFilesResponse[1]/return[1]}' );

fos.write( zipBytes.getBytes() );

fos.flush();

fos.close();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文