提交 3dea8b58 编写于 作者: J Jeremy Kemper

Dispatcher tests. References #9630.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7591 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 ea456801
......@@ -15,3 +15,13 @@
class Test::Unit::TestCase
# Add stuff here if you need it
end
# Wrap tests that use Mocha and skip if unavailable.
def uses_mocha(test_name)
require 'rubygems'
gem 'mocha', '>= 0.5.5'
require 'mocha'
yield
rescue LoadError
$stderr.puts "Skipping #{test_name} tests. `gem install mocha` and try again."
end
require "#{File.dirname(__FILE__)}/abstract_unit"
uses_mocha 'dispatcher tests' do
$:.unshift File.dirname(__FILE__) + "/../../actionmailer/lib"
require 'stringio'
......@@ -8,15 +11,6 @@
require 'action_controller'
require 'action_mailer'
ACTION_MAILER_DEF = <<AM
class DispatcherTestMailer < ActionMailer::Base
end
AM
ACTION_CONTROLLER_DEF = <<AM
class DispatcherControllerTest < ActionController::Base
end
AM
class DispatcherTest < Test::Unit::TestCase
def setup
......@@ -26,75 +20,46 @@ def setup
Dispatcher.send(:preparation_callbacks).clear
Dispatcher.send(:preparation_callbacks_run=, false)
Object.const_set :ApplicationController, nil
class << ActionController::Routing::Routes
alias_method :old_reload, :reload
def reload() end
end
Object.const_set 'ApplicationController', nil
end
def teardown
Object.send :remove_const, :ApplicationController
ENV['REQUEST_METHOD'] = nil
class << ActionController::Routing::Routes
alias_method :reload, :old_reload
end
Object.send :remove_const, 'ApplicationController'
end
def test_ac_subclasses_cleared_on_reset
Object.class_eval(ACTION_CONTROLLER_DEF)
assert_subclasses 1, ActionController::Base
dispatch
def test_clears_dependencies_after_dispatch_if_in_loading_mode
Dependencies.stubs(:load?).returns(true)
GC.start # force the subclass to be collected
assert_subclasses 0, ActionController::Base
end
ActionController::Routing::Routes.expects(:reload).once
Dependencies.expects(:clear).once
def test_am_subclasses_cleared_on_reset
Object.class_eval(ACTION_MAILER_DEF)
assert_subclasses 1, ActionMailer::Base
dispatch
end
def test_clears_dependencies_after_dispatch_if_not_in_loading_mode
Dependencies.stubs(:load?).returns(false)
ActionController::Routing::Routes.expects(:reload).never
Dependencies.expects(:clear).never
GC.start # force the subclass to be collected
assert_subclasses 0, ActionMailer::Base
dispatch
end
def test_failsafe_response
CGI.expects(:new).raises('some multipart parsing failure')
INVALID_MULTIPART = [
'POST /foo HTTP/1.0',
'Host: example.com',
'Content-Type: multipart/form-data;boundary=foo'
]
EMPTY_CONTENT = (INVALID_MULTIPART + [
'Content-Length: 100',
nil, nil
]).join("\r\n")
CONTENT_LENGTH_MISMATCH = (INVALID_MULTIPART + [
'Content-Length: 100',
nil, nil,
'foobar'
]).join("\r\n")
NONINTEGER_CONTENT_LENGTH = (INVALID_MULTIPART + [
'Content-Length: abc',
nil, nil
]).join("\r\n")
def test_bad_multipart_request
old_stdin = $stdin
[EMPTY_CONTENT, CONTENT_LENGTH_MISMATCH, NONINTEGER_CONTENT_LENGTH].each do |bad_request|
$stdin = StringIO.new(bad_request)
output = StringIO.new
assert_nothing_raised { dispatch output }
assert_equal "Status: 400 Bad Request\r\n", output.string
end
ensure
$stdin = old_stdin
ActionController::Routing::Routes.stubs(:reload)
Dispatcher.stubs(:log_failsafe_exception)
assert_nothing_raised { dispatch }
assert_equal "Status: 400 Bad Request\r\nContent-Type: text/html\r\n\r\n<html><body><h1>400 Bad Request</h1></body></html>", @output.string
end
def test_preparation_callbacks
ActionController::Routing::Routes.stubs(:reload)
old_mechanism = Dependencies.mechanism
a = b = c = nil
......@@ -126,6 +91,8 @@ def test_preparation_callbacks
end
def test_to_prepare_with_identifier_replaces
ActionController::Routing::Routes.stubs(:reload)
a = b = nil
Dispatcher.to_prepare(:unique_id) { a = b = 1 }
Dispatcher.to_prepare(:unique_id) { a = 2 }
......@@ -137,6 +104,12 @@ def test_to_prepare_with_identifier_replaces
private
def dispatch(output = @output)
controller = mock
controller.stubs(:process).returns(controller)
controller.stubs(:out).with(output).returns('response')
ActionController::Routing::Routes.stubs(:recognize).returns(controller)
Dispatcher.dispatch(nil, {}, output)
end
......@@ -144,3 +117,5 @@ def assert_subclasses(howmany, klass, message = klass.subclasses.inspect)
assert_equal howmany, klass.subclasses.size, message
end
end
end # uses_mocha
class Dispatcher
class <<self
attr_accessor :time_to_sleep
attr_accessor :raise_exception
attr_accessor :dispatch_hook
def dispatch(cgi, session_options = nil, output = $stdout)
dispatch_hook.call(cgi) if dispatch_hook
sleep(time_to_sleep || 0)
raise raise_exception, "Something died" if raise_exception
end
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册