使用 wslite 从剩余响应中获取 excel 文件
get excel file from rest response using wslite
我正在尝试测试通过 REST 响应发送 excel 文件 (.xlsx) 的功能。我已经使用邮递员尝试了端点并且我知道 excel 已下载(因此它有效)但我仍然无法进行测试。
这是我到目前为止尝试过的(使用 wslite 读取测试中的响应):
@Subject(ExcelController)
class ExcelFuncSpec {
RESTClient http
void setup() {
http = new RESTClient("http://localhost:33089")
}
void "It generates an excel"() {
given:
int randomId = 23
when:
def res = http.get([
path : "/$randomId/excel",
])
then: "The response is OK"
res?.statusCode == OK.value()
res.response.data //this kinda works because it returns an array of bytes
}
}
正如您在我的评论中看到的那样:
res.response.data //this kinda works because it returns an array of bytes
我可以获得一个字节数组,但我需要的是一个 java 文件或 InputStream,我可以使用它来读取 excel 文件的内容。
¿如何使用 wslite 将响应作为 InputStream 获取?
I can get an array of bytes but what I need is a java File or
InputStream that I can use to read the content of the excel file.
如果 res.response.data
评估字节数组,并且您想要一个与这些字节关联的 InputStream
,那么 new ByteArrayInputStream(res.response.data)
将创建一个 InputStream
,您可以从中读取这些字节字节。
我正在尝试测试通过 REST 响应发送 excel 文件 (.xlsx) 的功能。我已经使用邮递员尝试了端点并且我知道 excel 已下载(因此它有效)但我仍然无法进行测试。
这是我到目前为止尝试过的(使用 wslite 读取测试中的响应):
@Subject(ExcelController)
class ExcelFuncSpec {
RESTClient http
void setup() {
http = new RESTClient("http://localhost:33089")
}
void "It generates an excel"() {
given:
int randomId = 23
when:
def res = http.get([
path : "/$randomId/excel",
])
then: "The response is OK"
res?.statusCode == OK.value()
res.response.data //this kinda works because it returns an array of bytes
}
}
正如您在我的评论中看到的那样:
res.response.data //this kinda works because it returns an array of bytes
我可以获得一个字节数组,但我需要的是一个 java 文件或 InputStream,我可以使用它来读取 excel 文件的内容。
¿如何使用 wslite 将响应作为 InputStream 获取?
I can get an array of bytes but what I need is a java File or InputStream that I can use to read the content of the excel file.
如果 res.response.data
评估字节数组,并且您想要一个与这些字节关联的 InputStream
,那么 new ByteArrayInputStream(res.response.data)
将创建一个 InputStream
,您可以从中读取这些字节字节。