Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

match - Matching streams

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 }

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

and the error is:

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

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

PS1: If I add the following line to F1, it completes successfully by itself:

* match aPdf1 == aPdf2

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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')

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 })

or:

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

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

EDIT: 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)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...