Posted about 2 years ago. Visible to the public.

Spreewald: Content-Disposition not set when testing a download's filename


  • You are not using javascript tests
  • The file is served from a public folder (not via controller)

Problem description

If you deliver files from a public folder it might be that the Content-Disposition header is not set. That's why the following spreewald step might raise an error:

Then I should get a download with filename "..."
expected: /filename="some.pdf"$/ got: nil (using =~) (RSpec::Expectations::ExpectationNotMetError)


One solution is to check the Content-Type header only with the following spreewald step:

Then I should get a response with content-type "..."


How to test file downloads with tests that need javascript.

Public served

  • Look at the link of the url and check if this file exists in your public folder (Example for an image)

Server via controller

  • Write an own step which downloads the file and then makes tests with it (you can not check the url because this would be something like localhost:3000/users/4/avatar/3)
  • Override the send_file method of your controller (maybe application controller is also fine): If you are in the test suite return a text with the name of the file. Otherwise super.

Read the browser downloads folder

By refactoring problematic code and creating automated tests, makandra can vastly improve the maintainability of your Rails application.

Owner of this card:

Emanuel De
Last edit:
7 months ago
by Emanuel De
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Emanuel De to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more