Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Response body as bytearray #600

Closed
ndr-brt opened this issue Dec 5, 2017 · 8 comments
Closed

Response body as bytearray #600

ndr-brt opened this issue Dec 5, 2017 · 8 comments

Comments

@ndr-brt
Copy link

ndr-brt commented Dec 5, 2017

Hi, we're trying to introduce pact in our CI, but we've come to a standstill...

There's an api that returns binary file, I'm defining the RequestResponsePath in the consumer, but there's no method with sign body(byte[] content).

String is not enough because reading a binary into stream transform it in a text content.

Does a workaround on this exist?
thanks

@uglyog
Copy link
Member

uglyog commented Dec 6, 2017

There is no way to compare a binary file apart from byte array equality. How do you expect the binary content to be verified?

@ndr-brt
Copy link
Author

ndr-brt commented Dec 6, 2017

in that case I wouldn't expect any content verification, I will be ok with the fact that the body is a binary

there's a way to do it?

@uglyog
Copy link
Member

uglyog commented Dec 6, 2017

It will need to include an update to the DSL to add a method to accept a byte array, then the body data will need to be stored as a byte array and converted appropriately when used. We will also have to add a matcher that returns success if the body is not empty.

What is the content type? If it is application/octet-stream, this will work ok. But if there is a custom one, or no content type, then it will be hard to work out how to verify it.

@verkhoro
Copy link

verkhoro commented Feb 5, 2018

Hi, we ran into the same problem. application/octet-stream should work for us. Our use case doesn't require verification of the content because producer is a pass-through although I don't see why comparing through byte array equality shouldn't be an option.

@IraRe
Copy link

IraRe commented Nov 26, 2018

In my case, I would like to test a method returning a byte array. I also don't like methods returning byte arrays as the comparison of a json response would be much better. Now I have to live with it. The possibility to use at least the byte array equality would make my life less painful :)

@dAnjou
Copy link

dAnjou commented Aug 9, 2023

Hi, we'd like to verify a byte stream as well.

There is no way to compare a binary file apart from byte array equality. How do you expect the binary content to be verified?

Maybe by us implementing some custom verification? Besides the String ones, I can see that there are 2 more variants of body(...):

override fun body(dslPart: DslPart): HttpResponseBuilder 
override fun body(builder: BodyBuilder): HttpResponseBuilder

How do these work, how can I use them? I tried to find documentation, but couldn't find any.

@rholshausen
Copy link
Contributor

This is an old issue and the comment is no longer valid. Current versions of Pact-JVM store body content internally as byte arrays, converting it as appropriate. There is a method withBinaryData that can set the body up as a byte array. You can see an example here:

As to the question on the other methods above:

  • body(dslPart: DslPart) - This sets up a JSON body using the JSON DSL classes (which extend DslPart)
  • body(builder: BodyBuilder) - This allows the body to be setup using a builder pattern. There are only a few implementations of this, like for MIME multi-part or FORM POST bodies.

@dAnjou
Copy link

dAnjou commented Sep 1, 2023

Did some digging because I couldn't find that method, and I noticed that we're using PactBuilder while withBinaryData is only available with PactDslWithProvider.

Which one is recommended?

I found the following pieces of code that seem to suggest that PactDslWithProvider is "old" or "legacy":

public RequestResponsePact articles(PactDslWithProvider builder) { // This is using the old DSL

/**
* Use the old HTTP Pact DSL
*/
fun usingLegacyDsl(): PactDslWithProvider {
return PactDslWithProvider(ConsumerPactBuilder(consumer), provider, pactVersion)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants