Dispatcher tests. References #9630.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7591 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
ea45680118
commit
3dea8b580b
|
@ -15,3 +15,13 @@ end
|
||||||
class Test::Unit::TestCase
|
class Test::Unit::TestCase
|
||||||
# Add stuff here if you need it
|
# Add stuff here if you need it
|
||||||
end
|
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
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
require "#{File.dirname(__FILE__)}/abstract_unit"
|
require "#{File.dirname(__FILE__)}/abstract_unit"
|
||||||
|
|
||||||
|
uses_mocha 'dispatcher tests' do
|
||||||
|
|
||||||
$:.unshift File.dirname(__FILE__) + "/../../actionmailer/lib"
|
$:.unshift File.dirname(__FILE__) + "/../../actionmailer/lib"
|
||||||
|
|
||||||
require 'stringio'
|
require 'stringio'
|
||||||
|
@ -8,15 +11,6 @@ require 'dispatcher'
|
||||||
require 'action_controller'
|
require 'action_controller'
|
||||||
require 'action_mailer'
|
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
|
class DispatcherTest < Test::Unit::TestCase
|
||||||
def setup
|
def setup
|
||||||
|
@ -26,75 +20,46 @@ class DispatcherTest < Test::Unit::TestCase
|
||||||
Dispatcher.send(:preparation_callbacks).clear
|
Dispatcher.send(:preparation_callbacks).clear
|
||||||
Dispatcher.send(:preparation_callbacks_run=, false)
|
Dispatcher.send(:preparation_callbacks_run=, false)
|
||||||
|
|
||||||
Object.const_set :ApplicationController, nil
|
Object.const_set 'ApplicationController', nil
|
||||||
class << ActionController::Routing::Routes
|
|
||||||
alias_method :old_reload, :reload
|
|
||||||
def reload() end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def teardown
|
def teardown
|
||||||
Object.send :remove_const, :ApplicationController
|
|
||||||
ENV['REQUEST_METHOD'] = nil
|
ENV['REQUEST_METHOD'] = nil
|
||||||
class << ActionController::Routing::Routes
|
Object.send :remove_const, 'ApplicationController'
|
||||||
alias_method :reload, :old_reload
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_ac_subclasses_cleared_on_reset
|
def test_clears_dependencies_after_dispatch_if_in_loading_mode
|
||||||
Object.class_eval(ACTION_CONTROLLER_DEF)
|
Dependencies.stubs(:load?).returns(true)
|
||||||
assert_subclasses 1, ActionController::Base
|
|
||||||
|
ActionController::Routing::Routes.expects(:reload).once
|
||||||
|
Dependencies.expects(:clear).once
|
||||||
|
|
||||||
dispatch
|
dispatch
|
||||||
|
|
||||||
GC.start # force the subclass to be collected
|
|
||||||
assert_subclasses 0, ActionController::Base
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_am_subclasses_cleared_on_reset
|
def test_clears_dependencies_after_dispatch_if_not_in_loading_mode
|
||||||
Object.class_eval(ACTION_MAILER_DEF)
|
Dependencies.stubs(:load?).returns(false)
|
||||||
assert_subclasses 1, ActionMailer::Base
|
|
||||||
|
ActionController::Routing::Routes.expects(:reload).never
|
||||||
|
Dependencies.expects(:clear).never
|
||||||
|
|
||||||
dispatch
|
dispatch
|
||||||
|
|
||||||
GC.start # force the subclass to be collected
|
|
||||||
assert_subclasses 0, ActionMailer::Base
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_failsafe_response
|
||||||
|
CGI.expects(:new).raises('some multipart parsing failure')
|
||||||
|
|
||||||
INVALID_MULTIPART = [
|
ActionController::Routing::Routes.stubs(:reload)
|
||||||
'POST /foo HTTP/1.0',
|
Dispatcher.stubs(:log_failsafe_exception)
|
||||||
'Host: example.com',
|
|
||||||
'Content-Type: multipart/form-data;boundary=foo'
|
|
||||||
]
|
|
||||||
|
|
||||||
EMPTY_CONTENT = (INVALID_MULTIPART + [
|
assert_nothing_raised { dispatch }
|
||||||
'Content-Length: 100',
|
|
||||||
nil, nil
|
|
||||||
]).join("\r\n")
|
|
||||||
|
|
||||||
CONTENT_LENGTH_MISMATCH = (INVALID_MULTIPART + [
|
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
|
||||||
'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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_preparation_callbacks
|
def test_preparation_callbacks
|
||||||
|
ActionController::Routing::Routes.stubs(:reload)
|
||||||
|
|
||||||
old_mechanism = Dependencies.mechanism
|
old_mechanism = Dependencies.mechanism
|
||||||
|
|
||||||
a = b = c = nil
|
a = b = c = nil
|
||||||
|
@ -126,6 +91,8 @@ class DispatcherTest < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_to_prepare_with_identifier_replaces
|
def test_to_prepare_with_identifier_replaces
|
||||||
|
ActionController::Routing::Routes.stubs(:reload)
|
||||||
|
|
||||||
a = b = nil
|
a = b = nil
|
||||||
Dispatcher.to_prepare(:unique_id) { a = b = 1 }
|
Dispatcher.to_prepare(:unique_id) { a = b = 1 }
|
||||||
Dispatcher.to_prepare(:unique_id) { a = 2 }
|
Dispatcher.to_prepare(:unique_id) { a = 2 }
|
||||||
|
@ -137,6 +104,12 @@ class DispatcherTest < Test::Unit::TestCase
|
||||||
|
|
||||||
private
|
private
|
||||||
def dispatch(output = @output)
|
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)
|
Dispatcher.dispatch(nil, {}, output)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -144,3 +117,5 @@ class DispatcherTest < Test::Unit::TestCase
|
||||||
assert_equal howmany, klass.subclasses.size, message
|
assert_equal howmany, klass.subclasses.size, message
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
end # uses_mocha
|
||||||
|
|
|
@ -1,13 +0,0 @@
|
||||||
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
|
|
Loading…
Reference in New Issue