aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/controller/new_base/base_test.rb
blob: 3765d406fc6869b22a1e5bd2db6d64cdff3d7351 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
require "abstract_unit"

# Tests the controller dispatching happy path
module Dispatching
  class SimpleController < ActionController::Base
    before_action :authenticate

    def index
      render body: "success"
    end

    def modify_response_body
      self.response_body = "success"
    end

    def modify_response_body_twice
      ret = (self.response_body = "success")
      self.response_body = "#{ret}!"
    end

    def modify_response_headers
    end

    def show_actions
      render body: "actions: #{action_methods.to_a.sort.join(', ')}"
    end

    protected
      def authenticate
      end
  end

  class EmptyController < ActionController::Base ; end
  class SubEmptyController < EmptyController ; end
  class NonDefaultPathController < ActionController::Base
    def self.controller_path; "i_am_not_default"; end
  end

  module Submodule
    class ContainedEmptyController < ActionController::Base ; end
    class ContainedSubEmptyController < ContainedEmptyController ; end
    class ContainedNonDefaultPathController < ActionController::Base
      def self.controller_path; "i_am_extremely_not_default"; end
    end
  end

  class BaseTest < Rack::TestCase
    # :api: plugin
    test "simple dispatching" do
      get "/dispatching/simple/index"

      assert_body "success"
      assert_status 200
      assert_content_type "text/plain; charset=utf-8"
    end

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

      assert_body "success"
    end

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

      assert_body "success!"
    end

    test "controller path" do
      assert_equal "dispatching/empty", EmptyController.controller_path
      assert_equal EmptyController.controller_path, EmptyController.new.controller_path
    end

    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

    test "namespaced controller path" do
      assert_equal "dispatching/submodule/contained_empty", Submodule::ContainedEmptyController.controller_path
      assert_equal Submodule::ContainedEmptyController.controller_path, Submodule::ContainedEmptyController.new.controller_path
    end

    test "namespaced non-default controller path" do
      assert_equal "i_am_extremely_not_default", Submodule::ContainedNonDefaultPathController.controller_path
      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

    test "controller name" do
      assert_equal "empty", EmptyController.controller_name
      assert_equal "contained_empty", Submodule::ContainedEmptyController.controller_name
    end

    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

    test "action methods" do
      assert_equal Set.new(%w(
        index
        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"
      assert_body "actions: index, modify_response_body, modify_response_body_twice, modify_response_headers, show_actions"
    end
  end
end