SpectoLabs / hoverfly-java

Java binding for Hoverfly
Apache License 2.0
168 stars 58 forks source link

Cannot serve binary files through a custom HttpBodyConverter #305

Closed anandbibek closed 2 months ago

anandbibek commented 2 months ago

Description of the bug

I'm trying to make hoverfly respond with a zip file. But unable to do so in DSL java.

Steps to reproduce the issue

I can serve a binary file ( zip ) via simulation file with following approach

      "response" : {
        "status" : 200,
        "bodyFile" : "myfile.zip",
        "encodedBody" : false,
        "templated" : false,
        "headers" : {......

However, the same is not possible via Java DSL code. My code looks like this:

  private static HttpBodyConverter fileBodyConverter(String fileName) {
    return new HttpBodyConverter() {
      @Override
      public String body() {
        byte[] bytes = null;
        try {
          bytes = Files.readAllBytes(Paths.get("src/test/resources/hoverfly", fileName));
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
        return new String(Base64.getEncoder().encode(bytes));
      }

      @Override
      public String contentType() {
        return "application/octet-stream";
      }
    };
  }

Observed result

The problem is that the returned data is no longer valid and zip file can not be constructed correctly out of it.

Expected result

Hoverfly should return valid zip file data.

Additional relevant information

  1. Hoverfly version: 0.18.1
  2. Anything that might help us to diagnose the problem
tommysitu commented 2 months ago

Hey @anandbibek that's because hoverfly-java DSL doesn't support bodyFile yet. I can look into it.

tommysitu commented 2 months ago

This commit should fix it: https://github.com/SpectoLabs/hoverfly-java/commit/784de025f959fb1cd0954fbdd47ab8ece225219e