Sha256: 394f997fd1bb9b0f75762e771e18945b410a018db9bb14d5b85355f9b10f9522

Contents?: true

Size: 1.62 KB

Versions: 5

Compression:

Stored size: 1.62 KB

Contents

require File.join(File.dirname(__FILE__), '..', 'abstract_unit')


module TestFileUtils
  def file_name() File.basename(__FILE__) end
  def file_path() File.expand_path(__FILE__) end
  def file_data() File.open(file_path, 'rb') { |f| f.read } end
end


class SendFileController < ActionController::Base
  include TestFileUtils

  attr_writer :options
  def options() @options ||= {} end

  def file() send_file(file_path, options) end
  def data() send_data(file_data, options) end

  def rescue_action(e) raise end
end


class SendFileTest < Test::Unit::TestCase
  include TestFileUtils

  def setup
    @controller = SendFileController.new
    @request = ActionController::TestRequest.new
    @response = ActionController::TestResponse.new
  end

  def test_file_nostream
    @controller.options = { :stream => false }
    response = nil
    assert_nothing_raised { response = process('file') }
    assert_not_nil response
    assert_kind_of String, response.body
    assert_equal file_data, response.body
  end

  def test_file_stream
    response = nil
    assert_nothing_raised { response = process('file') }
    assert_not_nil response
    assert_kind_of Proc, response.body

    old_stdout = $stdout
    begin
      require 'stringio'
      $stdout = StringIO.new
      $stdout.binmode
      assert_nothing_raised { response.body.call }
      assert_equal file_data, $stdout.string
    ensure
      $stdout = old_stdout
    end
  end

  def test_data
    response = nil
    assert_nothing_raised { response = process('data') }
    assert_not_nil response

    assert_kind_of String, response.body
    assert_equal file_data, response.body
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
actionpack-0.9.5 test/controller/send_file_test.rb
actionpack-1.1.0 test/controller/send_file_test.rb
actionpack-1.0.0 test/controller/send_file_test.rb
actionpack-1.0.1 test/controller/send_file_test.rb
actionpack-1.2.0 test/controller/send_file_test.rb