I have code for streaming a CSV similar to the following in a controller:
def download
filename = 'data.csv'
headers.merge!(
'Content-Type' => 'text/csv',
'Content-Disposition' => "attachment; filename=\"#{filename}\"",
'Content-Transfer-Encoding' => 'binary')
render :status => 200, :text => Proc.new { |response, output|
headings = ["ID", "Name"]
output.write CSV.generate_line(headings)
accounts = Account.find(:all)
accounts.each { |account|
data = [account.id, account.name]
output.write CSV.generate_line(data)
}
}
end
This works fine to stream the CSV to the browser and the following unit test passes:
def test_csv_data
get :download
assert_response :success
assert_content_type 'text/csv'
assert_equal("attachment; filename=data.csv", @response.headers["Content-Disposition"])
rows = @response.binary_content.split("\n")
assert_equal(["id", "name",], rows[0].split(','))
assert_equal(["1", "bob",], rows[1].split(','))
end
Now I am trying to test this using cucumber and webrat in a step definition like this:
Then /^I should receive a CSV file containing the following data$/ do |table|
rows = @response.binary_content.split("\n")
table.diff! rows
end
But all I get is the header row not the data. I think it is something to do with webrats handling of streamed data. Is there a way to get the full stream from webrat?