匹配流 [英] Matching streams

查看:18
本文介绍了匹配流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个功能,我们称之为 F1,它返回一个和两个流相同的文件:

I have a feature, let's call it F1, which returns one and the same file as two streams:

* def aPdf1 = read('classpath:pdf.pdf')
* def aPdf2 = read('classpath:pdf.pdf')
* def out = { one: aPdf1, two: aPdf2 }

当我从另一个功能调用 F1 时,比如说 F2,并比较流,它们不匹配:

When I call F1 from another feature, let's say F2, and compare the streams, they do not match:

* def out = call read('F1.feature')
* match out.aPdf1 == out.aPdf2

错误是:

com.intuit.karate.exception.KarateException:意外类型:类 java.io.BufferedInputStream

com.intuit.karate.exception.KarateException: unexpected type: class java.io.BufferedInputStream

这是一个错误吗?还是尚未实现的功能?

Is this a bug? Or is it a not yet implemented feature?

PS1:如果我将以下行添加到 F1,它会自行成功完成:

* 匹配 aPdf1 == aPdf2

PS2:利用这个问题的答案中的代码,我能够匹配 F2 中的流.

PS2: Utilizing the code from the answer to this question, I was able to match the streams in F2.

推荐答案

问题是您创建了一个无效的 JSON,该 JSON 恰好具有二进制流作为值.只需坚持将流与流进行比较 - 这将像您已经看到的那样工作.如果您需要将 PDF 转换为字符串,您可以这样做:

The problem is you have created an invalid JSON that happens to have binary streams as the values. Just stick to comparing streams with streams - which will work as you have seen already. If you need to convert the PDF to a string you can do this:

* string aPdf2 = read('classpath:pdf.pdf')

您也可能忽略了嵌入式表达式和封闭式 javascript"之间的区别.你是故意这样做的吗?

Also you may have missed the difference between embedded-expressions and "enclosed javascript". Did you mean to do this ?

* def out = ({ one: aPdf1, two: aPdf2 })

或:

* def out = { one: '#(aPdf1)', two: '#(aPdf2)' }

还有关于 JSON 和二进制值的更多上下文 - 请参阅此答案:https://stackoverflow.com/a/52541026/143475

Also for more context on JSON and binary values - refer this answer: https://stackoverflow.com/a/52541026/143475

所以如果你想比较两个流,你必须先将它们转换为字节数组.试试这个,你可以子你自己的流到字节转换器的实现:

so if you want to compare two streams you have to convert them to byte-arrays first. Try this, and you can sub your own implementation of a stream-to-byte converter:

* def Utils = Java.type('com.intuit.karate.FileUtils')
* def stream1 = read('karate-logo.png')
* def bytes1 = Utils.toBytes(stream1)
* def stream2 = read('karate-logo.png')
* def bytes2 = Utils.toBytes(stream2)
* assert java.util.Arrays.equals(bytes1, bytes2)

编辑 - 在较新版本的空手道中,您可以投射"到 bytesmatch 关键字也支持字节数据

EDIt - in newer versions of Karate you can "cast" to bytes, and the match keyword supports data which is bytes as well

这篇关于匹配流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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