Topic: Checking the file content from send_file

I am test-driving a controller that sends back something other than a page template--in this case, PDF. I'd like to check the content coming back. This is what I've done; does anyone have any suggestions for how I could improve it? Has someone written something in a library I could have used? Is there a way I can do this that is less tightly coupled to the current implementation of send_file?

def byte_content_of(response)
  output_collector = Struct.new(:content).new
  def output_collector.syswrite(bytes)
    self.content = "" if self.content.nil?
    self.content << bytes
  end

  response.body.call(nil, output_collector)
   
  output_collector.content
end


I use it this way:

def test_whatever
  post :whatever, ...
  assert_equal(File.open("goldenmaster.pdf", "rb").read, byte_content_of(@response)
end

Thanks for your help.

Last edited by jbrains (2006-11-23 19:15:10)