Skip to content
This repository
Browse code

Dispatcher tests. References #9630.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7591 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 3dea8b580b9c67bb27c01290fb3b17f446544b78 1 parent ea45680
Jeremy Kemper jeremy authored
10 railties/test/abstract_unit.rb
@@ -15,3 +15,13 @@
15 15 class Test::Unit::TestCase
16 16 # Add stuff here if you need it
17 17 end
  18 +
  19 +# Wrap tests that use Mocha and skip if unavailable.
  20 +def uses_mocha(test_name)
  21 + require 'rubygems'
  22 + gem 'mocha', '>= 0.5.5'
  23 + require 'mocha'
  24 + yield
  25 +rescue LoadError
  26 + $stderr.puts "Skipping #{test_name} tests. `gem install mocha` and try again."
  27 +end
101 railties/test/dispatcher_test.rb
... ... @@ -1,4 +1,7 @@
1 1 require "#{File.dirname(__FILE__)}/abstract_unit"
  2 +
  3 +uses_mocha 'dispatcher tests' do
  4 +
2 5 $:.unshift File.dirname(__FILE__) + "/../../actionmailer/lib"
3 6
4 7 require 'stringio'
@@ -8,15 +11,6 @@
8 11 require 'action_controller'
9 12 require 'action_mailer'
10 13
11   -ACTION_MAILER_DEF = <<AM
12   - class DispatcherTestMailer < ActionMailer::Base
13   - end
14   -AM
15   -
16   -ACTION_CONTROLLER_DEF = <<AM
17   - class DispatcherControllerTest < ActionController::Base
18   - end
19   -AM
20 14
21 15 class DispatcherTest < Test::Unit::TestCase
22 16 def setup
@@ -26,75 +20,46 @@ def setup
26 20 Dispatcher.send(:preparation_callbacks).clear
27 21 Dispatcher.send(:preparation_callbacks_run=, false)
28 22
29   - Object.const_set :ApplicationController, nil
30   - class << ActionController::Routing::Routes
31   - alias_method :old_reload, :reload
32   - def reload() end
33   - end
  23 + Object.const_set 'ApplicationController', nil
34 24 end
35 25
36 26 def teardown
37   - Object.send :remove_const, :ApplicationController
38 27 ENV['REQUEST_METHOD'] = nil
39   - class << ActionController::Routing::Routes
40   - alias_method :reload, :old_reload
41   - end
  28 + Object.send :remove_const, 'ApplicationController'
42 29 end
43 30
44   - def test_ac_subclasses_cleared_on_reset
45   - Object.class_eval(ACTION_CONTROLLER_DEF)
46   - assert_subclasses 1, ActionController::Base
47   - dispatch
  31 + def test_clears_dependencies_after_dispatch_if_in_loading_mode
  32 + Dependencies.stubs(:load?).returns(true)
48 33
49   - GC.start # force the subclass to be collected
50   - assert_subclasses 0, ActionController::Base
51   - end
  34 + ActionController::Routing::Routes.expects(:reload).once
  35 + Dependencies.expects(:clear).once
52 36
53   - def test_am_subclasses_cleared_on_reset
54   - Object.class_eval(ACTION_MAILER_DEF)
55   - assert_subclasses 1, ActionMailer::Base
56 37 dispatch
  38 + end
  39 +
  40 + def test_clears_dependencies_after_dispatch_if_not_in_loading_mode
  41 + Dependencies.stubs(:load?).returns(false)
  42 +
  43 + ActionController::Routing::Routes.expects(:reload).never
  44 + Dependencies.expects(:clear).never
57 45
58   - GC.start # force the subclass to be collected
59   - assert_subclasses 0, ActionMailer::Base
  46 + dispatch
60 47 end
61 48
  49 + def test_failsafe_response
  50 + CGI.expects(:new).raises('some multipart parsing failure')
62 51
63   - INVALID_MULTIPART = [
64   - 'POST /foo HTTP/1.0',
65   - 'Host: example.com',
66   - 'Content-Type: multipart/form-data;boundary=foo'
67   - ]
68   -
69   - EMPTY_CONTENT = (INVALID_MULTIPART + [
70   - 'Content-Length: 100',
71   - nil, nil
72   - ]).join("\r\n")
73   -
74   - CONTENT_LENGTH_MISMATCH = (INVALID_MULTIPART + [
75   - 'Content-Length: 100',
76   - nil, nil,
77   - 'foobar'
78   - ]).join("\r\n")
79   -
80   - NONINTEGER_CONTENT_LENGTH = (INVALID_MULTIPART + [
81   - 'Content-Length: abc',
82   - nil, nil
83   - ]).join("\r\n")
84   -
85   - def test_bad_multipart_request
86   - old_stdin = $stdin
87   - [EMPTY_CONTENT, CONTENT_LENGTH_MISMATCH, NONINTEGER_CONTENT_LENGTH].each do |bad_request|
88   - $stdin = StringIO.new(bad_request)
89   - output = StringIO.new
90   - assert_nothing_raised { dispatch output }
91   - assert_equal "Status: 400 Bad Request\r\n", output.string
92   - end
93   - ensure
94   - $stdin = old_stdin
  52 + ActionController::Routing::Routes.stubs(:reload)
  53 + Dispatcher.stubs(:log_failsafe_exception)
  54 +
  55 + assert_nothing_raised { dispatch }
  56 +
  57 + 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
95 58 end
96   -
  59 +
97 60 def test_preparation_callbacks
  61 + ActionController::Routing::Routes.stubs(:reload)
  62 +
98 63 old_mechanism = Dependencies.mechanism
99 64
100 65 a = b = c = nil
@@ -126,6 +91,8 @@ def test_preparation_callbacks
126 91 end
127 92
128 93 def test_to_prepare_with_identifier_replaces
  94 + ActionController::Routing::Routes.stubs(:reload)
  95 +
129 96 a = b = nil
130 97 Dispatcher.to_prepare(:unique_id) { a = b = 1 }
131 98 Dispatcher.to_prepare(:unique_id) { a = 2 }
@@ -137,6 +104,12 @@ def test_to_prepare_with_identifier_replaces
137 104
138 105 private
139 106 def dispatch(output = @output)
  107 + controller = mock
  108 + controller.stubs(:process).returns(controller)
  109 + controller.stubs(:out).with(output).returns('response')
  110 +
  111 + ActionController::Routing::Routes.stubs(:recognize).returns(controller)
  112 +
140 113 Dispatcher.dispatch(nil, {}, output)
141 114 end
142 115
@@ -144,3 +117,5 @@ def assert_subclasses(howmany, klass, message = klass.subclasses.inspect)
144 117 assert_equal howmany, klass.subclasses.size, message
145 118 end
146 119 end
  120 +
  121 +end # uses_mocha
13 railties/test/mocks/dispatcher.rb
... ... @@ -1,13 +0,0 @@
1   -class Dispatcher
2   - class <<self
3   - attr_accessor :time_to_sleep
4   - attr_accessor :raise_exception
5   - attr_accessor :dispatch_hook
6   -
7   - def dispatch(cgi, session_options = nil, output = $stdout)
8   - dispatch_hook.call(cgi) if dispatch_hook
9   - sleep(time_to_sleep || 0)
10   - raise raise_exception, "Something died" if raise_exception
11   - end
12   - end
13   -end

0 comments on commit 3dea8b5

Please sign in to comment.
Something went wrong with that request. Please try again.