如何从空手道的api端点获取下载的xlsx文件? [英] How to get the downloaded xlsx file from the api endpoint in karate?

查看:36
本文介绍了如何从空手道的api端点获取下载的xlsx文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下载 xlsx 文件的端点.在我的测试中,我需要检查文件的内容(不是将文件与另一个文件进行比较,而是读取内容并检查).我正在使用空手道框架进行测试,并且我正在尝试使用 apache POI 来处理 Excel 表.但是,我在调用下载端点时从空手道得到的响应是一个字符串.要使用 POI 创建 excel 文件,我需要一个 InputStream 或实际文件的路径.我已经尝试过转换,但它不起作用.

I have an endpoint that downloads an xlsx file. In my test, I need to check the content of the file (not comparing the file with another file, but reading the content and checking). I am using karate framework for testing and I am trying to use apache POI for working with the excel sheet. However, the response I get from karate when calling the download endpoint is a String. For creating an excel file with POI I need an InputStream or the path to the actual file. I have tried the conversion, but it does not work.

我想我在这里遗漏了一些联系,或者转换不好,我是空手道和整个事情的新手.我感谢任何帮助,谢谢!

I guess I am missing some connection here, or maybe the conversion is bad, I am new to karate and to the whole thing. I appreciate any help, thanks!

 Given url baseUrl
    Given path downloadURI
     When method GET
     Then status 200
    And match header Content-disposition contains 'attachment'
    And match header Content-disposition contains 'example.xlsx'
    And match header Content-Type == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'

        * def value= FileChecker.createExcelFile(response)
        * print value

Java 代码:

public static String createExcelFile(String excel) throws IOException, InvalidFormatException {
        InputStream stream = IOUtils.toInputStream(excel, Charset.forName("UTF-8"));
        Workbook workbook = WorkbookFactory.create(stream);
        return ("Workbook has " + workbook.getNumberOfSheets() + " Sheets : ");
    }

运行场景时,出现以下错误:javascript 评估失败:FileChecker.createExcelFile(response),java.io.IOException:无法读取 zip 条目源

When running the scenario, I get the following error: javascript evaluation failed: FileChecker.createExcelFile(response), java.io.IOException: Failed to read zip entry source

在 Postman 中测试同一个端点时,我得到了一个有效的 Excel 表格.

When testing the same endpoint in Postman, I am getting a valid excelsheet.

推荐答案

在 Karate 0.9.X 之后,你有一个 responseBytes 变量将是原始字节,这可能是您需要的.

In Karate 0.9.X onwards you have a responseBytes variable which will be raw bytes, which may be what you need.

* def value = FileChecker.createExcelFile(responseBytes)

您可以将方法签名更改为:

And you can change your method signature to be:

public static String createExcelFile(byte[] excel) {}

您应该能够轻松地将字节数组转换为 InputStream 并从那里获取它.

You should be easily able to convert a byte array to an InputStream and take it from there.

附言只是说它适用于 Postman"没有帮助:P

P.S. just saying that it "works in Postman" is not helpful :P

这篇关于如何从空手道的api端点获取下载的xlsx文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆