| 1 |
require 'abstract_unit' |
|---|
| 2 |
|
|---|
| 3 |
class CallerController < ActionController::Base |
|---|
| 4 |
def calling_from_controller |
|---|
| 5 |
render_component(:controller => "callee", :action => "being_called") |
|---|
| 6 |
end |
|---|
| 7 |
|
|---|
| 8 |
def calling_from_controller_with_params |
|---|
| 9 |
render_component(:controller => "callee", :action => "being_called", :params => { "name" => "David" }) |
|---|
| 10 |
end |
|---|
| 11 |
|
|---|
| 12 |
def calling_from_controller_with_different_status_code |
|---|
| 13 |
render_component(:controller => "callee", :action => "blowing_up") |
|---|
| 14 |
end |
|---|
| 15 |
|
|---|
| 16 |
def calling_from_template |
|---|
| 17 |
render :inline => "Ring, ring: <%= render_component(:controller => 'callee', :action => 'being_called') %>" |
|---|
| 18 |
end |
|---|
| 19 |
|
|---|
| 20 |
def internal_caller |
|---|
| 21 |
render :inline => "Are you there? <%= render_component(:action => 'internal_callee') %>" |
|---|
| 22 |
end |
|---|
| 23 |
|
|---|
| 24 |
def internal_callee |
|---|
| 25 |
render :text => "Yes, ma'am" |
|---|
| 26 |
end |
|---|
| 27 |
|
|---|
| 28 |
def set_flash |
|---|
| 29 |
render_component(:controller => "callee", :action => "set_flash") |
|---|
| 30 |
end |
|---|
| 31 |
|
|---|
| 32 |
def use_flash |
|---|
| 33 |
render_component(:controller => "callee", :action => "use_flash") |
|---|
| 34 |
end |
|---|
| 35 |
|
|---|
| 36 |
def calling_redirected |
|---|
| 37 |
render_component(:controller => "callee", :action => "redirected") |
|---|
| 38 |
end |
|---|
| 39 |
|
|---|
| 40 |
def calling_redirected_as_string |
|---|
| 41 |
render :inline => "<%= render_component(:controller => 'callee', :action => 'redirected') %>" |
|---|
| 42 |
end |
|---|
| 43 |
|
|---|
| 44 |
def rescue_action(e) raise end |
|---|
| 45 |
end |
|---|
| 46 |
|
|---|
| 47 |
class CalleeController < ActionController::Base |
|---|
| 48 |
def being_called |
|---|
| 49 |
render :text => "#{params[:name] || "Lady"} of the House, speaking" |
|---|
| 50 |
end |
|---|
| 51 |
|
|---|
| 52 |
def blowing_up |
|---|
| 53 |
render :text => "It's game over, man, just game over, man!", :status => 500 |
|---|
| 54 |
end |
|---|
| 55 |
|
|---|
| 56 |
def set_flash |
|---|
| 57 |
flash[:notice] = 'My stoney baby' |
|---|
| 58 |
render :text => 'flash is set' |
|---|
| 59 |
end |
|---|
| 60 |
|
|---|
| 61 |
def use_flash |
|---|
| 62 |
render :text => flash[:notice] || 'no flash' |
|---|
| 63 |
end |
|---|
| 64 |
|
|---|
| 65 |
def redirected |
|---|
| 66 |
redirect_to :controller => "callee", :action => "being_called" |
|---|
| 67 |
end |
|---|
| 68 |
|
|---|
| 69 |
def rescue_action(e) raise end |
|---|
| 70 |
end |
|---|
| 71 |
|
|---|
| 72 |
class ComponentsTest < Test::Unit::TestCase |
|---|
| 73 |
def setup |
|---|
| 74 |
@controller = CallerController.new |
|---|
| 75 |
@request = ActionController::TestRequest.new |
|---|
| 76 |
@response = ActionController::TestResponse.new |
|---|
| 77 |
end |
|---|
| 78 |
|
|---|
| 79 |
def test_calling_from_controller |
|---|
| 80 |
get :calling_from_controller |
|---|
| 81 |
assert_equal "Lady of the House, speaking", @response.body |
|---|
| 82 |
end |
|---|
| 83 |
|
|---|
| 84 |
def test_calling_from_controller_with_params |
|---|
| 85 |
get :calling_from_controller_with_params |
|---|
| 86 |
assert_equal "David of the House, speaking", @response.body |
|---|
| 87 |
end |
|---|
| 88 |
|
|---|
| 89 |
def test_calling_from_controller_with_different_status_code |
|---|
| 90 |
get :calling_from_controller_with_different_status_code |
|---|
| 91 |
assert_equal 500, @response.response_code |
|---|
| 92 |
end |
|---|
| 93 |
|
|---|
| 94 |
def test_calling_from_template |
|---|
| 95 |
get :calling_from_template |
|---|
| 96 |
assert_equal "Ring, ring: Lady of the House, speaking", @response.body |
|---|
| 97 |
end |
|---|
| 98 |
|
|---|
| 99 |
def test_etag_is_set_for_parent_template_when_calling_from_template |
|---|
| 100 |
get :calling_from_template |
|---|
| 101 |
expected_etag = etag_for("Ring, ring: Lady of the House, speaking") |
|---|
| 102 |
assert_equal expected_etag, @response.headers['ETag'] |
|---|
| 103 |
end |
|---|
| 104 |
|
|---|
| 105 |
def test_internal_calling |
|---|
| 106 |
get :internal_caller |
|---|
| 107 |
assert_equal "Are you there? Yes, ma'am", @response.body |
|---|
| 108 |
end |
|---|
| 109 |
|
|---|
| 110 |
def test_flash |
|---|
| 111 |
get :set_flash |
|---|
| 112 |
assert_equal 'My stoney baby', flash[:notice] |
|---|
| 113 |
get :use_flash |
|---|
| 114 |
assert_equal 'My stoney baby', @response.body |
|---|
| 115 |
get :use_flash |
|---|
| 116 |
assert_equal 'no flash', @response.body |
|---|
| 117 |
end |
|---|
| 118 |
|
|---|
| 119 |
def test_component_redirect_redirects |
|---|
| 120 |
get :calling_redirected |
|---|
| 121 |
|
|---|
| 122 |
assert_redirected_to :action => "being_called" |
|---|
| 123 |
end |
|---|
| 124 |
|
|---|
| 125 |
def test_component_multiple_redirect_redirects |
|---|
| 126 |
test_component_redirect_redirects |
|---|
| 127 |
test_internal_calling |
|---|
| 128 |
end |
|---|
| 129 |
|
|---|
| 130 |
def test_component_as_string_redirect_renders_redirected_action |
|---|
| 131 |
get :calling_redirected_as_string |
|---|
| 132 |
|
|---|
| 133 |
assert_equal "Lady of the House, speaking", @response.body |
|---|
| 134 |
end |
|---|
| 135 |
|
|---|
| 136 |
protected |
|---|
| 137 |
def etag_for(text) |
|---|
| 138 |
%("#{Digest::MD5.hexdigest(text)}") |
|---|
| 139 |
end |
|---|
| 140 |
end |
|---|