fbpx

Testing a Multipart Form Upload in Rails/Rspec

I was working on a Rails API the other day and had to setup multipart form uploads.  A lot of the time we will Base64 encode a file and send it along with the JSON so this was a bit new to me.  The only snag I hit was in setting up the post in our request spec.  So here is an example of how to create a post request in RSpec with a file upload under the key of video along with headers.

 post users_video_path(user.id),
      params: { video: Rack::Test::UploadedFile.new("#{Rails.root}/spec/support/attachments/test_video.m4v", 'video/m4v') },
      headers:  { 'Authorization' => 'Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXX' }