如何从空手道中的api端点获取下载的xlsx文件? [英] How to get the downloaded xlsx file from the api endpoint in karate?
问题描述
我有一个下载xlsx文件的端点.在测试中,我需要检查文件的内容(不要将文件与另一个文件进行比较,而是要读取内容并进行检查).我正在使用空手道框架进行测试,并且正在尝试使用apache POI来处理Excel工作表.但是,我从空手道获得的响应是在调用下载终结点时为String.为了用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中测试同一终结点时,我得到的是有效的excelsheet.
When testing the same endpoint in Postman, I am getting a valid excelsheet.
推荐答案
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.
P.S.仅仅说它在邮递员中工作"是没有用的:P
P.S. just saying that it "works in Postman" is not helpful :P
这篇关于如何从空手道中的api端点获取下载的xlsx文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!