SpectoLabs / hoverfly

Lightweight service virtualization/ API simulation / API mocking tool for developers and testers
https://hoverfly.io
Apache License 2.0
2.36k stars 208 forks source link

Binary data in request not working #467

Open Khazrak opened 7 years ago

Khazrak commented 7 years ago

I'm trying to record a http post to Docker using Remote API where I send a tar file (via a stream) in Java via OkHttp Client. Hoverfly records the binary data and shows green in captureMode but fails in simulation mode. My guess is that the JSON doesn't like the binary data in the request. (I got Wiremock to do this and I would like to avoid having Wiremock for one measly test).

image_tarball_import.json.txt

https://docs.docker.com/engine/api/v1.26/#operation/ImageLoad "Tar archive containing images"

benjih commented 7 years ago

Hey @Khazrak,

There is some checks around binary data in the body, but this is only on response. These should be moved to request body too.

Was this simulation working before being exported? My assumption is that binary data worked as a body and the only issues occur when trying to encode the bianry data into a JSON document.

Khazrak commented 7 years ago

I used the HoverflyRule.inCaptureOrSimulationMode and called, in this case my local, docker via HTTP and got a correct response that was recorded. It then fails when it uses simulation mode. But as you say, since I send binary data and it gets recorded and is used to match, this fails since it's binary in JSON. So it would be awesome if the encodining of body can be on both request and response (instead of just response).

mogronalol commented 7 years ago

@Khazrak You are correct and we will aim to fix this after our v0.11 release.

If you want to workaround this temporarily, one way is to open your json file and change the matcher from type recording to type template. Once you've done this you can remove the body field from the json, meaning it won't use the body for matching.

benjih commented 7 years ago

Having looked at this issue, I think the best way to go about implementing this would be implement a binaryMatch matcher for requests. The binaryMatch would work the same as the Response.Body field and the Response.BodyEncoded field. It would take a base64 encoded string of the binary data.

This seems cleaner than trying to overload exactMatch.

JohnFDavenport commented 7 years ago

Wouldn't it be easier to ignore the binary data in the request (which isn't of interest anyway and could be treated as noise) and match on using something in the JSON request?

tommysitu commented 5 years ago

I think capturing and simulating the binary data in the request body is a bit overkill. One workaround is to remove the request body so that it's not used for matching.

We could provide a binary matcher but it'll be low priority in our roadmap.