PageRenderTime 26ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/actionpack/test/abstract/callbacks_test.rb

https://github.com/awscloudtest/rails
Ruby | 238 lines | 189 code | 49 blank | 0 comment | 2 complexity | 4d50cb0fdacbbda7f4b725979fd90f19 MD5 | raw file
  1. require 'abstract_unit'
  2. module AbstractController
  3. module Testing
  4. class ControllerWithCallbacks < AbstractController::Base
  5. include AbstractController::Callbacks
  6. end
  7. class Callback1 < ControllerWithCallbacks
  8. set_callback :process_action, :before, :first
  9. def first
  10. @text = "Hello world"
  11. end
  12. def index
  13. self.response_body = @text
  14. end
  15. end
  16. class TestCallbacks1 < ActiveSupport::TestCase
  17. test "basic callbacks work" do
  18. controller = Callback1.new
  19. result = controller.process(:index)
  20. assert_equal "Hello world", controller.response_body
  21. end
  22. end
  23. class Callback2 < ControllerWithCallbacks
  24. before_filter :first
  25. after_filter :second
  26. around_filter :aroundz
  27. def first
  28. @text = "Hello world"
  29. end
  30. def second
  31. @second = "Goodbye"
  32. end
  33. def aroundz
  34. @aroundz = "FIRST"
  35. yield
  36. @aroundz << "SECOND"
  37. end
  38. def index
  39. self.response_body = @text
  40. end
  41. end
  42. class TestCallbacks2 < ActiveSupport::TestCase
  43. def setup
  44. @controller = Callback2.new
  45. end
  46. test "before_filter works" do
  47. result = @controller.process(:index)
  48. assert_equal "Hello world", @controller.response_body
  49. end
  50. test "after_filter works" do
  51. @controller.process(:index)
  52. assert_equal "Goodbye", @controller.instance_variable_get("@second")
  53. end
  54. test "around_filter works" do
  55. @controller.process(:index)
  56. assert_equal "FIRSTSECOND", @controller.instance_variable_get("@aroundz")
  57. end
  58. end
  59. class Callback3 < ControllerWithCallbacks
  60. before_filter do |c|
  61. c.instance_variable_set("@text", "Hello world")
  62. end
  63. after_filter do |c|
  64. c.instance_variable_set("@second", "Goodbye")
  65. end
  66. def index
  67. self.response_body = @text
  68. end
  69. end
  70. class TestCallbacks3 < ActiveSupport::TestCase
  71. def setup
  72. @controller = Callback3.new
  73. end
  74. test "before_filter works with procs" do
  75. result = @controller.process(:index)
  76. assert_equal "Hello world", @controller.response_body
  77. end
  78. test "after_filter works with procs" do
  79. result = @controller.process(:index)
  80. assert_equal "Goodbye", @controller.instance_variable_get("@second")
  81. end
  82. end
  83. class CallbacksWithConditions < ControllerWithCallbacks
  84. before_filter :list, :only => :index
  85. before_filter :authenticate, :except => :index
  86. def index
  87. self.response_body = @list.join(", ")
  88. end
  89. def sekrit_data
  90. self.response_body = (@list + [@authenticated]).join(", ")
  91. end
  92. private
  93. def list
  94. @list = ["Hello", "World"]
  95. end
  96. def authenticate
  97. @list = []
  98. @authenticated = "true"
  99. end
  100. end
  101. class TestCallbacksWithConditions < ActiveSupport::TestCase
  102. def setup
  103. @controller = CallbacksWithConditions.new
  104. end
  105. test "when :only is specified, a before filter is triggered on that action" do
  106. @controller.process(:index)
  107. assert_equal "Hello, World", @controller.response_body
  108. end
  109. test "when :only is specified, a before filter is not triggered on other actions" do
  110. @controller.process(:sekrit_data)
  111. assert_equal "true", @controller.response_body
  112. end
  113. test "when :except is specified, an after filter is not triggered on that action" do
  114. result = @controller.process(:index)
  115. assert_nil @controller.instance_variable_get("@authenticated")
  116. end
  117. end
  118. class CallbacksWithArrayConditions < ControllerWithCallbacks
  119. before_filter :list, :only => [:index, :listy]
  120. before_filter :authenticate, :except => [:index, :listy]
  121. def index
  122. self.response_body = @list.join(", ")
  123. end
  124. def sekrit_data
  125. self.response_body = (@list + [@authenticated]).join(", ")
  126. end
  127. private
  128. def list
  129. @list = ["Hello", "World"]
  130. end
  131. def authenticate
  132. @list = []
  133. @authenticated = "true"
  134. end
  135. end
  136. class TestCallbacksWithArrayConditions < ActiveSupport::TestCase
  137. def setup
  138. @controller = CallbacksWithArrayConditions.new
  139. end
  140. test "when :only is specified with an array, a before filter is triggered on that action" do
  141. result = @controller.process(:index)
  142. assert_equal "Hello, World", @controller.response_body
  143. end
  144. test "when :only is specified with an array, a before filter is not triggered on other actions" do
  145. result = @controller.process(:sekrit_data)
  146. assert_equal "true", @controller.response_body
  147. end
  148. test "when :except is specified with an array, an after filter is not triggered on that action" do
  149. result = @controller.process(:index)
  150. assert_nil @controller.instance_variable_get("@authenticated")
  151. end
  152. end
  153. class ChangedConditions < Callback2
  154. before_filter :first, :only => :index
  155. def not_index
  156. self.response_body = @text.to_s
  157. end
  158. end
  159. class TestCallbacksWithChangedConditions < ActiveSupport::TestCase
  160. def setup
  161. @controller = ChangedConditions.new
  162. end
  163. test "when a callback is modified in a child with :only, it works for the :only action" do
  164. result = @controller.process(:index)
  165. assert_equal "Hello world", @controller.response_body
  166. end
  167. test "when a callback is modified in a child with :only, it does not work for other actions" do
  168. result = @controller.process(:not_index)
  169. assert_equal "", @controller.response_body
  170. end
  171. end
  172. class SetsResponseBody < ControllerWithCallbacks
  173. before_filter :set_body
  174. def index
  175. self.response_body = "Fail"
  176. end
  177. def set_body
  178. self.response_body = "Success"
  179. end
  180. end
  181. class TestHalting < ActiveSupport::TestCase
  182. test "when a callback sets the response body, the action should not be invoked" do
  183. controller = SetsResponseBody.new
  184. controller.process(:index)
  185. assert_equal "Success", controller.response_body
  186. end
  187. end
  188. end
  189. end