elixir-maru / maru

Elixir RESTful Framework
https://maru.readme.io
BSD 3-Clause "New" or "Revised" License
1.32k stars 84 forks source link

Proper way to test a File parameter? #60

Closed fdorothy closed 6 years ago

fdorothy commented 7 years ago

What's the proper way to unit test a route that has a File parameter? For example, a route that would update a user's avatar on s3:

  params do
    requires :avatar, type: File
  end
  post "/info/avatar" do
    # update user's avatar on S3...
  end

In order to test this manually I have done multipart requests with HTTPoison:

name = "avatar"
filename = "avatar.png"
path = "assets/avatar.png"
body = {:multipart, [{:file, path, {["form-data"], [name: Poison.encode!(name), filename: Poison.encode!(filename)]},[]}]}
HTTPoison.post!("http://localhost:8080/info/avatar", body)

Is there some easy way to do this in a Maru test?

jalcine commented 6 years ago

You might have to make this into a helper method and wrap it into your test suite.

OOTH, providing that as a snippet + PR to Maru.Test could help as well!

jalcine commented 6 years ago

There's also this answer from StackOverflow - link

fdorothy commented 6 years ago

that works