wiremock / wiremock

A tool for mocking HTTP services
https://wiremock.org/
Apache License 2.0
6.37k stars 1.44k forks source link

Support sending binary file in response #1681

Open ayevee opened 3 years ago

ayevee commented 3 years ago

I need to mock an service which has one of the endpoint serving zip files.

Unfortunately bodyFileName cannot be used for this purpose as it expects file to be a UTF8 string, not binary data, the downloaded archive is corrupt.

Using base64Body is not a convenient option in my case as well as:

  1. Some files are quite big (over 1MB)
  2. Files are updated from time to time, therefore base64 values will have to be manually kept in sync

Could you please add an option to send binary file from disk in stub responses (e.g. something like binaryBodyFileName).

I've noticed a couple of issues while trying to make it work:

  1. Content-Length is not set in response and I didn't find a way to manully populate it as response header based on actual size of file on disk
  2. On some files I've encountered handlebars exceptions, probably when "{{" was found within the binary file.
vbartacek commented 2 years ago

I was able to use Wiremock for serving static binary files, example of the mapping JSON for serving the file __files/download/document.pdf:

{
  "id" : "20221014-1032-aaaa-aaaa-aaaaaaaaaaaa",
  "uuid" : "20221014-1032-aaaa-aaaa-aaaaaaaaaaaa",
  "request" : {
    "method" : "GET",
    "url" : "/download/pdf"
  },
  "response" : {
    "status" : 200,
    "bodyFileName" : "download/document.pdf",
    "headers" : {
      "Content-Type" : "application/pdf"
    }
  }
}

You can add Content-Length response header for static files with known size as well.

The handlebars "{{" exception can be avoided by disabling global templating. You have to enable templating only for particualr mocks/mappings.

oleg-nenashev commented 1 year ago

Seems to be a use-case for https://github.com/wiremock/wiremock/issues/2145 (or not, this seems to demand extra logic to define whole response in the pattern)