aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/dispatcher_test.rb
blob: bf5d235dfedc99ddbe4c611d48f3cdb84ce10b3c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
$:.unshift File.dirname(__FILE__) + "/../lib"
$:.unshift File.dirname(__FILE__) + "/../../actionpack/lib"
$:.unshift File.dirname(__FILE__) + "/../../actionmailer/lib"

require 'test/unit'
require 'stringio'
require 'cgi'

require 'dispatcher'
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
    @output = StringIO.new
    ENV['REQUEST_METHOD'] = "GET"
    setup_minimal_environment
  end

  def teardown
    ENV['REQUEST_METHOD'] = nil
    teardown_minimal_environment
  end

  def test_ac_subclasses_cleared_on_reset
    Object.class_eval(ACTION_CONTROLLER_DEF)
    assert_equal 1, ActionController::Base.subclasses.length
    dispatch

    GC.start # force the subclass to be collected
    assert_equal 0, ActionController::Base.subclasses.length
  end

  def test_am_subclasses_cleared_on_reset
    Object.class_eval(ACTION_MAILER_DEF)
    assert_equal 1, ActionMailer::Base.subclasses.length
    dispatch

    GC.start # force the subclass to be collected
    assert_equal 0, ActionMailer::Base.subclasses.length
  end


  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 do
        Dispatcher.dispatch(nil, ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS, output)
      end
      assert_equal "Status: 400 Bad Request\r\n", output.string
    end
  ensure
    $stdin = old_stdin
  end

  private
    def dispatch
      Dispatcher.dispatch(nil, ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS, @output)
    end

    def setup_minimal_environment
      value = Dependencies::LoadingModule.root
      Object.const_set("Controllers", value)
    end

    def teardown_minimal_environment
      Object.send(:remove_const, "Controllers")
    end
end