Search code examples
ruby-on-railsrubyruby-on-rails-3unit-testingfunctional-testing

RoR - Testing POST of file chunk


My rails app contains code to handle large file uploads, which basically consists of splitting up the file in javascript and making a number of posts for each chunk to a route where they are then reconstructed back to the original file.

I'm trying to figure out how to write tests for this logic, as up until now I've simply used fixture_file_upload for posting files.

I basically need to split a given file up into a range of bytes, and post that in a way that my route would handle it just as though it has been posted by my javascript.

Anyone know of a way to accomplish this in a rails test?


Solution

  • You could just create multiple fixture files (e.g. file.part1.txt, file.part2.txt, etc.) , upload all the parts and then check that they get concatenated together.

    For example, if there are 10 fixture files:

    (1..10).each do |part_no|
      fixture_name = "file.part#{part_no}.txt"
      fixture_file = fixture_file_upload("/files/#{fixture_name}", "text/plain")
      post :part_upload, :part => fixture_file
    end
    # code to check result here