base_test.rb 4.4 KB
Newer Older
J
Joshua Peek 已提交
1
require 'abstract_unit'
2

3
# Tests the controller dispatching happy path
4
module Dispatching
5
  class SimpleController < ActionController::Base
6 7
    before_filter :authenticate

8 9
    def index
      render :text => "success"
10 11
    end

12 13
    def modify_response_body
      self.response_body = "success"
14
    end
15

16
    def modify_response_body_twice
17
      ret = (self.response_body = "success")
18
      self.response_body = "#{ret}!"
19
    end
20

21
    def modify_response_headers
22
    end
23 24

    def show_actions
25
      render :text => "actions: #{action_methods.to_a.sort.join(', ')}"
26 27 28 29 30
    end

    protected
      def authenticate
      end
31
  end
32 33

  class EmptyController < ActionController::Base ; end
34 35 36 37
  class SubEmptyController < EmptyController ; end
  class NonDefaultPathController < ActionController::Base
    def self.controller_path; "i_am_not_default"; end
  end
38 39 40

  module Submodule
    class ContainedEmptyController < ActionController::Base ; end
41 42
    class ContainedSubEmptyController < ContainedEmptyController ; end
    class ContainedNonDefaultPathController < ActionController::Base
43
      def self.controller_path; "i_am_extremely_not_default"; end
44
    end
45 46
  end

47
  class BaseTest < Rack::TestCase
48 49 50 51
    # :api: plugin
    test "simple dispatching" do
      get "/dispatching/simple/index"

52 53
      assert_body "success"
      assert_status 200
Y
Yehuda Katz + Carl Lerche 已提交
54
      assert_content_type "text/html; charset=utf-8"
55
    end
56 57 58 59 60

    # :api: plugin
    test "directly modifying response body" do
      get "/dispatching/simple/modify_response_body"

61 62
      assert_body "success"
    end
63 64 65 66 67

    # :api: plugin
    test "directly modifying response body twice" do
      get "/dispatching/simple/modify_response_body_twice"

68
      assert_body "success!"
69 70
    end

71
    test "controller path" do
72 73
      assert_equal 'dispatching/empty', EmptyController.controller_path
      assert_equal EmptyController.controller_path, EmptyController.new.controller_path
74 75
    end

76 77 78 79 80 81 82 83 84 85
    test "non-default controller path" do
      assert_equal 'i_am_not_default', NonDefaultPathController.controller_path
      assert_equal NonDefaultPathController.controller_path, NonDefaultPathController.new.controller_path
    end

    test "sub controller path" do
      assert_equal 'dispatching/sub_empty', SubEmptyController.controller_path
      assert_equal SubEmptyController.controller_path, SubEmptyController.new.controller_path
    end

86
    test "namespaced controller path" do
87 88 89
      assert_equal 'dispatching/submodule/contained_empty', Submodule::ContainedEmptyController.controller_path
      assert_equal Submodule::ContainedEmptyController.controller_path, Submodule::ContainedEmptyController.new.controller_path
    end
90

91
    test "namespaced non-default controller path" do
92
      assert_equal 'i_am_extremely_not_default', Submodule::ContainedNonDefaultPathController.controller_path
93 94 95 96 97 98 99 100
      assert_equal Submodule::ContainedNonDefaultPathController.controller_path, Submodule::ContainedNonDefaultPathController.new.controller_path
    end

    test "namespaced sub controller path" do
      assert_equal 'dispatching/submodule/contained_sub_empty', Submodule::ContainedSubEmptyController.controller_path
      assert_equal Submodule::ContainedSubEmptyController.controller_path, Submodule::ContainedSubEmptyController.new.controller_path
    end

101
    test "controller name" do
102 103
      assert_equal 'empty', EmptyController.controller_name
      assert_equal 'contained_empty', Submodule::ContainedEmptyController.controller_name
104
    end
105

106 107 108 109 110 111 112 113 114 115
    test "non-default path controller name" do
      assert_equal 'non_default_path', NonDefaultPathController.controller_name
      assert_equal 'contained_non_default_path', Submodule::ContainedNonDefaultPathController.controller_name
    end

    test "sub controller name" do
      assert_equal 'sub_empty', SubEmptyController.controller_name
      assert_equal 'contained_sub_empty', Submodule::ContainedSubEmptyController.controller_name
    end

116 117
    test "action methods" do
      assert_equal Set.new(%w(
J
José Valim 已提交
118
        index
119 120 121 122 123 124 125 126 127 128
        modify_response_headers
        modify_response_body_twice
        modify_response_body
        show_actions
      )), SimpleController.action_methods

      assert_equal Set.new, EmptyController.action_methods
      assert_equal Set.new, Submodule::ContainedEmptyController.action_methods

      get "/dispatching/simple/show_actions"
J
José Valim 已提交
129
      assert_body "actions: index, modify_response_body, modify_response_body_twice, modify_response_headers, show_actions"
130
    end
131
  end
132
end