PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/rails/actionpack/test/controller/dispatcher_test.rb

https://bitbucket.org/hfwang/good_shepherd
Ruby | 105 lines | 77 code | 22 blank | 6 comment | 4 complexity | c789771f4614fae3f6e7a32f51b93592 MD5 | raw file
  1. require 'abstract_unit'
  2. class DispatcherTest < Test::Unit::TestCase
  3. Dispatcher = ActionController::Dispatcher
  4. def setup
  5. ENV['REQUEST_METHOD'] = 'GET'
  6. Dispatcher.middleware = ActionController::MiddlewareStack.new do |middleware|
  7. middlewares = File.expand_path(File.join(File.dirname(__FILE__), "../../lib/action_controller/middlewares.rb"))
  8. middleware.instance_eval(File.read(middlewares))
  9. end
  10. # Clear callbacks as they are redefined by Dispatcher#define_dispatcher_callbacks
  11. Dispatcher.instance_variable_set("@prepare_dispatch_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
  12. Dispatcher.instance_variable_set("@before_dispatch_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
  13. Dispatcher.instance_variable_set("@after_dispatch_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
  14. Dispatcher.stubs(:require_dependency)
  15. end
  16. def teardown
  17. ENV.delete 'REQUEST_METHOD'
  18. end
  19. def test_clears_dependencies_after_dispatch_if_in_loading_mode
  20. ActiveSupport::Dependencies.expects(:clear).once
  21. # Close the response so dependencies kicks in
  22. dispatch(false).last.close
  23. end
  24. def test_reloads_routes_before_dispatch_if_in_loading_mode
  25. ActionController::Routing::Routes.expects(:reload).once
  26. dispatch(false)
  27. end
  28. def test_leaves_dependencies_after_dispatch_if_not_in_loading_mode
  29. ActionController::Routing::Routes.expects(:reload).never
  30. ActiveSupport::Dependencies.expects(:clear).never
  31. dispatch
  32. end
  33. # Stub out dispatch error logger
  34. class << Dispatcher
  35. def log_failsafe_exception(status, exception); end
  36. end
  37. def test_failsafe_response
  38. Dispatcher.any_instance.expects(:dispatch).raises('b00m')
  39. ActionController::Failsafe.any_instance.expects(:log_failsafe_exception)
  40. response = nil
  41. assert_nothing_raised do
  42. response = dispatch
  43. end
  44. assert_equal 3, response.size
  45. assert_equal 500, response[0]
  46. assert_equal({"Content-Type" => "text/html"}, response[1])
  47. assert_match /500 Internal Server Error/, response[2].join
  48. end
  49. def test_prepare_callbacks
  50. a = b = c = nil
  51. Dispatcher.to_prepare { |*args| a = b = c = 1 }
  52. Dispatcher.to_prepare { |*args| b = c = 2 }
  53. Dispatcher.to_prepare { |*args| c = 3 }
  54. # Ensure to_prepare callbacks are not run when defined
  55. assert_nil a || b || c
  56. # Run callbacks
  57. Dispatcher.run_prepare_callbacks
  58. assert_equal 1, a
  59. assert_equal 2, b
  60. assert_equal 3, c
  61. # Make sure they are only run once
  62. a = b = c = nil
  63. dispatch
  64. assert_nil a || b || c
  65. end
  66. def test_to_prepare_with_identifier_replaces
  67. a = b = nil
  68. Dispatcher.to_prepare(:unique_id) { |*args| a = b = 1 }
  69. Dispatcher.to_prepare(:unique_id) { |*args| a = 2 }
  70. Dispatcher.run_prepare_callbacks
  71. assert_equal 2, a
  72. assert_equal nil, b
  73. end
  74. private
  75. def dispatch(cache_classes = true)
  76. ActionController::Routing::RouteSet.any_instance.stubs(:call).returns([200, {}, 'response'])
  77. Dispatcher.define_dispatcher_callbacks(cache_classes)
  78. Dispatcher.new.call({'rack.input' => StringIO.new('')})
  79. end
  80. def assert_subclasses(howmany, klass, message = klass.subclasses.inspect)
  81. assert_equal howmany, klass.subclasses.size, message
  82. end
  83. end